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