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 <ardour/session.h>
41 #include <ardour/panner.h>
42 #include <ardour/utils.h>
44 #include <ardour/runtime_functions.h>
45 #include <ardour/buffer_set.h>
49 #include <pbd/mathfix.h>
52 using namespace ARDOUR;
55 float Panner::current_automation_version_number = 1.0;
57 string EqualPowerStereoPanner::name = "Equal Power Stereo";
58 string Multi2dPanner::name = "Multiple (2D)";
60 /* this is a default mapper of control values to a pan position
61 others can be imagined.
64 static pan_t direct_control_to_pan (double fract) {
68 static double direct_pan_to_control (pan_t val) {
72 StreamPanner::StreamPanner (Panner& p, Parameter param)
74 , _control (new PanControllable(p.session(), X_("panner"), *this, param))
76 assert(param.type() != NullAutomation);
80 parent.session().add_controllable (_control);
87 StreamPanner::~StreamPanner ()
92 StreamPanner::PanControllable::set_value (float val)
94 panner.set_position (direct_control_to_pan (val));
98 StreamPanner::PanControllable::get_value (void) const
101 panner.get_effective_position (xpos);
102 return direct_pan_to_control (xpos);
106 StreamPanner::PanControllable::can_send_feedback () const
108 AutoState astate = panner.get_parent().automation_state ();
110 if ((astate == Play) || (astate == Touch && !panner.get_parent().touching())) {
118 StreamPanner::set_muted (bool yn)
127 StreamPanner::set_position (float xpos, bool link_call)
129 if (!link_call && parent.linked()) {
130 parent.set_position (xpos, *this);
137 _control->Changed ();
142 StreamPanner::set_position (float xpos, float ypos, bool link_call)
144 if (!link_call && parent.linked()) {
145 parent.set_position (xpos, ypos, *this);
148 if (x != xpos || y != ypos) {
158 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
160 if (!link_call && parent.linked()) {
161 parent.set_position (xpos, ypos, zpos, *this);
164 if (x != xpos || y != ypos || z != zpos) {
174 StreamPanner::set_state (const XMLNode& node)
176 const XMLProperty* prop;
177 XMLNodeConstIterator iter;
179 if ((prop = node.property (X_("muted")))) {
180 set_muted (prop->value() == "yes");
187 StreamPanner::add_state (XMLNode& node)
189 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
192 /*---------------------------------------------------------------------- */
194 BaseStereoPanner::BaseStereoPanner (Panner& p, Parameter param)
195 : StreamPanner (p, param)
199 BaseStereoPanner::~BaseStereoPanner ()
204 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
207 LocaleGuard lg (X_("POSIX"));
209 _control->list()->clear ();
211 while (in.getline (line, sizeof (line), '\n')) {
217 if (strcmp (line, "end") == 0) {
221 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
222 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
226 _control->list()->fast_simple_add (when, value);
229 /* now that we are done loading */
231 _control->list()->StateChanged ();
237 BaseStereoPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
239 assert(obufs.count().n_audio() == 2);
249 Sample* const src = srcbuf.data();
253 dst = obufs.get_audio(0).data();
255 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
257 /* interpolate over 64 frames or nframes, whichever is smaller */
259 nframes_t limit = min ((nframes_t)64, nframes);
262 delta = -(delta / (float) (limit));
264 for (n = 0; n < limit; n++) {
265 left_interp = left_interp + delta;
266 left = left_interp + 0.9 * (left - left_interp);
267 dst[n] += src[n] * left * gain_coeff;
270 pan = left * gain_coeff;
272 mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
279 if ((pan = (left * gain_coeff)) != 1.0f) {
283 mix_buffers_with_gain(dst,src,nframes,pan);
285 /* mark that we wrote into the buffer */
293 mix_buffers_no_gain(dst,src,nframes);
295 /* mark that we wrote into the buffer */
303 dst = obufs.get_audio(1).data();
305 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
307 /* interpolate over 64 frames or nframes, whichever is smaller */
309 nframes_t limit = min ((nframes_t)64, nframes);
312 delta = -(delta / (float) (limit));
314 for (n = 0; n < limit; n++) {
315 right_interp = right_interp + delta;
316 right = right_interp + 0.9 * (right - right_interp);
317 dst[n] += src[n] * right * gain_coeff;
320 pan = right * gain_coeff;
322 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
324 /* XXX it would be nice to mark the buffer as written to */
328 right = desired_right;
329 right_interp = right;
331 if ((pan = (right * gain_coeff)) != 1.0f) {
335 mix_buffers_with_gain(dst,src,nframes,pan);
337 /* XXX it would be nice to mark the buffer as written to */
342 mix_buffers_no_gain(dst,src,nframes);
344 /* XXX it would be nice to mark the buffer as written to */
349 /*---------------------------------------------------------------------- */
351 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p, Parameter param)
352 : BaseStereoPanner (p, param)
357 right = desired_right;
359 right_interp = right;
362 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
367 EqualPowerStereoPanner::update ()
369 /* it would be very nice to split this out into a virtual function
370 that can be accessed from BaseStereoPanner and used in distribute_automated().
372 but the place where its used in distribute_automated() is a tight inner loop,
373 and making "nframes" virtual function calls to compute values is an absurd
377 /* x == 0 => hard left
382 float panL = 1 - panR;
384 const float pan_law_attenuation = -3.0f;
385 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
387 desired_left = panL * (scale * panL + 1.0f - scale);
388 desired_right = panR * (scale * panR + 1.0f - scale);
391 _control->set_value(x);
395 EqualPowerStereoPanner::distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs,
396 nframes_t start, nframes_t end, nframes_t nframes,
399 assert(obufs.count().n_audio() == 2);
403 Sample* const src = srcbuf.data();
405 /* fetch positional data */
407 if (!_control->list()->curve().rt_safe_get_vector (start, end, buffers[0], nframes)) {
410 distribute (srcbuf, obufs, 1.0, nframes);
415 /* store effective pan position. do this even if we are muted */
418 effective_x = buffers[0][nframes-1];
419 _control->set_value(effective_x); // signal, update UI
426 /* apply pan law to convert positional data into pan coefficients for
430 const float pan_law_attenuation = -3.0f;
431 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
433 for (nframes_t n = 0; n < nframes; ++n) {
435 float panR = buffers[0][n];
436 float panL = 1 - panR;
438 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
439 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
444 dst = obufs.get_audio(0).data();
447 for (nframes_t n = 0; n < nframes; ++n) {
448 dst[n] += src[n] * pbuf[n];
451 /* XXX it would be nice to mark the buffer as written to */
455 dst = obufs.get_audio(1).data();
458 for (nframes_t n = 0; n < nframes; ++n) {
459 dst[n] += src[n] * pbuf[n];
462 /* XXX it would be nice to mark the buffer as written to */
466 EqualPowerStereoPanner::factory (Panner& parent, Parameter param)
468 return new EqualPowerStereoPanner (parent, param);
472 EqualPowerStereoPanner::get_state (void)
478 EqualPowerStereoPanner::state (bool full_state)
480 XMLNode* root = new XMLNode ("StreamPanner");
482 LocaleGuard lg (X_("POSIX"));
484 snprintf (buf, sizeof (buf), "%.12g", x);
485 root->add_property (X_("x"), buf);
486 root->add_property (X_("type"), EqualPowerStereoPanner::name);
488 XMLNode* autonode = new XMLNode (X_("Automation"));
489 autonode->add_child_nocopy (_control->list()->state (full_state));
490 root->add_child_nocopy (*autonode);
492 StreamPanner::add_state (*root);
494 root->add_child_nocopy (_control->get_state ());
500 EqualPowerStereoPanner::set_state (const XMLNode& node)
502 const XMLProperty* prop;
504 LocaleGuard lg (X_("POSIX"));
506 if ((prop = node.property (X_("x")))) {
507 pos = atof (prop->value().c_str());
508 set_position (pos, true);
511 StreamPanner::set_state (node);
513 for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
515 if ((*iter)->name() == X_("controllable")) {
516 if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
517 _control->set_state (**iter);
520 } else if ((*iter)->name() == X_("Automation")) {
522 _control->list()->set_state (*((*iter)->children().front()));
524 if (_control->list()->automation_state() != Off) {
525 set_position (_control->list()->eval (parent.session().transport_frame()));
533 /*----------------------------------------------------------------------*/
535 Multi2dPanner::Multi2dPanner (Panner& p, Parameter param)
536 : StreamPanner (p, param)
541 Multi2dPanner::~Multi2dPanner ()
546 Multi2dPanner::update ()
548 static const float BIAS = FLT_MIN;
550 uint32_t nouts = parent.outputs.size();
557 for (i = 0; i < nouts; i++) {
558 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
562 f += dsq[i] * dsq[i];
565 // terrible hack to support OSX < 10.3.9 builds
566 fr = (float) (1.0 / sqrt((double)f));
570 for (i = 0; i < nouts; ++i) {
571 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
575 _control->set_value(x);
579 Multi2dPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
583 vector<Panner::Output>::iterator o;
590 Sample* const src = srcbuf.data();
593 for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
595 dst = obufs.get_audio(n).data();
598 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
600 /* interpolate over 64 frames or nframes, whichever is smaller */
602 nframes_t limit = min ((nframes_t)64, nframes);
605 delta = -(delta / (float) (limit));
607 for (n = 0; n < limit; n++) {
608 left_interp = left_interp + delta;
609 left = left_interp + 0.9 * (left - left_interp);
610 dst[n] += src[n] * left * gain_coeff;
613 pan = left * gain_coeff;
614 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
619 pan = (*o).desired_pan;
621 if ((pan *= gain_coeff) != 1.0f) {
624 mix_buffers_with_gain(dst,src,nframes,pan);
627 mix_buffers_no_gain(dst,src,nframes);
639 Multi2dPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs,
640 nframes_t start, nframes_t end, nframes_t nframes,
653 Multi2dPanner::factory (Panner& p, Parameter param)
655 return new Multi2dPanner (p, param);
659 Multi2dPanner::load (istream& in, string path, uint32_t& linecnt)
665 Multi2dPanner::get_state (void)
671 Multi2dPanner::state (bool full_state)
673 XMLNode* root = new XMLNode ("StreamPanner");
675 LocaleGuard lg (X_("POSIX"));
677 snprintf (buf, sizeof (buf), "%.12g", x);
678 root->add_property (X_("x"), buf);
679 snprintf (buf, sizeof (buf), "%.12g", y);
680 root->add_property (X_("y"), buf);
681 root->add_property (X_("type"), Multi2dPanner::name);
683 /* XXX no meaningful automation yet */
689 Multi2dPanner::set_state (const XMLNode& node)
691 const XMLProperty* prop;
693 LocaleGuard lg (X_("POSIX"));
698 if ((prop = node.property (X_("x")))) {
699 newx = atof (prop->value().c_str());
702 if ((prop = node.property (X_("y")))) {
703 newy = atof (prop->value().c_str());
706 if (x < 0 || y < 0) {
707 error << _("badly-formed positional data for Multi2dPanner - ignored")
712 set_position (newx, newy);
716 /*---------------------------------------------------------------------- */
718 Panner::Panner (string name, Session& s)
724 _link_direction = SameDirection;
733 Panner::set_linked (bool yn)
737 _session.set_dirty ();
738 LinkStateChanged (); /* EMIT SIGNAL */
743 Panner::set_link_direction (LinkDirection ld)
745 if (ld != _link_direction) {
746 _link_direction = ld;
747 _session.set_dirty ();
748 LinkStateChanged (); /* EMIT SIGNAL */
753 Panner::set_bypassed (bool yn)
755 if (yn != _bypassed) {
763 Panner::reset (uint32_t nouts, uint32_t npans)
766 bool changed = false;
768 if (nouts < 2 || (nouts == outputs.size() && npans == size())) {
791 fatal << _("programming error:")
792 << X_("Panner::reset() called with a single output")
799 outputs.push_back (Output (0, 0));
800 outputs.push_back (Output (1.0, 0));
802 for (n = 0; n < npans; ++n) {
803 push_back (new EqualPowerStereoPanner (*this, Parameter(PanAutomation, n)));
808 outputs.push_back (Output (0.5, 0));
809 outputs.push_back (Output (0, 1.0));
810 outputs.push_back (Output (1.0, 1.0));
812 for (n = 0; n < npans; ++n) {
813 push_back (new Multi2dPanner (*this, Parameter(PanAutomation, n)));
819 outputs.push_back (Output (0, 0));
820 outputs.push_back (Output (1.0, 0));
821 outputs.push_back (Output (1.0, 1.0));
822 outputs.push_back (Output (0, 1.0));
824 for (n = 0; n < npans; ++n) {
825 push_back (new Multi2dPanner (*this, Parameter(PanAutomation, n)));
830 case 5: //square+offcenter center
831 outputs.push_back (Output (0, 0));
832 outputs.push_back (Output (1.0, 0));
833 outputs.push_back (Output (1.0, 1.0));
834 outputs.push_back (Output (0, 1.0));
835 outputs.push_back (Output (0.5, 0.75));
837 for (n = 0; n < npans; ++n) {
838 push_back (new Multi2dPanner (*this, Parameter(PanAutomation, n)));
844 /* XXX horrible placement. FIXME */
845 for (n = 0; n < nouts; ++n) {
846 outputs.push_back (Output (0.1 * n, 0.1 * n));
849 for (n = 0; n < npans; ++n) {
850 push_back (new Multi2dPanner (*this, Parameter(PanAutomation, n)));
856 for (iterator x = begin(); x != end(); ++x) {
860 /* force hard left/right panning in a common case: 2in/2out
863 if (npans == 2 && outputs.size() == 2) {
865 /* Do this only if we changed configuration, or our configuration
866 appears to be the default set up (center).
872 front()->get_position (left);
873 back()->get_position (right);
875 if (changed || ((left == 0.5) && (right == 0.5))) {
877 front()->set_position (0.0);
878 front()->pan_control()->list()->reset_default (0.0);
880 back()->set_position (1.0);
881 back()->pan_control()->list()->reset_default (1.0);
888 Changed (); /* EMIT SIGNAL */
895 Panner::remove (uint32_t which)
897 vector<StreamPanner*>::iterator i;
898 for (i = begin(); i != end() && which; ++i, --which);
909 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
913 vector<StreamPanner*>::clear ();
917 Panner::set_automation_style (AutoStyle style)
919 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
920 (*i)->pan_control()->list()->set_automation_style (style);
922 _session.set_dirty ();
926 Panner::set_automation_state (AutoState state)
928 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
929 (*i)->pan_control()->list()->set_automation_state (state);
931 _session.set_dirty ();
935 Panner::automation_state () const
938 return front()->pan_control()->list()->automation_state ();
945 Panner::automation_style () const
948 return front()->pan_control()->list()->automation_style ();
955 Panner::transport_stopped (nframes_t frame)
957 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
958 (*i)->pan_control()->list()->reposition_for_rt_add (frame);
963 Panner::snapshot (nframes_t now)
965 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
966 boost::shared_ptr<AutomationList> list = (*i)->pan_control()->list();
967 if (list->automation_write())
968 list->rt_add(now, (*i)->pan_control()->get_value());
973 Panner::clear_automation ()
975 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
976 (*i)->pan_control()->list()->clear ();
978 _session.set_dirty ();
984 StreamPanner* (*factory)(Panner&, Parameter);
987 PanPlugins pan_plugins[] = {
988 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
989 { Multi2dPanner::name, 3, Multi2dPanner::factory },
990 { string (""), 0, 0 }
994 Panner::get_state (void)
1000 Panner::state (bool full)
1002 XMLNode* root = new XMLNode (X_("Panner"));
1005 root->add_property (X_("linked"), (_linked ? "yes" : "no"));
1006 root->add_property (X_("link_direction"), enum_2_string (_link_direction));
1007 root->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1009 /* add each output */
1011 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1012 XMLNode* onode = new XMLNode (X_("Output"));
1013 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1014 onode->add_property (X_("x"), buf);
1015 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1016 onode->add_property (X_("y"), buf);
1017 root->add_child_nocopy (*onode);
1020 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1021 root->add_child_nocopy ((*i)->state (full));
1028 Panner::set_state (const XMLNode& node)
1031 XMLNodeConstIterator niter;
1032 const XMLProperty *prop;
1035 LocaleGuard lg (X_("POSIX"));
1040 if ((prop = node.property (X_("linked"))) != 0) {
1041 set_linked (prop->value() == "yes");
1045 if ((prop = node.property (X_("bypassed"))) != 0) {
1046 set_bypassed (prop->value() == "yes");
1049 if ((prop = node.property (X_("link_direction"))) != 0) {
1050 LinkDirection ld; /* here to provide type information */
1051 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1054 nlist = node.children();
1056 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1057 if ((*niter)->name() == X_("Output")) {
1061 prop = (*niter)->property (X_("x"));
1062 sscanf (prop->value().c_str(), "%g", &x);
1064 prop = (*niter)->property (X_("y"));
1065 sscanf (prop->value().c_str(), "%g", &y);
1067 outputs.push_back (Output (x, y));
1071 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1073 if ((*niter)->name() == X_("StreamPanner")) {
1075 if ((prop = (*niter)->property (X_("type")))) {
1077 for (i = 0; pan_plugins[i].factory; ++i) {
1078 if (prop->value() == pan_plugins[i].name) {
1081 /* note that we assume that all the stream panners
1082 are of the same type. pretty good
1083 assumption, but its still an assumption.
1086 sp = pan_plugins[i].factory (*this, Parameter(PanAutomation, 0));
1088 if (sp->set_state (**niter) == 0) {
1097 if (!pan_plugins[i].factory) {
1098 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1104 error << _("panner plugin node has no type information!")
1112 /* don't try to do old-school automation loading if it wasn't marked as existing */
1114 if ((prop = node.property (X_("automation")))) {
1116 /* automation path is relative */
1118 automation_path = _session.automation_dir();
1119 automation_path += prop->value ();
1128 Panner::touching () const
1130 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1131 if ((*i)->pan_control()->list()->touching ()) {
1140 Panner::set_position (float xpos, StreamPanner& orig)
1146 orig.get_position (xnow);
1147 xdelta = xpos - xnow;
1149 if (_link_direction == SameDirection) {
1151 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1153 (*i)->set_position (xpos, true);
1155 (*i)->get_position (xnow);
1156 xnew = min (1.0f, xnow + xdelta);
1157 xnew = max (0.0f, xnew);
1158 (*i)->set_position (xnew, true);
1164 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1166 (*i)->set_position (xpos, true);
1168 (*i)->get_position (xnow);
1169 xnew = min (1.0f, xnow - xdelta);
1170 xnew = max (0.0f, xnew);
1171 (*i)->set_position (xnew, true);
1178 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1181 float xdelta, ydelta;
1184 orig.get_position (xnow, ynow);
1185 xdelta = xpos - xnow;
1186 ydelta = ypos - ynow;
1188 if (_link_direction == SameDirection) {
1190 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1192 (*i)->set_position (xpos, ypos, true);
1194 (*i)->get_position (xnow, ynow);
1196 xnew = min (1.0f, xnow + xdelta);
1197 xnew = max (0.0f, xnew);
1199 ynew = min (1.0f, ynow + ydelta);
1200 ynew = max (0.0f, ynew);
1202 (*i)->set_position (xnew, ynew, true);
1208 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1210 (*i)->set_position (xpos, ypos, true);
1212 (*i)->get_position (xnow, ynow);
1214 xnew = min (1.0f, xnow - xdelta);
1215 xnew = max (0.0f, xnew);
1217 ynew = min (1.0f, ynow - ydelta);
1218 ynew = max (0.0f, ynew);
1220 (*i)->set_position (xnew, ynew, true);
1227 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1229 float xnow, ynow, znow;
1230 float xdelta, ydelta, zdelta;
1231 float xnew, ynew, znew;
1233 orig.get_position (xnow, ynow, znow);
1234 xdelta = xpos - xnow;
1235 ydelta = ypos - ynow;
1236 zdelta = zpos - znow;
1238 if (_link_direction == SameDirection) {
1240 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1242 (*i)->set_position (xpos, ypos, zpos, true);
1244 (*i)->get_position (xnow, ynow, znow);
1246 xnew = min (1.0f, xnow + xdelta);
1247 xnew = max (0.0f, xnew);
1249 ynew = min (1.0f, ynow + ydelta);
1250 ynew = max (0.0f, ynew);
1252 znew = min (1.0f, znow + zdelta);
1253 znew = max (0.0f, znew);
1255 (*i)->set_position (xnew, ynew, znew, true);
1261 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1263 (*i)->set_position (xpos, ypos, true);
1265 (*i)->get_position (xnow, ynow, znow);
1267 xnew = min (1.0f, xnow - xdelta);
1268 xnew = max (0.0f, xnew);
1270 ynew = min (1.0f, ynow - ydelta);
1271 ynew = max (0.0f, ynew);
1273 znew = min (1.0f, znow + zdelta);
1274 znew = max (0.0f, znew);
1276 (*i)->set_position (xnew, ynew, znew, true);
1283 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, nframes_t offset, gain_t gain_coeff)
1285 if (outbufs.count().n_audio() == 0) {
1286 // Don't want to lose audio...
1287 assert(inbufs.count().n_audio() == 0);
1291 // We shouldn't be called in the first place...
1292 assert(!bypassed());
1296 if (outbufs.count().n_audio() == 1) {
1298 AudioBuffer& dst = outbufs.get_audio(0);
1300 if (gain_coeff == 0.0f) {
1302 /* only one output, and gain was zero, so make it silent */
1304 dst.silence(offset);
1306 } else if (gain_coeff == 1.0f){
1308 /* mix all buffers into the output */
1311 dst.read_from(inbufs.get_audio(0), nframes, offset);
1313 // accumulate starting with the second
1314 BufferSet::audio_iterator i = inbufs.audio_begin();
1315 for (++i; i != inbufs.audio_end(); ++i) {
1316 dst.accumulate_from(*i, nframes, offset);
1321 /* mix all buffers into the output, scaling them all by the gain */
1324 dst.read_from(inbufs.get_audio(0), nframes, offset);
1326 // accumulate (with gain) starting with the second
1327 BufferSet::audio_iterator i = inbufs.audio_begin();
1328 for (++i; i != inbufs.audio_end(); ++i) {
1329 dst.accumulate_with_gain_from(*i, nframes, offset, gain_coeff);
1337 /* the terrible silence ... */
1338 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1339 i->silence(nframes, offset);
1342 BufferSet::audio_iterator i = inbufs.audio_begin();
1344 for (iterator pan = begin(); pan != end() && i != inbufs.audio_end(); ++pan, ++i) {
1345 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1350 Panner::distribute (BufferSet& inbufs, BufferSet& outbufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
1352 if (outbufs.count().n_audio() == 0) {
1353 // Failing to deliver audio we were asked to deliver is a bug
1354 assert(inbufs.count().n_audio() == 0);
1358 // We shouldn't be called in the first place...
1359 assert(!bypassed());
1362 // If we shouldn't play automation defer to distribute_no_automation
1363 if ( !( automation_state() & Play ||
1364 ((automation_state() & Touch) && !touching()) ) ) {
1367 gain_t gain_coeff = 1.0;
1368 if (fabsf(_session.transport_speed()) > 1.5f) {
1369 gain_coeff = speed_quietning;
1372 distribute_no_automation(inbufs, outbufs, nframes, offset, gain_coeff);
1376 // Otherwise.. let the automation flow, baby
1378 if (outbufs.count().n_audio() == 1) {
1380 AudioBuffer& dst = outbufs.get_audio(0);
1382 // FIXME: apply gain automation?
1385 dst.read_from(inbufs.get_audio(0), nframes, offset);
1387 // accumulate starting with the second
1388 BufferSet::audio_iterator i = inbufs.audio_begin();
1389 for (++i; i != inbufs.audio_end(); ++i) {
1390 dst.accumulate_from(*i, nframes, offset);
1396 // More than 1 output, we should have 1 panner for each input
1397 assert(size() == inbufs.count().n_audio());
1399 /* the terrible silence ... */
1400 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1401 i->silence(nframes, offset);
1404 BufferSet::audio_iterator i = inbufs.audio_begin();
1405 for (iterator pan = begin(); pan != end(); ++pan, ++i) {
1406 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
1410 /* old school automation handling */
1413 Panner::set_name (string str)
1415 automation_path = _session.automation_dir();
1416 automation_path += _session.snap_name();
1417 automation_path += "-pan-";
1418 automation_path += legalize_for_path (str);
1419 automation_path += ".automation";
1426 uint32_t linecnt = 0;
1429 LocaleGuard lg (X_("POSIX"));
1431 if (automation_path.length() == 0) {
1435 if (access (automation_path.c_str(), F_OK)) {
1439 ifstream in (automation_path.c_str());
1442 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1443 automation_path, strerror (errno))
1450 while (in.getline (line, sizeof(line), '\n')) {
1452 if (++linecnt == 1) {
1453 if (memcmp (line, X_("version"), 7) == 0) {
1454 if (sscanf (line, "version %f", &version) != 1) {
1455 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1459 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1460 automation_path, line) << endmsg;
1467 if (strlen (line) == 0 || line[0] == '#') {
1471 if (strcmp (line, "begin") == 0) {
1474 error << string_compose (_("too many panner states found in pan automation file %1"),
1480 if ((*sp)->load (in, automation_path, linecnt)) {