properly calculate requrired thread buffers
[ardour.git] / libs / ardour / panner_shell.cc
1 /*
2     Copyright (C) 2004-2011 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <inttypes.h>
21
22 #include <cmath>
23 #include <cerrno>
24 #include <cstdlib>
25 #include <string>
26 #include <cstdio>
27 #include <locale.h>
28 #include <unistd.h>
29 #include <float.h>
30 #include <iomanip>
31
32 #include <glibmm.h>
33
34 #include "pbd/cartesian.h"
35 #include "pbd/boost_debug.h"
36 #include "pbd/convert.h"
37 #include "pbd/error.h"
38 #include "pbd/failed_constructor.h"
39 #include "pbd/xml++.h"
40 #include "pbd/enumwriter.h"
41
42 #include "evoral/Curve.hpp"
43
44 #include "ardour/audio_buffer.h"
45 #include "ardour/audioengine.h"
46 #include "ardour/buffer_set.h"
47 #include "ardour/debug.h"
48 #include "ardour/pannable.h"
49 #include "ardour/panner.h"
50 #include "ardour/panner_manager.h"
51 #include "ardour/panner_shell.h"
52 #include "ardour/session.h"
53 #include "ardour/speakers.h"
54
55 #include "i18n.h"
56
57 #include "pbd/mathfix.h"
58
59 using namespace std;
60 using namespace ARDOUR;
61 using namespace PBD;
62
63 PannerShell::PannerShell (string name, Session& s, boost::shared_ptr<Pannable> p, bool is_send)
64         : SessionObject (s, name)
65         , _pannable_route (p)
66         , _is_send (is_send)
67         , _panlinked (true)
68         , _bypassed (false)
69         , _current_panner_uri("")
70         , _user_selected_panner_uri("")
71         , _panner_gui_uri("")
72         , _force_reselect (false)
73 {
74         if (is_send) {
75                 _pannable_internal.reset(new Pannable (s));
76                 if (Config->get_link_send_and_route_panner()) {
77                         _panlinked = true;
78                 } else {
79                         _panlinked = false;
80                 }
81         }
82         set_name (name);
83 }
84
85 PannerShell::~PannerShell ()
86 {
87         DEBUG_TRACE(DEBUG::Destruction, string_compose ("panner shell %3 for %1 destructor, panner is %4, pannable is %2\n", _name, _pannable_route, this, _panner));
88 }
89
90 void
91 PannerShell::configure_io (ChanCount in, ChanCount out)
92 {
93         uint32_t nouts = out.n_audio();
94         uint32_t nins = in.n_audio();
95
96         /* if new and old config don't need panning, or if
97            the config hasn't changed, we're done.
98         */
99
100         if (!_force_reselect && _panner && (_panner->in().n_audio() == nins) && (_panner->out().n_audio() == nouts)) {
101                 return;
102         }
103         _force_reselect = false;
104
105         if (nouts < 2 || nins == 0) {
106                 /* no need for panning with less than 2 outputs or no inputs */
107                 if (_panner) {
108                         _panner.reset ();
109                         _current_panner_uri = "";
110                         _panner_gui_uri = "";
111                         if (!_is_send || !_panlinked) {
112                                 pannable()->set_panner(_panner);
113                         }
114                         Changed (); /* EMIT SIGNAL */
115                 }
116                 return;
117         }
118
119         PannerInfo* pi = PannerManager::instance().select_panner (in, out, _user_selected_panner_uri);
120         if (!pi) {
121                 fatal << _("No panner found: check that panners are being discovered correctly during startup.") << endmsg;
122                 abort(); /*NOTREACHED*/
123         }
124
125         DEBUG_TRACE (DEBUG::Panning, string_compose (_("select panner: %1\n"), pi->descriptor.name.c_str()));
126
127         boost::shared_ptr<Speakers> speakers = _session.get_speakers ();
128
129         if (nouts != speakers->size()) {
130                 /* hmm, output count doesn't match session speaker count so
131                    create a new speaker set.
132                 */
133                 Speakers* s = new Speakers ();
134                 s->setup_default_speakers (nouts);
135                 speakers.reset (s);
136         }
137
138         /* TODO  don't allow to link  _is_send if internal & route panners are different types */
139         Panner* p = pi->descriptor.factory (pannable(), speakers);
140         // boost_debug_shared_ptr_mark_interesting (p, "Panner");
141         _panner.reset (p);
142         _panner->configure_io (in, out);
143         _current_panner_uri = pi->descriptor.panner_uri;
144         _panner_gui_uri = pi->descriptor.gui_uri;
145
146         if (!_is_send || !_panlinked) {
147                 pannable()->set_panner(_panner);
148         }
149         Changed (); /* EMIT SIGNAL */
150 }
151
152 XMLNode&
153 PannerShell::get_state ()
154 {
155         XMLNode* node = new XMLNode ("PannerShell");
156
157         node->add_property (X_("bypassed"), _bypassed ? X_("yes") : X_("no"));
158         node->add_property (X_("user-panner"), _user_selected_panner_uri);
159         node->add_property (X_("linked-to-route"), _panlinked ? X_("yes") : X_("no"));
160
161         if (_panner && _is_send) {
162                 node->add_child_nocopy (_panner->get_state ());
163         }
164
165         return *node;
166 }
167
168 int
169 PannerShell::set_state (const XMLNode& node, int version)
170 {
171         XMLNodeList nlist = node.children ();
172         XMLNodeConstIterator niter;
173         const XMLProperty *prop;
174         LocaleGuard lg (X_("C"));
175
176         if ((prop = node.property (X_("bypassed"))) != 0) {
177                 set_bypassed (string_is_affirmative (prop->value ()));
178         }
179
180         if ((prop = node.property (X_("linked-to-route"))) != 0) {
181                 _panlinked = string_is_affirmative (prop->value ());
182         }
183
184         if ((prop = node.property (X_("user-panner"))) != 0) {
185                 _user_selected_panner_uri = prop->value ();
186         }
187
188         _panner.reset ();
189
190         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
191
192                 if ((*niter)->name() == X_("Panner")) {
193
194                         if ((prop = (*niter)->property (X_("uri")))) {
195                                 PannerInfo* p = PannerManager::instance().get_by_uri(prop->value());
196                                 if (p) {
197                                         _panner.reset (p->descriptor.factory (
198                                                                 _is_send ? _pannable_internal : _pannable_route, _session.get_speakers ()));
199                                         _current_panner_uri = p->descriptor.panner_uri;
200                                         _panner_gui_uri = p->descriptor.gui_uri;
201                                         if (_is_send) {
202                                                 if (!_panlinked) {
203                                                         _pannable_internal->set_panner(_panner);
204                                                 } else {
205                                                         _force_reselect = true;
206                                                 }
207                                         } else {
208                                                 _pannable_route->set_panner(_panner);
209                                         }
210                                         if (_panner->set_state (**niter, version) == 0) {
211                                                 return -1;
212                                         }
213                                 }
214                         }
215
216                         else /* backwards compatibility */
217                         if ((prop = (*niter)->property (X_("type")))) {
218
219                                 list<PannerInfo*>::iterator p;
220                                 PannerManager& pm (PannerManager::instance());
221
222                                 for (p = pm.panner_info.begin(); p != pm.panner_info.end(); ++p) {
223                                         if (prop->value() == (*p)->descriptor.name) {
224
225                                                 /* note that we assume that all the stream panners
226                                                    are of the same type. pretty good
227                                                    assumption, but it's still an assumption.
228                                                 */
229
230                                                 _panner.reset ((*p)->descriptor.factory (
231                                                                         _is_send ? _pannable_internal : _pannable_route, _session.get_speakers ()));
232                                                 _current_panner_uri = (*p)->descriptor.panner_uri;
233                                                 _panner_gui_uri = (*p)->descriptor.gui_uri;
234
235                                                 if (_is_send) {
236                                                         if (!_panlinked) {
237                                                                 _pannable_internal->set_panner(_panner);
238                                                         } else {
239                                                                 _force_reselect = true;
240                                                         }
241                                                 } else {
242                                                         _pannable_route->set_panner(_panner);
243                                                 }
244
245                                                 if (_panner->set_state (**niter, version) == 0) {
246                                                         return -1;
247                                                 }
248
249                                                 break;
250                                         }
251                                 }
252
253                                 if (p == pm.panner_info.end()) {
254                                         error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
255                                                                  prop->value())
256                                               << endmsg;
257                                 }
258
259                         } else {
260                                 error << _("panner plugin node has no type information!")
261                                       << endmsg;
262                                 return -1;
263                         }
264                 }
265         }
266
267         return 0;
268 }
269
270
271 void
272 PannerShell::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, pframes_t nframes, gain_t gain_coeff)
273 {
274         if (outbufs.count().n_audio() == 0) {
275                 // Don't want to lose audio...
276                 assert(inbufs.count().n_audio() == 0);
277                 return;
278         }
279
280         if (outbufs.count().n_audio() == 1) {
281
282                 /* just one output: no real panning going on */
283
284                 AudioBuffer& dst = outbufs.get_audio(0);
285
286                 if (gain_coeff == GAIN_COEFF_ZERO) {
287
288                         /* gain was zero, so make it silent */
289
290                         dst.silence (nframes);
291
292                 } else if (gain_coeff == GAIN_COEFF_UNITY){
293
294                         /* mix all input buffers into the output */
295
296                         // copy the first
297                         dst.read_from(inbufs.get_audio(0), nframes);
298
299                         // accumulate starting with the second
300                         if (inbufs.count().n_audio() > 0) {
301                                 BufferSet::audio_iterator i = inbufs.audio_begin();
302                                 for (++i; i != inbufs.audio_end(); ++i) {
303                                         dst.merge_from(*i, nframes);
304                                 }
305                         }
306
307                 } else {
308
309                         /* mix all buffers into the output, scaling them all by the gain */
310
311                         // copy the first
312                         dst.read_from(inbufs.get_audio(0), nframes);
313
314                         // accumulate (with gain) starting with the second
315                         if (inbufs.count().n_audio() > 0) {
316                                 BufferSet::audio_iterator i = inbufs.audio_begin();
317                                 for (++i; i != inbufs.audio_end(); ++i) {
318                                         dst.accumulate_with_gain_from(*i, nframes, gain_coeff);
319                                 }
320                         }
321
322                 }
323
324                 return;
325         }
326
327         /* multiple outputs ... we must have a panner */
328
329         assert (_panner);
330
331         /* setup silent buffers so that we can mix into the outbuffers (slightly suboptimal -
332            better to copy the first set of data then mix after that, but hey, its 2011)
333         */
334
335         for (BufferSet::audio_iterator b = outbufs.audio_begin(); b != outbufs.audio_end(); ++b) {
336                 (*b).silence (nframes);
337         }
338
339         _panner->distribute (inbufs, outbufs, gain_coeff, nframes);
340 }
341
342 void
343 PannerShell::run (BufferSet& inbufs, BufferSet& outbufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
344 {
345         if (inbufs.count().n_audio() == 0) {
346                 /* Input has no audio buffers (e.g. Aux Send in a MIDI track at a
347                    point with no audio because there is no preceding instrument)
348                 */
349                 outbufs.silence(nframes, 0);
350                 return;
351         }
352
353         if (outbufs.count().n_audio() == 0) {
354                 // Failing to deliver audio we were asked to deliver is a bug
355                 assert(inbufs.count().n_audio() == 0);
356                 return;
357         }
358
359         if (outbufs.count().n_audio() == 1) {
360
361                 /* one output only: no panner */
362
363                 AudioBuffer& dst = outbufs.get_audio(0);
364
365                 // FIXME: apply gain automation?
366
367                 // copy the first
368                 dst.read_from (inbufs.get_audio(0), nframes);
369
370                 // accumulate starting with the second
371                 BufferSet::audio_iterator i = inbufs.audio_begin();
372                 for (++i; i != inbufs.audio_end(); ++i) {
373                         dst.merge_from (*i, nframes);
374                 }
375
376                 return;
377         }
378
379         // More than 1 output
380
381         AutoState as = _panner->automation_state ();
382
383         // If we shouldn't play automation defer to distribute_no_automation
384
385         if (!(as & Play || ((as & Touch) && !_panner->touching()))) {
386
387                 distribute_no_automation (inbufs, outbufs, nframes, 1.0);
388
389         } else {
390
391                 /* setup the terrible silence so that we can mix into the outbuffers (slightly suboptimal -
392                    better to copy the first set of data then mix after that, but hey, its 2011)
393                 */
394                 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
395                         i->silence(nframes);
396                 }
397
398                 _panner->distribute_automated (inbufs, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
399         }
400 }
401
402 void
403 PannerShell::set_bypassed (bool yn)
404 {
405         if (yn == _bypassed) {
406                 return;
407         }
408
409         _bypassed = yn;
410         _session.set_dirty ();
411         Changed (); /* EMIT SIGNAL */
412 }
413
414 bool
415 PannerShell::bypassed () const
416 {
417         return _bypassed;
418 }
419
420 /* set custom-panner config
421  *
422  * This function is intended to be only called from
423  * Route::set_custom_panner()
424  * which will trigger IO-reconfigutaion if this fn return true
425  */
426 bool
427 PannerShell::set_user_selected_panner_uri (std::string const uri)
428 {
429         if (uri == _user_selected_panner_uri) return false;
430         _user_selected_panner_uri = uri;
431         if (uri == _current_panner_uri) return false;
432         _force_reselect = true;
433         return true;
434 }
435
436 bool
437 PannerShell::select_panner_by_uri (std::string const uri)
438 {
439         if (uri == _user_selected_panner_uri) return false;
440         _user_selected_panner_uri = uri;
441         if (uri == _current_panner_uri) return false;
442         _force_reselect = true;
443         if (_panner) {
444                 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
445                         ChanCount in = _panner->in();
446                         ChanCount out = _panner->out();
447                         configure_io(in, out);
448                         if (!_is_send || !_panlinked) {
449                                 pannable()->set_panner(_panner);
450                         }
451                         _session.set_dirty ();
452         }
453         return true;
454 }
455
456 void
457 PannerShell::set_linked_to_route (bool onoff)
458 {
459         assert(_is_send);
460         if (onoff == _panlinked) {
461                 return;
462         }
463
464         /* set _pannable-_has_state = true
465          * this way the panners will pick it up
466          * when it is re-created
467          */
468         if (pannable()) {
469                 XMLNode state = pannable()->get_state();
470                 pannable()->set_state(state, 3000);
471         }
472
473         _panlinked = onoff;
474
475         _force_reselect = true;
476         if (_panner) {
477                 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
478                         ChanCount in = _panner->in();
479                         ChanCount out = _panner->out();
480                         configure_io(in, out);
481                         if (!_panlinked) {
482                                 pannable()->set_panner(_panner);
483                         }
484                         _session.set_dirty ();
485         }
486         PannableChanged();
487 }