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, ParamID param)
74 , _control (new PanControllable(p.session(), X_("panner"), *this, param))
78 parent.session().add_controllable (_control);
85 StreamPanner::~StreamPanner ()
90 StreamPanner::PanControllable::set_value (float val)
92 panner.set_position (direct_control_to_pan (val));
96 StreamPanner::PanControllable::get_value (void) const
99 panner.get_effective_position (xpos);
100 return direct_pan_to_control (xpos);
104 StreamPanner::PanControllable::can_send_feedback () const
106 AutoState astate = panner.get_parent().automation_state ();
108 if ((astate == Play) || (astate == Touch && !panner.get_parent().touching())) {
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) {
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) {
172 StreamPanner::set_state (const XMLNode& node)
174 const XMLProperty* prop;
175 XMLNodeConstIterator iter;
177 if ((prop = node.property (X_("muted")))) {
178 set_muted (prop->value() == "yes");
185 StreamPanner::add_state (XMLNode& node)
187 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
190 /*---------------------------------------------------------------------- */
192 BaseStereoPanner::BaseStereoPanner (Panner& p)
193 : StreamPanner (p, ParamID(PanAutomation, 0))
197 BaseStereoPanner::~BaseStereoPanner ()
202 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
205 LocaleGuard lg (X_("POSIX"));
207 _control->list()->clear ();
209 while (in.getline (line, sizeof (line), '\n')) {
215 if (strcmp (line, "end") == 0) {
219 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
220 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
224 _control->list()->fast_simple_add (when, value);
227 /* now that we are done loading */
229 _control->list()->StateChanged ();
235 BaseStereoPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
237 assert(obufs.count().n_audio() == 2);
247 Sample* const src = srcbuf.data();
251 dst = obufs.get_audio(0).data();
253 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
255 /* interpolate over 64 frames or nframes, whichever is smaller */
257 nframes_t limit = min ((nframes_t)64, nframes);
260 delta = -(delta / (float) (limit));
262 for (n = 0; n < limit; n++) {
263 left_interp = left_interp + delta;
264 left = left_interp + 0.9 * (left - left_interp);
265 dst[n] += src[n] * left * gain_coeff;
268 pan = left * gain_coeff;
270 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
277 if ((pan = (left * gain_coeff)) != 1.0f) {
281 mix_buffers_with_gain(dst,src,nframes,pan);
283 /* mark that we wrote into the buffer */
291 mix_buffers_no_gain(dst,src,nframes);
293 /* mark that we wrote into the buffer */
301 dst = obufs.get_audio(1).data();
303 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
305 /* interpolate over 64 frames or nframes, whichever is smaller */
307 nframes_t limit = min ((nframes_t)64, nframes);
310 delta = -(delta / (float) (limit));
312 for (n = 0; n < limit; n++) {
313 right_interp = right_interp + delta;
314 right = right_interp + 0.9 * (right - right_interp);
315 dst[n] += src[n] * right * gain_coeff;
318 pan = right * gain_coeff;
320 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
322 /* XXX it would be nice to mark the buffer as written to */
326 right = desired_right;
327 right_interp = right;
329 if ((pan = (right * gain_coeff)) != 1.0f) {
333 mix_buffers_with_gain(dst,src,nframes,pan);
335 /* XXX it would be nice to mark the buffer as written to */
340 mix_buffers_no_gain(dst,src,nframes);
342 /* XXX it would be nice to mark the buffer as written to */
347 /*---------------------------------------------------------------------- */
349 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p)
350 : BaseStereoPanner (p)
355 right = desired_right;
357 right_interp = right;
360 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
365 EqualPowerStereoPanner::update ()
367 /* it would be very nice to split this out into a virtual function
368 that can be accessed from BaseStereoPanner and used in distribute_automated().
370 but the place where its used in distribute_automated() is a tight inner loop,
371 and making "nframes" virtual function calls to compute values is an absurd
375 /* x == 0 => hard left
380 float panL = 1 - panR;
382 const float pan_law_attenuation = -3.0f;
383 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
385 desired_left = panL * (scale * panL + 1.0f - scale);
386 desired_right = panR * (scale * panR + 1.0f - scale);
389 _control->set_value(x);
393 EqualPowerStereoPanner::distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs,
394 nframes_t start, nframes_t end, nframes_t nframes,
397 assert(obufs.count().n_audio() == 2);
401 Sample* const src = srcbuf.data();
403 /* fetch positional data */
405 if (!_control->list()->curve().rt_safe_get_vector (start, end, buffers[0], nframes)) {
408 distribute (srcbuf, obufs, 1.0, nframes);
413 /* store effective pan position. do this even if we are muted */
416 effective_x = buffers[0][nframes-1];
417 _control->set_value(effective_x); // signal, update UI
424 /* apply pan law to convert positional data into pan coefficients for
428 const float pan_law_attenuation = -3.0f;
429 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
431 for (nframes_t n = 0; n < nframes; ++n) {
433 float panR = buffers[0][n];
434 float panL = 1 - panR;
436 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
437 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
442 dst = obufs.get_audio(0).data();
445 for (nframes_t n = 0; n < nframes; ++n) {
446 dst[n] += src[n] * pbuf[n];
449 /* XXX it would be nice to mark the buffer as written to */
453 dst = obufs.get_audio(1).data();
456 for (nframes_t n = 0; n < nframes; ++n) {
457 dst[n] += src[n] * pbuf[n];
460 /* XXX it would be nice to mark the buffer as written to */
464 EqualPowerStereoPanner::factory (Panner& parent, ParamID who_cares)
466 return new EqualPowerStereoPanner (parent);
470 EqualPowerStereoPanner::get_state (void)
476 EqualPowerStereoPanner::state (bool full_state)
478 XMLNode* root = new XMLNode ("StreamPanner");
480 LocaleGuard lg (X_("POSIX"));
482 snprintf (buf, sizeof (buf), "%.12g", x);
483 root->add_property (X_("x"), buf);
484 root->add_property (X_("type"), EqualPowerStereoPanner::name);
486 XMLNode* autonode = new XMLNode (X_("Automation"));
487 autonode->add_child_nocopy (_control->list()->state (full_state));
488 root->add_child_nocopy (*autonode);
490 StreamPanner::add_state (*root);
492 root->add_child_nocopy (_control->get_state ());
498 EqualPowerStereoPanner::set_state (const XMLNode& node)
500 const XMLProperty* prop;
502 LocaleGuard lg (X_("POSIX"));
504 if ((prop = node.property (X_("x")))) {
505 pos = atof (prop->value().c_str());
506 set_position (pos, true);
509 StreamPanner::set_state (node);
511 for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
513 if ((*iter)->name() == X_("controllable")) {
514 if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
515 _control->set_state (**iter);
518 } else if ((*iter)->name() == X_("Automation")) {
520 _control->list()->set_state (*((*iter)->children().front()));
522 if (_control->list()->automation_state() != Off) {
523 set_position (_control->list()->eval (parent.session().transport_frame()));
531 /*----------------------------------------------------------------------*/
533 Multi2dPanner::Multi2dPanner (Panner& p, ParamID param)
534 : StreamPanner (p, param)
539 Multi2dPanner::~Multi2dPanner ()
544 Multi2dPanner::update ()
546 static const float BIAS = FLT_MIN;
548 uint32_t nouts = parent.outputs.size();
555 for (i = 0; i < nouts; i++) {
556 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
560 f += dsq[i] * dsq[i];
563 // terrible hack to support OSX < 10.3.9 builds
564 fr = (float) (1.0 / sqrt((double)f));
568 for (i = 0; i < nouts; ++i) {
569 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
573 _control->set_value(x);
577 Multi2dPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
581 vector<Panner::Output>::iterator o;
588 Sample* const src = srcbuf.data();
591 for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
593 dst = obufs.get_audio(n).data();
596 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
598 /* interpolate over 64 frames or nframes, whichever is smaller */
600 nframes_t limit = min ((nframes_t)64, nframes);
603 delta = -(delta / (float) (limit));
605 for (n = 0; n < limit; n++) {
606 left_interp = left_interp + delta;
607 left = left_interp + 0.9 * (left - left_interp);
608 dst[n] += src[n] * left * gain_coeff;
611 pan = left * gain_coeff;
612 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
617 pan = (*o).desired_pan;
619 if ((pan *= gain_coeff) != 1.0f) {
622 mix_buffers_with_gain(dst,src,nframes,pan);
625 mix_buffers_no_gain(dst,src,nframes);
637 Multi2dPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs,
638 nframes_t start, nframes_t end, nframes_t nframes,
651 Multi2dPanner::factory (Panner& p, ParamID param)
653 return new Multi2dPanner (p, param);
657 Multi2dPanner::load (istream& in, string path, uint32_t& linecnt)
663 Multi2dPanner::get_state (void)
669 Multi2dPanner::state (bool full_state)
671 XMLNode* root = new XMLNode ("StreamPanner");
673 LocaleGuard lg (X_("POSIX"));
675 snprintf (buf, sizeof (buf), "%.12g", x);
676 root->add_property (X_("x"), buf);
677 snprintf (buf, sizeof (buf), "%.12g", y);
678 root->add_property (X_("y"), buf);
679 root->add_property (X_("type"), Multi2dPanner::name);
681 /* XXX no meaningful automation yet */
687 Multi2dPanner::set_state (const XMLNode& node)
689 const XMLProperty* prop;
691 LocaleGuard lg (X_("POSIX"));
696 if ((prop = node.property (X_("x")))) {
697 newx = atof (prop->value().c_str());
700 if ((prop = node.property (X_("y")))) {
701 newy = atof (prop->value().c_str());
704 if (x < 0 || y < 0) {
705 error << _("badly-formed positional data for Multi2dPanner - ignored")
710 set_position (newx, newy);
714 /*---------------------------------------------------------------------- */
716 Panner::Panner (string name, Session& s)
722 _link_direction = SameDirection;
731 Panner::set_linked (bool yn)
735 _session.set_dirty ();
736 LinkStateChanged (); /* EMIT SIGNAL */
741 Panner::set_link_direction (LinkDirection ld)
743 if (ld != _link_direction) {
744 _link_direction = ld;
745 _session.set_dirty ();
746 LinkStateChanged (); /* EMIT SIGNAL */
751 Panner::set_bypassed (bool yn)
753 if (yn != _bypassed) {
761 Panner::reset (uint32_t nouts, uint32_t npans)
764 bool changed = false;
766 if (nouts < 2 || (nouts == outputs.size() && npans == size())) {
789 fatal << _("programming error:")
790 << X_("Panner::reset() called with a single output")
797 outputs.push_back (Output (0, 0));
798 outputs.push_back (Output (1.0, 0));
800 for (n = 0; n < npans; ++n) {
801 push_back (new EqualPowerStereoPanner (*this));
806 outputs.push_back (Output (0.5, 0));
807 outputs.push_back (Output (0, 1.0));
808 outputs.push_back (Output (1.0, 1.0));
810 for (n = 0; n < npans; ++n) {
811 push_back (new Multi2dPanner (*this, ParamID(PanAutomation, n)));
817 outputs.push_back (Output (0, 0));
818 outputs.push_back (Output (1.0, 0));
819 outputs.push_back (Output (1.0, 1.0));
820 outputs.push_back (Output (0, 1.0));
822 for (n = 0; n < npans; ++n) {
823 push_back (new Multi2dPanner (*this, ParamID(PanAutomation, n)));
828 case 5: //square+offcenter center
829 outputs.push_back (Output (0, 0));
830 outputs.push_back (Output (1.0, 0));
831 outputs.push_back (Output (1.0, 1.0));
832 outputs.push_back (Output (0, 1.0));
833 outputs.push_back (Output (0.5, 0.75));
835 for (n = 0; n < npans; ++n) {
836 push_back (new Multi2dPanner (*this, ParamID(PanAutomation, n)));
842 /* XXX horrible placement. FIXME */
843 for (n = 0; n < nouts; ++n) {
844 outputs.push_back (Output (0.1 * n, 0.1 * n));
847 for (n = 0; n < npans; ++n) {
848 push_back (new Multi2dPanner (*this, ParamID(PanAutomation, n)));
854 for (iterator x = begin(); x != end(); ++x) {
858 /* force hard left/right panning in a common case: 2in/2out
861 if (npans == 2 && outputs.size() == 2) {
863 /* Do this only if we changed configuration, or our configuration
864 appears to be the default set up (center).
870 front()->get_position (left);
871 back()->get_position (right);
873 if (changed || ((left == 0.5) && (right == 0.5))) {
875 front()->set_position (0.0);
876 front()->pan_control()->list()->reset_default (0.0);
878 back()->set_position (1.0);
879 back()->pan_control()->list()->reset_default (1.0);
886 Changed (); /* EMIT SIGNAL */
893 Panner::remove (uint32_t which)
895 vector<StreamPanner*>::iterator i;
896 for (i = begin(); i != end() && which; ++i, --which);
907 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
911 vector<StreamPanner*>::clear ();
915 Panner::set_automation_style (AutoStyle style)
917 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
918 (*i)->pan_control()->list()->set_automation_style (style);
920 _session.set_dirty ();
924 Panner::set_automation_state (AutoState state)
926 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
927 (*i)->pan_control()->list()->set_automation_state (state);
929 _session.set_dirty ();
933 Panner::automation_state () const
936 return front()->pan_control()->list()->automation_state ();
943 Panner::automation_style () const
946 return front()->pan_control()->list()->automation_style ();
953 Panner::transport_stopped (nframes_t frame)
955 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
956 (*i)->pan_control()->list()->reposition_for_rt_add (frame);
961 Panner::snapshot (nframes_t now)
963 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
964 boost::shared_ptr<AutomationList> list = (*i)->pan_control()->list();
965 if (list->automation_write())
966 list->rt_add(now, (*i)->pan_control()->get_value());
971 Panner::clear_automation ()
973 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
974 (*i)->pan_control()->list()->clear ();
976 _session.set_dirty ();
982 StreamPanner* (*factory)(Panner&, ParamID);
985 PanPlugins pan_plugins[] = {
986 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
987 { Multi2dPanner::name, 3, Multi2dPanner::factory },
988 { string (""), 0, 0 }
992 Panner::get_state (void)
998 Panner::state (bool full)
1000 XMLNode* root = new XMLNode (X_("Panner"));
1003 root->add_property (X_("linked"), (_linked ? "yes" : "no"));
1004 root->add_property (X_("link_direction"), enum_2_string (_link_direction));
1005 root->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1007 /* add each output */
1009 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1010 XMLNode* onode = new XMLNode (X_("Output"));
1011 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1012 onode->add_property (X_("x"), buf);
1013 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1014 onode->add_property (X_("y"), buf);
1015 root->add_child_nocopy (*onode);
1018 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1019 root->add_child_nocopy ((*i)->state (full));
1026 Panner::set_state (const XMLNode& node)
1029 XMLNodeConstIterator niter;
1030 const XMLProperty *prop;
1033 LocaleGuard lg (X_("POSIX"));
1038 if ((prop = node.property (X_("linked"))) != 0) {
1039 set_linked (prop->value() == "yes");
1043 if ((prop = node.property (X_("bypassed"))) != 0) {
1044 set_bypassed (prop->value() == "yes");
1047 if ((prop = node.property (X_("link_direction"))) != 0) {
1048 LinkDirection ld; /* here to provide type information */
1049 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1052 nlist = node.children();
1054 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1055 if ((*niter)->name() == X_("Output")) {
1059 prop = (*niter)->property (X_("x"));
1060 sscanf (prop->value().c_str(), "%g", &x);
1062 prop = (*niter)->property (X_("y"));
1063 sscanf (prop->value().c_str(), "%g", &y);
1065 outputs.push_back (Output (x, y));
1069 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1071 if ((*niter)->name() == X_("StreamPanner")) {
1073 if ((prop = (*niter)->property (X_("type")))) {
1075 for (i = 0; pan_plugins[i].factory; ++i) {
1076 if (prop->value() == pan_plugins[i].name) {
1079 /* note that we assume that all the stream panners
1080 are of the same type. pretty good
1081 assumption, but its still an assumption.
1084 sp = pan_plugins[i].factory (*this, ParamID(PanAutomation, 0));
1086 if (sp->set_state (**niter) == 0) {
1095 if (!pan_plugins[i].factory) {
1096 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1102 error << _("panner plugin node has no type information!")
1110 /* don't try to do old-school automation loading if it wasn't marked as existing */
1112 if ((prop = node.property (X_("automation")))) {
1114 /* automation path is relative */
1116 automation_path = _session.automation_dir();
1117 automation_path += prop->value ();
1126 Panner::touching () const
1128 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1129 if ((*i)->pan_control()->list()->touching ()) {
1138 Panner::set_position (float xpos, StreamPanner& orig)
1144 orig.get_position (xnow);
1145 xdelta = xpos - xnow;
1147 if (_link_direction == SameDirection) {
1149 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1151 (*i)->set_position (xpos, true);
1153 (*i)->get_position (xnow);
1154 xnew = min (1.0f, xnow + xdelta);
1155 xnew = max (0.0f, xnew);
1156 (*i)->set_position (xnew, true);
1162 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1164 (*i)->set_position (xpos, true);
1166 (*i)->get_position (xnow);
1167 xnew = min (1.0f, xnow - xdelta);
1168 xnew = max (0.0f, xnew);
1169 (*i)->set_position (xnew, true);
1176 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1179 float xdelta, ydelta;
1182 orig.get_position (xnow, ynow);
1183 xdelta = xpos - xnow;
1184 ydelta = ypos - ynow;
1186 if (_link_direction == SameDirection) {
1188 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1190 (*i)->set_position (xpos, ypos, true);
1192 (*i)->get_position (xnow, ynow);
1194 xnew = min (1.0f, xnow + xdelta);
1195 xnew = max (0.0f, xnew);
1197 ynew = min (1.0f, ynow + ydelta);
1198 ynew = max (0.0f, ynew);
1200 (*i)->set_position (xnew, ynew, true);
1206 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1208 (*i)->set_position (xpos, ypos, true);
1210 (*i)->get_position (xnow, ynow);
1212 xnew = min (1.0f, xnow - xdelta);
1213 xnew = max (0.0f, xnew);
1215 ynew = min (1.0f, ynow - ydelta);
1216 ynew = max (0.0f, ynew);
1218 (*i)->set_position (xnew, ynew, true);
1225 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1227 float xnow, ynow, znow;
1228 float xdelta, ydelta, zdelta;
1229 float xnew, ynew, znew;
1231 orig.get_position (xnow, ynow, znow);
1232 xdelta = xpos - xnow;
1233 ydelta = ypos - ynow;
1234 zdelta = zpos - znow;
1236 if (_link_direction == SameDirection) {
1238 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1240 (*i)->set_position (xpos, ypos, zpos, true);
1242 (*i)->get_position (xnow, ynow, znow);
1244 xnew = min (1.0f, xnow + xdelta);
1245 xnew = max (0.0f, xnew);
1247 ynew = min (1.0f, ynow + ydelta);
1248 ynew = max (0.0f, ynew);
1250 znew = min (1.0f, znow + zdelta);
1251 znew = max (0.0f, znew);
1253 (*i)->set_position (xnew, ynew, znew, true);
1259 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1261 (*i)->set_position (xpos, ypos, true);
1263 (*i)->get_position (xnow, ynow, znow);
1265 xnew = min (1.0f, xnow - xdelta);
1266 xnew = max (0.0f, xnew);
1268 ynew = min (1.0f, ynow - ydelta);
1269 ynew = max (0.0f, ynew);
1271 znew = min (1.0f, znow + zdelta);
1272 znew = max (0.0f, znew);
1274 (*i)->set_position (xnew, ynew, znew, true);
1281 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, nframes_t offset, gain_t gain_coeff)
1283 if (outbufs.count().n_audio() == 0) {
1284 // Don't want to lose audio...
1285 assert(inbufs.count().n_audio() == 0);
1289 // We shouldn't be called in the first place...
1290 assert(!bypassed());
1294 if (outbufs.count().n_audio() == 1) {
1296 AudioBuffer& dst = outbufs.get_audio(0);
1298 if (gain_coeff == 0.0f) {
1300 /* only one output, and gain was zero, so make it silent */
1302 dst.silence(offset);
1304 } else if (gain_coeff == 1.0f){
1306 /* mix all buffers into the output */
1309 dst.read_from(inbufs.get_audio(0), nframes, offset);
1311 // accumulate starting with the second
1312 BufferSet::audio_iterator i = inbufs.audio_begin();
1313 for (++i; i != inbufs.audio_end(); ++i) {
1314 dst.accumulate_from(*i, nframes, offset);
1319 /* mix all buffers into the output, scaling them all by the gain */
1322 dst.read_from(inbufs.get_audio(0), nframes, offset);
1324 // accumulate (with gain) starting with the second
1325 BufferSet::audio_iterator i = inbufs.audio_begin();
1326 for (++i; i != inbufs.audio_end(); ++i) {
1327 dst.accumulate_with_gain_from(*i, nframes, offset, gain_coeff);
1335 /* the terrible silence ... */
1336 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1337 i->silence(nframes, offset);
1340 BufferSet::audio_iterator i = inbufs.audio_begin();
1342 for (iterator pan = begin(); pan != end() && i != inbufs.audio_end(); ++pan, ++i) {
1343 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1348 Panner::distribute (BufferSet& inbufs, BufferSet& outbufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
1350 if (outbufs.count().n_audio() == 0) {
1351 // Failing to deliver audio we were asked to deliver is a bug
1352 assert(inbufs.count().n_audio() == 0);
1356 // We shouldn't be called in the first place...
1357 assert(!bypassed());
1360 // If we shouldn't play automation defer to distribute_no_automation
1361 if ( !( automation_state() & Play ||
1362 ((automation_state() & Touch) && !touching()) ) ) {
1365 gain_t gain_coeff = 1.0;
1366 if (fabsf(_session.transport_speed()) > 1.5f) {
1367 gain_coeff = speed_quietning;
1370 distribute_no_automation(inbufs, outbufs, nframes, offset, gain_coeff);
1374 // Otherwise.. let the automation flow, baby
1376 if (outbufs.count().n_audio() == 1) {
1378 AudioBuffer& dst = outbufs.get_audio(0);
1380 // FIXME: apply gain automation?
1383 dst.read_from(inbufs.get_audio(0), nframes, offset);
1385 // accumulate starting with the second
1386 BufferSet::audio_iterator i = inbufs.audio_begin();
1387 for (++i; i != inbufs.audio_end(); ++i) {
1388 dst.accumulate_from(*i, nframes, offset);
1394 // More than 1 output, we should have 1 panner for each input
1395 assert(size() == inbufs.count().n_audio());
1397 /* the terrible silence ... */
1398 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1399 i->silence(nframes, offset);
1402 BufferSet::audio_iterator i = inbufs.audio_begin();
1403 for (iterator pan = begin(); pan != end(); ++pan, ++i) {
1404 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
1408 /* old school automation handling */
1411 Panner::set_name (string str)
1413 automation_path = _session.automation_dir();
1414 automation_path += _session.snap_name();
1415 automation_path += "-pan-";
1416 automation_path += legalize_for_path (str);
1417 automation_path += ".automation";
1424 uint32_t linecnt = 0;
1427 LocaleGuard lg (X_("POSIX"));
1429 if (automation_path.length() == 0) {
1433 if (access (automation_path.c_str(), F_OK)) {
1437 ifstream in (automation_path.c_str());
1440 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1441 automation_path, strerror (errno))
1448 while (in.getline (line, sizeof(line), '\n')) {
1450 if (++linecnt == 1) {
1451 if (memcmp (line, X_("version"), 7) == 0) {
1452 if (sscanf (line, "version %f", &version) != 1) {
1453 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1457 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1458 automation_path, line) << endmsg;
1465 if (strlen (line) == 0 || line[0] == '#') {
1469 if (strcmp (line, "begin") == 0) {
1472 error << string_compose (_("too many panner states found in pan automation file %1"),
1478 if ((*sp)->load (in, automation_path, linecnt)) {