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/buffer_set.h"
47 #include "ardour/debug.h"
48 #include "ardour/panner.h"
49 #include "ardour/panner_manager.h"
50 #include "ardour/panner_shell.h"
51 #include "ardour/session.h"
52 #include "ardour/speakers.h"
56 #include "pbd/mathfix.h"
59 using namespace ARDOUR;
62 PannerShell::PannerShell (string name, Session& s, boost::shared_ptr<Pannable> p)
63 : SessionObject (s, name)
66 , _current_panner_uri("")
67 , _user_selected_panner_uri("")
69 , _force_reselect (false)
74 PannerShell::~PannerShell ()
76 DEBUG_TRACE(DEBUG::Destruction, string_compose ("panner shell %3 for %1 destructor, panner is %4, pannable is %2\n", _name, _pannable, this, _panner));
80 PannerShell::configure_io (ChanCount in, ChanCount out)
82 uint32_t nouts = out.n_audio();
83 uint32_t nins = in.n_audio();
85 /* if new and old config don't need panning, or if
86 the config hasn't changed, we're done.
89 if (!_force_reselect && _panner && (_panner->in().n_audio() == nins) && (_panner->out().n_audio() == nouts)) {
93 if (nouts < 2 || nins == 0) {
94 /* no need for panning with less than 2 outputs or no inputs */
97 _current_panner_uri = "";
99 Changed (); /* EMIT SIGNAL */
104 PannerInfo* pi = PannerManager::instance().select_panner (in, out, _user_selected_panner_uri);
106 cerr << "No panner found: check that panners are being discovered correctly during startup.\n";
110 DEBUG_TRACE (DEBUG::Panning, string_compose (_("select panner: %1\n"), pi->descriptor.name.c_str()));
112 boost::shared_ptr<Speakers> speakers = _session.get_speakers ();
114 if (nouts != speakers->size()) {
115 /* hmm, output count doesn't match session speaker count so
116 create a new speaker set.
118 Speakers* s = new Speakers ();
119 s->setup_default_speakers (nouts);
123 Panner* p = pi->descriptor.factory (_pannable, speakers);
124 // boost_debug_shared_ptr_mark_interesting (p, "Panner");
126 _panner->configure_io (in, out);
127 _current_panner_uri = pi->descriptor.panner_uri;
128 _panner_gui_uri = pi->descriptor.gui_uri;
130 Changed (); /* EMIT SIGNAL */
134 PannerShell::get_state ()
136 XMLNode* node = new XMLNode ("PannerShell");
138 node->add_property (X_("bypassed"), _bypassed ? X_("yes") : X_("no"));
139 node->add_property (X_("user-panner"), _user_selected_panner_uri);
142 node->add_child_nocopy (_panner->get_state ());
149 PannerShell::set_state (const XMLNode& node, int version)
151 XMLNodeList nlist = node.children ();
152 XMLNodeConstIterator niter;
153 const XMLProperty *prop;
154 LocaleGuard lg (X_("POSIX"));
156 if ((prop = node.property (X_("bypassed"))) != 0) {
157 set_bypassed (string_is_affirmative (prop->value ()));
160 if ((prop = node.property (X_("user-panner"))) != 0) {
161 _user_selected_panner_uri = prop->value ();
166 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
168 if ((*niter)->name() == X_("Panner")) {
170 if ((prop = (*niter)->property (X_("uri")))) {
171 PannerInfo* p = PannerManager::instance().get_by_uri(prop->value());
173 _panner.reset (p->descriptor.factory (_pannable, _session.get_speakers ()));
174 _current_panner_uri = p->descriptor.panner_uri;
175 _panner_gui_uri = p->descriptor.gui_uri;
176 if (_panner->set_state (**niter, version) == 0) {
182 else /* backwards compatibility */
183 if ((prop = (*niter)->property (X_("type")))) {
185 list<PannerInfo*>::iterator p;
186 PannerManager& pm (PannerManager::instance());
188 for (p = pm.panner_info.begin(); p != pm.panner_info.end(); ++p) {
189 if (prop->value() == (*p)->descriptor.name) {
191 /* note that we assume that all the stream panners
192 are of the same type. pretty good
193 assumption, but it's still an assumption.
196 _panner.reset ((*p)->descriptor.factory (_pannable, _session.get_speakers ()));
197 _current_panner_uri = (*p)->descriptor.panner_uri;
198 _panner_gui_uri = (*p)->descriptor.gui_uri;
200 if (_panner->set_state (**niter, version) == 0) {
208 if (p == pm.panner_info.end()) {
209 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
215 error << _("panner plugin node has no type information!")
227 PannerShell::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, pframes_t nframes, gain_t gain_coeff)
229 if (outbufs.count().n_audio() == 0) {
230 // Don't want to lose audio...
231 assert(inbufs.count().n_audio() == 0);
235 if (outbufs.count().n_audio() == 1) {
237 /* just one output: no real panning going on */
239 AudioBuffer& dst = outbufs.get_audio(0);
241 if (gain_coeff == 0.0f) {
243 /* gain was zero, so make it silent */
245 dst.silence (nframes);
247 } else if (gain_coeff == 1.0f){
249 /* mix all input buffers into the output */
252 dst.read_from(inbufs.get_audio(0), nframes);
254 // accumulate starting with the second
255 if (inbufs.count().n_audio() > 0) {
256 BufferSet::audio_iterator i = inbufs.audio_begin();
257 for (++i; i != inbufs.audio_end(); ++i) {
258 dst.merge_from(*i, nframes);
264 /* mix all buffers into the output, scaling them all by the gain */
267 dst.read_from(inbufs.get_audio(0), nframes);
269 // accumulate (with gain) starting with the second
270 if (inbufs.count().n_audio() > 0) {
271 BufferSet::audio_iterator i = inbufs.audio_begin();
272 for (++i; i != inbufs.audio_end(); ++i) {
273 dst.accumulate_with_gain_from(*i, nframes, gain_coeff);
282 /* multiple outputs ... we must have a panner */
286 /* setup silent buffers so that we can mix into the outbuffers (slightly suboptimal -
287 better to copy the first set of data then mix after that, but hey, its 2011)
290 for (BufferSet::audio_iterator b = outbufs.audio_begin(); b != outbufs.audio_end(); ++b) {
291 (*b).silence (nframes);
294 _panner->distribute (inbufs, outbufs, gain_coeff, nframes);
298 PannerShell::run (BufferSet& inbufs, BufferSet& outbufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
300 if (inbufs.count().n_audio() == 0) {
301 /* Input has no audio buffers (e.g. Aux Send in a MIDI track at a
302 point with no audio because there is no preceding instrument)
304 outbufs.silence(nframes, 0);
308 if (outbufs.count().n_audio() == 0) {
309 // Failing to deliver audio we were asked to deliver is a bug
310 assert(inbufs.count().n_audio() == 0);
314 if (outbufs.count().n_audio() == 1) {
316 /* one output only: no panner */
318 AudioBuffer& dst = outbufs.get_audio(0);
320 // FIXME: apply gain automation?
323 dst.read_from (inbufs.get_audio(0), nframes);
325 // accumulate starting with the second
326 BufferSet::audio_iterator i = inbufs.audio_begin();
327 for (++i; i != inbufs.audio_end(); ++i) {
328 dst.merge_from (*i, nframes);
334 // More than 1 output
336 AutoState as = _panner->automation_state ();
338 // If we shouldn't play automation defer to distribute_no_automation
340 if (!(as & Play || ((as & Touch) && !_panner->touching()))) {
343 gain_t gain_coeff = 1.0;
345 if (fabsf(_session.transport_speed()) > 1.5f && Config->get_quieten_at_speed ()) {
346 gain_coeff = speed_quietning;
349 distribute_no_automation (inbufs, outbufs, nframes, gain_coeff);
353 /* setup the terrible silence so that we can mix into the outbuffers (slightly suboptimal -
354 better to copy the first set of data then mix after that, but hey, its 2011)
356 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
360 _panner->distribute_automated (inbufs, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
365 PannerShell::set_bypassed (bool yn)
367 if (yn == _bypassed) {
372 Changed (); /* EMIT SIGNAL */
376 PannerShell::bypassed () const
381 /* set custom-panner config
383 * This function is intended to be only called from
384 * Route::set_custom_panner()
385 * which will trigger IO-reconfigutaion if this fn return true
388 PannerShell::set_user_selected_panner_uri (std::string const uri)
390 if (uri == _user_selected_panner_uri) return false;
391 _user_selected_panner_uri = uri;
392 if (uri == _current_panner_uri) return false;
393 _force_reselect = true;