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) {
151 _control->Changed ();
156 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
158 if (!link_call && parent.linked()) {
159 parent.set_position (xpos, ypos, zpos, *this);
162 if (_x != xpos || _y != ypos || _z != zpos) {
168 _control->Changed ();
173 StreamPanner::set_state (const XMLNode& node, int /*version*/)
175 const XMLProperty* prop;
176 XMLNodeConstIterator iter;
178 if ((prop = node.property (X_("muted")))) {
179 set_muted (string_is_affirmative (prop->value()));
182 if ((prop = node.property (X_("mono")))) {
183 set_mono (string_is_affirmative (prop->value()));
190 StreamPanner::add_state (XMLNode& node)
192 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
193 node.add_property (X_("mono"), (_mono ? "yes" : "no"));
197 StreamPanner::distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
200 /* we're in mono mode, so just pan the input to all outputs equally */
201 int const N = parent.nouts ();
202 for (int i = 0; i < N; ++i) {
203 mix_buffers_with_gain (obufs.get_audio(i).data(), src.data(), nframes, gain_coeff);
206 /* normal mode, call the `real' distribute method */
207 do_distribute (src, obufs, gain_coeff, nframes);
212 StreamPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs,
213 nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers)
216 /* we're in mono mode, so just pan the input to all outputs equally */
217 int const N = parent.nouts ();
218 for (int i = 0; i < N; ++i) {
219 mix_buffers_with_gain (obufs.get_audio(i).data(), src.data(), nframes, 1.0);
222 /* normal mode, call the `real' distribute method */
223 do_distribute_automated (src, obufs, start, end, nframes, buffers);
229 /*---------------------------------------------------------------------- */
231 BaseStereoPanner::BaseStereoPanner (Panner& p, Evoral::Parameter param)
232 : StreamPanner (p, param)
236 , right_interp (right)
240 BaseStereoPanner::~BaseStereoPanner ()
245 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
248 LocaleGuard lg (X_("POSIX"));
250 _control->list()->clear ();
252 while (in.getline (line, sizeof (line), '\n')) {
258 if (strcmp (line, "end") == 0) {
262 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
263 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
267 _control->list()->fast_simple_add (when, value);
270 /* now that we are done loading */
272 ((AutomationList*)_control->list().get())->StateChanged ();
278 BaseStereoPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
280 assert(obufs.count().n_audio() == 2);
290 Sample* const src = srcbuf.data();
294 dst = obufs.get_audio(0).data();
296 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
298 /* we've moving the pan by an appreciable amount, so we must
299 interpolate over 64 frames or nframes, whichever is smaller */
301 nframes_t const limit = min ((nframes_t)64, nframes);
304 delta = -(delta / (float) (limit));
306 for (n = 0; n < limit; n++) {
307 left_interp = left_interp + delta;
308 left = left_interp + 0.9 * (left - left_interp);
309 dst[n] += src[n] * left * gain_coeff;
312 /* then pan the rest of the buffer; no need for interpolation for this bit */
314 pan = left * gain_coeff;
316 mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
323 if ((pan = (left * gain_coeff)) != 1.0f) {
327 /* pan is 1 but also not 0, so we must do it "properly" */
329 mix_buffers_with_gain(dst,src,nframes,pan);
331 /* mark that we wrote into the buffer */
339 /* pan is 1 so we can just copy the input samples straight in */
341 mix_buffers_no_gain(dst,src,nframes);
343 /* mark that we wrote into the buffer */
351 dst = obufs.get_audio(1).data();
353 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
355 /* we're moving the pan by an appreciable amount, so we must
356 interpolate over 64 frames or nframes, whichever is smaller */
358 nframes_t const limit = min ((nframes_t)64, nframes);
361 delta = -(delta / (float) (limit));
363 for (n = 0; n < limit; n++) {
364 right_interp = right_interp + delta;
365 right = right_interp + 0.9 * (right - right_interp);
366 dst[n] += src[n] * right * gain_coeff;
369 /* then pan the rest of the buffer, no need for interpolation for this bit */
371 pan = right * gain_coeff;
373 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
375 /* XXX it would be nice to mark the buffer as written to */
379 right = desired_right;
380 right_interp = right;
382 if ((pan = (right * gain_coeff)) != 1.0f) {
386 /* pan is not 1 but also not 0, so we must do it "properly" */
388 mix_buffers_with_gain(dst,src,nframes,pan);
390 /* XXX it would be nice to mark the buffer as written to */
395 /* pan is 1 so we can just copy the input samples straight in */
397 mix_buffers_no_gain(dst,src,nframes);
399 /* XXX it would be nice to mark the buffer as written to */
404 /*---------------------------------------------------------------------- */
406 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p, Evoral::Parameter param)
407 : BaseStereoPanner (p, param)
412 right = desired_right;
414 right_interp = right;
417 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
422 EqualPowerStereoPanner::update ()
424 /* it would be very nice to split this out into a virtual function
425 that can be accessed from BaseStereoPanner and used in do_distribute_automated().
427 but the place where its used in do_distribute_automated() is a tight inner loop,
428 and making "nframes" virtual function calls to compute values is an absurd
432 /* x == 0 => hard left
436 float const panR = _x;
437 float const panL = 1 - panR;
439 float const pan_law_attenuation = -3.0f;
440 float const scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
442 desired_left = panL * (scale * panL + 1.0f - scale);
443 desired_right = panR * (scale * panR + 1.0f - scale);
446 //_control->set_value(x);
450 EqualPowerStereoPanner::do_distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs,
451 nframes_t start, nframes_t end, nframes_t nframes,
454 assert (obufs.count().n_audio() == 2);
458 Sample* const src = srcbuf.data();
460 /* fetch positional data */
462 if (!_control->list()->curve().rt_safe_get_vector (start, end, buffers[0], nframes)) {
465 do_distribute (srcbuf, obufs, 1.0, nframes);
470 /* store effective pan position. do this even if we are muted */
473 effective_x = buffers[0][nframes-1];
480 /* apply pan law to convert positional data into pan coefficients for
484 const float pan_law_attenuation = -3.0f;
485 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
487 for (nframes_t n = 0; n < nframes; ++n) {
489 float const panR = buffers[0][n];
490 float const panL = 1 - panR;
492 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
493 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
498 dst = obufs.get_audio(0).data();
501 for (nframes_t n = 0; n < nframes; ++n) {
502 dst[n] += src[n] * pbuf[n];
505 /* XXX it would be nice to mark the buffer as written to */
509 dst = obufs.get_audio(1).data();
512 for (nframes_t n = 0; n < nframes; ++n) {
513 dst[n] += src[n] * pbuf[n];
516 /* XXX it would be nice to mark the buffer as written to */
520 EqualPowerStereoPanner::factory (Panner& parent, Evoral::Parameter param)
522 return new EqualPowerStereoPanner (parent, param);
526 EqualPowerStereoPanner::get_state (void)
532 EqualPowerStereoPanner::state (bool /*full_state*/)
534 XMLNode* root = new XMLNode ("StreamPanner");
536 LocaleGuard lg (X_("POSIX"));
538 snprintf (buf, sizeof (buf), "%.12g", _x);
539 root->add_property (X_("x"), buf);
540 root->add_property (X_("type"), EqualPowerStereoPanner::name);
542 // XXX: dont save automation here... its part of the automatable panner now.
544 StreamPanner::add_state (*root);
546 root->add_child_nocopy (_control->get_state ());
552 EqualPowerStereoPanner::set_state (const XMLNode& node, int version)
554 const XMLProperty* prop;
555 LocaleGuard lg (X_("POSIX"));
557 if ((prop = node.property (X_("x")))) {
558 const float pos = atof (prop->value().c_str());
559 set_position (pos, true);
562 StreamPanner::set_state (node, version);
564 for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
566 if ((*iter)->name() == X_("Controllable")) {
567 if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
568 _control->set_state (**iter, version);
571 } else if ((*iter)->name() == X_("Automation")) {
573 _control->alist()->set_state (*((*iter)->children().front()), version);
575 if (_control->alist()->automation_state() != Off) {
576 set_position (_control->list()->eval (parent.session().transport_frame()));
584 /*----------------------------------------------------------------------*/
586 Multi2dPanner::Multi2dPanner (Panner& p, Evoral::Parameter param)
587 : StreamPanner (p, param)
592 Multi2dPanner::~Multi2dPanner ()
597 Multi2dPanner::update ()
599 static const float BIAS = FLT_MIN;
601 uint32_t const nouts = parent.nouts ();
608 for (i = 0; i < nouts; i++) {
609 dsq[i] = ((_x - parent.output(i).x) * (_x - parent.output(i).x) + (_y - parent.output(i).y) * (_y - parent.output(i).y) + BIAS);
613 f += dsq[i] * dsq[i];
616 // terrible hack to support OSX < 10.3.9 builds
617 fr = (float) (1.0 / sqrt((double)f));
621 for (i = 0; i < nouts; ++i) {
622 parent.output(i).desired_pan = 1.0f - (dsq[i] * fr);
629 Multi2dPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
638 Sample* const src = srcbuf.data();
640 uint32_t const N = parent.nouts ();
641 for (uint32_t n = 0; n < N; ++n) {
642 Panner::Output& o = parent.output (n);
644 dst = obufs.get_audio(n).data();
647 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
649 /* interpolate over 64 frames or nframes, whichever is smaller */
651 nframes_t limit = min ((nframes_t)64, nframes);
654 delta = -(delta / (float) (limit));
656 for (n = 0; n < limit; n++) {
657 left_interp = left_interp + delta;
658 left = left_interp + 0.9 * (left - left_interp);
659 dst[n] += src[n] * left * gain_coeff;
662 pan = left * gain_coeff;
663 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
670 if ((pan *= gain_coeff) != 1.0f) {
673 mix_buffers_with_gain(dst,src,nframes,pan);
677 mix_buffers_no_gain(dst,src,nframes);
689 Multi2dPanner::do_distribute_automated (AudioBuffer& /*src*/, BufferSet& /*obufs*/,
690 nframes_t /*start*/, nframes_t /*end*/, nframes_t /*nframes*/,
703 Multi2dPanner::factory (Panner& p, Evoral::Parameter param)
705 return new Multi2dPanner (p, param);
709 Multi2dPanner::load (istream& /*in*/, string /*path*/, uint32_t& /*linecnt*/)
715 Multi2dPanner::get_state (void)
721 Multi2dPanner::state (bool /*full_state*/)
723 XMLNode* root = new XMLNode ("StreamPanner");
725 LocaleGuard lg (X_("POSIX"));
727 snprintf (buf, sizeof (buf), "%.12g", _x);
728 root->add_property (X_("x"), buf);
729 snprintf (buf, sizeof (buf), "%.12g", _y);
730 root->add_property (X_("y"), buf);
731 root->add_property (X_("type"), Multi2dPanner::name);
733 /* XXX no meaningful automation yet */
739 Multi2dPanner::set_state (const XMLNode& node, int /*version*/)
741 const XMLProperty* prop;
743 LocaleGuard lg (X_("POSIX"));
748 if ((prop = node.property (X_("x")))) {
749 newx = atof (prop->value().c_str());
752 if ((prop = node.property (X_("y")))) {
753 newy = atof (prop->value().c_str());
756 if (_x < 0 || _y < 0) {
757 error << _("badly-formed positional data for Multi2dPanner - ignored")
762 set_position (newx, newy);
766 /*---------------------------------------------------------------------- */
768 Panner::Panner (string name, Session& s)
769 : SessionObject (s, name)
772 //set_name_old_auto (name);
776 _link_direction = SameDirection;
786 Panner::set_linked (bool yn)
790 _session.set_dirty ();
791 LinkStateChanged (); /* EMIT SIGNAL */
796 Panner::set_link_direction (LinkDirection ld)
798 if (ld != _link_direction) {
799 _link_direction = ld;
800 _session.set_dirty ();
801 LinkStateChanged (); /* EMIT SIGNAL */
807 Panner::set_bypassed (bool yn)
809 if (yn != _bypassed) {
817 Panner::reset_to_default ()
819 vector<float> positions;
821 switch (outputs.size()) {
827 if (outputs.size() == 2) {
828 switch (_streampanners.size()) {
830 _streampanners.front()->set_position (0.5);
831 _streampanners.front()->pan_control()->list()->reset_default (0.5);
835 _streampanners.front()->set_position (0.0);
836 _streampanners.front()->pan_control()->list()->reset_default (0.0);
837 _streampanners.back()->set_position (1.0);
838 _streampanners.back()->pan_control()->list()->reset_default (1.0);
845 vector<Output>::iterator o;
846 vector<StreamPanner*>::iterator p;
848 for (o = outputs.begin(), p = _streampanners.begin(); o != outputs.end() && p != _streampanners.end(); ++o, ++p) {
849 (*p)->set_position ((*o).x, (*o).y);
854 Panner::reset_streampanner (uint32_t which)
856 if (which >= _streampanners.size() || which >= outputs.size()) {
860 switch (outputs.size()) {
866 switch (_streampanners.size()) {
868 /* stereo out, 1 stream, default = middle */
869 _streampanners.front()->set_position (0.5);
870 _streampanners.front()->pan_control()->list()->reset_default (0.5);
873 /* stereo out, 2 streams, default = hard left/right */
875 _streampanners.front()->set_position (0.0);
876 _streampanners.front()->pan_control()->list()->reset_default (0.0);
878 _streampanners.back()->set_position (1.0);
879 _streampanners.back()->pan_control()->list()->reset_default (1.0);
886 _streampanners[which]->set_position (outputs[which].x, outputs[which].y);
891 * Reset the panner with a given number of outs and panners (and hence inputs)
893 * \param nouts Number of outputs.
894 * \param npans Number of panners.
897 Panner::reset (uint32_t nouts, uint32_t npans)
900 bool changed = false;
901 bool do_not_and_did_not_need_panning = ((nouts < 2) && (outputs.size() < 2));
903 /* if new and old config don't need panning, or if
904 the config hasn't changed, we're done.
907 if (do_not_and_did_not_need_panning ||
908 ((nouts == outputs.size()) && (npans == _streampanners.size()))) {
912 n = _streampanners.size();
927 /* no need for panning with less than 2 outputs */
929 Changed (); /* EMIT SIGNAL */
936 /* XXX: this can never happen */
940 /* XXX: this can never happen */
941 fatal << _("programming error:")
942 << X_("Panner::reset() called with a single output")
948 outputs.push_back (Output (0, 0));
949 outputs.push_back (Output (1.0, 0));
951 for (n = 0; n < npans; ++n) {
952 _streampanners.push_back (new EqualPowerStereoPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
957 outputs.push_back (Output (0.5, 0));
958 outputs.push_back (Output (0, 1.0));
959 outputs.push_back (Output (1.0, 1.0));
961 for (n = 0; n < npans; ++n) {
962 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
968 outputs.push_back (Output (0, 0));
969 outputs.push_back (Output (1.0, 0));
970 outputs.push_back (Output (1.0, 1.0));
971 outputs.push_back (Output (0, 1.0));
973 for (n = 0; n < npans; ++n) {
974 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
979 case 5: //square+offcenter center
980 outputs.push_back (Output (0, 0));
981 outputs.push_back (Output (1.0, 0));
982 outputs.push_back (Output (1.0, 1.0));
983 outputs.push_back (Output (0, 1.0));
984 outputs.push_back (Output (0.5, 0.75));
986 for (n = 0; n < npans; ++n) {
987 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
993 /* XXX horrible placement. FIXME */
994 for (n = 0; n < nouts; ++n) {
995 outputs.push_back (Output (0.1 * n, 0.1 * n));
998 for (n = 0; n < npans; ++n) {
999 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
1005 for (std::vector<StreamPanner*>::iterator x = _streampanners.begin(); x != _streampanners.end(); ++x) {
1009 /* must emit Changed here, otherwise the changes to the pan_control below raise further
1010 signals which the GUI is not prepared for until it has seen the Changed here.
1014 Changed (); /* EMIT SIGNAL */
1017 /* force hard left/right panning in a common case: 2in/2out
1020 if (npans == 2 && outputs.size() == 2) {
1022 /* Do this only if we changed configuration, or our configuration
1023 appears to be the default set up (center).
1029 _streampanners.front()->get_position (left);
1030 _streampanners.back()->get_position (right);
1032 if (changed || ((left == 0.5) && (right == 0.5))) {
1034 _streampanners.front()->set_position (0.0);
1035 _streampanners.front()->pan_control()->list()->reset_default (0.0);
1037 _streampanners.back()->set_position (1.0);
1038 _streampanners.back()->pan_control()->list()->reset_default (1.0);
1044 Panner::remove (uint32_t which)
1046 vector<StreamPanner*>::iterator i;
1047 for (i = _streampanners.begin(); i != _streampanners.end() && which; ++i, --which) {}
1049 if (i != _streampanners.end()) {
1051 _streampanners.erase (i);
1056 /** Remove all our StreamPanners */
1058 Panner::clear_panners ()
1060 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1064 _streampanners.clear ();
1068 Panner::set_automation_style (AutoStyle style)
1070 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1071 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_style (style);
1073 _session.set_dirty ();
1077 Panner::set_automation_state (AutoState state)
1079 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1080 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_state (state);
1082 _session.set_dirty ();
1086 Panner::automation_state () const
1088 boost::shared_ptr<AutomationList> l;
1090 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1092 l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1096 return l ? l->automation_state() : Off;
1100 Panner::automation_style () const
1102 boost::shared_ptr<AutomationList> l;
1104 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1106 l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1110 return l ? l->automation_style() : Absolute;
1116 StreamPanner* (*factory)(Panner&, Evoral::Parameter);
1119 PanPlugins pan_plugins[] = {
1120 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1121 { Multi2dPanner::name, 3, Multi2dPanner::factory },
1122 { string (""), 0, 0 }
1126 Panner::get_state (void)
1128 return state (true);
1132 Panner::state (bool full)
1134 XMLNode* node = new XMLNode ("Panner");
1138 node->add_property (X_("linked"), (_linked ? "yes" : "no"));
1139 node->add_property (X_("link_direction"), enum_2_string (_link_direction));
1140 node->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1142 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1143 XMLNode* onode = new XMLNode (X_("Output"));
1144 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1145 onode->add_property (X_("x"), buf);
1146 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1147 onode->add_property (X_("y"), buf);
1148 node->add_child_nocopy (*onode);
1151 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1152 node->add_child_nocopy ((*i)->state (full));
1160 Panner::set_state (const XMLNode& node, int version)
1163 XMLNodeConstIterator niter;
1164 const XMLProperty *prop;
1166 uint32_t num_panners = 0;
1168 LocaleGuard lg (X_("POSIX"));
1172 ChanCount ins = ChanCount::ZERO;
1173 ChanCount outs = ChanCount::ZERO;
1175 // XXX: this might not be necessary anymore
1178 if ((prop = node.property (X_("linked"))) != 0) {
1179 set_linked (string_is_affirmative (prop->value()));
1183 if ((prop = node.property (X_("bypassed"))) != 0) {
1184 set_bypassed (string_is_affirmative (prop->value()));
1187 if ((prop = node.property (X_("link_direction"))) != 0) {
1188 LinkDirection ld; /* here to provide type information */
1189 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1192 nlist = node.children();
1194 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1195 if ((*niter)->name() == X_("Output")) {
1199 prop = (*niter)->property (X_("x"));
1200 sscanf (prop->value().c_str(), "%g", &x);
1202 prop = (*niter)->property (X_("y"));
1203 sscanf (prop->value().c_str(), "%g", &y);
1205 outputs.push_back (Output (x, y));
1209 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1211 if ((*niter)->name() == X_("StreamPanner")) {
1213 if ((prop = (*niter)->property (X_("type")))) {
1215 for (i = 0; pan_plugins[i].factory; ++i) {
1216 if (prop->value() == pan_plugins[i].name) {
1219 /* note that we assume that all the stream panners
1220 are of the same type. pretty good
1221 assumption, but it's still an assumption.
1224 sp = pan_plugins[i].factory (*this, Evoral::Parameter(PanAutomation, 0, num_panners));
1227 if (sp->set_state (**niter, version) == 0) {
1228 _streampanners.push_back (sp);
1236 if (!pan_plugins[i].factory) {
1237 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1243 error << _("panner plugin node has no type information!")
1251 reset (outputs.size (), num_panners);
1252 /* don't try to do old-school automation loading if it wasn't marked as existing */
1254 if ((prop = node.property (X_("automation")))) {
1256 /* automation path is relative */
1258 automation_path = Glib::build_filename(_session.automation_dir(), prop->value ());
1265 Panner::touching () const
1267 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1268 if (((AutomationList*)(*i)->pan_control()->list().get())->touching ()) {
1277 Panner::set_position (float xpos, StreamPanner& orig)
1283 orig.get_position (xnow);
1284 xdelta = xpos - xnow;
1286 if (_link_direction == SameDirection) {
1288 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1290 (*i)->set_position (xpos, true);
1292 (*i)->get_position (xnow);
1293 xnew = min (1.0f, xnow + xdelta);
1294 xnew = max (0.0f, xnew);
1295 (*i)->set_position (xnew, true);
1301 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1303 (*i)->set_position (xpos, true);
1305 (*i)->get_position (xnow);
1306 xnew = min (1.0f, xnow - xdelta);
1307 xnew = max (0.0f, xnew);
1308 (*i)->set_position (xnew, true);
1315 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1318 float xdelta, ydelta;
1321 orig.get_position (xnow, ynow);
1322 xdelta = xpos - xnow;
1323 ydelta = ypos - ynow;
1325 if (_link_direction == SameDirection) {
1327 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1329 (*i)->set_position (xpos, ypos, true);
1331 (*i)->get_position (xnow, ynow);
1333 xnew = min (1.0f, xnow + xdelta);
1334 xnew = max (0.0f, xnew);
1336 ynew = min (1.0f, ynow + ydelta);
1337 ynew = max (0.0f, ynew);
1339 (*i)->set_position (xnew, ynew, true);
1345 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1347 (*i)->set_position (xpos, ypos, true);
1349 (*i)->get_position (xnow, ynow);
1351 xnew = min (1.0f, xnow - xdelta);
1352 xnew = max (0.0f, xnew);
1354 ynew = min (1.0f, ynow - ydelta);
1355 ynew = max (0.0f, ynew);
1357 (*i)->set_position (xnew, ynew, true);
1364 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1366 float xnow, ynow, znow;
1367 float xdelta, ydelta, zdelta;
1368 float xnew, ynew, znew;
1370 orig.get_position (xnow, ynow, znow);
1371 xdelta = xpos - xnow;
1372 ydelta = ypos - ynow;
1373 zdelta = zpos - znow;
1375 if (_link_direction == SameDirection) {
1377 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1379 (*i)->set_position (xpos, ypos, zpos, true);
1381 (*i)->get_position (xnow, ynow, znow);
1383 xnew = min (1.0f, xnow + xdelta);
1384 xnew = max (0.0f, xnew);
1386 ynew = min (1.0f, ynow + ydelta);
1387 ynew = max (0.0f, ynew);
1389 znew = min (1.0f, znow + zdelta);
1390 znew = max (0.0f, znew);
1392 (*i)->set_position (xnew, ynew, znew, true);
1398 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1400 (*i)->set_position (xpos, ypos, true);
1402 (*i)->get_position (xnow, ynow, znow);
1404 xnew = min (1.0f, xnow - xdelta);
1405 xnew = max (0.0f, xnew);
1407 ynew = min (1.0f, ynow - ydelta);
1408 ynew = max (0.0f, ynew);
1410 znew = min (1.0f, znow + zdelta);
1411 znew = max (0.0f, znew);
1413 (*i)->set_position (xnew, ynew, znew, true);
1420 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, gain_t gain_coeff)
1422 if (outbufs.count().n_audio() == 0) {
1423 // Don't want to lose audio...
1424 assert(inbufs.count().n_audio() == 0);
1428 // We shouldn't be called in the first place...
1429 assert(!bypassed());
1433 if (outbufs.count().n_audio() == 1) {
1435 AudioBuffer& dst = outbufs.get_audio(0);
1437 if (gain_coeff == 0.0f) {
1439 /* only one output, and gain was zero, so make it silent */
1441 dst.silence (nframes);
1443 } else if (gain_coeff == 1.0f){
1445 /* mix all buffers into the output */
1448 dst.read_from(inbufs.get_audio(0), nframes);
1450 // accumulate starting with the second
1451 if (inbufs.count().n_audio() > 0) {
1452 BufferSet::audio_iterator i = inbufs.audio_begin();
1453 for (++i; i != inbufs.audio_end(); ++i) {
1454 dst.merge_from(*i, nframes);
1460 /* mix all buffers into the output, scaling them all by the gain */
1463 dst.read_from(inbufs.get_audio(0), nframes);
1465 // accumulate (with gain) starting with the second
1466 if (inbufs.count().n_audio() > 0) {
1467 BufferSet::audio_iterator i = inbufs.audio_begin();
1468 for (++i; i != inbufs.audio_end(); ++i) {
1469 dst.accumulate_with_gain_from(*i, nframes, gain_coeff);
1478 /* the terrible silence ... */
1479 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1480 i->silence(nframes);
1483 BufferSet::audio_iterator i = inbufs.audio_begin();
1485 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end() && i != inbufs.audio_end(); ++pan, ++i) {
1486 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1491 Panner::run (BufferSet& inbufs, BufferSet& outbufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
1493 if (outbufs.count().n_audio() == 0) {
1494 // Failing to deliver audio we were asked to deliver is a bug
1495 assert(inbufs.count().n_audio() == 0);
1499 // We shouldn't be called in the first place...
1500 assert(!bypassed());
1503 // If we shouldn't play automation defer to distribute_no_automation
1504 if (!(automation_state() & Play || ((automation_state() & Touch) && !touching()))) {
1507 gain_t gain_coeff = 1.0;
1509 if (fabsf(_session.transport_speed()) > 1.5f && Config->get_quieten_at_speed ()) {
1510 gain_coeff = speed_quietning;
1513 distribute_no_automation (inbufs, outbufs, nframes, gain_coeff);
1517 // Otherwise.. let the automation flow, baby
1519 if (outbufs.count().n_audio() == 1) {
1521 AudioBuffer& dst = outbufs.get_audio(0);
1523 // FIXME: apply gain automation?
1526 dst.read_from(inbufs.get_audio(0), nframes);
1528 // accumulate starting with the second
1529 BufferSet::audio_iterator i = inbufs.audio_begin();
1530 for (++i; i != inbufs.audio_end(); ++i) {
1531 dst.merge_from(*i, nframes);
1537 // More than 1 output, we should have 1 panner for each input
1538 //assert(_streampanners.size() == inbufs.count().n_audio());
1540 /* the terrible silence ... */
1541 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1542 i->silence(nframes);
1545 BufferSet::audio_iterator i = inbufs.audio_begin();
1546 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end(); ++pan, ++i) {
1547 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
1551 /* old school automation handling */
1555 Panner::set_name (string str)
1557 automation_path = Glib::build_filename(_session.automation_dir(),
1558 _session.snap_name() + "-pan-" + legalize_for_path (str) + ".automation");
1566 uint32_t linecnt = 0;
1568 vector<StreamPanner*>::iterator sp;
1569 LocaleGuard lg (X_("POSIX"));
1571 if (automation_path.length() == 0) {
1575 if (access (automation_path.c_str(), F_OK)) {
1579 ifstream in (automation_path.c_str());
1582 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1583 automation_path, strerror (errno))
1588 sp = _streampanners.begin();
1590 while (in.getline (line, sizeof(line), '\n')) {
1592 if (++linecnt == 1) {
1593 if (memcmp (line, X_("version"), 7) == 0) {
1594 if (sscanf (line, "version %f", &version) != 1) {
1595 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1599 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1600 automation_path, line) << endmsg;
1607 if (strlen (line) == 0 || line[0] == '#') {
1611 if (strcmp (line, "begin") == 0) {
1613 if (sp == _streampanners.end()) {
1614 error << string_compose (_("too many panner states found in pan automation file %1"),
1620 if ((*sp)->load (in, automation_path, linecnt)) {
1632 Panner::set_mono (bool yn)
1639 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1640 (*i)->set_mono (yn);