b489354e21142479decd3bbccdfc4913d7a908b1
[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 <fstream>
25 #include <cstdlib>
26 #include <string>
27 #include <cstdio>
28 #include <locale.h>
29 #include <unistd.h>
30 #include <float.h>
31 #include <iomanip>
32
33 #include <glibmm.h>
34
35 #include "pbd/cartesian.h"
36 #include "pbd/boost_debug.h"
37 #include "pbd/convert.h"
38 #include "pbd/error.h"
39 #include "pbd/failed_constructor.h"
40 #include "pbd/xml++.h"
41 #include "pbd/enumwriter.h"
42
43 #include "evoral/Curve.hpp"
44
45 #include "ardour/audio_buffer.h"
46 #include "ardour/audioengine.h"
47 #include "ardour/buffer_set.h"
48 #include "ardour/debug.h"
49 #include "ardour/pannable.h"
50 #include "ardour/panner.h"
51 #include "ardour/panner_manager.h"
52 #include "ardour/panner_shell.h"
53 #include "ardour/session.h"
54 #include "ardour/speakers.h"
55
56 #include "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()) {
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
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                         Changed (); /* EMIT SIGNAL */
112                 }
113                 return;
114         }
115
116         PannerInfo* pi = PannerManager::instance().select_panner (in, out, _user_selected_panner_uri);
117         if (!pi) {
118                 cerr << "No panner found: check that panners are being discovered correctly during startup.\n";
119                 assert (pi);
120         }
121
122         DEBUG_TRACE (DEBUG::Panning, string_compose (_("select panner: %1\n"), pi->descriptor.name.c_str()));
123
124         boost::shared_ptr<Speakers> speakers = _session.get_speakers ();
125
126         if (nouts != speakers->size()) {
127                 /* hmm, output count doesn't match session speaker count so
128                    create a new speaker set.
129                 */
130                 Speakers* s = new Speakers ();
131                 s->setup_default_speakers (nouts);
132                 speakers.reset (s);
133         }
134
135         /* TODO  don't allow to link  _is_send if internal & route panners are different types */
136         Panner* p = pi->descriptor.factory (pannable(), speakers);
137         // boost_debug_shared_ptr_mark_interesting (p, "Panner");
138         _panner.reset (p);
139         _panner->configure_io (in, out);
140         _current_panner_uri = pi->descriptor.panner_uri;
141         _panner_gui_uri = pi->descriptor.gui_uri;
142
143         Changed (); /* EMIT SIGNAL */
144 }
145
146 XMLNode&
147 PannerShell::get_state ()
148 {
149         XMLNode* node = new XMLNode ("PannerShell");
150
151         node->add_property (X_("bypassed"), _bypassed ? X_("yes") : X_("no"));
152         node->add_property (X_("user-panner"), _user_selected_panner_uri);
153         node->add_property (X_("linked-to-route"), _panlinked ? X_("yes") : X_("no"));
154
155         if (_panner && _is_send) {
156                 node->add_child_nocopy (_panner->get_state ());
157         }
158
159         return *node;
160 }
161
162 int
163 PannerShell::set_state (const XMLNode& node, int version)
164 {
165         XMLNodeList nlist = node.children ();
166         XMLNodeConstIterator niter;
167         const XMLProperty *prop;
168         LocaleGuard lg (X_("POSIX"));
169
170         if ((prop = node.property (X_("bypassed"))) != 0) {
171                 set_bypassed (string_is_affirmative (prop->value ()));
172         }
173
174         if ((prop = node.property (X_("linked-to-route"))) != 0) {
175                 _panlinked = string_is_affirmative (prop->value ());
176         }
177
178         if ((prop = node.property (X_("user-panner"))) != 0) {
179                 _user_selected_panner_uri = prop->value ();
180         }
181
182         _panner.reset ();
183         
184         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
185
186                 if ((*niter)->name() == X_("Panner")) {
187
188                         if ((prop = (*niter)->property (X_("uri")))) {
189                                 PannerInfo* p = PannerManager::instance().get_by_uri(prop->value());
190                                 if (p) {
191                                         _panner.reset (p->descriptor.factory (
192                                                                 _is_send ? _pannable_internal : _pannable_route, _session.get_speakers ()));
193                                         _current_panner_uri = p->descriptor.panner_uri;
194                                         _panner_gui_uri = p->descriptor.gui_uri;
195                                         if (_panner->set_state (**niter, version) == 0) {
196                                                 return -1;
197                                         }
198                                 }
199                         }
200
201                         else /* backwards compatibility */
202                         if ((prop = (*niter)->property (X_("type")))) {
203
204                                 list<PannerInfo*>::iterator p;
205                                 PannerManager& pm (PannerManager::instance());
206
207                                 for (p = pm.panner_info.begin(); p != pm.panner_info.end(); ++p) {
208                                         if (prop->value() == (*p)->descriptor.name) {
209
210                                                 /* note that we assume that all the stream panners
211                                                    are of the same type. pretty good
212                                                    assumption, but it's still an assumption.
213                                                 */
214
215                                                 _panner.reset ((*p)->descriptor.factory (
216                                                                         _is_send ? _pannable_internal : _pannable_route, _session.get_speakers ()));
217                                                 _current_panner_uri = (*p)->descriptor.panner_uri;
218                                                 _panner_gui_uri = (*p)->descriptor.gui_uri;
219
220                                                 if (_panner->set_state (**niter, version) == 0) {
221                                                         return -1;
222                                                 }
223
224                                                 break;
225                                         }
226                                 }
227
228                                 if (p == pm.panner_info.end()) {
229                                         error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
230                                                                  prop->value())
231                                               << endmsg;
232                                 }
233
234                         } else {
235                                 error << _("panner plugin node has no type information!")
236                                       << endmsg;
237                                 return -1;
238                         }
239                 }
240         }
241
242         return 0;
243 }
244
245
246 void
247 PannerShell::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, pframes_t nframes, gain_t gain_coeff)
248 {
249         if (outbufs.count().n_audio() == 0) {
250                 // Don't want to lose audio...
251                 assert(inbufs.count().n_audio() == 0);
252                 return;
253         }
254
255         if (outbufs.count().n_audio() == 1) {
256
257                 /* just one output: no real panning going on */
258
259                 AudioBuffer& dst = outbufs.get_audio(0);
260
261                 if (gain_coeff == 0.0f) {
262
263                         /* gain was zero, so make it silent */
264
265                         dst.silence (nframes);
266
267                 } else if (gain_coeff == 1.0f){
268
269                         /* mix all input buffers into the output */
270
271                         // copy the first
272                         dst.read_from(inbufs.get_audio(0), nframes);
273
274                         // accumulate starting with the second
275                         if (inbufs.count().n_audio() > 0) {
276                                 BufferSet::audio_iterator i = inbufs.audio_begin();
277                                 for (++i; i != inbufs.audio_end(); ++i) {
278                                         dst.merge_from(*i, nframes);
279                                 }
280                         }
281
282                 } else {
283
284                         /* mix all buffers into the output, scaling them all by the gain */
285
286                         // copy the first
287                         dst.read_from(inbufs.get_audio(0), nframes);
288
289                         // accumulate (with gain) starting with the second
290                         if (inbufs.count().n_audio() > 0) {
291                                 BufferSet::audio_iterator i = inbufs.audio_begin();
292                                 for (++i; i != inbufs.audio_end(); ++i) {
293                                         dst.accumulate_with_gain_from(*i, nframes, gain_coeff);
294                                 }
295                         }
296
297                 }
298
299                 return;
300         }
301
302         /* multiple outputs ... we must have a panner */
303
304         assert (_panner);
305
306         /* setup silent buffers so that we can mix into the outbuffers (slightly suboptimal -
307            better to copy the first set of data then mix after that, but hey, its 2011)
308         */
309
310         for (BufferSet::audio_iterator b = outbufs.audio_begin(); b != outbufs.audio_end(); ++b) {
311                 (*b).silence (nframes);
312         }
313
314         _panner->distribute (inbufs, outbufs, gain_coeff, nframes);
315 }
316
317 void
318 PannerShell::run (BufferSet& inbufs, BufferSet& outbufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
319 {
320         if (inbufs.count().n_audio() == 0) {
321                 /* Input has no audio buffers (e.g. Aux Send in a MIDI track at a
322                    point with no audio because there is no preceding instrument)
323                 */
324                 outbufs.silence(nframes, 0);
325                 return;
326         }
327
328         if (outbufs.count().n_audio() == 0) {
329                 // Failing to deliver audio we were asked to deliver is a bug
330                 assert(inbufs.count().n_audio() == 0);
331                 return;
332         }
333
334         if (outbufs.count().n_audio() == 1) {
335
336                 /* one output only: no panner */
337
338                 AudioBuffer& dst = outbufs.get_audio(0);
339
340                 // FIXME: apply gain automation?
341
342                 // copy the first
343                 dst.read_from (inbufs.get_audio(0), nframes);
344
345                 // accumulate starting with the second
346                 BufferSet::audio_iterator i = inbufs.audio_begin();
347                 for (++i; i != inbufs.audio_end(); ++i) {
348                         dst.merge_from (*i, nframes);
349                 }
350
351                 return;
352         }
353
354         // More than 1 output
355
356         AutoState as = _panner->automation_state ();
357
358         // If we shouldn't play automation defer to distribute_no_automation
359
360         if (!(as & Play || ((as & Touch) && !_panner->touching()))) {
361
362                 // Speed quietning
363                 gain_t gain_coeff = 1.0;
364
365                 if (fabsf(_session.transport_speed()) > 1.5f && Config->get_quieten_at_speed ()) {
366                         gain_coeff = speed_quietning;
367                 }
368
369                 distribute_no_automation (inbufs, outbufs, nframes, gain_coeff);
370
371         } else {
372
373                 /* setup the terrible silence so that we can mix into the outbuffers (slightly suboptimal -
374                    better to copy the first set of data then mix after that, but hey, its 2011)
375                 */
376                 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
377                         i->silence(nframes);
378                 }
379
380                 _panner->distribute_automated (inbufs, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
381         }
382 }
383
384 void
385 PannerShell::set_bypassed (bool yn)
386 {
387         if (yn == _bypassed) {
388                 return;
389         }
390         
391         _bypassed = yn;
392         Changed (); /* EMIT SIGNAL */
393 }
394
395 bool
396 PannerShell::bypassed () const
397 {
398         return _bypassed;
399 }
400
401 /* set custom-panner config
402  *
403  * This function is intended to be only called from
404  * Route::set_custom_panner()
405  * which will trigger IO-reconfigutaion if this fn return true
406  */
407 bool
408 PannerShell::set_user_selected_panner_uri (std::string const uri)
409 {
410         if (uri == _user_selected_panner_uri) return false;
411         _user_selected_panner_uri = uri;
412         if (uri == _current_panner_uri) return false;
413         _force_reselect = true;
414         return true;
415 }
416
417 bool
418 PannerShell::select_panner_by_uri (std::string const uri)
419 {
420         if (uri == _user_selected_panner_uri) return false;
421         _user_selected_panner_uri = uri;
422         if (uri == _current_panner_uri) return false;
423         _force_reselect = true;
424         if (_panner) {
425                 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
426                         ChanCount in = _panner->in();
427                         ChanCount out = _panner->out();
428                         configure_io(in, out);
429                         pannable()->set_panner(_panner);
430                         _session.set_dirty ();
431         }
432         return true;
433 }
434
435 void
436 PannerShell::set_linked_to_route (bool onoff)
437 {
438         if (!_is_send || onoff == _panlinked) {
439                 return;
440         }
441         _panlinked = onoff;
442
443         _force_reselect = true;
444         if (_panner) {
445                 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
446                         ChanCount in = _panner->in();
447                         ChanCount out = _panner->out();
448                         configure_io(in, out);
449                         pannable()->set_panner(_panner);
450                         _session.set_dirty ();
451         }
452         PannableChanged();
453 }