2 Copyright (C) 2004-2011 Paul Davis
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.
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.
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.
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"
43 #include "evoral/Curve.hpp"
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"
58 #include "pbd/mathfix.h"
61 using namespace ARDOUR;
64 PannerShell::PannerShell (string name, Session& s, boost::shared_ptr<Pannable> p, bool is_send)
65 : SessionObject (s, name)
70 , _current_panner_uri("")
71 , _user_selected_panner_uri("")
73 , _force_reselect (false)
76 _pannable_internal.reset(new Pannable (s));
77 if (Config->get_link_send_and_route_panner()) {
86 PannerShell::~PannerShell ()
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));
92 PannerShell::configure_io (ChanCount in, ChanCount out)
94 uint32_t nouts = out.n_audio();
95 uint32_t nins = in.n_audio();
97 /* if new and old config don't need panning, or if
98 the config hasn't changed, we're done.
101 if (!_force_reselect && _panner && (_panner->in().n_audio() == nins) && (_panner->out().n_audio() == nouts)) {
104 _force_reselect = false;
106 if (nouts < 2 || nins == 0) {
107 /* no need for panning with less than 2 outputs or no inputs */
110 _current_panner_uri = "";
111 _panner_gui_uri = "";
112 if (!_is_send || !_panlinked) {
113 pannable()->set_panner(_panner);
115 Changed (); /* EMIT SIGNAL */
120 PannerInfo* pi = PannerManager::instance().select_panner (in, out, _user_selected_panner_uri);
122 cerr << "No panner found: check that panners are being discovered correctly during startup.\n";
126 DEBUG_TRACE (DEBUG::Panning, string_compose (_("select panner: %1\n"), pi->descriptor.name.c_str()));
128 boost::shared_ptr<Speakers> speakers = _session.get_speakers ();
130 if (nouts != speakers->size()) {
131 /* hmm, output count doesn't match session speaker count so
132 create a new speaker set.
134 Speakers* s = new Speakers ();
135 s->setup_default_speakers (nouts);
139 /* TODO don't allow to link _is_send if internal & route panners are different types */
140 Panner* p = pi->descriptor.factory (pannable(), speakers);
141 // boost_debug_shared_ptr_mark_interesting (p, "Panner");
143 _panner->configure_io (in, out);
144 _current_panner_uri = pi->descriptor.panner_uri;
145 _panner_gui_uri = pi->descriptor.gui_uri;
147 if (!_is_send || !_panlinked) {
148 pannable()->set_panner(_panner);
150 Changed (); /* EMIT SIGNAL */
154 PannerShell::get_state ()
156 XMLNode* node = new XMLNode ("PannerShell");
158 node->add_property (X_("bypassed"), _bypassed ? X_("yes") : X_("no"));
159 node->add_property (X_("user-panner"), _user_selected_panner_uri);
160 node->add_property (X_("linked-to-route"), _panlinked ? X_("yes") : X_("no"));
162 if (_panner && _is_send) {
163 node->add_child_nocopy (_panner->get_state ());
170 PannerShell::set_state (const XMLNode& node, int version)
172 XMLNodeList nlist = node.children ();
173 XMLNodeConstIterator niter;
174 const XMLProperty *prop;
175 LocaleGuard lg (X_("POSIX"));
177 if ((prop = node.property (X_("bypassed"))) != 0) {
178 set_bypassed (string_is_affirmative (prop->value ()));
181 if ((prop = node.property (X_("linked-to-route"))) != 0) {
182 _panlinked = string_is_affirmative (prop->value ());
185 if ((prop = node.property (X_("user-panner"))) != 0) {
186 _user_selected_panner_uri = prop->value ();
191 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
193 if ((*niter)->name() == X_("Panner")) {
195 if ((prop = (*niter)->property (X_("uri")))) {
196 PannerInfo* p = PannerManager::instance().get_by_uri(prop->value());
198 _panner.reset (p->descriptor.factory (
199 _is_send ? _pannable_internal : _pannable_route, _session.get_speakers ()));
200 _current_panner_uri = p->descriptor.panner_uri;
201 _panner_gui_uri = p->descriptor.gui_uri;
204 _pannable_internal->set_panner(_panner);
206 _force_reselect = true;
209 _pannable_route->set_panner(_panner);
211 if (_panner->set_state (**niter, version) == 0) {
217 else /* backwards compatibility */
218 if ((prop = (*niter)->property (X_("type")))) {
220 list<PannerInfo*>::iterator p;
221 PannerManager& pm (PannerManager::instance());
223 for (p = pm.panner_info.begin(); p != pm.panner_info.end(); ++p) {
224 if (prop->value() == (*p)->descriptor.name) {
226 /* note that we assume that all the stream panners
227 are of the same type. pretty good
228 assumption, but it's still an assumption.
231 _panner.reset ((*p)->descriptor.factory (
232 _is_send ? _pannable_internal : _pannable_route, _session.get_speakers ()));
233 _current_panner_uri = (*p)->descriptor.panner_uri;
234 _panner_gui_uri = (*p)->descriptor.gui_uri;
236 if (_panner->set_state (**niter, version) == 0) {
244 if (p == pm.panner_info.end()) {
245 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
251 error << _("panner plugin node has no type information!")
263 PannerShell::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, pframes_t nframes, gain_t gain_coeff)
265 if (outbufs.count().n_audio() == 0) {
266 // Don't want to lose audio...
267 assert(inbufs.count().n_audio() == 0);
271 if (outbufs.count().n_audio() == 1) {
273 /* just one output: no real panning going on */
275 AudioBuffer& dst = outbufs.get_audio(0);
277 if (gain_coeff == 0.0f) {
279 /* gain was zero, so make it silent */
281 dst.silence (nframes);
283 } else if (gain_coeff == 1.0f){
285 /* mix all input buffers into the output */
288 dst.read_from(inbufs.get_audio(0), nframes);
290 // accumulate starting with the second
291 if (inbufs.count().n_audio() > 0) {
292 BufferSet::audio_iterator i = inbufs.audio_begin();
293 for (++i; i != inbufs.audio_end(); ++i) {
294 dst.merge_from(*i, nframes);
300 /* mix all buffers into the output, scaling them all by the gain */
303 dst.read_from(inbufs.get_audio(0), nframes);
305 // accumulate (with gain) 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.accumulate_with_gain_from(*i, nframes, gain_coeff);
318 /* multiple outputs ... we must have a panner */
322 /* setup silent buffers so that we can mix into the outbuffers (slightly suboptimal -
323 better to copy the first set of data then mix after that, but hey, its 2011)
326 for (BufferSet::audio_iterator b = outbufs.audio_begin(); b != outbufs.audio_end(); ++b) {
327 (*b).silence (nframes);
330 _panner->distribute (inbufs, outbufs, gain_coeff, nframes);
334 PannerShell::run (BufferSet& inbufs, BufferSet& outbufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
336 if (inbufs.count().n_audio() == 0) {
337 /* Input has no audio buffers (e.g. Aux Send in a MIDI track at a
338 point with no audio because there is no preceding instrument)
340 outbufs.silence(nframes, 0);
344 if (outbufs.count().n_audio() == 0) {
345 // Failing to deliver audio we were asked to deliver is a bug
346 assert(inbufs.count().n_audio() == 0);
350 if (outbufs.count().n_audio() == 1) {
352 /* one output only: no panner */
354 AudioBuffer& dst = outbufs.get_audio(0);
356 // FIXME: apply gain automation?
359 dst.read_from (inbufs.get_audio(0), nframes);
361 // accumulate starting with the second
362 BufferSet::audio_iterator i = inbufs.audio_begin();
363 for (++i; i != inbufs.audio_end(); ++i) {
364 dst.merge_from (*i, nframes);
370 // More than 1 output
372 AutoState as = _panner->automation_state ();
374 // If we shouldn't play automation defer to distribute_no_automation
376 if (!(as & Play || ((as & Touch) && !_panner->touching()))) {
379 gain_t gain_coeff = 1.0;
381 if (fabsf(_session.transport_speed()) > 1.5f && Config->get_quieten_at_speed ()) {
382 gain_coeff = speed_quietning;
385 distribute_no_automation (inbufs, outbufs, nframes, gain_coeff);
389 /* setup the terrible silence so that we can mix into the outbuffers (slightly suboptimal -
390 better to copy the first set of data then mix after that, but hey, its 2011)
392 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
396 _panner->distribute_automated (inbufs, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
401 PannerShell::set_bypassed (bool yn)
403 if (yn == _bypassed) {
408 Changed (); /* EMIT SIGNAL */
412 PannerShell::bypassed () const
417 /* set custom-panner config
419 * This function is intended to be only called from
420 * Route::set_custom_panner()
421 * which will trigger IO-reconfigutaion if this fn return true
424 PannerShell::set_user_selected_panner_uri (std::string const uri)
426 if (uri == _user_selected_panner_uri) return false;
427 _user_selected_panner_uri = uri;
428 if (uri == _current_panner_uri) return false;
429 _force_reselect = true;
434 PannerShell::select_panner_by_uri (std::string const uri)
436 if (uri == _user_selected_panner_uri) return false;
437 _user_selected_panner_uri = uri;
438 if (uri == _current_panner_uri) return false;
439 _force_reselect = true;
441 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
442 ChanCount in = _panner->in();
443 ChanCount out = _panner->out();
444 configure_io(in, out);
445 if (!_is_send || !_panlinked) {
446 pannable()->set_panner(_panner);
448 _session.set_dirty ();
454 PannerShell::set_linked_to_route (bool onoff)
457 if (onoff == _panlinked) {
461 /* set _pannable-_has_state = true
462 * this way the panners will pick it up
463 * when it is re-created
466 XMLNode state = pannable()->get_state();
467 pannable()->set_state(state, 3000);
472 _force_reselect = true;
474 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
475 ChanCount in = _panner->in();
476 ChanCount out = _panner->out();
477 configure_io(in, out);
479 pannable()->set_panner(_panner);
481 _session.set_dirty ();