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 //static double direct_pan_to_control (pan_t val) {
77 StreamPanner::StreamPanner (Panner& p, Evoral::Parameter param)
80 assert(param.type() != NullAutomation);
85 _control = boost::dynamic_pointer_cast<AutomationControl>( parent.control( param, true ) );
92 StreamPanner::~StreamPanner ()
97 StreamPanner::set_mono (bool yn)
106 Panner::PanControllable::set_value (float val)
108 panner.streampanner(parameter().id()).set_position (direct_control_to_pan (val));
109 AutomationControl::set_value(val);
113 Panner::PanControllable::get_value (void) const
115 return AutomationControl::get_value();
119 StreamPanner::set_muted (bool yn)
128 StreamPanner::set_position (float xpos, bool link_call)
130 if (!link_call && parent.linked()) {
131 parent.set_position (xpos, *this);
138 _control->Changed ();
143 StreamPanner::set_position (float xpos, float ypos, bool link_call)
145 if (!link_call && parent.linked()) {
146 parent.set_position (xpos, ypos, *this);
149 if (x != xpos || y != ypos) {
159 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
161 if (!link_call && parent.linked()) {
162 parent.set_position (xpos, ypos, zpos, *this);
165 if (x != xpos || y != ypos || z != zpos) {
175 StreamPanner::set_state (const XMLNode& node, int /*version*/)
177 const XMLProperty* prop;
178 XMLNodeConstIterator iter;
180 if ((prop = node.property (X_("muted")))) {
181 set_muted (string_is_affirmative (prop->value()));
188 StreamPanner::add_state (XMLNode& node)
190 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
194 StreamPanner::distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
197 /* we're in mono mode, so just pan the input to all outputs equally */
198 int const N = parent.nouts ();
199 for (int i = 0; i < N; ++i) {
200 mix_buffers_with_gain (obufs.get_audio(i).data(), src.data(), nframes, gain_coeff);
203 /* normal mode, call the `real' distribute method */
204 do_distribute (src, obufs, gain_coeff, nframes);
209 StreamPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs,
210 nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers)
213 /* we're in mono mode, so just pan the input to all outputs equally */
214 int const N = parent.nouts ();
215 for (int i = 0; i < N; ++i) {
216 mix_buffers_with_gain (obufs.get_audio(i).data(), src.data(), nframes, 1.0);
219 /* normal mode, call the `real' distribute method */
220 do_distribute_automated (src, obufs, start, end, nframes, buffers);
226 /*---------------------------------------------------------------------- */
228 BaseStereoPanner::BaseStereoPanner (Panner& p, Evoral::Parameter param)
229 : StreamPanner (p, param)
233 BaseStereoPanner::~BaseStereoPanner ()
238 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
241 LocaleGuard lg (X_("POSIX"));
243 _control->list()->clear ();
245 while (in.getline (line, sizeof (line), '\n')) {
251 if (strcmp (line, "end") == 0) {
255 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
256 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
260 _control->list()->fast_simple_add (when, value);
263 /* now that we are done loading */
265 ((AutomationList*)_control->list().get())->StateChanged ();
271 BaseStereoPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
273 assert(obufs.count().n_audio() == 2);
283 Sample* const src = srcbuf.data();
287 dst = obufs.get_audio(0).data();
289 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
291 /* we've moving the pan by an appreciable amount, so we must
292 interpolate over 64 frames or nframes, whichever is smaller */
294 nframes_t const limit = min ((nframes_t)64, nframes);
297 delta = -(delta / (float) (limit));
299 for (n = 0; n < limit; n++) {
300 left_interp = left_interp + delta;
301 left = left_interp + 0.9 * (left - left_interp);
302 dst[n] += src[n] * left * gain_coeff;
305 /* then pan the rest of the buffer; no need for interpolation for this bit */
307 pan = left * gain_coeff;
309 mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
316 if ((pan = (left * gain_coeff)) != 1.0f) {
320 /* pan is 1 but also not 0, so we must do it "properly" */
322 mix_buffers_with_gain(dst,src,nframes,pan);
324 /* mark that we wrote into the buffer */
332 /* pan is 1 so we can just copy the input samples straight in */
334 mix_buffers_no_gain(dst,src,nframes);
336 /* mark that we wrote into the buffer */
344 dst = obufs.get_audio(1).data();
346 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
348 /* we're moving the pan by an appreciable amount, so we must
349 interpolate over 64 frames or nframes, whichever is smaller */
351 nframes_t const limit = min ((nframes_t)64, nframes);
354 delta = -(delta / (float) (limit));
356 for (n = 0; n < limit; n++) {
357 right_interp = right_interp + delta;
358 right = right_interp + 0.9 * (right - right_interp);
359 dst[n] += src[n] * right * gain_coeff;
362 /* then pan the rest of the buffer, no need for interpolation for this bit */
364 pan = right * gain_coeff;
366 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
368 /* XXX it would be nice to mark the buffer as written to */
372 right = desired_right;
373 right_interp = right;
375 if ((pan = (right * gain_coeff)) != 1.0f) {
379 /* pan is not 1 but also not 0, so we must do it "properly" */
381 mix_buffers_with_gain(dst,src,nframes,pan);
383 /* XXX it would be nice to mark the buffer as written to */
388 /* pan is 1 so we can just copy the input samples straight in */
390 mix_buffers_no_gain(dst,src,nframes);
392 /* XXX it would be nice to mark the buffer as written to */
397 /*---------------------------------------------------------------------- */
399 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p, Evoral::Parameter param)
400 : BaseStereoPanner (p, param)
405 right = desired_right;
407 right_interp = right;
410 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
415 EqualPowerStereoPanner::update ()
417 /* it would be very nice to split this out into a virtual function
418 that can be accessed from BaseStereoPanner and used in do_distribute_automated().
420 but the place where its used in do_distribute_automated() is a tight inner loop,
421 and making "nframes" virtual function calls to compute values is an absurd
425 /* x == 0 => hard left
430 float panL = 1 - panR;
432 const float pan_law_attenuation = -3.0f;
433 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
435 desired_left = panL * (scale * panL + 1.0f - scale);
436 desired_right = panR * (scale * panR + 1.0f - scale);
439 //_control->set_value(x);
443 EqualPowerStereoPanner::do_distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs,
444 nframes_t start, nframes_t end, nframes_t nframes,
447 assert(obufs.count().n_audio() == 2);
451 Sample* const src = srcbuf.data();
453 /* fetch positional data */
455 if (!_control->list()->curve().rt_safe_get_vector (start, end, buffers[0], nframes)) {
458 do_distribute (srcbuf, obufs, 1.0, nframes);
463 /* store effective pan position. do this even if we are muted */
466 effective_x = buffers[0][nframes-1];
473 /* apply pan law to convert positional data into pan coefficients for
477 const float pan_law_attenuation = -3.0f;
478 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
480 for (nframes_t n = 0; n < nframes; ++n) {
482 float panR = buffers[0][n];
483 float panL = 1 - panR;
485 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
486 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
491 dst = obufs.get_audio(0).data();
494 for (nframes_t n = 0; n < nframes; ++n) {
495 dst[n] += src[n] * pbuf[n];
498 /* XXX it would be nice to mark the buffer as written to */
502 dst = obufs.get_audio(1).data();
505 for (nframes_t n = 0; n < nframes; ++n) {
506 dst[n] += src[n] * pbuf[n];
509 /* XXX it would be nice to mark the buffer as written to */
513 EqualPowerStereoPanner::factory (Panner& parent, Evoral::Parameter param)
515 return new EqualPowerStereoPanner (parent, param);
519 EqualPowerStereoPanner::get_state (void)
525 EqualPowerStereoPanner::state (bool /*full_state*/)
527 XMLNode* root = new XMLNode ("StreamPanner");
529 LocaleGuard lg (X_("POSIX"));
531 snprintf (buf, sizeof (buf), "%.12g", x);
532 root->add_property (X_("x"), buf);
533 root->add_property (X_("type"), EqualPowerStereoPanner::name);
535 // XXX: dont save automation here... its part of the automatable panner now.
537 StreamPanner::add_state (*root);
539 root->add_child_nocopy (_control->get_state ());
545 EqualPowerStereoPanner::set_state (const XMLNode& node, int version)
547 const XMLProperty* prop;
549 LocaleGuard lg (X_("POSIX"));
551 if ((prop = node.property (X_("x")))) {
552 pos = atof (prop->value().c_str());
553 set_position (pos, true);
556 StreamPanner::set_state (node, version);
558 for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
560 if ((*iter)->name() == X_("Controllable")) {
561 if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
562 _control->set_state (**iter, version);
565 } else if ((*iter)->name() == X_("Automation")) {
567 _control->alist()->set_state (*((*iter)->children().front()), version);
569 if (_control->alist()->automation_state() != Off) {
570 set_position (_control->list()->eval (parent.session().transport_frame()));
578 /*----------------------------------------------------------------------*/
580 Multi2dPanner::Multi2dPanner (Panner& p, Evoral::Parameter param)
581 : StreamPanner (p, param)
586 Multi2dPanner::~Multi2dPanner ()
591 Multi2dPanner::update ()
593 static const float BIAS = FLT_MIN;
595 uint32_t nouts = parent.outputs.size();
602 for (i = 0; i < nouts; i++) {
603 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
607 f += dsq[i] * dsq[i];
610 // terrible hack to support OSX < 10.3.9 builds
611 fr = (float) (1.0 / sqrt((double)f));
615 for (i = 0; i < nouts; ++i) {
616 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
623 Multi2dPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
627 vector<Panner::Output>::iterator o;
634 Sample* const src = srcbuf.data();
637 for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
639 dst = obufs.get_audio(n).data();
642 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
644 /* interpolate over 64 frames or nframes, whichever is smaller */
646 nframes_t limit = min ((nframes_t)64, nframes);
649 delta = -(delta / (float) (limit));
651 for (n = 0; n < limit; n++) {
652 left_interp = left_interp + delta;
653 left = left_interp + 0.9 * (left - left_interp);
654 dst[n] += src[n] * left * gain_coeff;
657 pan = left * gain_coeff;
658 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
663 pan = (*o).desired_pan;
665 if ((pan *= gain_coeff) != 1.0f) {
668 mix_buffers_with_gain(dst,src,nframes,pan);
671 mix_buffers_no_gain(dst,src,nframes);
683 Multi2dPanner::do_distribute_automated (AudioBuffer& /*src*/, BufferSet& /*obufs*/,
684 nframes_t /*start*/, nframes_t /*end*/, nframes_t /*nframes*/,
697 Multi2dPanner::factory (Panner& p, Evoral::Parameter param)
699 return new Multi2dPanner (p, param);
703 Multi2dPanner::load (istream& /*in*/, string /*path*/, uint32_t& /*linecnt*/)
709 Multi2dPanner::get_state (void)
715 Multi2dPanner::state (bool /*full_state*/)
717 XMLNode* root = new XMLNode ("StreamPanner");
719 LocaleGuard lg (X_("POSIX"));
721 snprintf (buf, sizeof (buf), "%.12g", x);
722 root->add_property (X_("x"), buf);
723 snprintf (buf, sizeof (buf), "%.12g", y);
724 root->add_property (X_("y"), buf);
725 root->add_property (X_("type"), Multi2dPanner::name);
727 /* XXX no meaningful automation yet */
733 Multi2dPanner::set_state (const XMLNode& node, int /*version*/)
735 const XMLProperty* prop;
737 LocaleGuard lg (X_("POSIX"));
742 if ((prop = node.property (X_("x")))) {
743 newx = atof (prop->value().c_str());
746 if ((prop = node.property (X_("y")))) {
747 newy = atof (prop->value().c_str());
750 if (x < 0 || y < 0) {
751 error << _("badly-formed positional data for Multi2dPanner - ignored")
756 set_position (newx, newy);
760 /*---------------------------------------------------------------------- */
762 Panner::Panner (string name, Session& s)
763 : SessionObject (s, name)
764 , AutomatableControls (s)
766 //set_name_old_auto (name);
770 _link_direction = SameDirection;
780 Panner::set_linked (bool yn)
784 _session.set_dirty ();
785 LinkStateChanged (); /* EMIT SIGNAL */
790 Panner::set_link_direction (LinkDirection ld)
792 if (ld != _link_direction) {
793 _link_direction = ld;
794 _session.set_dirty ();
795 LinkStateChanged (); /* EMIT SIGNAL */
801 Panner::set_bypassed (bool yn)
803 if (yn != _bypassed) {
811 Panner::reset_to_default ()
813 vector<float> positions;
815 switch (outputs.size()) {
821 if (outputs.size() == 2) {
822 switch (_streampanners.size()) {
824 _streampanners.front()->set_position (0.5);
825 _streampanners.front()->pan_control()->list()->reset_default (0.5);
829 _streampanners.front()->set_position (0.0);
830 _streampanners.front()->pan_control()->list()->reset_default (0.0);
831 _streampanners.back()->set_position (1.0);
832 _streampanners.back()->pan_control()->list()->reset_default (1.0);
839 vector<Output>::iterator o;
840 vector<StreamPanner*>::iterator p;
842 for (o = outputs.begin(), p = _streampanners.begin(); o != outputs.end() && p != _streampanners.end(); ++o, ++p) {
843 (*p)->set_position ((*o).x, (*o).y);
848 Panner::reset_streampanner (uint32_t which)
850 if (which >= _streampanners.size() || which >= outputs.size()) {
854 switch (outputs.size()) {
860 switch (_streampanners.size()) {
862 /* stereo out, 1 stream, default = middle */
863 _streampanners.front()->set_position (0.5);
864 _streampanners.front()->pan_control()->list()->reset_default (0.5);
867 /* stereo out, 2 streams, default = hard left/right */
869 _streampanners.front()->set_position (0.0);
870 _streampanners.front()->pan_control()->list()->reset_default (0.0);
872 _streampanners.back()->set_position (1.0);
873 _streampanners.back()->pan_control()->list()->reset_default (1.0);
880 _streampanners[which]->set_position (outputs[which].x, outputs[which].y);
885 * Reset the panner with a given number of outs and panners (and hence inputs)
887 * \param nouts Number of outputs.
888 * \param npans Number of panners.
891 Panner::reset (uint32_t nouts, uint32_t npans)
894 bool changed = false;
895 bool do_not_and_did_not_need_panning = ((nouts < 2) && (outputs.size() < 2));
897 //cout << "Reset panner for " << nouts << " " << npans << "\n";
899 /* if new and old config don't need panning, or if
900 the config hasn't changed, we're done.
903 if (do_not_and_did_not_need_panning ||
904 ((nouts == outputs.size()) && (npans == _streampanners.size()))) {
908 n = _streampanners.size();
923 /* no need for panning with less than 2 outputs */
929 /* XXX: this can never happen */
933 /* XXX: this can never happen */
934 fatal << _("programming error:")
935 << X_("Panner::reset() called with a single output")
941 outputs.push_back (Output (0, 0));
942 outputs.push_back (Output (1.0, 0));
944 for (n = 0; n < npans; ++n) {
945 _streampanners.push_back (new EqualPowerStereoPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
950 outputs.push_back (Output (0.5, 0));
951 outputs.push_back (Output (0, 1.0));
952 outputs.push_back (Output (1.0, 1.0));
954 for (n = 0; n < npans; ++n) {
955 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
961 outputs.push_back (Output (0, 0));
962 outputs.push_back (Output (1.0, 0));
963 outputs.push_back (Output (1.0, 1.0));
964 outputs.push_back (Output (0, 1.0));
966 for (n = 0; n < npans; ++n) {
967 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
972 case 5: //square+offcenter center
973 outputs.push_back (Output (0, 0));
974 outputs.push_back (Output (1.0, 0));
975 outputs.push_back (Output (1.0, 1.0));
976 outputs.push_back (Output (0, 1.0));
977 outputs.push_back (Output (0.5, 0.75));
979 for (n = 0; n < npans; ++n) {
980 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
986 /* XXX horrible placement. FIXME */
987 for (n = 0; n < nouts; ++n) {
988 outputs.push_back (Output (0.1 * n, 0.1 * n));
991 for (n = 0; n < npans; ++n) {
992 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
998 for (std::vector<StreamPanner*>::iterator x = _streampanners.begin(); x != _streampanners.end(); ++x) {
1002 /* force hard left/right panning in a common case: 2in/2out
1005 if (npans == 2 && outputs.size() == 2) {
1007 /* Do this only if we changed configuration, or our configuration
1008 appears to be the default set up (center).
1014 _streampanners.front()->get_position (left);
1015 _streampanners.back()->get_position (right);
1017 if (changed || ((left == 0.5) && (right == 0.5))) {
1019 _streampanners.front()->set_position (0.0);
1020 _streampanners.front()->pan_control()->list()->reset_default (0.0);
1022 _streampanners.back()->set_position (1.0);
1023 _streampanners.back()->pan_control()->list()->reset_default (1.0);
1031 Changed (); /* EMIT SIGNAL */
1038 Panner::remove (uint32_t which)
1040 vector<StreamPanner*>::iterator i;
1041 for (i = _streampanners.begin(); i != _streampanners.end() && which; ++i, --which) {}
1043 if (i != _streampanners.end()) {
1045 _streampanners.erase (i);
1050 /** Remove all our StreamPanners */
1052 Panner::clear_panners ()
1054 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1058 _streampanners.clear ();
1062 Panner::set_automation_style (AutoStyle style)
1064 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1065 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_style (style);
1067 _session.set_dirty ();
1071 Panner::set_automation_state (AutoState state)
1073 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1074 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_state (state);
1076 _session.set_dirty ();
1080 Panner::automation_state () const
1082 boost::shared_ptr<AutomationList> l;
1084 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1086 l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1090 return l ? l->automation_state() : Off;
1094 Panner::automation_style () const
1096 boost::shared_ptr<AutomationList> l;
1098 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1100 l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1104 return l ? l->automation_style() : Absolute;
1110 StreamPanner* (*factory)(Panner&, Evoral::Parameter);
1113 PanPlugins pan_plugins[] = {
1114 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1115 { Multi2dPanner::name, 3, Multi2dPanner::factory },
1116 { string (""), 0, 0 }
1120 Panner::get_state (void)
1122 return state (true);
1126 Panner::state (bool full)
1128 XMLNode* node = new XMLNode ("Panner");
1132 node->add_property (X_("linked"), (_linked ? "yes" : "no"));
1133 node->add_property (X_("link_direction"), enum_2_string (_link_direction));
1134 node->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1136 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1137 XMLNode* onode = new XMLNode (X_("Output"));
1138 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1139 onode->add_property (X_("x"), buf);
1140 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1141 onode->add_property (X_("y"), buf);
1142 node->add_child_nocopy (*onode);
1145 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1146 node->add_child_nocopy ((*i)->state (full));
1154 Panner::set_state (const XMLNode& node, int version)
1157 XMLNodeConstIterator niter;
1158 const XMLProperty *prop;
1160 uint32_t num_panners = 0;
1162 LocaleGuard lg (X_("POSIX"));
1166 ChanCount ins = ChanCount::ZERO;
1167 ChanCount outs = ChanCount::ZERO;
1169 // XXX: this might not be necessary anymore
1172 if ((prop = node.property (X_("linked"))) != 0) {
1173 set_linked (string_is_affirmative (prop->value()));
1177 if ((prop = node.property (X_("bypassed"))) != 0) {
1178 set_bypassed (string_is_affirmative (prop->value()));
1181 if ((prop = node.property (X_("link_direction"))) != 0) {
1182 LinkDirection ld; /* here to provide type information */
1183 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1186 nlist = node.children();
1188 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1189 if ((*niter)->name() == X_("Output")) {
1193 prop = (*niter)->property (X_("x"));
1194 sscanf (prop->value().c_str(), "%g", &x);
1196 prop = (*niter)->property (X_("y"));
1197 sscanf (prop->value().c_str(), "%g", &y);
1199 outputs.push_back (Output (x, y));
1203 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1205 if ((*niter)->name() == X_("StreamPanner")) {
1207 if ((prop = (*niter)->property (X_("type")))) {
1209 for (i = 0; pan_plugins[i].factory; ++i) {
1210 if (prop->value() == pan_plugins[i].name) {
1213 /* note that we assume that all the stream panners
1214 are of the same type. pretty good
1215 assumption, but it's still an assumption.
1218 sp = pan_plugins[i].factory (*this, Evoral::Parameter(PanAutomation, 0, num_panners));
1221 if (sp->set_state (**niter, version) == 0) {
1222 _streampanners.push_back (sp);
1230 if (!pan_plugins[i].factory) {
1231 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1237 error << _("panner plugin node has no type information!")
1245 reset (outputs.size (), num_panners);
1246 /* don't try to do old-school automation loading if it wasn't marked as existing */
1248 if ((prop = node.property (X_("automation")))) {
1250 /* automation path is relative */
1252 automation_path = Glib::build_filename(_session.automation_dir(), prop->value ());
1259 Panner::touching () const
1261 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1262 if (((AutomationList*)(*i)->pan_control()->list().get())->touching ()) {
1271 Panner::set_position (float xpos, StreamPanner& orig)
1277 orig.get_position (xnow);
1278 xdelta = xpos - xnow;
1280 if (_link_direction == SameDirection) {
1282 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1284 (*i)->set_position (xpos, true);
1286 (*i)->get_position (xnow);
1287 xnew = min (1.0f, xnow + xdelta);
1288 xnew = max (0.0f, xnew);
1289 (*i)->set_position (xnew, true);
1295 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1297 (*i)->set_position (xpos, true);
1299 (*i)->get_position (xnow);
1300 xnew = min (1.0f, xnow - xdelta);
1301 xnew = max (0.0f, xnew);
1302 (*i)->set_position (xnew, true);
1309 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1312 float xdelta, ydelta;
1315 orig.get_position (xnow, ynow);
1316 xdelta = xpos - xnow;
1317 ydelta = ypos - ynow;
1319 if (_link_direction == SameDirection) {
1321 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1323 (*i)->set_position (xpos, ypos, true);
1325 (*i)->get_position (xnow, ynow);
1327 xnew = min (1.0f, xnow + xdelta);
1328 xnew = max (0.0f, xnew);
1330 ynew = min (1.0f, ynow + ydelta);
1331 ynew = max (0.0f, ynew);
1333 (*i)->set_position (xnew, ynew, true);
1339 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1341 (*i)->set_position (xpos, ypos, true);
1343 (*i)->get_position (xnow, ynow);
1345 xnew = min (1.0f, xnow - xdelta);
1346 xnew = max (0.0f, xnew);
1348 ynew = min (1.0f, ynow - ydelta);
1349 ynew = max (0.0f, ynew);
1351 (*i)->set_position (xnew, ynew, true);
1358 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1360 float xnow, ynow, znow;
1361 float xdelta, ydelta, zdelta;
1362 float xnew, ynew, znew;
1364 orig.get_position (xnow, ynow, znow);
1365 xdelta = xpos - xnow;
1366 ydelta = ypos - ynow;
1367 zdelta = zpos - znow;
1369 if (_link_direction == SameDirection) {
1371 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1373 (*i)->set_position (xpos, ypos, zpos, true);
1375 (*i)->get_position (xnow, ynow, znow);
1377 xnew = min (1.0f, xnow + xdelta);
1378 xnew = max (0.0f, xnew);
1380 ynew = min (1.0f, ynow + ydelta);
1381 ynew = max (0.0f, ynew);
1383 znew = min (1.0f, znow + zdelta);
1384 znew = max (0.0f, znew);
1386 (*i)->set_position (xnew, ynew, znew, true);
1392 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1394 (*i)->set_position (xpos, ypos, true);
1396 (*i)->get_position (xnow, ynow, znow);
1398 xnew = min (1.0f, xnow - xdelta);
1399 xnew = max (0.0f, xnew);
1401 ynew = min (1.0f, ynow - ydelta);
1402 ynew = max (0.0f, ynew);
1404 znew = min (1.0f, znow + zdelta);
1405 znew = max (0.0f, znew);
1407 (*i)->set_position (xnew, ynew, znew, true);
1414 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, gain_t gain_coeff)
1416 if (outbufs.count().n_audio() == 0) {
1417 // Don't want to lose audio...
1418 assert(inbufs.count().n_audio() == 0);
1422 // We shouldn't be called in the first place...
1423 assert(!bypassed());
1427 if (outbufs.count().n_audio() == 1) {
1429 AudioBuffer& dst = outbufs.get_audio(0);
1431 if (gain_coeff == 0.0f) {
1433 /* only one output, and gain was zero, so make it silent */
1435 dst.silence (nframes);
1437 } else if (gain_coeff == 1.0f){
1439 /* mix all buffers into the output */
1442 dst.read_from(inbufs.get_audio(0), nframes);
1444 // accumulate starting with the second
1445 if (inbufs.count().n_audio() > 0) {
1446 BufferSet::audio_iterator i = inbufs.audio_begin();
1447 for (++i; i != inbufs.audio_end(); ++i) {
1448 dst.merge_from(*i, nframes);
1454 /* mix all buffers into the output, scaling them all by the gain */
1457 dst.read_from(inbufs.get_audio(0), nframes);
1459 // accumulate (with gain) starting with the second
1460 if (inbufs.count().n_audio() > 0) {
1461 BufferSet::audio_iterator i = inbufs.audio_begin();
1462 for (++i; i != inbufs.audio_end(); ++i) {
1463 dst.accumulate_with_gain_from(*i, nframes, gain_coeff);
1472 /* the terrible silence ... */
1473 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1474 i->silence(nframes);
1477 BufferSet::audio_iterator i = inbufs.audio_begin();
1479 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end() && i != inbufs.audio_end(); ++pan, ++i) {
1480 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1485 Panner::run (BufferSet& inbufs, BufferSet& outbufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
1487 if (outbufs.count().n_audio() == 0) {
1488 // Failing to deliver audio we were asked to deliver is a bug
1489 assert(inbufs.count().n_audio() == 0);
1493 // We shouldn't be called in the first place...
1494 assert(!bypassed());
1497 // If we shouldn't play automation defer to distribute_no_automation
1498 if (!(automation_state() & Play || ((automation_state() & Touch) && !touching()))) {
1501 gain_t gain_coeff = 1.0;
1503 if (fabsf(_session.transport_speed()) > 1.5f) {
1504 gain_coeff = speed_quietning;
1507 distribute_no_automation (inbufs, outbufs, nframes, gain_coeff);
1511 // Otherwise.. let the automation flow, baby
1513 if (outbufs.count().n_audio() == 1) {
1515 AudioBuffer& dst = outbufs.get_audio(0);
1517 // FIXME: apply gain automation?
1520 dst.read_from(inbufs.get_audio(0), nframes);
1522 // accumulate starting with the second
1523 BufferSet::audio_iterator i = inbufs.audio_begin();
1524 for (++i; i != inbufs.audio_end(); ++i) {
1525 dst.merge_from(*i, nframes);
1531 // More than 1 output, we should have 1 panner for each input
1532 //assert(_streampanners.size() == inbufs.count().n_audio());
1534 /* the terrible silence ... */
1535 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1536 i->silence(nframes);
1539 BufferSet::audio_iterator i = inbufs.audio_begin();
1540 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end(); ++pan, ++i) {
1541 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
1545 /* old school automation handling */
1549 Panner::set_name (string str)
1551 automation_path = Glib::build_filename(_session.automation_dir(),
1552 _session.snap_name() + "-pan-" + legalize_for_path (str) + ".automation");
1560 uint32_t linecnt = 0;
1562 vector<StreamPanner*>::iterator sp;
1563 LocaleGuard lg (X_("POSIX"));
1565 if (automation_path.length() == 0) {
1569 if (access (automation_path.c_str(), F_OK)) {
1573 ifstream in (automation_path.c_str());
1576 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1577 automation_path, strerror (errno))
1582 sp = _streampanners.begin();
1584 while (in.getline (line, sizeof(line), '\n')) {
1586 if (++linecnt == 1) {
1587 if (memcmp (line, X_("version"), 7) == 0) {
1588 if (sscanf (line, "version %f", &version) != 1) {
1589 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1593 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1594 automation_path, line) << endmsg;
1601 if (strlen (line) == 0 || line[0] == '#') {
1605 if (strcmp (line, "begin") == 0) {
1607 if (sp == _streampanners.end()) {
1608 error << string_compose (_("too many panner states found in pan automation file %1"),
1614 if ((*sp)->load (in, automation_path, linecnt)) {
1626 Panner::set_mono (bool yn)
1633 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1634 (*i)->set_mono (yn);