9 #include "pbd/cartesian.h"
11 #include "ardour/pannable.h"
12 #include "ardour/speakers.h"
13 #include "ardour/vbap.h"
14 #include "ardour/vbap_speakers.h"
15 #include "ardour/audio_buffer.h"
16 #include "ardour/buffer_set.h"
17 #include "ardour/pan_controllable.h"
20 using namespace ARDOUR;
23 static PanPluginDescriptor _descriptor = {
29 extern "C" { PanPluginDescriptor* panner_descriptor () { return &_descriptor; } }
31 VBAPanner::Signal::Signal (Session& session, VBAPanner& p, uint32_t n)
32 : azimuth_control (new PanControllable (session, string_compose (_("azimuth %1"), n+1), &p, Evoral::Parameter (PanAzimuthAutomation, 0, n)))
33 , elevation_control (new PanControllable (session, string_compose (_("elevation %1"), n+1), &p, Evoral::Parameter (PanElevationAutomation, 0, n)))
35 gains[0] = gains[1] = gains[2] = 0;
36 desired_gains[0] = desired_gains[1] = desired_gains[2] = 0;
37 outputs[0] = outputs[1] = outputs[2] = -1;
38 desired_outputs[0] = desired_outputs[1] = desired_outputs[2] = -1;
41 VBAPanner::VBAPanner (boost::shared_ptr<Pannable> p, Speakers& s)
44 , _speakers (VBAPSpeakers::instance (s))
48 VBAPanner::~VBAPanner ()
50 for (vector<Signal*>::iterator i = _signals.begin(); i != _signals.end(); ++i) {
56 VBAPanner::configure_io (const ChanCount& in, const ChanCount& /* ignored - we use Speakers */)
58 uint32_t n = in.n_audio();
60 /* 2d panning: spread signals equally around a circle */
62 double degree_step = 360.0 / _speakers.n_speakers();
65 /* even number of signals? make sure the top two are either side of "top".
66 otherwise, just start at the "top" (90.0 degrees) and rotate around
70 deg = 90.0 - degree_step;
77 for (uint32_t i = 0; i < n; ++i) {
78 _signals.push_back (new Signal (_pannable->session(), *this, i));
79 _signals[i]->direction = AngularVector (deg, 0.0);
85 VBAPanner::compute_gains (double gains[3], int speaker_ids[3], int azi, int ele)
87 /* calculates gain factors using loudspeaker setup and given direction */
92 double big_sm_g, gtmp[3];
94 azi_ele_to_cart (azi,ele, cartdir[0], cartdir[1], cartdir[2]);
97 gains[0] = gains[1] = gains[2] = 0;
98 speaker_ids[0] = speaker_ids[1] = speaker_ids[2] = 0;
100 for (i = 0; i < _speakers.n_tuples(); i++) {
102 small_g = 10000000.0;
104 for (j = 0; j < _speakers.dimension(); j++) {
108 for (k = 0; k < _speakers.dimension(); k++) {
109 gtmp[j] += cartdir[k] * _speakers.matrix(i)[j*_speakers.dimension()+k];
112 if (gtmp[j] < small_g) {
117 if (small_g > big_sm_g) {
124 speaker_ids[0] = _speakers.speaker_for_tuple (i, 0);
125 speaker_ids[1] = _speakers.speaker_for_tuple (i, 1);
127 if (_speakers.dimension() == 3) {
129 speaker_ids[2] = _speakers.speaker_for_tuple (i, 2);
137 power = sqrt (gains[0]*gains[0] + gains[1]*gains[1] + gains[2]*gains[2]);
149 VBAPanner::do_distribute (BufferSet& inbufs, BufferSet& obufs, gain_t gain_coefficient, pframes_t nframes)
151 bool was_dirty = _dirty;
153 vector<Signal*>::iterator s;
155 assert (inbufs.count().n_audio() == _signals.size());
157 /* XXX need to handle mono case */
159 for (s = _signals.begin(), n = 0; s != _signals.end(); ++s, ++n) {
164 compute_gains (signal->desired_gains, signal->desired_outputs, signal->direction.azi, signal->direction.ele);
165 cerr << " @ " << signal->direction.azi << " /= " << signal->direction.ele
167 << signal->desired_outputs[0] + 1 << ' '
168 << signal->desired_outputs[1] + 1 << ' '
170 << signal->desired_gains[0] << ' '
171 << signal->desired_gains[1] << ' '
175 do_distribute_one (inbufs.get_audio (n), obufs, gain_coefficient, nframes, n);
178 memcpy (signal->gains, signal->desired_gains, sizeof (signal->gains));
179 memcpy (signal->outputs, signal->desired_outputs, sizeof (signal->outputs));
186 VBAPanner::do_distribute_one (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coefficient, pframes_t nframes, uint32_t which)
188 Sample* const src = srcbuf.data();
191 uint32_t n_audio = obufs.count().n_audio();
193 Signal* signal (_signals[which]);
195 for (uint32_t o = 0; o < n_audio; ++o) {
199 /* VBAP may distribute the signal across up to 3 speakers depending on
200 the configuration of the speakers.
203 for (int o = 0; o < 3; ++o) {
204 if (signal->desired_outputs[o] != -1) {
208 /* XXX TODO: interpolate across changes in gain and/or outputs
211 dst = obufs.get_audio (signal->desired_outputs[o]).data();
213 pan = gain_coefficient * signal->desired_gains[o];
214 mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
220 for (uint32_t o = 0; o < n_audio; ++o) {
222 /* VBAP decided not to deliver any audio to this output, so we write silence */
223 dst = obufs.get_audio(o).data();
224 memset (dst, 0, sizeof (Sample) * nframes);
231 VBAPanner::do_distribute_one_automated (AudioBuffer& src, BufferSet& obufs,
232 framepos_t start, framepos_t end, pframes_t nframes, pan_t** buffers, uint32_t which)
237 VBAPanner::get_state ()
243 VBAPanner::state (bool full_state)
245 XMLNode& node (Panner::get_state());
246 node.add_property (X_("type"), _descriptor.name);
251 VBAPanner::set_state (const XMLNode& node, int /*version*/)
256 boost::shared_ptr<AutomationControl>
257 VBAPanner::azimuth_control (uint32_t n)
259 if (n >= _signals.size()) {
260 return boost::shared_ptr<AutomationControl>();
262 return _signals[n]->azimuth_control;
265 boost::shared_ptr<AutomationControl>
266 VBAPanner::evelation_control (uint32_t n)
268 if (n >= _signals.size()) {
269 return boost::shared_ptr<AutomationControl>();
271 return _signals[n]->elevation_control;
275 VBAPanner::factory (boost::shared_ptr<Pannable> p, Speakers& s)
277 return new VBAPanner (p, s);
281 VBAPanner::describe_parameter (Evoral::Parameter param)
284 switch (param.type()) {
285 case PanElevationAutomation:
286 return string_compose ( _("Pan:elevation %1"), param.id() + 1);
287 case PanWidthAutomation:
288 return string_compose ( _("Pan:diffusion %1"), param.id() + 1);
289 case PanAzimuthAutomation:
290 return string_compose ( _("Pan:azimuth %1"), param.id() + 1);
293 return Automatable::describe_parameter (param);
297 VBAPanner::in() const
299 return ChanCount (DataType::AUDIO, _signals.size());
303 VBAPanner::out() const
305 return ChanCount (DataType::AUDIO, _speakers.n_speakers());