2 Copyright (C) 2004 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.
20 #define __STDC_FORMAT_MACROS 1
36 #include "pbd/error.h"
37 #include "pbd/failed_constructor.h"
38 #include "pbd/xml++.h"
39 #include "pbd/enumwriter.h"
41 #include "evoral/Curve.hpp"
43 #include "ardour/session.h"
44 #include "ardour/panner.h"
45 #include "ardour/utils.h"
46 #include "ardour/audio_buffer.h"
48 #include "ardour/runtime_functions.h"
49 #include "ardour/buffer_set.h"
50 #include "ardour/audio_buffer.h"
54 #include "pbd/mathfix.h"
57 using namespace ARDOUR;
60 float Panner::current_automation_version_number = 1.0;
62 string EqualPowerStereoPanner::name = "Equal Power Stereo";
63 string Multi2dPanner::name = "Multiple (2D)";
65 /* this is a default mapper of control values to a pan position
66 others can be imagined.
69 static pan_t direct_control_to_pan (double fract)
74 StreamPanner::StreamPanner (Panner& p, Evoral::Parameter param)
77 assert (param.type() != NullAutomation);
82 /* get our AutomationControl from our parent Panner, creating it if required */
83 _control = boost::dynamic_pointer_cast<AutomationControl> (parent.control (param, true));
90 StreamPanner::~StreamPanner ()
95 StreamPanner::set_mono (bool yn)
104 Panner::PanControllable::set_value (double val)
106 panner.streampanner (parameter().id()).set_position (direct_control_to_pan (val));
107 AutomationControl::set_value(val);
111 Panner::PanControllable::get_value (void) const
113 return AutomationControl::get_value();
117 StreamPanner::set_muted (bool yn)
126 StreamPanner::set_position (float xpos, bool link_call)
128 if (!link_call && parent.linked()) {
129 parent.set_position (xpos, *this);
136 _control->Changed ();
141 StreamPanner::set_position (float xpos, float ypos, bool link_call)
143 if (!link_call && parent.linked()) {
144 parent.set_position (xpos, ypos, *this);
147 if (_x != xpos || _y != ypos) {
152 _control->Changed ();
157 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
159 if (!link_call && parent.linked()) {
160 parent.set_position (xpos, ypos, zpos, *this);
163 if (_x != xpos || _y != ypos || _z != zpos) {
169 _control->Changed ();
174 StreamPanner::set_state (const XMLNode& node, int /*version*/)
176 const XMLProperty* prop;
177 XMLNodeConstIterator iter;
179 if ((prop = node.property (X_("muted")))) {
180 set_muted (string_is_affirmative (prop->value()));
183 if ((prop = node.property (X_("mono")))) {
184 set_mono (string_is_affirmative (prop->value()));
191 StreamPanner::add_state (XMLNode& node)
193 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
194 node.add_property (X_("mono"), (_mono ? "yes" : "no"));
198 StreamPanner::distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
201 /* we're in mono mode, so just pan the input to all outputs equally */
202 int const N = parent.nouts ();
203 for (int i = 0; i < N; ++i) {
204 mix_buffers_with_gain (obufs.get_audio(i).data(), src.data(), nframes, gain_coeff);
207 /* normal mode, call the `real' distribute method */
208 do_distribute (src, obufs, gain_coeff, nframes);
213 StreamPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs,
214 nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers)
217 /* we're in mono mode, so just pan the input to all outputs equally */
218 int const N = parent.nouts ();
219 for (int i = 0; i < N; ++i) {
220 mix_buffers_with_gain (obufs.get_audio(i).data(), src.data(), nframes, 1.0);
223 /* normal mode, call the `real' distribute method */
224 do_distribute_automated (src, obufs, start, end, nframes, buffers);
230 /*---------------------------------------------------------------------- */
232 BaseStereoPanner::BaseStereoPanner (Panner& p, Evoral::Parameter param)
233 : StreamPanner (p, param)
237 , right_interp (right)
241 BaseStereoPanner::~BaseStereoPanner ()
246 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
249 LocaleGuard lg (X_("POSIX"));
251 _control->list()->clear ();
253 while (in.getline (line, sizeof (line), '\n')) {
259 if (strcmp (line, "end") == 0) {
263 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
264 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
268 _control->list()->fast_simple_add (when, value);
271 /* now that we are done loading */
273 ((AutomationList*)_control->list().get())->StateChanged ();
279 BaseStereoPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
281 assert(obufs.count().n_audio() == 2);
291 Sample* const src = srcbuf.data();
295 dst = obufs.get_audio(0).data();
297 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
299 /* we've moving the pan by an appreciable amount, so we must
300 interpolate over 64 frames or nframes, whichever is smaller */
302 nframes_t const limit = min ((nframes_t)64, nframes);
305 delta = -(delta / (float) (limit));
307 for (n = 0; n < limit; n++) {
308 left_interp = left_interp + delta;
309 left = left_interp + 0.9 * (left - left_interp);
310 dst[n] += src[n] * left * gain_coeff;
313 /* then pan the rest of the buffer; no need for interpolation for this bit */
315 pan = left * gain_coeff;
317 mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
324 if ((pan = (left * gain_coeff)) != 1.0f) {
328 /* pan is 1 but also not 0, so we must do it "properly" */
330 mix_buffers_with_gain(dst,src,nframes,pan);
332 /* mark that we wrote into the buffer */
340 /* pan is 1 so we can just copy the input samples straight in */
342 mix_buffers_no_gain(dst,src,nframes);
344 /* mark that we wrote into the buffer */
352 dst = obufs.get_audio(1).data();
354 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
356 /* we're moving the pan by an appreciable amount, so we must
357 interpolate over 64 frames or nframes, whichever is smaller */
359 nframes_t const limit = min ((nframes_t)64, nframes);
362 delta = -(delta / (float) (limit));
364 for (n = 0; n < limit; n++) {
365 right_interp = right_interp + delta;
366 right = right_interp + 0.9 * (right - right_interp);
367 dst[n] += src[n] * right * gain_coeff;
370 /* then pan the rest of the buffer, no need for interpolation for this bit */
372 pan = right * gain_coeff;
374 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
376 /* XXX it would be nice to mark the buffer as written to */
380 right = desired_right;
381 right_interp = right;
383 if ((pan = (right * gain_coeff)) != 1.0f) {
387 /* pan is not 1 but also not 0, so we must do it "properly" */
389 mix_buffers_with_gain(dst,src,nframes,pan);
391 /* XXX it would be nice to mark the buffer as written to */
396 /* pan is 1 so we can just copy the input samples straight in */
398 mix_buffers_no_gain(dst,src,nframes);
400 /* XXX it would be nice to mark the buffer as written to */
405 /*---------------------------------------------------------------------- */
407 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p, Evoral::Parameter param)
408 : BaseStereoPanner (p, param)
413 right = desired_right;
415 right_interp = right;
418 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
423 EqualPowerStereoPanner::update ()
425 /* it would be very nice to split this out into a virtual function
426 that can be accessed from BaseStereoPanner and used in do_distribute_automated().
428 but the place where its used in do_distribute_automated() is a tight inner loop,
429 and making "nframes" virtual function calls to compute values is an absurd
433 /* x == 0 => hard left
437 float const panR = _x;
438 float const panL = 1 - panR;
440 float const pan_law_attenuation = -3.0f;
441 float const scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
443 desired_left = panL * (scale * panL + 1.0f - scale);
444 desired_right = panR * (scale * panR + 1.0f - scale);
447 //_control->set_value(x);
451 EqualPowerStereoPanner::do_distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs,
452 nframes_t start, nframes_t end, nframes_t nframes,
455 assert (obufs.count().n_audio() == 2);
459 Sample* const src = srcbuf.data();
461 /* fetch positional data */
463 if (!_control->list()->curve().rt_safe_get_vector (start, end, buffers[0], nframes)) {
466 do_distribute (srcbuf, obufs, 1.0, nframes);
471 /* store effective pan position. do this even if we are muted */
474 effective_x = buffers[0][nframes-1];
481 /* apply pan law to convert positional data into pan coefficients for
485 const float pan_law_attenuation = -3.0f;
486 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
488 for (nframes_t n = 0; n < nframes; ++n) {
490 float const panR = buffers[0][n];
491 float const panL = 1 - panR;
493 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
494 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
499 dst = obufs.get_audio(0).data();
502 for (nframes_t n = 0; n < nframes; ++n) {
503 dst[n] += src[n] * pbuf[n];
506 /* XXX it would be nice to mark the buffer as written to */
510 dst = obufs.get_audio(1).data();
513 for (nframes_t n = 0; n < nframes; ++n) {
514 dst[n] += src[n] * pbuf[n];
517 /* XXX it would be nice to mark the buffer as written to */
521 EqualPowerStereoPanner::factory (Panner& parent, Evoral::Parameter param)
523 return new EqualPowerStereoPanner (parent, param);
527 EqualPowerStereoPanner::get_state (void)
533 EqualPowerStereoPanner::state (bool /*full_state*/)
535 XMLNode* root = new XMLNode ("StreamPanner");
537 LocaleGuard lg (X_("POSIX"));
539 snprintf (buf, sizeof (buf), "%.12g", _x);
540 root->add_property (X_("x"), buf);
541 root->add_property (X_("type"), EqualPowerStereoPanner::name);
543 // XXX: dont save automation here... its part of the automatable panner now.
545 StreamPanner::add_state (*root);
547 root->add_child_nocopy (_control->get_state ());
553 EqualPowerStereoPanner::set_state (const XMLNode& node, int version)
555 const XMLProperty* prop;
556 LocaleGuard lg (X_("POSIX"));
558 if ((prop = node.property (X_("x")))) {
559 const float pos = atof (prop->value().c_str());
560 set_position (pos, true);
563 StreamPanner::set_state (node, version);
565 for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
567 if ((*iter)->name() == X_("Controllable")) {
568 if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
569 _control->set_state (**iter, version);
572 } else if ((*iter)->name() == X_("Automation")) {
574 _control->alist()->set_state (*((*iter)->children().front()), version);
576 if (_control->alist()->automation_state() != Off) {
577 set_position (_control->list()->eval (parent.session().transport_frame()));
585 /*----------------------------------------------------------------------*/
587 Multi2dPanner::Multi2dPanner (Panner& p, Evoral::Parameter param)
588 : StreamPanner (p, param)
593 Multi2dPanner::~Multi2dPanner ()
598 Multi2dPanner::update ()
600 static const float BIAS = FLT_MIN;
602 uint32_t const nouts = parent.nouts ();
609 for (i = 0; i < nouts; i++) {
610 dsq[i] = ((_x - parent.output(i).x) * (_x - parent.output(i).x) + (_y - parent.output(i).y) * (_y - parent.output(i).y) + BIAS);
614 f += dsq[i] * dsq[i];
617 // terrible hack to support OSX < 10.3.9 builds
618 fr = (float) (1.0 / sqrt((double)f));
622 for (i = 0; i < nouts; ++i) {
623 parent.output(i).desired_pan = 1.0f - (dsq[i] * fr);
630 Multi2dPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
639 Sample* const src = srcbuf.data();
641 uint32_t const N = parent.nouts ();
642 for (uint32_t n = 0; n < N; ++n) {
643 Panner::Output& o = parent.output (n);
645 dst = obufs.get_audio(n).data();
648 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
650 /* interpolate over 64 frames or nframes, whichever is smaller */
652 nframes_t limit = min ((nframes_t)64, nframes);
655 delta = -(delta / (float) (limit));
657 for (n = 0; n < limit; n++) {
658 left_interp = left_interp + delta;
659 left = left_interp + 0.9 * (left - left_interp);
660 dst[n] += src[n] * left * gain_coeff;
663 pan = left * gain_coeff;
664 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
671 if ((pan *= gain_coeff) != 1.0f) {
674 mix_buffers_with_gain(dst,src,nframes,pan);
678 mix_buffers_no_gain(dst,src,nframes);
690 Multi2dPanner::do_distribute_automated (AudioBuffer& /*src*/, BufferSet& /*obufs*/,
691 nframes_t /*start*/, nframes_t /*end*/, nframes_t /*nframes*/,
704 Multi2dPanner::factory (Panner& p, Evoral::Parameter param)
706 return new Multi2dPanner (p, param);
710 Multi2dPanner::load (istream& /*in*/, string /*path*/, uint32_t& /*linecnt*/)
716 Multi2dPanner::get_state (void)
722 Multi2dPanner::state (bool /*full_state*/)
724 XMLNode* root = new XMLNode ("StreamPanner");
726 LocaleGuard lg (X_("POSIX"));
728 snprintf (buf, sizeof (buf), "%.12g", _x);
729 root->add_property (X_("x"), buf);
730 snprintf (buf, sizeof (buf), "%.12g", _y);
731 root->add_property (X_("y"), buf);
732 root->add_property (X_("type"), Multi2dPanner::name);
734 /* XXX no meaningful automation yet */
740 Multi2dPanner::set_state (const XMLNode& node, int /*version*/)
742 const XMLProperty* prop;
744 LocaleGuard lg (X_("POSIX"));
749 if ((prop = node.property (X_("x")))) {
750 newx = atof (prop->value().c_str());
753 if ((prop = node.property (X_("y")))) {
754 newy = atof (prop->value().c_str());
757 if (_x < 0 || _y < 0) {
758 error << _("badly-formed positional data for Multi2dPanner - ignored")
763 set_position (newx, newy);
767 /*---------------------------------------------------------------------- */
769 Panner::Panner (string name, Session& s)
770 : SessionObject (s, name)
773 //set_name_old_auto (name);
777 _link_direction = SameDirection;
787 Panner::set_linked (bool yn)
791 _session.set_dirty ();
792 LinkStateChanged (); /* EMIT SIGNAL */
797 Panner::set_link_direction (LinkDirection ld)
799 if (ld != _link_direction) {
800 _link_direction = ld;
801 _session.set_dirty ();
802 LinkStateChanged (); /* EMIT SIGNAL */
808 Panner::set_bypassed (bool yn)
810 if (yn != _bypassed) {
818 Panner::reset_to_default ()
820 vector<float> positions;
822 switch (outputs.size()) {
828 if (outputs.size() == 2) {
829 switch (_streampanners.size()) {
831 _streampanners.front()->set_position (0.5);
832 _streampanners.front()->pan_control()->list()->reset_default (0.5);
836 _streampanners.front()->set_position (0.0);
837 _streampanners.front()->pan_control()->list()->reset_default (0.0);
838 _streampanners.back()->set_position (1.0);
839 _streampanners.back()->pan_control()->list()->reset_default (1.0);
846 vector<Output>::iterator o;
847 vector<StreamPanner*>::iterator p;
849 for (o = outputs.begin(), p = _streampanners.begin(); o != outputs.end() && p != _streampanners.end(); ++o, ++p) {
850 (*p)->set_position ((*o).x, (*o).y);
855 Panner::reset_streampanner (uint32_t which)
857 if (which >= _streampanners.size() || which >= outputs.size()) {
861 switch (outputs.size()) {
867 switch (_streampanners.size()) {
869 /* stereo out, 1 stream, default = middle */
870 _streampanners.front()->set_position (0.5);
871 _streampanners.front()->pan_control()->list()->reset_default (0.5);
874 /* stereo out, 2 streams, default = hard left/right */
876 _streampanners.front()->set_position (0.0);
877 _streampanners.front()->pan_control()->list()->reset_default (0.0);
879 _streampanners.back()->set_position (1.0);
880 _streampanners.back()->pan_control()->list()->reset_default (1.0);
887 _streampanners[which]->set_position (outputs[which].x, outputs[which].y);
892 * Reset the panner with a given number of outs and panners (and hence inputs)
894 * \param nouts Number of outputs.
895 * \param npans Number of panners.
898 Panner::reset (uint32_t nouts, uint32_t npans)
901 bool changed = false;
902 bool do_not_and_did_not_need_panning = ((nouts < 2) && (outputs.size() < 2));
904 /* if new and old config don't need panning, or if
905 the config hasn't changed, we're done.
908 if (do_not_and_did_not_need_panning ||
909 ((nouts == outputs.size()) && (npans == _streampanners.size()))) {
913 n = _streampanners.size();
928 /* no need for panning with less than 2 outputs */
930 Changed (); /* EMIT SIGNAL */
937 /* XXX: this can never happen */
941 /* XXX: this can never happen */
942 fatal << _("programming error:")
943 << X_("Panner::reset() called with a single output")
949 outputs.push_back (Output (0, 0));
950 outputs.push_back (Output (1.0, 0));
952 for (n = 0; n < npans; ++n) {
953 _streampanners.push_back (new EqualPowerStereoPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
958 outputs.push_back (Output (0.5, 0));
959 outputs.push_back (Output (0, 1.0));
960 outputs.push_back (Output (1.0, 1.0));
962 for (n = 0; n < npans; ++n) {
963 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
969 outputs.push_back (Output (0, 0));
970 outputs.push_back (Output (1.0, 0));
971 outputs.push_back (Output (1.0, 1.0));
972 outputs.push_back (Output (0, 1.0));
974 for (n = 0; n < npans; ++n) {
975 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
980 case 5: //square+offcenter center
981 outputs.push_back (Output (0, 0));
982 outputs.push_back (Output (1.0, 0));
983 outputs.push_back (Output (1.0, 1.0));
984 outputs.push_back (Output (0, 1.0));
985 outputs.push_back (Output (0.5, 0.75));
987 for (n = 0; n < npans; ++n) {
988 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
994 /* XXX horrible placement. FIXME */
995 for (n = 0; n < nouts; ++n) {
996 outputs.push_back (Output (0.1 * n, 0.1 * n));
999 for (n = 0; n < npans; ++n) {
1000 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
1006 for (std::vector<StreamPanner*>::iterator x = _streampanners.begin(); x != _streampanners.end(); ++x) {
1010 /* must emit Changed here, otherwise the changes to the pan_control below raise further
1011 signals which the GUI is not prepared for until it has seen the Changed here.
1015 Changed (); /* EMIT SIGNAL */
1018 /* force hard left/right panning in a common case: 2in/2out
1021 if (npans == 2 && outputs.size() == 2) {
1023 /* Do this only if we changed configuration, or our configuration
1024 appears to be the default set up (center).
1030 _streampanners.front()->get_position (left);
1031 _streampanners.back()->get_position (right);
1033 if (changed || ((left == 0.5) && (right == 0.5))) {
1035 _streampanners.front()->set_position (0.0);
1036 _streampanners.front()->pan_control()->list()->reset_default (0.0);
1038 _streampanners.back()->set_position (1.0);
1039 _streampanners.back()->pan_control()->list()->reset_default (1.0);
1045 Panner::remove (uint32_t which)
1047 vector<StreamPanner*>::iterator i;
1048 for (i = _streampanners.begin(); i != _streampanners.end() && which; ++i, --which) {}
1050 if (i != _streampanners.end()) {
1052 _streampanners.erase (i);
1057 /** Remove all our StreamPanners */
1059 Panner::clear_panners ()
1061 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1065 _streampanners.clear ();
1069 Panner::set_automation_style (AutoStyle style)
1071 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1072 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_style (style);
1074 _session.set_dirty ();
1078 Panner::set_automation_state (AutoState state)
1080 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1081 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_state (state);
1083 _session.set_dirty ();
1087 Panner::automation_state () const
1089 boost::shared_ptr<AutomationList> l;
1091 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1093 l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1097 return l ? l->automation_state() : Off;
1101 Panner::automation_style () const
1103 boost::shared_ptr<AutomationList> l;
1105 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1107 l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1111 return l ? l->automation_style() : Absolute;
1117 StreamPanner* (*factory)(Panner&, Evoral::Parameter);
1120 PanPlugins pan_plugins[] = {
1121 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1122 { Multi2dPanner::name, 3, Multi2dPanner::factory },
1123 { string (""), 0, 0 }
1127 Panner::get_state (void)
1129 return state (true);
1133 Panner::state (bool full)
1135 XMLNode* node = new XMLNode ("Panner");
1139 node->add_property (X_("linked"), (_linked ? "yes" : "no"));
1140 node->add_property (X_("link_direction"), enum_2_string (_link_direction));
1141 node->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1143 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1144 XMLNode* onode = new XMLNode (X_("Output"));
1145 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1146 onode->add_property (X_("x"), buf);
1147 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1148 onode->add_property (X_("y"), buf);
1149 node->add_child_nocopy (*onode);
1152 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1153 node->add_child_nocopy ((*i)->state (full));
1156 node->add_child_nocopy (get_automation_xml_state ());
1162 Panner::set_state (const XMLNode& node, int version)
1165 XMLNodeConstIterator niter;
1166 const XMLProperty *prop;
1168 uint32_t num_panners = 0;
1170 LocaleGuard lg (X_("POSIX"));
1174 ChanCount ins = ChanCount::ZERO;
1175 ChanCount outs = ChanCount::ZERO;
1177 // XXX: this might not be necessary anymore
1180 if ((prop = node.property (X_("linked"))) != 0) {
1181 set_linked (string_is_affirmative (prop->value()));
1185 if ((prop = node.property (X_("bypassed"))) != 0) {
1186 set_bypassed (string_is_affirmative (prop->value()));
1189 if ((prop = node.property (X_("link_direction"))) != 0) {
1190 LinkDirection ld; /* here to provide type information */
1191 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1194 nlist = node.children();
1196 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1197 if ((*niter)->name() == X_("Output")) {
1201 prop = (*niter)->property (X_("x"));
1202 sscanf (prop->value().c_str(), "%g", &x);
1204 prop = (*niter)->property (X_("y"));
1205 sscanf (prop->value().c_str(), "%g", &y);
1207 outputs.push_back (Output (x, y));
1211 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1213 if ((*niter)->name() == X_("StreamPanner")) {
1215 if ((prop = (*niter)->property (X_("type")))) {
1217 for (i = 0; pan_plugins[i].factory; ++i) {
1218 if (prop->value() == pan_plugins[i].name) {
1221 /* note that we assume that all the stream panners
1222 are of the same type. pretty good
1223 assumption, but it's still an assumption.
1226 sp = pan_plugins[i].factory (*this, Evoral::Parameter(PanAutomation, 0, num_panners));
1229 if (sp->set_state (**niter, version) == 0) {
1230 _streampanners.push_back (sp);
1238 if (!pan_plugins[i].factory) {
1239 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1245 error << _("panner plugin node has no type information!")
1253 reset (outputs.size (), num_panners);
1254 /* don't try to do old-school automation loading if it wasn't marked as existing */
1256 if ((prop = node.property (X_("automation")))) {
1258 /* automation path is relative */
1260 automation_path = Glib::build_filename(_session.automation_dir(), prop->value ());
1263 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1264 if ((*niter)->name() == X_("Automation")) {
1265 set_automation_xml_state (**niter, Evoral::Parameter (PanAutomation));
1273 Panner::touching () const
1275 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1276 if (((AutomationList*)(*i)->pan_control()->list().get())->touching ()) {
1285 Panner::set_position (float xpos, StreamPanner& orig)
1291 orig.get_position (xnow);
1292 xdelta = xpos - xnow;
1294 if (_link_direction == SameDirection) {
1296 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1298 (*i)->set_position (xpos, true);
1300 (*i)->get_position (xnow);
1301 xnew = min (1.0f, xnow + xdelta);
1302 xnew = max (0.0f, xnew);
1303 (*i)->set_position (xnew, true);
1309 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1311 (*i)->set_position (xpos, true);
1313 (*i)->get_position (xnow);
1314 xnew = min (1.0f, xnow - xdelta);
1315 xnew = max (0.0f, xnew);
1316 (*i)->set_position (xnew, true);
1323 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1326 float xdelta, ydelta;
1329 orig.get_position (xnow, ynow);
1330 xdelta = xpos - xnow;
1331 ydelta = ypos - ynow;
1333 if (_link_direction == SameDirection) {
1335 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1337 (*i)->set_position (xpos, ypos, true);
1339 (*i)->get_position (xnow, ynow);
1341 xnew = min (1.0f, xnow + xdelta);
1342 xnew = max (0.0f, xnew);
1344 ynew = min (1.0f, ynow + ydelta);
1345 ynew = max (0.0f, ynew);
1347 (*i)->set_position (xnew, ynew, true);
1353 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1355 (*i)->set_position (xpos, ypos, true);
1357 (*i)->get_position (xnow, ynow);
1359 xnew = min (1.0f, xnow - xdelta);
1360 xnew = max (0.0f, xnew);
1362 ynew = min (1.0f, ynow - ydelta);
1363 ynew = max (0.0f, ynew);
1365 (*i)->set_position (xnew, ynew, true);
1372 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1374 float xnow, ynow, znow;
1375 float xdelta, ydelta, zdelta;
1376 float xnew, ynew, znew;
1378 orig.get_position (xnow, ynow, znow);
1379 xdelta = xpos - xnow;
1380 ydelta = ypos - ynow;
1381 zdelta = zpos - znow;
1383 if (_link_direction == SameDirection) {
1385 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1387 (*i)->set_position (xpos, ypos, zpos, true);
1389 (*i)->get_position (xnow, ynow, znow);
1391 xnew = min (1.0f, xnow + xdelta);
1392 xnew = max (0.0f, xnew);
1394 ynew = min (1.0f, ynow + ydelta);
1395 ynew = max (0.0f, ynew);
1397 znew = min (1.0f, znow + zdelta);
1398 znew = max (0.0f, znew);
1400 (*i)->set_position (xnew, ynew, znew, true);
1406 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1408 (*i)->set_position (xpos, ypos, true);
1410 (*i)->get_position (xnow, ynow, znow);
1412 xnew = min (1.0f, xnow - xdelta);
1413 xnew = max (0.0f, xnew);
1415 ynew = min (1.0f, ynow - ydelta);
1416 ynew = max (0.0f, ynew);
1418 znew = min (1.0f, znow + zdelta);
1419 znew = max (0.0f, znew);
1421 (*i)->set_position (xnew, ynew, znew, true);
1428 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, gain_t gain_coeff)
1430 if (outbufs.count().n_audio() == 0) {
1431 // Don't want to lose audio...
1432 assert(inbufs.count().n_audio() == 0);
1436 // We shouldn't be called in the first place...
1437 assert(!bypassed());
1441 if (outbufs.count().n_audio() == 1) {
1443 AudioBuffer& dst = outbufs.get_audio(0);
1445 if (gain_coeff == 0.0f) {
1447 /* only one output, and gain was zero, so make it silent */
1449 dst.silence (nframes);
1451 } else if (gain_coeff == 1.0f){
1453 /* mix all buffers into the output */
1456 dst.read_from(inbufs.get_audio(0), nframes);
1458 // accumulate starting with the second
1459 if (inbufs.count().n_audio() > 0) {
1460 BufferSet::audio_iterator i = inbufs.audio_begin();
1461 for (++i; i != inbufs.audio_end(); ++i) {
1462 dst.merge_from(*i, nframes);
1468 /* mix all buffers into the output, scaling them all by the gain */
1471 dst.read_from(inbufs.get_audio(0), nframes);
1473 // accumulate (with gain) starting with the second
1474 if (inbufs.count().n_audio() > 0) {
1475 BufferSet::audio_iterator i = inbufs.audio_begin();
1476 for (++i; i != inbufs.audio_end(); ++i) {
1477 dst.accumulate_with_gain_from(*i, nframes, gain_coeff);
1486 /* the terrible silence ... */
1487 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1488 i->silence(nframes);
1491 BufferSet::audio_iterator i = inbufs.audio_begin();
1493 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end() && i != inbufs.audio_end(); ++pan, ++i) {
1494 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1499 Panner::run (BufferSet& inbufs, BufferSet& outbufs, framepos_t start_frame, framepos_t end_frame, nframes_t nframes)
1501 if (outbufs.count().n_audio() == 0) {
1502 // Failing to deliver audio we were asked to deliver is a bug
1503 assert(inbufs.count().n_audio() == 0);
1507 // We shouldn't be called in the first place...
1508 assert(!bypassed());
1511 // If we shouldn't play automation defer to distribute_no_automation
1512 if (!(automation_state() & Play || ((automation_state() & Touch) && !touching()))) {
1515 gain_t gain_coeff = 1.0;
1517 if (fabsf(_session.transport_speed()) > 1.5f && Config->get_quieten_at_speed ()) {
1518 gain_coeff = speed_quietning;
1521 distribute_no_automation (inbufs, outbufs, nframes, gain_coeff);
1525 // Otherwise.. let the automation flow, baby
1527 if (outbufs.count().n_audio() == 1) {
1529 AudioBuffer& dst = outbufs.get_audio(0);
1531 // FIXME: apply gain automation?
1534 dst.read_from(inbufs.get_audio(0), nframes);
1536 // accumulate starting with the second
1537 BufferSet::audio_iterator i = inbufs.audio_begin();
1538 for (++i; i != inbufs.audio_end(); ++i) {
1539 dst.merge_from(*i, nframes);
1545 // More than 1 output, we should have 1 panner for each input
1546 //assert(_streampanners.size() == inbufs.count().n_audio());
1548 /* the terrible silence ... */
1549 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1550 i->silence(nframes);
1553 BufferSet::audio_iterator i = inbufs.audio_begin();
1554 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end(); ++pan, ++i) {
1555 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
1559 /* old school automation handling */
1563 Panner::set_name (string str)
1565 automation_path = Glib::build_filename(_session.automation_dir(),
1566 _session.snap_name() + "-pan-" + legalize_for_path (str) + ".automation");
1574 uint32_t linecnt = 0;
1576 vector<StreamPanner*>::iterator sp;
1577 LocaleGuard lg (X_("POSIX"));
1579 if (automation_path.length() == 0) {
1583 if (access (automation_path.c_str(), F_OK)) {
1587 ifstream in (automation_path.c_str());
1590 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1591 automation_path, strerror (errno))
1596 sp = _streampanners.begin();
1598 while (in.getline (line, sizeof(line), '\n')) {
1600 if (++linecnt == 1) {
1601 if (memcmp (line, X_("version"), 7) == 0) {
1602 if (sscanf (line, "version %f", &version) != 1) {
1603 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1607 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1608 automation_path, line) << endmsg;
1615 if (strlen (line) == 0 || line[0] == '#') {
1619 if (strcmp (line, "begin") == 0) {
1621 if (sp == _streampanners.end()) {
1622 error << string_compose (_("too many panner states found in pan automation file %1"),
1628 if ((*sp)->load (in, automation_path, linecnt)) {
1640 Panner::set_mono (bool yn)
1647 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1648 (*i)->set_mono (yn);
1653 Panner::value_as_string (double v)
1655 if (Panner::equivalent (v, 0.5)) {
1657 } else if (equivalent (v, 0)) {
1659 } else if (equivalent (v, 1)) {
1661 } else if (v < 0.5) {
1663 s << fixed << setprecision (0) << _("L") << ((0.5 - v) * 200) << "%";
1667 s << fixed << setprecision (0) << _("R") << ((v -0.5) * 200) << "%";