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/convert.h"
37 #include "pbd/error.h"
38 #include "pbd/failed_constructor.h"
39 #include "pbd/xml++.h"
40 #include "pbd/enumwriter.h"
42 #include "evoral/Curve.hpp"
44 #include "ardour/audio_buffer.h"
45 #include "ardour/audio_buffer.h"
46 #include "ardour/automatable.h"
47 #include "ardour/buffer_set.h"
48 #include "ardour/pannable.h"
49 #include "ardour/panner.h"
50 #include "ardour/panner_manager.h"
51 #include "ardour/panner_shell.h"
52 #include "ardour/runtime_functions.h"
53 #include "ardour/session.h"
54 #include "ardour/utils.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)
71 PannerShell::~PannerShell ()
76 PannerShell::configure_io (ChanCount in, ChanCount out)
78 uint32_t nouts = out.n_audio();
79 uint32_t nins = in.n_audio();
81 /* if new and old config don't need panning, or if
82 the config hasn't changed, we're done.
85 if (_panner && _panner->in().n_audio() == nins && _panner->out().n_audio() == nouts) {
89 if (nouts < 2 || nins == 0) {
90 /* no need for panning with less than 2 outputs or no inputs */
93 Changed (); /* EMIT SIGNAL */
98 PannerInfo* pi = PannerManager::instance().select_panner (in, out);
104 _panner.reset (pi->descriptor.factory (_pannable, _session.get_speakers()));
106 Changed (); /* EMIT SIGNAL */
110 PannerShell::get_state (void)
116 PannerShell::state (bool full)
118 XMLNode* node = new XMLNode ("PannerShell");
121 node->add_child_nocopy (_panner->state (full));
128 PannerShell::set_state (const XMLNode& node, int version)
130 XMLNodeList nlist = node.children ();
131 XMLNodeConstIterator niter;
132 const XMLProperty *prop;
133 LocaleGuard lg (X_("POSIX"));
137 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
139 if ((*niter)->name() == X_("Panner")) {
141 if ((prop = (*niter)->property (X_("type")))) {
143 list<PannerInfo*>::iterator p;
144 PannerManager& pm (PannerManager::instance());
146 for (p = pm.panner_info.begin(); p != pm.panner_info.end(); ++p) {
147 if (prop->value() == (*p)->descriptor.name) {
149 /* note that we assume that all the stream panners
150 are of the same type. pretty good
151 assumption, but it's still an assumption.
154 _panner.reset ((*p)->descriptor.factory (_pannable, _session.get_speakers ()));
156 if (_panner->set_state (**niter, version) == 0) {
164 if (p == pm.panner_info.end()) {
165 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
171 error << _("panner plugin node has no type information!")
183 PannerShell::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, pframes_t nframes, gain_t gain_coeff)
185 if (outbufs.count().n_audio() == 0) {
186 // Don't want to lose audio...
187 assert(inbufs.count().n_audio() == 0);
191 if (outbufs.count().n_audio() == 1) {
193 /* just one output: no real panning going on */
195 AudioBuffer& dst = outbufs.get_audio(0);
197 if (gain_coeff == 0.0f) {
199 /* gain was zero, so make it silent */
201 dst.silence (nframes);
203 } else if (gain_coeff == 1.0f){
205 /* mix all input buffers into the output */
208 dst.read_from(inbufs.get_audio(0), nframes);
210 // accumulate starting with the second
211 if (inbufs.count().n_audio() > 0) {
212 BufferSet::audio_iterator i = inbufs.audio_begin();
213 for (++i; i != inbufs.audio_end(); ++i) {
214 dst.merge_from(*i, nframes);
220 /* mix all buffers into the output, scaling them all by the gain */
223 dst.read_from(inbufs.get_audio(0), nframes);
225 // accumulate (with gain) starting with the second
226 if (inbufs.count().n_audio() > 0) {
227 BufferSet::audio_iterator i = inbufs.audio_begin();
228 for (++i; i != inbufs.audio_end(); ++i) {
229 dst.accumulate_with_gain_from(*i, nframes, gain_coeff);
238 /* multiple outputs ... we must have a panner */
242 /* setup silent buffers so that we can mix into the outbuffers (slightly suboptimal -
243 better to copy the first set of data then mix after that, but hey, its 2011)
246 for (BufferSet::audio_iterator b = outbufs.audio_begin(); b != outbufs.audio_end(); ++b) {
247 (*b).silence (nframes);
250 _panner->distribute (inbufs, outbufs, gain_coeff, nframes);
254 PannerShell::run (BufferSet& inbufs, BufferSet& outbufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
256 if (outbufs.count().n_audio() == 0) {
257 // Failing to deliver audio we were asked to deliver is a bug
258 assert(inbufs.count().n_audio() == 0);
262 if (outbufs.count().n_audio() == 1) {
264 /* one output only: no panner */
266 AudioBuffer& dst = outbufs.get_audio(0);
268 // FIXME: apply gain automation?
271 dst.read_from (inbufs.get_audio(0), nframes);
273 // accumulate starting with the second
274 BufferSet::audio_iterator i = inbufs.audio_begin();
275 for (++i; i != inbufs.audio_end(); ++i) {
276 dst.merge_from (*i, nframes);
282 // More than 1 output
284 AutoState as = _panner->automation_state ();
286 // If we shouldn't play automation defer to distribute_no_automation
288 if (!(as & Play || ((as & Touch) && !_panner->touching()))) {
291 gain_t gain_coeff = 1.0;
293 if (fabsf(_session.transport_speed()) > 1.5f && Config->get_quieten_at_speed ()) {
294 gain_coeff = speed_quietning;
297 distribute_no_automation (inbufs, outbufs, nframes, gain_coeff);
301 /* setup the terrible silence so that we can mix into the outbuffers (slightly suboptimal -
302 better to copy the first set of data then mix after that, but hey, its 2011)
304 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
308 _panner->distribute_automated (inbufs, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());