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);
84 _control = boost::dynamic_pointer_cast<AutomationControl>( parent.control( param, true ) );
91 StreamPanner::~StreamPanner ()
96 Panner::PanControllable::set_value (float val)
98 panner.streampanner(parameter().id()).set_position (direct_control_to_pan (val));
99 AutomationControl::set_value(val);
103 Panner::PanControllable::get_value (void) const
105 return AutomationControl::get_value();
109 StreamPanner::set_muted (bool yn)
118 StreamPanner::set_position (float xpos, bool link_call)
120 if (!link_call && parent.linked()) {
121 parent.set_position (xpos, *this);
128 _control->Changed ();
133 StreamPanner::set_position (float xpos, float ypos, bool link_call)
135 if (!link_call && parent.linked()) {
136 parent.set_position (xpos, ypos, *this);
139 if (x != xpos || y != ypos) {
149 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
151 if (!link_call && parent.linked()) {
152 parent.set_position (xpos, ypos, zpos, *this);
155 if (x != xpos || y != ypos || z != zpos) {
165 StreamPanner::set_state (const XMLNode& node)
167 const XMLProperty* prop;
168 XMLNodeConstIterator iter;
170 if ((prop = node.property (X_("muted")))) {
171 set_muted (prop->value() == "yes");
178 StreamPanner::add_state (XMLNode& node)
180 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
183 /*---------------------------------------------------------------------- */
185 BaseStereoPanner::BaseStereoPanner (Panner& p, Evoral::Parameter param)
186 : StreamPanner (p, param)
190 BaseStereoPanner::~BaseStereoPanner ()
195 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
198 LocaleGuard lg (X_("POSIX"));
200 _control->list()->clear ();
202 while (in.getline (line, sizeof (line), '\n')) {
208 if (strcmp (line, "end") == 0) {
212 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
213 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
217 _control->list()->fast_simple_add (when, value);
220 /* now that we are done loading */
222 ((AutomationList*)_control->list().get())->StateChanged ();
228 BaseStereoPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
230 assert(obufs.count().n_audio() == 2);
240 Sample* const src = srcbuf.data();
244 dst = obufs.get_audio(0).data();
246 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
248 /* interpolate over 64 frames or nframes, whichever is smaller */
250 nframes_t limit = min ((nframes_t)64, nframes);
253 delta = -(delta / (float) (limit));
255 for (n = 0; n < limit; n++) {
256 left_interp = left_interp + delta;
257 left = left_interp + 0.9 * (left - left_interp);
258 dst[n] += src[n] * left * gain_coeff;
261 pan = left * gain_coeff;
263 mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
270 if ((pan = (left * gain_coeff)) != 1.0f) {
274 mix_buffers_with_gain(dst,src,nframes,pan);
276 /* mark that we wrote into the buffer */
284 mix_buffers_no_gain(dst,src,nframes);
286 /* mark that we wrote into the buffer */
294 dst = obufs.get_audio(1).data();
296 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
298 /* interpolate over 64 frames or nframes, whichever is smaller */
300 nframes_t limit = min ((nframes_t)64, nframes);
303 delta = -(delta / (float) (limit));
305 for (n = 0; n < limit; n++) {
306 right_interp = right_interp + delta;
307 right = right_interp + 0.9 * (right - right_interp);
308 dst[n] += src[n] * right * gain_coeff;
311 pan = right * gain_coeff;
313 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
315 /* XXX it would be nice to mark the buffer as written to */
319 right = desired_right;
320 right_interp = right;
322 if ((pan = (right * gain_coeff)) != 1.0f) {
326 mix_buffers_with_gain(dst,src,nframes,pan);
328 /* XXX it would be nice to mark the buffer as written to */
333 mix_buffers_no_gain(dst,src,nframes);
335 /* XXX it would be nice to mark the buffer as written to */
340 /*---------------------------------------------------------------------- */
342 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p, Evoral::Parameter param)
343 : BaseStereoPanner (p, param)
348 right = desired_right;
350 right_interp = right;
353 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
358 EqualPowerStereoPanner::update ()
360 /* it would be very nice to split this out into a virtual function
361 that can be accessed from BaseStereoPanner and used in distribute_automated().
363 but the place where its used in distribute_automated() is a tight inner loop,
364 and making "nframes" virtual function calls to compute values is an absurd
368 /* x == 0 => hard left
373 float panL = 1 - panR;
375 const float pan_law_attenuation = -3.0f;
376 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
378 desired_left = panL * (scale * panL + 1.0f - scale);
379 desired_right = panR * (scale * panR + 1.0f - scale);
382 //_control->set_value(x);
386 EqualPowerStereoPanner::distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs,
387 nframes_t start, nframes_t end, nframes_t nframes,
390 assert(obufs.count().n_audio() == 2);
394 Sample* const src = srcbuf.data();
396 /* fetch positional data */
398 if (!_control->list()->curve().rt_safe_get_vector (start, end, buffers[0], nframes)) {
401 distribute (srcbuf, obufs, 1.0, nframes);
406 /* store effective pan position. do this even if we are muted */
409 effective_x = buffers[0][nframes-1];
416 /* apply pan law to convert positional data into pan coefficients for
420 const float pan_law_attenuation = -3.0f;
421 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
423 for (nframes_t n = 0; n < nframes; ++n) {
425 float panR = buffers[0][n];
426 float panL = 1 - panR;
428 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
429 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
434 dst = obufs.get_audio(0).data();
437 for (nframes_t n = 0; n < nframes; ++n) {
438 dst[n] += src[n] * pbuf[n];
441 /* XXX it would be nice to mark the buffer as written to */
445 dst = obufs.get_audio(1).data();
448 for (nframes_t n = 0; n < nframes; ++n) {
449 dst[n] += src[n] * pbuf[n];
452 /* XXX it would be nice to mark the buffer as written to */
456 EqualPowerStereoPanner::factory (Panner& parent, Evoral::Parameter param)
458 return new EqualPowerStereoPanner (parent, param);
462 EqualPowerStereoPanner::get_state (void)
468 EqualPowerStereoPanner::state (bool full_state)
470 XMLNode* root = new XMLNode ("StreamPanner");
472 LocaleGuard lg (X_("POSIX"));
474 snprintf (buf, sizeof (buf), "%.12g", x);
475 root->add_property (X_("x"), buf);
476 root->add_property (X_("type"), EqualPowerStereoPanner::name);
478 // XXX: dont save automation here... its part of the automatable panner now.
480 StreamPanner::add_state (*root);
482 root->add_child_nocopy (_control->get_state ());
488 EqualPowerStereoPanner::set_state (const XMLNode& node)
490 const XMLProperty* prop;
492 LocaleGuard lg (X_("POSIX"));
494 if ((prop = node.property (X_("x")))) {
495 pos = atof (prop->value().c_str());
496 set_position (pos, true);
499 StreamPanner::set_state (node);
501 for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
503 if ((*iter)->name() == X_("Controllable")) {
504 if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
505 _control->set_state (**iter);
508 } else if ((*iter)->name() == X_("Automation")) {
510 _control->alist()->set_state (*((*iter)->children().front()));
512 if (_control->alist()->automation_state() != Off) {
513 set_position (_control->list()->eval (parent.session().transport_frame()));
521 /*----------------------------------------------------------------------*/
523 Multi2dPanner::Multi2dPanner (Panner& p, Evoral::Parameter param)
524 : StreamPanner (p, param)
529 Multi2dPanner::~Multi2dPanner ()
534 Multi2dPanner::update ()
536 static const float BIAS = FLT_MIN;
538 uint32_t nouts = parent.outputs.size();
545 for (i = 0; i < nouts; i++) {
546 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
550 f += dsq[i] * dsq[i];
553 // terrible hack to support OSX < 10.3.9 builds
554 fr = (float) (1.0 / sqrt((double)f));
558 for (i = 0; i < nouts; ++i) {
559 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
566 Multi2dPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
570 vector<Panner::Output>::iterator o;
577 Sample* const src = srcbuf.data();
580 for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
582 dst = obufs.get_audio(n).data();
585 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
587 /* interpolate over 64 frames or nframes, whichever is smaller */
589 nframes_t limit = min ((nframes_t)64, nframes);
592 delta = -(delta / (float) (limit));
594 for (n = 0; n < limit; n++) {
595 left_interp = left_interp + delta;
596 left = left_interp + 0.9 * (left - left_interp);
597 dst[n] += src[n] * left * gain_coeff;
600 pan = left * gain_coeff;
601 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
606 pan = (*o).desired_pan;
608 if ((pan *= gain_coeff) != 1.0f) {
611 mix_buffers_with_gain(dst,src,nframes,pan);
614 mix_buffers_no_gain(dst,src,nframes);
626 Multi2dPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs,
627 nframes_t start, nframes_t end, nframes_t nframes,
640 Multi2dPanner::factory (Panner& p, Evoral::Parameter param)
642 return new Multi2dPanner (p, param);
646 Multi2dPanner::load (istream& in, string path, uint32_t& linecnt)
652 Multi2dPanner::get_state (void)
658 Multi2dPanner::state (bool full_state)
660 XMLNode* root = new XMLNode ("StreamPanner");
662 LocaleGuard lg (X_("POSIX"));
664 snprintf (buf, sizeof (buf), "%.12g", x);
665 root->add_property (X_("x"), buf);
666 snprintf (buf, sizeof (buf), "%.12g", y);
667 root->add_property (X_("y"), buf);
668 root->add_property (X_("type"), Multi2dPanner::name);
670 /* XXX no meaningful automation yet */
676 Multi2dPanner::set_state (const XMLNode& node)
678 const XMLProperty* prop;
680 LocaleGuard lg (X_("POSIX"));
685 if ((prop = node.property (X_("x")))) {
686 newx = atof (prop->value().c_str());
689 if ((prop = node.property (X_("y")))) {
690 newy = atof (prop->value().c_str());
693 if (x < 0 || y < 0) {
694 error << _("badly-formed positional data for Multi2dPanner - ignored")
699 set_position (newx, newy);
703 /*---------------------------------------------------------------------- */
705 Panner::Panner (string name, Session& s)
706 : SessionObject (s, name)
707 , AutomatableControls (s)
709 //set_name_old_auto (name);
713 _link_direction = SameDirection;
722 Panner::set_linked (bool yn)
726 _session.set_dirty ();
727 LinkStateChanged (); /* EMIT SIGNAL */
732 Panner::set_link_direction (LinkDirection ld)
734 if (ld != _link_direction) {
735 _link_direction = ld;
736 _session.set_dirty ();
737 LinkStateChanged (); /* EMIT SIGNAL */
743 Panner::set_bypassed (bool yn)
745 if (yn != _bypassed) {
753 Panner::reset_to_default ()
755 vector<float> positions;
757 switch (outputs.size()) {
763 if (outputs.size() == 2) {
764 switch (_streampanners.size()) {
766 _streampanners.front()->set_position (0.5);
767 _streampanners.front()->pan_control()->list()->reset_default (0.5);
771 _streampanners.front()->set_position (0.0);
772 _streampanners.front()->pan_control()->list()->reset_default (0.0);
773 _streampanners.back()->set_position (1.0);
774 _streampanners.back()->pan_control()->list()->reset_default (1.0);
781 vector<Output>::iterator o;
782 vector<StreamPanner*>::iterator p;
784 for (o = outputs.begin(), p = _streampanners.begin(); o != outputs.end() && p != _streampanners.end(); ++o, ++p) {
785 (*p)->set_position ((*o).x, (*o).y);
790 Panner::reset_streampanner (uint32_t which)
792 if (which >= _streampanners.size() || which >= outputs.size()) {
796 switch (outputs.size()) {
802 switch (_streampanners.size()) {
804 /* stereo out, 1 stream, default = middle */
805 _streampanners.front()->set_position (0.5);
806 _streampanners.front()->pan_control()->list()->reset_default (0.5);
809 /* stereo out, 2 streams, default = hard left/right */
811 _streampanners.front()->set_position (0.0);
812 _streampanners.front()->pan_control()->list()->reset_default (0.0);
814 _streampanners.back()->set_position (1.0);
815 _streampanners.back()->pan_control()->list()->reset_default (1.0);
822 _streampanners[which]->set_position (outputs[which].x, outputs[which].y);
827 Panner::reset (uint32_t nouts, uint32_t npans)
830 bool changed = false;
831 bool do_not_and_did_not_need_panning = ((nouts < 2) && (outputs.size() < 2));
833 cerr << "panner " << _name << " reset to " << nouts << " / " << npans << endl;
835 /* if new and old config don't need panning, or if
836 the config hasn't changed, we're done.
839 if (do_not_and_did_not_need_panning ||
840 ((nouts == outputs.size()) && (npans == _streampanners.size()))) {
844 n = _streampanners.size();
859 /* no need for panning with less than 2 outputs */
865 /* XXX: this can never happen */
869 /* XXX: this can never happen */
870 fatal << _("programming error:")
871 << X_("Panner::reset() called with a single output")
877 outputs.push_back (Output (0, 0));
878 outputs.push_back (Output (1.0, 0));
880 for (n = 0; n < npans; ++n) {
881 _streampanners.push_back (new EqualPowerStereoPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
886 outputs.push_back (Output (0.5, 0));
887 outputs.push_back (Output (0, 1.0));
888 outputs.push_back (Output (1.0, 1.0));
890 for (n = 0; n < npans; ++n) {
891 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
897 outputs.push_back (Output (0, 0));
898 outputs.push_back (Output (1.0, 0));
899 outputs.push_back (Output (1.0, 1.0));
900 outputs.push_back (Output (0, 1.0));
902 for (n = 0; n < npans; ++n) {
903 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
908 case 5: //square+offcenter center
909 outputs.push_back (Output (0, 0));
910 outputs.push_back (Output (1.0, 0));
911 outputs.push_back (Output (1.0, 1.0));
912 outputs.push_back (Output (0, 1.0));
913 outputs.push_back (Output (0.5, 0.75));
915 for (n = 0; n < npans; ++n) {
916 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
922 /* XXX horrible placement. FIXME */
923 for (n = 0; n < nouts; ++n) {
924 outputs.push_back (Output (0.1 * n, 0.1 * n));
927 for (n = 0; n < npans; ++n) {
928 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
934 for (std::vector<StreamPanner*>::iterator x = _streampanners.begin(); x != _streampanners.end(); ++x) {
938 /* force hard left/right panning in a common case: 2in/2out
941 if (npans == 2 && outputs.size() == 2) {
943 /* Do this only if we changed configuration, or our configuration
944 appears to be the default set up (center).
950 _streampanners.front()->get_position (left);
951 _streampanners.back()->get_position (right);
953 if (changed || ((left == 0.5) && (right == 0.5))) {
955 _streampanners.front()->set_position (0.0);
956 _streampanners.front()->pan_control()->list()->reset_default (0.0);
958 _streampanners.back()->set_position (1.0);
959 _streampanners.back()->pan_control()->list()->reset_default (1.0);
967 Changed (); /* EMIT SIGNAL */
974 Panner::remove (uint32_t which)
976 vector<StreamPanner*>::iterator i;
977 for (i = _streampanners.begin(); i != _streampanners.end() && which; ++i, --which) {}
979 if (i != _streampanners.end()) {
981 _streampanners.erase (i);
986 /** Remove all our StreamPanners */
988 Panner::clear_panners ()
990 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
994 _streampanners.clear ();
998 Panner::set_automation_style (AutoStyle style)
1000 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1001 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_style (style);
1003 _session.set_dirty ();
1007 Panner::set_automation_state (AutoState state)
1009 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1010 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_state (state);
1012 _session.set_dirty ();
1016 Panner::automation_state () const
1018 boost::shared_ptr<AutomationList> l;
1020 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1022 l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1026 return l ? l->automation_state() : Off;
1030 Panner::automation_style () const
1032 boost::shared_ptr<AutomationList> l;
1034 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1036 l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1040 return l ? l->automation_style() : Absolute;
1046 StreamPanner* (*factory)(Panner&, Evoral::Parameter);
1049 PanPlugins pan_plugins[] = {
1050 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1051 { Multi2dPanner::name, 3, Multi2dPanner::factory },
1052 { string (""), 0, 0 }
1056 Panner::get_state (void)
1058 return state (true);
1062 Panner::state (bool full)
1064 XMLNode* node = new XMLNode ("Panner");
1068 node->add_property (X_("linked"), (_linked ? "yes" : "no"));
1069 node->add_property (X_("link_direction"), enum_2_string (_link_direction));
1070 node->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1072 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1073 XMLNode* onode = new XMLNode (X_("Output"));
1074 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1075 onode->add_property (X_("x"), buf);
1076 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1077 onode->add_property (X_("y"), buf);
1078 node->add_child_nocopy (*onode);
1081 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1082 node->add_child_nocopy ((*i)->state (full));
1090 Panner::set_state (const XMLNode& node)
1093 XMLNodeConstIterator niter;
1094 const XMLProperty *prop;
1096 uint32_t num_panners = 0;
1098 LocaleGuard lg (X_("POSIX"));
1102 ChanCount ins = ChanCount::ZERO;
1103 ChanCount outs = ChanCount::ZERO;
1105 // XXX: this might not be necessary anymore
1108 if ((prop = node.property (X_("linked"))) != 0) {
1109 set_linked (prop->value() == "yes");
1113 if ((prop = node.property (X_("bypassed"))) != 0) {
1114 set_bypassed (prop->value() == "yes");
1117 if ((prop = node.property (X_("link_direction"))) != 0) {
1118 LinkDirection ld; /* here to provide type information */
1119 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1122 nlist = node.children();
1124 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1125 if ((*niter)->name() == X_("Output")) {
1129 prop = (*niter)->property (X_("x"));
1130 sscanf (prop->value().c_str(), "%g", &x);
1132 prop = (*niter)->property (X_("y"));
1133 sscanf (prop->value().c_str(), "%g", &y);
1135 outputs.push_back (Output (x, y));
1139 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1141 if ((*niter)->name() == X_("StreamPanner")) {
1143 if ((prop = (*niter)->property (X_("type")))) {
1145 for (i = 0; pan_plugins[i].factory; ++i) {
1146 if (prop->value() == pan_plugins[i].name) {
1149 /* note that we assume that all the stream panners
1150 are of the same type. pretty good
1151 assumption, but it's still an assumption.
1154 sp = pan_plugins[i].factory (*this, Evoral::Parameter(PanAutomation, 0, num_panners));
1157 if (sp->set_state (**niter) == 0) {
1158 _streampanners.push_back (sp);
1166 if (!pan_plugins[i].factory) {
1167 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1173 error << _("panner plugin node has no type information!")
1181 reset (outputs.size (), num_panners);
1182 /* don't try to do old-school automation loading if it wasn't marked as existing */
1184 if ((prop = node.property (X_("automation")))) {
1186 /* automation path is relative */
1188 automation_path = Glib::build_filename(_session.automation_dir(), prop->value ());
1195 Panner::touching () const
1197 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1198 if (((AutomationList*)(*i)->pan_control()->list().get())->touching ()) {
1207 Panner::set_position (float xpos, StreamPanner& orig)
1213 orig.get_position (xnow);
1214 xdelta = xpos - xnow;
1216 if (_link_direction == SameDirection) {
1218 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1220 (*i)->set_position (xpos, true);
1222 (*i)->get_position (xnow);
1223 xnew = min (1.0f, xnow + xdelta);
1224 xnew = max (0.0f, xnew);
1225 (*i)->set_position (xnew, true);
1231 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1233 (*i)->set_position (xpos, true);
1235 (*i)->get_position (xnow);
1236 xnew = min (1.0f, xnow - xdelta);
1237 xnew = max (0.0f, xnew);
1238 (*i)->set_position (xnew, true);
1245 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1248 float xdelta, ydelta;
1251 orig.get_position (xnow, ynow);
1252 xdelta = xpos - xnow;
1253 ydelta = ypos - ynow;
1255 if (_link_direction == SameDirection) {
1257 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1259 (*i)->set_position (xpos, ypos, true);
1261 (*i)->get_position (xnow, ynow);
1263 xnew = min (1.0f, xnow + xdelta);
1264 xnew = max (0.0f, xnew);
1266 ynew = min (1.0f, ynow + ydelta);
1267 ynew = max (0.0f, ynew);
1269 (*i)->set_position (xnew, ynew, true);
1275 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1277 (*i)->set_position (xpos, ypos, true);
1279 (*i)->get_position (xnow, ynow);
1281 xnew = min (1.0f, xnow - xdelta);
1282 xnew = max (0.0f, xnew);
1284 ynew = min (1.0f, ynow - ydelta);
1285 ynew = max (0.0f, ynew);
1287 (*i)->set_position (xnew, ynew, true);
1294 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1296 float xnow, ynow, znow;
1297 float xdelta, ydelta, zdelta;
1298 float xnew, ynew, znew;
1300 orig.get_position (xnow, ynow, znow);
1301 xdelta = xpos - xnow;
1302 ydelta = ypos - ynow;
1303 zdelta = zpos - znow;
1305 if (_link_direction == SameDirection) {
1307 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1309 (*i)->set_position (xpos, ypos, zpos, true);
1311 (*i)->get_position (xnow, ynow, znow);
1313 xnew = min (1.0f, xnow + xdelta);
1314 xnew = max (0.0f, xnew);
1316 ynew = min (1.0f, ynow + ydelta);
1317 ynew = max (0.0f, ynew);
1319 znew = min (1.0f, znow + zdelta);
1320 znew = max (0.0f, znew);
1322 (*i)->set_position (xnew, ynew, znew, true);
1328 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1330 (*i)->set_position (xpos, ypos, true);
1332 (*i)->get_position (xnow, ynow, znow);
1334 xnew = min (1.0f, xnow - xdelta);
1335 xnew = max (0.0f, xnew);
1337 ynew = min (1.0f, ynow - ydelta);
1338 ynew = max (0.0f, ynew);
1340 znew = min (1.0f, znow + zdelta);
1341 znew = max (0.0f, znew);
1343 (*i)->set_position (xnew, ynew, znew, true);
1350 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, gain_t gain_coeff)
1352 if (outbufs.count().n_audio() == 0) {
1353 // Don't want to lose audio...
1354 assert(inbufs.count().n_audio() == 0);
1358 // We shouldn't be called in the first place...
1359 assert(!bypassed());
1363 if (outbufs.count().n_audio() == 1) {
1365 AudioBuffer& dst = outbufs.get_audio(0);
1367 if (gain_coeff == 0.0f) {
1369 /* only one output, and gain was zero, so make it silent */
1371 dst.silence (nframes);
1373 } else if (gain_coeff == 1.0f){
1375 /* mix all buffers into the output */
1378 dst.read_from(inbufs.get_audio(0), nframes);
1380 // accumulate starting with the second
1381 if (inbufs.count().n_audio() > 0) {
1382 BufferSet::audio_iterator i = inbufs.audio_begin();
1383 for (++i; i != inbufs.audio_end(); ++i) {
1384 dst.accumulate_from(*i, nframes);
1390 /* mix all buffers into the output, scaling them all by the gain */
1393 dst.read_from(inbufs.get_audio(0), nframes);
1395 // accumulate (with gain) starting with the second
1396 if (inbufs.count().n_audio() > 0) {
1397 BufferSet::audio_iterator i = inbufs.audio_begin();
1398 for (++i; i != inbufs.audio_end(); ++i) {
1399 dst.accumulate_with_gain_from(*i, nframes, gain_coeff);
1408 /* the terrible silence ... */
1409 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1410 i->silence(nframes);
1413 BufferSet::audio_iterator i = inbufs.audio_begin();
1415 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end() && i != inbufs.audio_end(); ++pan, ++i) {
1416 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1421 Panner::run (BufferSet& inbufs, BufferSet& outbufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
1423 if (outbufs.count().n_audio() == 0) {
1424 // Failing to deliver audio we were asked to deliver is a bug
1425 assert(inbufs.count().n_audio() == 0);
1429 // We shouldn't be called in the first place...
1430 assert(!bypassed());
1433 // If we shouldn't play automation defer to distribute_no_automation
1434 if (!(automation_state() & Play || ((automation_state() & Touch) && !touching()))) {
1437 gain_t gain_coeff = 1.0;
1439 if (fabsf(_session.transport_speed()) > 1.5f) {
1440 gain_coeff = speed_quietning;
1443 distribute_no_automation (inbufs, outbufs, nframes, gain_coeff);
1447 // Otherwise.. let the automation flow, baby
1449 if (outbufs.count().n_audio() == 1) {
1451 AudioBuffer& dst = outbufs.get_audio(0);
1453 // FIXME: apply gain automation?
1456 dst.read_from(inbufs.get_audio(0), nframes);
1458 // accumulate starting with the second
1459 BufferSet::audio_iterator i = inbufs.audio_begin();
1460 for (++i; i != inbufs.audio_end(); ++i) {
1461 dst.accumulate_from(*i, nframes);
1467 // More than 1 output, we should have 1 panner for each input
1468 //assert(_streampanners.size() == inbufs.count().n_audio());
1470 /* the terrible silence ... */
1471 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1472 i->silence(nframes);
1475 BufferSet::audio_iterator i = inbufs.audio_begin();
1476 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end(); ++pan, ++i) {
1477 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
1481 /* old school automation handling */
1485 Panner::set_name (string str)
1487 automation_path = Glib::build_filename(_session.automation_dir(),
1488 _session.snap_name() + "-pan-" + legalize_for_path (str) + ".automation");
1496 uint32_t linecnt = 0;
1498 vector<StreamPanner*>::iterator sp;
1499 LocaleGuard lg (X_("POSIX"));
1501 if (automation_path.length() == 0) {
1505 if (access (automation_path.c_str(), F_OK)) {
1509 ifstream in (automation_path.c_str());
1512 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1513 automation_path, strerror (errno))
1518 sp = _streampanners.begin();
1520 while (in.getline (line, sizeof(line), '\n')) {
1522 if (++linecnt == 1) {
1523 if (memcmp (line, X_("version"), 7) == 0) {
1524 if (sscanf (line, "version %f", &version) != 1) {
1525 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1529 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1530 automation_path, line) << endmsg;
1537 if (strlen (line) == 0 || line[0] == '#') {
1541 if (strcmp (line, "begin") == 0) {
1543 if (sp == _streampanners.end()) {
1544 error << string_compose (_("too many panner states found in pan automation file %1"),
1550 if ((*sp)->load (in, automation_path, linecnt)) {