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