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
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/runtime_functions.h"
48 #include "ardour/buffer_set.h"
49 #include "ardour/audio_buffer.h"
53 #include "pbd/mathfix.h"
56 using namespace ARDOUR;
59 float Panner::current_automation_version_number = 1.0;
61 string EqualPowerStereoPanner::name = "Equal Power Stereo";
62 string Multi2dPanner::name = "Multiple (2D)";
64 /* this is a default mapper of control values to a pan position
65 others can be imagined.
68 static pan_t direct_control_to_pan (double fract)
73 StreamPanner::StreamPanner (Panner& p, Evoral::Parameter param)
76 assert (param.type() != NullAutomation);
81 /* get our AutomationControl from our parent Panner, creating it if required */
82 _control = boost::dynamic_pointer_cast<AutomationControl> (parent.control (param, true));
89 StreamPanner::~StreamPanner ()
94 StreamPanner::set_mono (bool yn)
103 Panner::PanControllable::set_value (float val)
105 panner.streampanner (parameter().id()).set_position (direct_control_to_pan (val));
106 AutomationControl::set_value(val);
110 Panner::PanControllable::get_value (void) const
112 return AutomationControl::get_value();
116 StreamPanner::set_muted (bool yn)
125 StreamPanner::set_position (float xpos, bool link_call)
127 if (!link_call && parent.linked()) {
128 parent.set_position (xpos, *this);
135 _control->Changed ();
140 StreamPanner::set_position (float xpos, float ypos, bool link_call)
142 if (!link_call && parent.linked()) {
143 parent.set_position (xpos, ypos, *this);
146 if (_x != xpos || _y != ypos) {
155 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
157 if (!link_call && parent.linked()) {
158 parent.set_position (xpos, ypos, zpos, *this);
161 if (_x != xpos || _y != ypos || _z != zpos) {
171 StreamPanner::set_state (const XMLNode& node, int /*version*/)
173 const XMLProperty* prop;
174 XMLNodeConstIterator iter;
176 if ((prop = node.property (X_("muted")))) {
177 set_muted (string_is_affirmative (prop->value()));
180 if ((prop = node.property (X_("mono")))) {
181 set_mono (string_is_affirmative (prop->value()));
188 StreamPanner::add_state (XMLNode& node)
190 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
191 node.add_property (X_("mono"), (_mono ? "yes" : "no"));
195 StreamPanner::distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
198 /* we're in mono mode, so just pan the input to all outputs equally */
199 int const N = parent.nouts ();
200 for (int i = 0; i < N; ++i) {
201 mix_buffers_with_gain (obufs.get_audio(i).data(), src.data(), nframes, gain_coeff);
204 /* normal mode, call the `real' distribute method */
205 do_distribute (src, obufs, gain_coeff, nframes);
210 StreamPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs,
211 nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers)
214 /* we're in mono mode, so just pan the input to all outputs equally */
215 int const N = parent.nouts ();
216 for (int i = 0; i < N; ++i) {
217 mix_buffers_with_gain (obufs.get_audio(i).data(), src.data(), nframes, 1.0);
220 /* normal mode, call the `real' distribute method */
221 do_distribute_automated (src, obufs, start, end, nframes, buffers);
227 /*---------------------------------------------------------------------- */
229 BaseStereoPanner::BaseStereoPanner (Panner& p, Evoral::Parameter param)
230 : StreamPanner (p, param)
234 , right_interp (right)
238 BaseStereoPanner::~BaseStereoPanner ()
243 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
246 LocaleGuard lg (X_("POSIX"));
248 _control->list()->clear ();
250 while (in.getline (line, sizeof (line), '\n')) {
256 if (strcmp (line, "end") == 0) {
260 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
261 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
265 _control->list()->fast_simple_add (when, value);
268 /* now that we are done loading */
270 ((AutomationList*)_control->list().get())->StateChanged ();
276 BaseStereoPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
278 assert(obufs.count().n_audio() == 2);
288 Sample* const src = srcbuf.data();
292 dst = obufs.get_audio(0).data();
294 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
296 /* we've moving the pan by an appreciable amount, so we must
297 interpolate over 64 frames or nframes, whichever is smaller */
299 nframes_t const limit = min ((nframes_t)64, nframes);
302 delta = -(delta / (float) (limit));
304 for (n = 0; n < limit; n++) {
305 left_interp = left_interp + delta;
306 left = left_interp + 0.9 * (left - left_interp);
307 dst[n] += src[n] * left * gain_coeff;
310 /* then pan the rest of the buffer; no need for interpolation for this bit */
312 pan = left * gain_coeff;
314 mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
321 if ((pan = (left * gain_coeff)) != 1.0f) {
325 /* pan is 1 but also not 0, so we must do it "properly" */
327 mix_buffers_with_gain(dst,src,nframes,pan);
329 /* mark that we wrote into the buffer */
337 /* pan is 1 so we can just copy the input samples straight in */
339 mix_buffers_no_gain(dst,src,nframes);
341 /* mark that we wrote into the buffer */
349 dst = obufs.get_audio(1).data();
351 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
353 /* we're moving the pan by an appreciable amount, so we must
354 interpolate over 64 frames or nframes, whichever is smaller */
356 nframes_t const limit = min ((nframes_t)64, nframes);
359 delta = -(delta / (float) (limit));
361 for (n = 0; n < limit; n++) {
362 right_interp = right_interp + delta;
363 right = right_interp + 0.9 * (right - right_interp);
364 dst[n] += src[n] * right * gain_coeff;
367 /* then pan the rest of the buffer, no need for interpolation for this bit */
369 pan = right * gain_coeff;
371 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
373 /* XXX it would be nice to mark the buffer as written to */
377 right = desired_right;
378 right_interp = right;
380 if ((pan = (right * gain_coeff)) != 1.0f) {
384 /* pan is not 1 but also not 0, so we must do it "properly" */
386 mix_buffers_with_gain(dst,src,nframes,pan);
388 /* XXX it would be nice to mark the buffer as written to */
393 /* pan is 1 so we can just copy the input samples straight in */
395 mix_buffers_no_gain(dst,src,nframes);
397 /* XXX it would be nice to mark the buffer as written to */
402 /*---------------------------------------------------------------------- */
404 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p, Evoral::Parameter param)
405 : BaseStereoPanner (p, param)
410 right = desired_right;
412 right_interp = right;
415 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
420 EqualPowerStereoPanner::update ()
422 /* it would be very nice to split this out into a virtual function
423 that can be accessed from BaseStereoPanner and used in do_distribute_automated().
425 but the place where its used in do_distribute_automated() is a tight inner loop,
426 and making "nframes" virtual function calls to compute values is an absurd
430 /* x == 0 => hard left
434 float const panR = _x;
435 float const panL = 1 - panR;
437 float const pan_law_attenuation = -3.0f;
438 float const scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
440 desired_left = panL * (scale * panL + 1.0f - scale);
441 desired_right = panR * (scale * panR + 1.0f - scale);
444 //_control->set_value(x);
448 EqualPowerStereoPanner::do_distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs,
449 nframes_t start, nframes_t end, nframes_t nframes,
452 assert (obufs.count().n_audio() == 2);
456 Sample* const src = srcbuf.data();
458 /* fetch positional data */
460 if (!_control->list()->curve().rt_safe_get_vector (start, end, buffers[0], nframes)) {
463 do_distribute (srcbuf, obufs, 1.0, nframes);
468 /* store effective pan position. do this even if we are muted */
471 effective_x = buffers[0][nframes-1];
478 /* apply pan law to convert positional data into pan coefficients for
482 const float pan_law_attenuation = -3.0f;
483 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
485 for (nframes_t n = 0; n < nframes; ++n) {
487 float const panR = buffers[0][n];
488 float const panL = 1 - panR;
490 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
491 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
496 dst = obufs.get_audio(0).data();
499 for (nframes_t n = 0; n < nframes; ++n) {
500 dst[n] += src[n] * pbuf[n];
503 /* XXX it would be nice to mark the buffer as written to */
507 dst = obufs.get_audio(1).data();
510 for (nframes_t n = 0; n < nframes; ++n) {
511 dst[n] += src[n] * pbuf[n];
514 /* XXX it would be nice to mark the buffer as written to */
518 EqualPowerStereoPanner::factory (Panner& parent, Evoral::Parameter param)
520 return new EqualPowerStereoPanner (parent, param);
524 EqualPowerStereoPanner::get_state (void)
530 EqualPowerStereoPanner::state (bool /*full_state*/)
532 XMLNode* root = new XMLNode ("StreamPanner");
534 LocaleGuard lg (X_("POSIX"));
536 snprintf (buf, sizeof (buf), "%.12g", _x);
537 root->add_property (X_("x"), buf);
538 root->add_property (X_("type"), EqualPowerStereoPanner::name);
540 // XXX: dont save automation here... its part of the automatable panner now.
542 StreamPanner::add_state (*root);
544 root->add_child_nocopy (_control->get_state ());
550 EqualPowerStereoPanner::set_state (const XMLNode& node, int version)
552 const XMLProperty* prop;
553 LocaleGuard lg (X_("POSIX"));
555 if ((prop = node.property (X_("x")))) {
556 const float pos = atof (prop->value().c_str());
557 set_position (pos, true);
560 StreamPanner::set_state (node, version);
562 for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
564 if ((*iter)->name() == X_("Controllable")) {
565 if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
566 _control->set_state (**iter, version);
569 } else if ((*iter)->name() == X_("Automation")) {
571 _control->alist()->set_state (*((*iter)->children().front()), version);
573 if (_control->alist()->automation_state() != Off) {
574 set_position (_control->list()->eval (parent.session().transport_frame()));
582 /*----------------------------------------------------------------------*/
584 Multi2dPanner::Multi2dPanner (Panner& p, Evoral::Parameter param)
585 : StreamPanner (p, param)
590 Multi2dPanner::~Multi2dPanner ()
595 Multi2dPanner::update ()
597 static const float BIAS = FLT_MIN;
599 uint32_t const nouts = parent.nouts ();
606 for (i = 0; i < nouts; i++) {
607 dsq[i] = ((_x - parent.output(i).x) * (_x - parent.output(i).x) + (_y - parent.output(i).y) * (_y - parent.output(i).y) + BIAS);
611 f += dsq[i] * dsq[i];
614 // terrible hack to support OSX < 10.3.9 builds
615 fr = (float) (1.0 / sqrt((double)f));
619 for (i = 0; i < nouts; ++i) {
620 parent.output(i).desired_pan = 1.0f - (dsq[i] * fr);
627 Multi2dPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
636 Sample* const src = srcbuf.data();
638 uint32_t const N = parent.nouts ();
639 for (uint32_t n = 0; n < N; ++n) {
640 Panner::Output& o = parent.output (n);
642 dst = obufs.get_audio(n).data();
645 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
647 /* interpolate over 64 frames or nframes, whichever is smaller */
649 nframes_t limit = min ((nframes_t)64, nframes);
652 delta = -(delta / (float) (limit));
654 for (n = 0; n < limit; n++) {
655 left_interp = left_interp + delta;
656 left = left_interp + 0.9 * (left - left_interp);
657 dst[n] += src[n] * left * gain_coeff;
660 pan = left * gain_coeff;
661 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
668 if ((pan *= gain_coeff) != 1.0f) {
671 mix_buffers_with_gain(dst,src,nframes,pan);
675 mix_buffers_no_gain(dst,src,nframes);
687 Multi2dPanner::do_distribute_automated (AudioBuffer& /*src*/, BufferSet& /*obufs*/,
688 nframes_t /*start*/, nframes_t /*end*/, nframes_t /*nframes*/,
701 Multi2dPanner::factory (Panner& p, Evoral::Parameter param)
703 return new Multi2dPanner (p, param);
707 Multi2dPanner::load (istream& /*in*/, string /*path*/, uint32_t& /*linecnt*/)
713 Multi2dPanner::get_state (void)
719 Multi2dPanner::state (bool /*full_state*/)
721 XMLNode* root = new XMLNode ("StreamPanner");
723 LocaleGuard lg (X_("POSIX"));
725 snprintf (buf, sizeof (buf), "%.12g", _x);
726 root->add_property (X_("x"), buf);
727 snprintf (buf, sizeof (buf), "%.12g", _y);
728 root->add_property (X_("y"), buf);
729 root->add_property (X_("type"), Multi2dPanner::name);
731 /* XXX no meaningful automation yet */
737 Multi2dPanner::set_state (const XMLNode& node, int /*version*/)
739 const XMLProperty* prop;
741 LocaleGuard lg (X_("POSIX"));
746 if ((prop = node.property (X_("x")))) {
747 newx = atof (prop->value().c_str());
750 if ((prop = node.property (X_("y")))) {
751 newy = atof (prop->value().c_str());
754 if (_x < 0 || _y < 0) {
755 error << _("badly-formed positional data for Multi2dPanner - ignored")
760 set_position (newx, newy);
764 /*---------------------------------------------------------------------- */
766 Panner::Panner (string name, Session& s)
767 : SessionObject (s, name)
768 , AutomatableControls (s)
770 //set_name_old_auto (name);
774 _link_direction = SameDirection;
784 Panner::set_linked (bool yn)
788 _session.set_dirty ();
789 LinkStateChanged (); /* EMIT SIGNAL */
794 Panner::set_link_direction (LinkDirection ld)
796 if (ld != _link_direction) {
797 _link_direction = ld;
798 _session.set_dirty ();
799 LinkStateChanged (); /* EMIT SIGNAL */
805 Panner::set_bypassed (bool yn)
807 if (yn != _bypassed) {
815 Panner::reset_to_default ()
817 vector<float> positions;
819 switch (outputs.size()) {
825 if (outputs.size() == 2) {
826 switch (_streampanners.size()) {
828 _streampanners.front()->set_position (0.5);
829 _streampanners.front()->pan_control()->list()->reset_default (0.5);
833 _streampanners.front()->set_position (0.0);
834 _streampanners.front()->pan_control()->list()->reset_default (0.0);
835 _streampanners.back()->set_position (1.0);
836 _streampanners.back()->pan_control()->list()->reset_default (1.0);
843 vector<Output>::iterator o;
844 vector<StreamPanner*>::iterator p;
846 for (o = outputs.begin(), p = _streampanners.begin(); o != outputs.end() && p != _streampanners.end(); ++o, ++p) {
847 (*p)->set_position ((*o).x, (*o).y);
852 Panner::reset_streampanner (uint32_t which)
854 if (which >= _streampanners.size() || which >= outputs.size()) {
858 switch (outputs.size()) {
864 switch (_streampanners.size()) {
866 /* stereo out, 1 stream, default = middle */
867 _streampanners.front()->set_position (0.5);
868 _streampanners.front()->pan_control()->list()->reset_default (0.5);
871 /* stereo out, 2 streams, default = hard left/right */
873 _streampanners.front()->set_position (0.0);
874 _streampanners.front()->pan_control()->list()->reset_default (0.0);
876 _streampanners.back()->set_position (1.0);
877 _streampanners.back()->pan_control()->list()->reset_default (1.0);
884 _streampanners[which]->set_position (outputs[which].x, outputs[which].y);
889 * Reset the panner with a given number of outs and panners (and hence inputs)
891 * \param nouts Number of outputs.
892 * \param npans Number of panners.
895 Panner::reset (uint32_t nouts, uint32_t npans)
898 bool changed = false;
899 bool do_not_and_did_not_need_panning = ((nouts < 2) && (outputs.size() < 2));
901 /* if new and old config don't need panning, or if
902 the config hasn't changed, we're done.
905 if (do_not_and_did_not_need_panning ||
906 ((nouts == outputs.size()) && (npans == _streampanners.size()))) {
910 n = _streampanners.size();
925 /* no need for panning with less than 2 outputs */
927 Changed (); /* EMIT SIGNAL */
934 /* XXX: this can never happen */
938 /* XXX: this can never happen */
939 fatal << _("programming error:")
940 << X_("Panner::reset() called with a single output")
946 outputs.push_back (Output (0, 0));
947 outputs.push_back (Output (1.0, 0));
949 for (n = 0; n < npans; ++n) {
950 _streampanners.push_back (new EqualPowerStereoPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
955 outputs.push_back (Output (0.5, 0));
956 outputs.push_back (Output (0, 1.0));
957 outputs.push_back (Output (1.0, 1.0));
959 for (n = 0; n < npans; ++n) {
960 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
966 outputs.push_back (Output (0, 0));
967 outputs.push_back (Output (1.0, 0));
968 outputs.push_back (Output (1.0, 1.0));
969 outputs.push_back (Output (0, 1.0));
971 for (n = 0; n < npans; ++n) {
972 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
977 case 5: //square+offcenter center
978 outputs.push_back (Output (0, 0));
979 outputs.push_back (Output (1.0, 0));
980 outputs.push_back (Output (1.0, 1.0));
981 outputs.push_back (Output (0, 1.0));
982 outputs.push_back (Output (0.5, 0.75));
984 for (n = 0; n < npans; ++n) {
985 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
991 /* XXX horrible placement. FIXME */
992 for (n = 0; n < nouts; ++n) {
993 outputs.push_back (Output (0.1 * n, 0.1 * n));
996 for (n = 0; n < npans; ++n) {
997 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
1003 for (std::vector<StreamPanner*>::iterator x = _streampanners.begin(); x != _streampanners.end(); ++x) {
1007 /* must emit Changed here, otherwise the changes to the pan_control below raise further
1008 signals which the GUI is not prepared for until it has seen the Changed here.
1012 Changed (); /* EMIT SIGNAL */
1015 /* force hard left/right panning in a common case: 2in/2out
1018 if (npans == 2 && outputs.size() == 2) {
1020 /* Do this only if we changed configuration, or our configuration
1021 appears to be the default set up (center).
1027 _streampanners.front()->get_position (left);
1028 _streampanners.back()->get_position (right);
1030 if (changed || ((left == 0.5) && (right == 0.5))) {
1032 _streampanners.front()->set_position (0.0);
1033 _streampanners.front()->pan_control()->list()->reset_default (0.0);
1035 _streampanners.back()->set_position (1.0);
1036 _streampanners.back()->pan_control()->list()->reset_default (1.0);
1042 Panner::remove (uint32_t which)
1044 vector<StreamPanner*>::iterator i;
1045 for (i = _streampanners.begin(); i != _streampanners.end() && which; ++i, --which) {}
1047 if (i != _streampanners.end()) {
1049 _streampanners.erase (i);
1054 /** Remove all our StreamPanners */
1056 Panner::clear_panners ()
1058 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1062 _streampanners.clear ();
1066 Panner::set_automation_style (AutoStyle style)
1068 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1069 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_style (style);
1071 _session.set_dirty ();
1075 Panner::set_automation_state (AutoState state)
1077 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1078 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_state (state);
1080 _session.set_dirty ();
1084 Panner::automation_state () const
1086 boost::shared_ptr<AutomationList> l;
1088 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1090 l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1094 return l ? l->automation_state() : Off;
1098 Panner::automation_style () const
1100 boost::shared_ptr<AutomationList> l;
1102 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1104 l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1108 return l ? l->automation_style() : Absolute;
1114 StreamPanner* (*factory)(Panner&, Evoral::Parameter);
1117 PanPlugins pan_plugins[] = {
1118 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1119 { Multi2dPanner::name, 3, Multi2dPanner::factory },
1120 { string (""), 0, 0 }
1124 Panner::get_state (void)
1126 return state (true);
1130 Panner::state (bool full)
1132 XMLNode* node = new XMLNode ("Panner");
1136 node->add_property (X_("linked"), (_linked ? "yes" : "no"));
1137 node->add_property (X_("link_direction"), enum_2_string (_link_direction));
1138 node->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1140 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1141 XMLNode* onode = new XMLNode (X_("Output"));
1142 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1143 onode->add_property (X_("x"), buf);
1144 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1145 onode->add_property (X_("y"), buf);
1146 node->add_child_nocopy (*onode);
1149 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1150 node->add_child_nocopy ((*i)->state (full));
1158 Panner::set_state (const XMLNode& node, int version)
1161 XMLNodeConstIterator niter;
1162 const XMLProperty *prop;
1164 uint32_t num_panners = 0;
1166 LocaleGuard lg (X_("POSIX"));
1170 ChanCount ins = ChanCount::ZERO;
1171 ChanCount outs = ChanCount::ZERO;
1173 // XXX: this might not be necessary anymore
1176 if ((prop = node.property (X_("linked"))) != 0) {
1177 set_linked (string_is_affirmative (prop->value()));
1181 if ((prop = node.property (X_("bypassed"))) != 0) {
1182 set_bypassed (string_is_affirmative (prop->value()));
1185 if ((prop = node.property (X_("link_direction"))) != 0) {
1186 LinkDirection ld; /* here to provide type information */
1187 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1190 nlist = node.children();
1192 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1193 if ((*niter)->name() == X_("Output")) {
1197 prop = (*niter)->property (X_("x"));
1198 sscanf (prop->value().c_str(), "%g", &x);
1200 prop = (*niter)->property (X_("y"));
1201 sscanf (prop->value().c_str(), "%g", &y);
1203 outputs.push_back (Output (x, y));
1207 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1209 if ((*niter)->name() == X_("StreamPanner")) {
1211 if ((prop = (*niter)->property (X_("type")))) {
1213 for (i = 0; pan_plugins[i].factory; ++i) {
1214 if (prop->value() == pan_plugins[i].name) {
1217 /* note that we assume that all the stream panners
1218 are of the same type. pretty good
1219 assumption, but it's still an assumption.
1222 sp = pan_plugins[i].factory (*this, Evoral::Parameter(PanAutomation, 0, num_panners));
1225 if (sp->set_state (**niter, version) == 0) {
1226 _streampanners.push_back (sp);
1234 if (!pan_plugins[i].factory) {
1235 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1241 error << _("panner plugin node has no type information!")
1249 reset (outputs.size (), num_panners);
1250 /* don't try to do old-school automation loading if it wasn't marked as existing */
1252 if ((prop = node.property (X_("automation")))) {
1254 /* automation path is relative */
1256 automation_path = Glib::build_filename(_session.automation_dir(), prop->value ());
1263 Panner::touching () const
1265 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1266 if (((AutomationList*)(*i)->pan_control()->list().get())->touching ()) {
1275 Panner::set_position (float xpos, StreamPanner& orig)
1281 orig.get_position (xnow);
1282 xdelta = xpos - xnow;
1284 if (_link_direction == SameDirection) {
1286 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1288 (*i)->set_position (xpos, true);
1290 (*i)->get_position (xnow);
1291 xnew = min (1.0f, xnow + xdelta);
1292 xnew = max (0.0f, xnew);
1293 (*i)->set_position (xnew, true);
1299 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1301 (*i)->set_position (xpos, true);
1303 (*i)->get_position (xnow);
1304 xnew = min (1.0f, xnow - xdelta);
1305 xnew = max (0.0f, xnew);
1306 (*i)->set_position (xnew, true);
1313 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1316 float xdelta, ydelta;
1319 orig.get_position (xnow, ynow);
1320 xdelta = xpos - xnow;
1321 ydelta = ypos - ynow;
1323 if (_link_direction == SameDirection) {
1325 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1327 (*i)->set_position (xpos, ypos, true);
1329 (*i)->get_position (xnow, ynow);
1331 xnew = min (1.0f, xnow + xdelta);
1332 xnew = max (0.0f, xnew);
1334 ynew = min (1.0f, ynow + ydelta);
1335 ynew = max (0.0f, ynew);
1337 (*i)->set_position (xnew, ynew, true);
1343 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1345 (*i)->set_position (xpos, ypos, true);
1347 (*i)->get_position (xnow, ynow);
1349 xnew = min (1.0f, xnow - xdelta);
1350 xnew = max (0.0f, xnew);
1352 ynew = min (1.0f, ynow - ydelta);
1353 ynew = max (0.0f, ynew);
1355 (*i)->set_position (xnew, ynew, true);
1362 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1364 float xnow, ynow, znow;
1365 float xdelta, ydelta, zdelta;
1366 float xnew, ynew, znew;
1368 orig.get_position (xnow, ynow, znow);
1369 xdelta = xpos - xnow;
1370 ydelta = ypos - ynow;
1371 zdelta = zpos - znow;
1373 if (_link_direction == SameDirection) {
1375 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1377 (*i)->set_position (xpos, ypos, zpos, true);
1379 (*i)->get_position (xnow, ynow, znow);
1381 xnew = min (1.0f, xnow + xdelta);
1382 xnew = max (0.0f, xnew);
1384 ynew = min (1.0f, ynow + ydelta);
1385 ynew = max (0.0f, ynew);
1387 znew = min (1.0f, znow + zdelta);
1388 znew = max (0.0f, znew);
1390 (*i)->set_position (xnew, ynew, znew, true);
1396 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1398 (*i)->set_position (xpos, ypos, true);
1400 (*i)->get_position (xnow, ynow, znow);
1402 xnew = min (1.0f, xnow - xdelta);
1403 xnew = max (0.0f, xnew);
1405 ynew = min (1.0f, ynow - ydelta);
1406 ynew = max (0.0f, ynew);
1408 znew = min (1.0f, znow + zdelta);
1409 znew = max (0.0f, znew);
1411 (*i)->set_position (xnew, ynew, znew, true);
1418 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, gain_t gain_coeff)
1420 if (outbufs.count().n_audio() == 0) {
1421 // Don't want to lose audio...
1422 assert(inbufs.count().n_audio() == 0);
1426 // We shouldn't be called in the first place...
1427 assert(!bypassed());
1431 if (outbufs.count().n_audio() == 1) {
1433 AudioBuffer& dst = outbufs.get_audio(0);
1435 if (gain_coeff == 0.0f) {
1437 /* only one output, and gain was zero, so make it silent */
1439 dst.silence (nframes);
1441 } else if (gain_coeff == 1.0f){
1443 /* mix all buffers into the output */
1446 dst.read_from(inbufs.get_audio(0), nframes);
1448 // accumulate starting with the second
1449 if (inbufs.count().n_audio() > 0) {
1450 BufferSet::audio_iterator i = inbufs.audio_begin();
1451 for (++i; i != inbufs.audio_end(); ++i) {
1452 dst.merge_from(*i, nframes);
1458 /* mix all buffers into the output, scaling them all by the gain */
1461 dst.read_from(inbufs.get_audio(0), nframes);
1463 // accumulate (with gain) starting with the second
1464 if (inbufs.count().n_audio() > 0) {
1465 BufferSet::audio_iterator i = inbufs.audio_begin();
1466 for (++i; i != inbufs.audio_end(); ++i) {
1467 dst.accumulate_with_gain_from(*i, nframes, gain_coeff);
1476 /* the terrible silence ... */
1477 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1478 i->silence(nframes);
1481 BufferSet::audio_iterator i = inbufs.audio_begin();
1483 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end() && i != inbufs.audio_end(); ++pan, ++i) {
1484 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1489 Panner::run (BufferSet& inbufs, BufferSet& outbufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
1491 if (outbufs.count().n_audio() == 0) {
1492 // Failing to deliver audio we were asked to deliver is a bug
1493 assert(inbufs.count().n_audio() == 0);
1497 // We shouldn't be called in the first place...
1498 assert(!bypassed());
1501 // If we shouldn't play automation defer to distribute_no_automation
1502 if (!(automation_state() & Play || ((automation_state() & Touch) && !touching()))) {
1505 gain_t gain_coeff = 1.0;
1507 if (fabsf(_session.transport_speed()) > 1.5f) {
1508 gain_coeff = speed_quietning;
1511 distribute_no_automation (inbufs, outbufs, nframes, gain_coeff);
1515 // Otherwise.. let the automation flow, baby
1517 if (outbufs.count().n_audio() == 1) {
1519 AudioBuffer& dst = outbufs.get_audio(0);
1521 // FIXME: apply gain automation?
1524 dst.read_from(inbufs.get_audio(0), nframes);
1526 // accumulate starting with the second
1527 BufferSet::audio_iterator i = inbufs.audio_begin();
1528 for (++i; i != inbufs.audio_end(); ++i) {
1529 dst.merge_from(*i, nframes);
1535 // More than 1 output, we should have 1 panner for each input
1536 //assert(_streampanners.size() == inbufs.count().n_audio());
1538 /* the terrible silence ... */
1539 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1540 i->silence(nframes);
1543 BufferSet::audio_iterator i = inbufs.audio_begin();
1544 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end(); ++pan, ++i) {
1545 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
1549 /* old school automation handling */
1553 Panner::set_name (string str)
1555 automation_path = Glib::build_filename(_session.automation_dir(),
1556 _session.snap_name() + "-pan-" + legalize_for_path (str) + ".automation");
1564 uint32_t linecnt = 0;
1566 vector<StreamPanner*>::iterator sp;
1567 LocaleGuard lg (X_("POSIX"));
1569 if (automation_path.length() == 0) {
1573 if (access (automation_path.c_str(), F_OK)) {
1577 ifstream in (automation_path.c_str());
1580 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1581 automation_path, strerror (errno))
1586 sp = _streampanners.begin();
1588 while (in.getline (line, sizeof(line), '\n')) {
1590 if (++linecnt == 1) {
1591 if (memcmp (line, X_("version"), 7) == 0) {
1592 if (sscanf (line, "version %f", &version) != 1) {
1593 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1597 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1598 automation_path, line) << endmsg;
1605 if (strlen (line) == 0 || line[0] == '#') {
1609 if (strcmp (line, "begin") == 0) {
1611 if (sp == _streampanners.end()) {
1612 error << string_compose (_("too many panner states found in pan automation file %1"),
1618 if ((*sp)->load (in, automation_path, linecnt)) {
1630 Panner::set_mono (bool yn)
1637 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1638 (*i)->set_mono (yn);