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)
65 : SessionObject (s, name)
68 , _current_panner_uri("")
69 , _user_selected_panner_uri("")
71 , _force_reselect (false)
76 PannerShell::~PannerShell ()
78 DEBUG_TRACE(DEBUG::Destruction, string_compose ("panner shell %3 for %1 destructor, panner is %4, pannable is %2\n", _name, _pannable, this, _panner));
82 PannerShell::configure_io (ChanCount in, ChanCount out)
84 uint32_t nouts = out.n_audio();
85 uint32_t nins = in.n_audio();
87 /* if new and old config don't need panning, or if
88 the config hasn't changed, we're done.
91 if (!_force_reselect && _panner && (_panner->in().n_audio() == nins) && (_panner->out().n_audio() == nouts)) {
95 if (nouts < 2 || nins == 0) {
96 /* no need for panning with less than 2 outputs or no inputs */
99 _current_panner_uri = "";
100 _panner_gui_uri = "";
101 Changed (); /* EMIT SIGNAL */
106 PannerInfo* pi = PannerManager::instance().select_panner (in, out, _user_selected_panner_uri);
108 cerr << "No panner found: check that panners are being discovered correctly during startup.\n";
112 DEBUG_TRACE (DEBUG::Panning, string_compose (_("select panner: %1\n"), pi->descriptor.name.c_str()));
114 boost::shared_ptr<Speakers> speakers = _session.get_speakers ();
116 if (nouts != speakers->size()) {
117 /* hmm, output count doesn't match session speaker count so
118 create a new speaker set.
120 Speakers* s = new Speakers ();
121 s->setup_default_speakers (nouts);
125 Panner* p = pi->descriptor.factory (_pannable, speakers);
126 // boost_debug_shared_ptr_mark_interesting (p, "Panner");
128 _panner->configure_io (in, out);
129 _current_panner_uri = pi->descriptor.panner_uri;
130 _panner_gui_uri = pi->descriptor.gui_uri;
132 Changed (); /* EMIT SIGNAL */
136 PannerShell::get_state ()
138 XMLNode* node = new XMLNode ("PannerShell");
140 node->add_property (X_("bypassed"), _bypassed ? X_("yes") : X_("no"));
141 node->add_property (X_("user-panner"), _user_selected_panner_uri);
144 node->add_child_nocopy (_panner->get_state ());
151 PannerShell::set_state (const XMLNode& node, int version)
153 XMLNodeList nlist = node.children ();
154 XMLNodeConstIterator niter;
155 const XMLProperty *prop;
156 LocaleGuard lg (X_("POSIX"));
158 if ((prop = node.property (X_("bypassed"))) != 0) {
159 set_bypassed (string_is_affirmative (prop->value ()));
162 if ((prop = node.property (X_("user-panner"))) != 0) {
163 _user_selected_panner_uri = prop->value ();
168 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
170 if ((*niter)->name() == X_("Panner")) {
172 if ((prop = (*niter)->property (X_("uri")))) {
173 PannerInfo* p = PannerManager::instance().get_by_uri(prop->value());
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) {
184 else /* backwards compatibility */
185 if ((prop = (*niter)->property (X_("type")))) {
187 list<PannerInfo*>::iterator p;
188 PannerManager& pm (PannerManager::instance());
190 for (p = pm.panner_info.begin(); p != pm.panner_info.end(); ++p) {
191 if (prop->value() == (*p)->descriptor.name) {
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.
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;
202 if (_panner->set_state (**niter, version) == 0) {
210 if (p == pm.panner_info.end()) {
211 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
217 error << _("panner plugin node has no type information!")
229 PannerShell::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, pframes_t nframes, gain_t gain_coeff)
231 if (outbufs.count().n_audio() == 0) {
232 // Don't want to lose audio...
233 assert(inbufs.count().n_audio() == 0);
237 if (outbufs.count().n_audio() == 1) {
239 /* just one output: no real panning going on */
241 AudioBuffer& dst = outbufs.get_audio(0);
243 if (gain_coeff == 0.0f) {
245 /* gain was zero, so make it silent */
247 dst.silence (nframes);
249 } else if (gain_coeff == 1.0f){
251 /* mix all input buffers into the output */
254 dst.read_from(inbufs.get_audio(0), nframes);
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);
266 /* mix all buffers into the output, scaling them all by the gain */
269 dst.read_from(inbufs.get_audio(0), nframes);
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);
284 /* multiple outputs ... we must have a panner */
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)
292 for (BufferSet::audio_iterator b = outbufs.audio_begin(); b != outbufs.audio_end(); ++b) {
293 (*b).silence (nframes);
296 _panner->distribute (inbufs, outbufs, gain_coeff, nframes);
300 PannerShell::run (BufferSet& inbufs, BufferSet& outbufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
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)
306 outbufs.silence(nframes, 0);
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);
316 if (outbufs.count().n_audio() == 1) {
318 /* one output only: no panner */
320 AudioBuffer& dst = outbufs.get_audio(0);
322 // FIXME: apply gain automation?
325 dst.read_from (inbufs.get_audio(0), nframes);
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);
336 // More than 1 output
338 AutoState as = _panner->automation_state ();
340 // If we shouldn't play automation defer to distribute_no_automation
342 if (!(as & Play || ((as & Touch) && !_panner->touching()))) {
345 gain_t gain_coeff = 1.0;
347 if (fabsf(_session.transport_speed()) > 1.5f && Config->get_quieten_at_speed ()) {
348 gain_coeff = speed_quietning;
351 distribute_no_automation (inbufs, outbufs, nframes, gain_coeff);
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)
358 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
362 _panner->distribute_automated (inbufs, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
367 PannerShell::set_bypassed (bool yn)
369 if (yn == _bypassed) {
374 Changed (); /* EMIT SIGNAL */
378 PannerShell::bypassed () const
383 /* set custom-panner config
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
390 PannerShell::set_user_selected_panner_uri (std::string const uri)
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;
400 PannerShell::select_panner_by_uri (std::string const uri)
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;
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 ();