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