tentative commit of new panners subtree
[ardour.git] / libs / panners / vbap / vbap.cc
1 #include <cmath>
2 #include <cstdlib>
3 #include <cstdio>
4 #include <cstring>
5
6 #include <iostream>
7 #include <string>
8
9 #include "pbd/cartesian.h"
10
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"
18
19 using namespace PBD;
20 using namespace ARDOUR;
21 using namespace std;
22
23 static PanPluginDescriptor _descriptor = {
24         "VBAP 2D panner",
25         1, -1, 2, -1,
26         VBAPanner::factory
27 };
28
29 extern "C" { PanPluginDescriptor* panner_descriptor () { return &_descriptor; } }
30
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)))
34 {
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;
39 };
40
41 VBAPanner::VBAPanner (boost::shared_ptr<Pannable> p, Speakers& s)
42         : Panner (p)
43         , _dirty (true)
44         , _speakers (VBAPSpeakers::instance (s))
45 {
46 }
47
48 VBAPanner::~VBAPanner ()
49 {
50         for (vector<Signal*>::iterator i = _signals.begin(); i != _signals.end(); ++i) {
51                 delete *i;
52         }
53 }
54
55 void
56 VBAPanner::configure_io (const ChanCount& in, const ChanCount& /* ignored - we use Speakers */)
57 {
58         uint32_t n = in.n_audio();
59
60         /* 2d panning: spread signals equally around a circle */
61         
62         double degree_step = 360.0 / _speakers.n_speakers();
63         double deg;
64         
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
67         */
68         
69         if (n % 2) {
70                 deg = 90.0 - degree_step;
71         } else {
72                 deg = 90.0;
73         }
74
75         _signals.clear ();
76         
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);
80                 deg += degree_step;
81         }
82 }
83
84 void 
85 VBAPanner::compute_gains (double gains[3], int speaker_ids[3], int azi, int ele) 
86 {
87         /* calculates gain factors using loudspeaker setup and given direction */
88         double cartdir[3];
89         double power;
90         int i,j,k;
91         double small_g;
92         double big_sm_g, gtmp[3];
93
94         azi_ele_to_cart (azi,ele, cartdir[0], cartdir[1], cartdir[2]);  
95         big_sm_g = -100000.0;
96
97         gains[0] = gains[1] = gains[2] = 0;
98         speaker_ids[0] = speaker_ids[1] = speaker_ids[2] = 0;
99
100         for (i = 0; i < _speakers.n_tuples(); i++) {
101
102                 small_g = 10000000.0;
103
104                 for (j = 0; j < _speakers.dimension(); j++) {
105
106                         gtmp[j] = 0.0;
107
108                         for (k = 0; k < _speakers.dimension(); k++) {
109                                 gtmp[j] += cartdir[k] * _speakers.matrix(i)[j*_speakers.dimension()+k]; 
110                         }
111
112                         if (gtmp[j] < small_g) {
113                                 small_g = gtmp[j];
114                         }
115                 }
116
117                 if (small_g > big_sm_g) {
118
119                         big_sm_g = small_g;
120
121                         gains[0] = gtmp[0]; 
122                         gains[1] = gtmp[1]; 
123
124                         speaker_ids[0] = _speakers.speaker_for_tuple (i, 0);
125                         speaker_ids[1] = _speakers.speaker_for_tuple (i, 1);
126                         
127                         if (_speakers.dimension() == 3) {
128                                 gains[2] = gtmp[2];
129                                 speaker_ids[2] = _speakers.speaker_for_tuple (i, 2);
130                         } else {
131                                 gains[2] = 0.0;
132                                 speaker_ids[2] = -1;
133                         }
134                 }
135         }
136         
137         power = sqrt (gains[0]*gains[0] + gains[1]*gains[1] + gains[2]*gains[2]);
138
139         if (power > 0) {
140                 gains[0] /= power; 
141                 gains[1] /= power;
142                 gains[2] /= power;
143         }
144
145         _dirty = false;
146 }
147
148 void
149 VBAPanner::do_distribute (BufferSet& inbufs, BufferSet& obufs, gain_t gain_coefficient, pframes_t nframes)
150 {
151         bool was_dirty = _dirty;
152         uint32_t n;
153         vector<Signal*>::iterator s;
154
155         assert (inbufs.count().n_audio() == _signals.size());
156
157         /* XXX need to handle mono case */
158
159         for (s = _signals.begin(), n = 0; s != _signals.end(); ++s, ++n) {
160
161                 Signal* signal (*s);
162
163                 if (was_dirty) {
164                         compute_gains (signal->desired_gains, signal->desired_outputs, signal->direction.azi, signal->direction.ele);
165                         cerr << " @ " << signal->direction.azi << " /= " << signal->direction.ele
166                              << " Outputs: "
167                              << signal->desired_outputs[0] + 1 << ' '
168                              << signal->desired_outputs[1] + 1 << ' '
169                              << " Gains "
170                              << signal->desired_gains[0] << ' '
171                              << signal->desired_gains[1] << ' '
172                              << endl;
173                 }
174                 
175                 do_distribute_one (inbufs.get_audio (n), obufs, gain_coefficient, nframes, n);
176
177                 if (was_dirty) {
178                         memcpy (signal->gains, signal->desired_gains, sizeof (signal->gains));
179                         memcpy (signal->outputs, signal->desired_outputs, sizeof (signal->outputs));
180                 }
181         }
182 }
183
184
185 void
186 VBAPanner::do_distribute_one (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coefficient, pframes_t nframes, uint32_t which)
187 {
188         Sample* const src = srcbuf.data();
189         Sample* dst;
190         pan_t pan;
191         uint32_t n_audio = obufs.count().n_audio();
192         bool todo[n_audio];
193         Signal* signal (_signals[which]);
194
195         for (uint32_t o = 0; o < n_audio; ++o) {
196                 todo[o] = true;
197         }
198         
199         /* VBAP may distribute the signal across up to 3 speakers depending on
200            the configuration of the speakers.
201         */
202
203         for (int o = 0; o < 3; ++o) {
204                 if (signal->desired_outputs[o] != -1) {
205                         
206                         pframes_t n = 0;
207
208                         /* XXX TODO: interpolate across changes in gain and/or outputs
209                          */
210
211                         dst = obufs.get_audio (signal->desired_outputs[o]).data();
212
213                         pan = gain_coefficient * signal->desired_gains[o];
214                         mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
215
216                         todo[o] = false;
217                 }
218         }
219         
220         for (uint32_t o = 0; o < n_audio; ++o) {
221                 if (todo[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);
225                 }
226         }
227         
228 }
229
230 void 
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)
233 {
234 }
235
236 XMLNode&
237 VBAPanner::get_state ()
238 {
239         return state (true);
240 }
241
242 XMLNode&
243 VBAPanner::state (bool full_state)
244 {
245         XMLNode& node (Panner::get_state());
246         node.add_property (X_("type"), _descriptor.name);
247         return node;
248 }
249
250 int
251 VBAPanner::set_state (const XMLNode& node, int /*version*/)
252 {
253         return 0;
254 }
255
256 boost::shared_ptr<AutomationControl>
257 VBAPanner::azimuth_control (uint32_t n)
258 {
259         if (n >= _signals.size()) {
260                 return boost::shared_ptr<AutomationControl>();
261         }
262         return _signals[n]->azimuth_control;
263 }
264
265 boost::shared_ptr<AutomationControl>
266 VBAPanner::evelation_control (uint32_t n)
267 {
268         if (n >= _signals.size()) {
269                 return boost::shared_ptr<AutomationControl>();
270         }
271         return _signals[n]->elevation_control;
272 }
273
274 Panner*
275 VBAPanner::factory (boost::shared_ptr<Pannable> p, Speakers& s)
276 {
277         return new VBAPanner (p, s);
278 }
279
280 string
281 VBAPanner::describe_parameter (Evoral::Parameter param)
282 {
283         stringstream ss;
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);
291         }
292
293         return Automatable::describe_parameter (param);
294 }
295
296 ChanCount
297 VBAPanner::in() const
298 {
299         return ChanCount (DataType::AUDIO, _signals.size());
300 }
301
302 ChanCount
303 VBAPanner::out() const
304 {
305         return ChanCount (DataType::AUDIO, _speakers.n_speakers());
306 }