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>
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.
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.
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.
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"
45 #include "evoral/Curve.hpp"
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"
62 #include "pbd/mathfix.h"
65 using namespace ARDOUR;
68 PannerShell::PannerShell (string name, Session& s, boost::shared_ptr<Pannable> p, bool is_send)
69 : SessionObject (s, name)
74 , _current_panner_uri("")
75 , _user_selected_panner_uri("")
77 , _force_reselect (false)
80 _pannable_internal.reset(new Pannable (s));
81 if (Config->get_link_send_and_route_panner() && !ARDOUR::Profile->get_mixbus()) {
90 PannerShell::~PannerShell ()
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));
96 PannerShell::configure_io (ChanCount in, ChanCount out)
98 uint32_t nouts = out.n_audio();
99 uint32_t nins = in.n_audio();
101 /* if new and old config don't need panning, or if
102 the config hasn't changed, we're done.
105 if (!_force_reselect && _panner && (_panner->in().n_audio() == nins) && (_panner->out().n_audio() == nouts)) {
108 _force_reselect = false;
110 if (nouts < 2 || nins == 0) {
111 /* no need for panning with less than 2 outputs or no inputs */
114 _current_panner_uri = "";
115 _panner_gui_uri = "";
116 if (!_is_send || !_panlinked) {
117 pannable()->set_panner(_panner);
119 Changed (); /* EMIT SIGNAL */
124 PannerInfo* pi = PannerManager::instance().select_panner (in, out, _user_selected_panner_uri);
126 fatal << _("No panner found: check that panners are being discovered correctly during startup.") << endmsg;
127 abort(); /*NOTREACHED*/
130 DEBUG_TRACE (DEBUG::Panning, string_compose (_("select panner: %1\n"), pi->descriptor.name.c_str()));
132 boost::shared_ptr<Speakers> speakers = _session.get_speakers ();
134 if (nouts != speakers->size()) {
135 /* hmm, output count doesn't match session speaker count so
136 create a new speaker set.
138 Speakers* s = new Speakers ();
139 s->setup_default_speakers (nouts);
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");
147 _panner->configure_io (in, out);
148 _current_panner_uri = pi->descriptor.panner_uri;
149 _panner_gui_uri = pi->descriptor.gui_uri;
151 if (!_is_send || !_panlinked) {
152 pannable()->set_panner(_panner);
154 Changed (); /* EMIT SIGNAL */
158 PannerShell::get_state ()
160 XMLNode* node = new XMLNode ("PannerShell");
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);
166 if (_panner && _is_send) {
167 node->add_child_nocopy (_panner->get_state ());
174 PannerShell::set_state (const XMLNode& node, int version)
176 XMLNodeList nlist = node.children ();
177 XMLNodeConstIterator niter;
181 if (node.get_property (X_("bypassed"), yn)) {
185 if (node.get_property (X_("linked-to-route"), yn)) {
186 if (!ARDOUR::Profile->get_mixbus()) {
191 node.get_property (X_("user-panner"), _user_selected_panner_uri);
195 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
197 if ((*niter)->name() == X_("Panner")) {
198 if ((*niter)->get_property (X_("uri"), str)) {
199 PannerInfo* p = PannerManager::instance().get_by_uri(str);
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;
207 _pannable_internal->set_panner(_panner);
209 _force_reselect = true;
212 _pannable_route->set_panner(_panner);
214 if (_panner->set_state (**niter, version) == 0) {
220 else /* backwards compatibility */
221 if ((*niter)->get_property (X_("type"), str)) {
223 list<PannerInfo*>::iterator p;
224 PannerManager& pm (PannerManager::instance());
226 for (p = pm.panner_info.begin(); p != pm.panner_info.end(); ++p) {
227 if (str == (*p)->descriptor.name) {
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.
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;
241 _pannable_internal->set_panner(_panner);
243 _force_reselect = true;
246 _pannable_route->set_panner(_panner);
249 if (_panner->set_state (**niter, version) == 0) {
257 if (p == pm.panner_info.end()) {
258 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
264 error << _("panner plugin node has no type information!")
276 PannerShell::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, pframes_t nframes, gain_t gain_coeff)
278 if (outbufs.count().n_audio() == 0) {
279 // Don't want to lose audio...
280 assert(inbufs.count().n_audio() == 0);
284 if (outbufs.count().n_audio() == 1) {
286 /* just one output: no real panning going on */
288 AudioBuffer& dst = outbufs.get_audio(0);
290 if (gain_coeff == GAIN_COEFF_ZERO) {
292 /* gain was zero, so make it silent */
294 dst.silence (nframes);
296 } else if (gain_coeff == GAIN_COEFF_UNITY){
298 /* mix all input buffers into the output */
301 dst.read_from(inbufs.get_audio(0), nframes);
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);
313 /* mix all buffers into the output, scaling them all by the gain */
316 dst.read_from(inbufs.get_audio(0), nframes);
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);
331 /* multiple outputs ... we must have a panner */
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)
339 for (BufferSet::audio_iterator b = outbufs.audio_begin(); b != outbufs.audio_end(); ++b) {
340 (*b).silence (nframes);
343 _panner->distribute (inbufs, outbufs, gain_coeff, nframes);
347 PannerShell::run (BufferSet& inbufs, BufferSet& outbufs, samplepos_t start_sample, samplepos_t end_sample, pframes_t nframes)
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)
353 outbufs.silence(nframes, 0);
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);
363 if (outbufs.count().n_audio() == 1) {
365 /* one output only: no panner */
367 AudioBuffer& dst = outbufs.get_audio(0);
369 // FIXME: apply gain automation?
372 dst.read_from (inbufs.get_audio(0), nframes);
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);
383 // More than 1 output
385 AutoState as = _panner->automation_state ();
387 // If we shouldn't play automation defer to distribute_no_automation
389 if (!((as & Play) || ((as & (Touch | Latch)) && !_panner->touching()))) {
391 distribute_no_automation (inbufs, outbufs, nframes, 1.0);
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)
398 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
402 _panner->distribute_automated (inbufs, outbufs, start_sample, end_sample, nframes, _session.pan_automation_buffer());
407 PannerShell::set_bypassed (bool yn)
409 if (yn == _bypassed) {
414 _session.set_dirty ();
415 Changed (); /* EMIT SIGNAL */
419 PannerShell::bypassed () const
424 /* set custom-panner config
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
431 PannerShell::set_user_selected_panner_uri (std::string const uri)
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;
441 PannerShell::select_panner_by_uri (std::string const uri)
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;
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);
455 _session.set_dirty ();
461 PannerShell::set_linked_to_route (bool onoff)
464 if (onoff == _panlinked) {
468 /* set _pannable-_has_state = true
469 * this way the panners will pick it up
470 * when it is re-created
473 XMLNode state = pannable()->get_state();
474 pannable()->set_state(state, 3000);
479 _force_reselect = true;
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);
486 pannable()->set_panner(_panner);
488 _session.set_dirty ();