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.
33 #include "pbd/cartesian.h"
34 #include "pbd/convert.h"
35 #include "pbd/error.h"
36 #include "pbd/failed_constructor.h"
37 #include "pbd/xml++.h"
38 #include "pbd/enumwriter.h"
40 #include "evoral/Curve.hpp"
42 #include "ardour/session.h"
43 #include "ardour/panner.h"
44 #include "ardour/utils.h"
45 #include "ardour/audio_buffer.h"
47 #include "ardour/debug.h"
48 #include "ardour/runtime_functions.h"
49 #include "ardour/buffer_set.h"
50 #include "ardour/audio_buffer.h"
51 #include "ardour/pannable.h"
52 #include "ardour/profile.h"
55 #include "panner_1in2out.h"
57 #include "pbd/mathfix.h"
60 using namespace ARDOUR;
63 static PanPluginDescriptor _descriptor = {
64 "Mono to Stereo Panner",
65 "http://ardour.org/plugin/panner_1in2out",
66 "http://ardour.org/plugin/panner_1in2out#ui",
69 Panner1in2out::factory
72 extern "C" ARDOURPANNER_API PanPluginDescriptor* panner_descriptor () { return &_descriptor; }
74 Panner1in2out::Panner1in2out (boost::shared_ptr<Pannable> p)
77 if (!Profile->get_trx () ) {
78 if (!_pannable->has_state ()) {
79 _pannable->pan_azimuth_control->set_value (0.5, Controllable::NoGroup);
86 right = desired_right;
90 _pannable->pan_azimuth_control->Changed.connect_same_thread (*this, boost::bind (&Panner1in2out::update, this));
93 Panner1in2out::~Panner1in2out ()
98 Panner1in2out::update ()
101 float const pan_law_attenuation = -3.0f;
102 float const scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
104 panR = _pannable->pan_azimuth_control->get_value();
107 desired_left = panL * (scale * panL + 1.0f - scale);
108 desired_right = panR * (scale * panR + 1.0f - scale);
112 Panner1in2out::set_position (double p)
114 if (clamp_position (p)) {
115 _pannable->pan_azimuth_control->set_value (p, Controllable::NoGroup);
120 Panner1in2out::clamp_position (double& p)
122 /* any position between 0.0 and 1.0 is legal */
123 DEBUG_TRACE (DEBUG::Panning, string_compose ("want to move panner to %1 - always allowed in 0.0-1.0 range\n", p));
124 p = max (min (p, 1.0), 0.0);
129 Panner1in2out::position_range () const
131 return make_pair (0, 1);
135 Panner1in2out::position () const
137 return _pannable->pan_azimuth_control->get_value ();
141 Panner1in2out::distribute_one (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, pframes_t nframes, uint32_t /* not used */)
143 assert (obufs.count().n_audio() == 2);
149 Sample* const src = srcbuf.data();
153 dst = obufs.get_audio(0).data();
155 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
157 /* we've moving the pan by an appreciable amount, so we must
158 interpolate over 64 samples or nframes, whichever is smaller */
160 pframes_t const limit = min ((pframes_t) 64, nframes);
163 delta = -(delta / (float) (limit));
165 for (n = 0; n < limit; n++) {
166 left_interp = left_interp + delta;
167 left = left_interp + 0.9 * (left - left_interp);
168 dst[n] += src[n] * left * gain_coeff;
171 /* then pan the rest of the buffer; no need for interpolation for this bit */
173 pan = left * gain_coeff;
175 mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
182 if ((pan = (left * gain_coeff)) != 1.0f) {
186 /* pan is 1 but also not 0, so we must do it "properly" */
188 mix_buffers_with_gain(dst,src,nframes,pan);
190 /* mark that we wrote into the buffer */
198 /* pan is 1 so we can just copy the input samples straight in */
200 mix_buffers_no_gain(dst,src,nframes);
202 /* XXX it would be nice to mark that we wrote into the buffer */
208 dst = obufs.get_audio(1).data();
210 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
212 /* we're moving the pan by an appreciable amount, so we must
213 interpolate over 64 samples or nframes, whichever is smaller */
215 pframes_t const limit = min ((pframes_t) 64, nframes);
218 delta = -(delta / (float) (limit));
220 for (n = 0; n < limit; n++) {
221 right_interp = right_interp + delta;
222 right = right_interp + 0.9 * (right - right_interp);
223 dst[n] += src[n] * right * gain_coeff;
226 /* then pan the rest of the buffer, no need for interpolation for this bit */
228 pan = right * gain_coeff;
230 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
232 /* XXX it would be nice to mark the buffer as written to */
236 right = desired_right;
237 right_interp = right;
239 if ((pan = (right * gain_coeff)) != 1.0f) {
243 /* pan is not 1 but also not 0, so we must do it "properly" */
245 mix_buffers_with_gain(dst,src,nframes,pan);
247 /* XXX it would be nice to mark the buffer as written to */
252 /* pan is 1 so we can just copy the input samples straight in */
254 mix_buffers_no_gain(dst,src,nframes);
256 /* XXX it would be nice to mark the buffer as written to */
263 Panner1in2out::distribute_one_automated (AudioBuffer& srcbuf, BufferSet& obufs,
264 samplepos_t start, samplepos_t end, pframes_t nframes,
265 pan_t** buffers, uint32_t which)
267 assert (obufs.count().n_audio() == 2);
271 Sample* const src = srcbuf.data();
272 pan_t* const position = buffers[0];
274 /* fetch positional data */
276 if (!_pannable->pan_azimuth_control->list()->curve().rt_safe_get_vector (start, end, position, nframes)) {
278 distribute_one (srcbuf, obufs, 1.0, nframes, which);
282 /* apply pan law to convert positional data into pan coefficients for
286 const float pan_law_attenuation = -3.0f;
287 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
289 for (pframes_t n = 0; n < nframes; ++n) {
291 float panR = position[n];
292 const float panL = 1 - panR;
294 /* note that are overwriting buffers, but its OK
295 because we're finished with their old contents
296 (position automation data) and are
297 replacing it with panning/gain coefficients
298 that we need to actually process the data.
301 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
302 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
307 dst = obufs.get_audio(0).data();
310 for (pframes_t n = 0; n < nframes; ++n) {
311 dst[n] += src[n] * pbuf[n];
314 /* XXX it would be nice to mark the buffer as written to */
318 dst = obufs.get_audio(1).data();
321 for (pframes_t n = 0; n < nframes; ++n) {
322 dst[n] += src[n] * pbuf[n];
325 /* XXX it would be nice to mark the buffer as written to */
330 Panner1in2out::factory (boost::shared_ptr<Pannable> p, boost::shared_ptr<Speakers> /* ignored */)
332 return new Panner1in2out (p);
336 Panner1in2out::get_state ()
338 XMLNode& root (Panner::get_state ());
339 root.set_property (X_("uri"), _descriptor.panner_uri);
340 /* this is needed to allow new sessions to load with old Ardour: */
341 root.set_property (X_("type"), _descriptor.name);
346 std::set<Evoral::Parameter>
347 Panner1in2out::what_can_be_automated() const
349 set<Evoral::Parameter> s;
350 s.insert (Evoral::Parameter (PanAzimuthAutomation));
355 Panner1in2out::describe_parameter (Evoral::Parameter p)
358 case PanAzimuthAutomation:
361 return _pannable->describe_parameter (p);
366 Panner1in2out::value_as_string (boost::shared_ptr<const AutomationControl> ac) const
368 /* DO NOT USE LocaleGuard HERE */
369 double val = ac->get_value();
371 switch (ac->parameter().type()) {
372 case PanAzimuthAutomation:
373 /* We show the position of the center of the image relative to the left & right.
374 This is expressed as a pair of percentage values that ranges from (100,0)
375 (hard left) through (50,50) (hard center) to (0,100) (hard right).
377 This is pretty wierd, but its the way audio engineers expect it. Just remember that
378 the center of the USA isn't Kansas, its (50LA, 50NY) and it will all make sense.
380 This is designed to be as narrow as possible. Dedicated
381 panner GUIs can do their own version of this if they need
382 something less compact.
385 return string_compose (_("L%1R%2"), (int) rint (100.0 * (1.0 - val)),
386 (int) rint (100.0 * val));
394 Panner1in2out::reset ()