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