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) {
69 //static double direct_pan_to_control (pan_t val) {
73 StreamPanner::StreamPanner (Panner& p, Evoral::Parameter param)
76 assert(param.type() != NullAutomation);
80 _control = boost::dynamic_pointer_cast<AutomationControl>( parent.control( param, true ) );
87 StreamPanner::~StreamPanner ()
92 Panner::PanControllable::set_value (float val)
94 panner.streampanner(parameter().id()).set_position (direct_control_to_pan (val));
95 AutomationControl::set_value(val);
99 Panner::PanControllable::get_value (void) const
101 return AutomationControl::get_value();
105 StreamPanner::set_muted (bool yn)
114 StreamPanner::set_position (float xpos, bool link_call)
116 if (!link_call && parent.linked()) {
117 parent.set_position (xpos, *this);
124 _control->Changed ();
129 StreamPanner::set_position (float xpos, float ypos, bool link_call)
131 if (!link_call && parent.linked()) {
132 parent.set_position (xpos, ypos, *this);
135 if (x != xpos || y != ypos) {
145 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
147 if (!link_call && parent.linked()) {
148 parent.set_position (xpos, ypos, zpos, *this);
151 if (x != xpos || y != ypos || z != zpos) {
161 StreamPanner::set_state (const XMLNode& node)
163 const XMLProperty* prop;
164 XMLNodeConstIterator iter;
166 if ((prop = node.property (X_("muted")))) {
167 set_muted (prop->value() == "yes");
174 StreamPanner::add_state (XMLNode& node)
176 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
179 /*---------------------------------------------------------------------- */
181 BaseStereoPanner::BaseStereoPanner (Panner& p, Evoral::Parameter param)
182 : StreamPanner (p, param)
186 BaseStereoPanner::~BaseStereoPanner ()
191 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
194 LocaleGuard lg (X_("POSIX"));
196 _control->list()->clear ();
198 while (in.getline (line, sizeof (line), '\n')) {
204 if (strcmp (line, "end") == 0) {
208 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
209 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
213 _control->list()->fast_simple_add (when, value);
216 /* now that we are done loading */
218 ((AutomationList*)_control->list().get())->StateChanged ();
224 BaseStereoPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
226 assert(obufs.count().n_audio() == 2);
236 Sample* const src = srcbuf.data();
240 dst = obufs.get_audio(0).data();
242 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
244 /* interpolate over 64 frames or nframes, whichever is smaller */
246 nframes_t limit = min ((nframes_t)64, nframes);
249 delta = -(delta / (float) (limit));
251 for (n = 0; n < limit; n++) {
252 left_interp = left_interp + delta;
253 left = left_interp + 0.9 * (left - left_interp);
254 dst[n] += src[n] * left * gain_coeff;
257 pan = left * gain_coeff;
259 mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
266 if ((pan = (left * gain_coeff)) != 1.0f) {
270 mix_buffers_with_gain(dst,src,nframes,pan);
272 /* mark that we wrote into the buffer */
280 mix_buffers_no_gain(dst,src,nframes);
282 /* mark that we wrote into the buffer */
290 dst = obufs.get_audio(1).data();
292 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
294 /* interpolate over 64 frames or nframes, whichever is smaller */
296 nframes_t limit = min ((nframes_t)64, nframes);
299 delta = -(delta / (float) (limit));
301 for (n = 0; n < limit; n++) {
302 right_interp = right_interp + delta;
303 right = right_interp + 0.9 * (right - right_interp);
304 dst[n] += src[n] * right * gain_coeff;
307 pan = right * gain_coeff;
309 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
311 /* XXX it would be nice to mark the buffer as written to */
315 right = desired_right;
316 right_interp = right;
318 if ((pan = (right * gain_coeff)) != 1.0f) {
322 mix_buffers_with_gain(dst,src,nframes,pan);
324 /* XXX it would be nice to mark the buffer as written to */
329 mix_buffers_no_gain(dst,src,nframes);
331 /* XXX it would be nice to mark the buffer as written to */
336 /*---------------------------------------------------------------------- */
338 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p, Evoral::Parameter param)
339 : BaseStereoPanner (p, param)
344 right = desired_right;
346 right_interp = right;
349 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
354 EqualPowerStereoPanner::update ()
356 /* it would be very nice to split this out into a virtual function
357 that can be accessed from BaseStereoPanner and used in distribute_automated().
359 but the place where its used in distribute_automated() is a tight inner loop,
360 and making "nframes" virtual function calls to compute values is an absurd
364 /* x == 0 => hard left
369 float panL = 1 - panR;
371 const float pan_law_attenuation = -3.0f;
372 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
374 desired_left = panL * (scale * panL + 1.0f - scale);
375 desired_right = panR * (scale * panR + 1.0f - scale);
378 //_control->set_value(x);
382 EqualPowerStereoPanner::distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs,
383 nframes_t start, nframes_t end, nframes_t nframes,
386 assert(obufs.count().n_audio() == 2);
390 Sample* const src = srcbuf.data();
392 /* fetch positional data */
394 if (!_control->list()->curve().rt_safe_get_vector (start, end, buffers[0], nframes)) {
397 distribute (srcbuf, obufs, 1.0, nframes);
402 /* store effective pan position. do this even if we are muted */
405 effective_x = buffers[0][nframes-1];
412 /* apply pan law to convert positional data into pan coefficients for
416 const float pan_law_attenuation = -3.0f;
417 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
419 for (nframes_t n = 0; n < nframes; ++n) {
421 float panR = buffers[0][n];
422 float panL = 1 - panR;
424 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
425 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
430 dst = obufs.get_audio(0).data();
433 for (nframes_t n = 0; n < nframes; ++n) {
434 dst[n] += src[n] * pbuf[n];
437 /* XXX it would be nice to mark the buffer as written to */
441 dst = obufs.get_audio(1).data();
444 for (nframes_t n = 0; n < nframes; ++n) {
445 dst[n] += src[n] * pbuf[n];
448 /* XXX it would be nice to mark the buffer as written to */
452 EqualPowerStereoPanner::factory (Panner& parent, Evoral::Parameter param)
454 return new EqualPowerStereoPanner (parent, param);
458 EqualPowerStereoPanner::get_state (void)
464 EqualPowerStereoPanner::state (bool full_state)
466 XMLNode* root = new XMLNode ("StreamPanner");
468 LocaleGuard lg (X_("POSIX"));
470 snprintf (buf, sizeof (buf), "%.12g", x);
471 root->add_property (X_("x"), buf);
472 root->add_property (X_("type"), EqualPowerStereoPanner::name);
474 // XXX: dont save automation here... its part of the automatable panner now.
476 StreamPanner::add_state (*root);
478 root->add_child_nocopy (_control->get_state ());
484 EqualPowerStereoPanner::set_state (const XMLNode& node)
486 const XMLProperty* prop;
488 LocaleGuard lg (X_("POSIX"));
490 if ((prop = node.property (X_("x")))) {
491 pos = atof (prop->value().c_str());
492 set_position (pos, true);
495 StreamPanner::set_state (node);
497 for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
499 if ((*iter)->name() == X_("controllable")) {
500 if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
501 _control->set_state (**iter);
504 } else if ((*iter)->name() == X_("Automation")) {
506 _control->alist()->set_state (*((*iter)->children().front()));
508 if (_control->alist()->automation_state() != Off) {
509 set_position (_control->list()->eval (parent.session().transport_frame()));
517 /*----------------------------------------------------------------------*/
519 Multi2dPanner::Multi2dPanner (Panner& p, Evoral::Parameter param)
520 : StreamPanner (p, param)
525 Multi2dPanner::~Multi2dPanner ()
530 Multi2dPanner::update ()
532 static const float BIAS = FLT_MIN;
534 uint32_t nouts = parent.outputs.size();
541 for (i = 0; i < nouts; i++) {
542 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
546 f += dsq[i] * dsq[i];
549 // terrible hack to support OSX < 10.3.9 builds
550 fr = (float) (1.0 / sqrt((double)f));
554 for (i = 0; i < nouts; ++i) {
555 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
562 Multi2dPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
566 vector<Panner::Output>::iterator o;
573 Sample* const src = srcbuf.data();
576 for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
578 dst = obufs.get_audio(n).data();
581 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
583 /* interpolate over 64 frames or nframes, whichever is smaller */
585 nframes_t limit = min ((nframes_t)64, nframes);
588 delta = -(delta / (float) (limit));
590 for (n = 0; n < limit; n++) {
591 left_interp = left_interp + delta;
592 left = left_interp + 0.9 * (left - left_interp);
593 dst[n] += src[n] * left * gain_coeff;
596 pan = left * gain_coeff;
597 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
602 pan = (*o).desired_pan;
604 if ((pan *= gain_coeff) != 1.0f) {
607 mix_buffers_with_gain(dst,src,nframes,pan);
610 mix_buffers_no_gain(dst,src,nframes);
622 Multi2dPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs,
623 nframes_t start, nframes_t end, nframes_t nframes,
636 Multi2dPanner::factory (Panner& p, Evoral::Parameter param)
638 return new Multi2dPanner (p, param);
642 Multi2dPanner::load (istream& in, string path, uint32_t& linecnt)
648 Multi2dPanner::get_state (void)
654 Multi2dPanner::state (bool full_state)
656 XMLNode* root = new XMLNode ("StreamPanner");
658 LocaleGuard lg (X_("POSIX"));
660 snprintf (buf, sizeof (buf), "%.12g", x);
661 root->add_property (X_("x"), buf);
662 snprintf (buf, sizeof (buf), "%.12g", y);
663 root->add_property (X_("y"), buf);
664 root->add_property (X_("type"), Multi2dPanner::name);
666 /* XXX no meaningful automation yet */
672 Multi2dPanner::set_state (const XMLNode& node)
674 const XMLProperty* prop;
676 LocaleGuard lg (X_("POSIX"));
681 if ((prop = node.property (X_("x")))) {
682 newx = atof (prop->value().c_str());
685 if ((prop = node.property (X_("y")))) {
686 newy = atof (prop->value().c_str());
689 if (x < 0 || y < 0) {
690 error << _("badly-formed positional data for Multi2dPanner - ignored")
695 set_position (newx, newy);
699 /*---------------------------------------------------------------------- */
701 Panner::Panner (string name, Session& s)
702 : Processor(s, name, PostFader)
704 //set_name_old_auto (name);
708 _link_direction = SameDirection;
717 Panner::set_linked (bool yn)
721 _session.set_dirty ();
722 LinkStateChanged (); /* EMIT SIGNAL */
727 Panner::set_link_direction (LinkDirection ld)
729 if (ld != _link_direction) {
730 _link_direction = ld;
731 _session.set_dirty ();
732 LinkStateChanged (); /* EMIT SIGNAL */
738 Panner::set_bypassed (bool yn)
740 if (yn != _bypassed) {
748 Panner::reset (uint32_t nouts, uint32_t npans)
751 bool changed = false;
753 //configure_io( ChanCount( DataType::AUDIO, nout ), ChanCount( DataType::AUDIO, nin ) )
755 if (nouts < 2 || (nouts == outputs.size() && npans == _streampanners.size())) {
759 n = _streampanners.size();
778 fatal << _("programming error:")
779 << X_("Panner::reset() called with a single output")
786 outputs.push_back (Output (0, 0));
787 outputs.push_back (Output (1.0, 0));
789 for (n = 0; n < npans; ++n) {
790 _streampanners.push_back (new EqualPowerStereoPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
795 outputs.push_back (Output (0.5, 0));
796 outputs.push_back (Output (0, 1.0));
797 outputs.push_back (Output (1.0, 1.0));
799 for (n = 0; n < npans; ++n) {
800 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
806 outputs.push_back (Output (0, 0));
807 outputs.push_back (Output (1.0, 0));
808 outputs.push_back (Output (1.0, 1.0));
809 outputs.push_back (Output (0, 1.0));
811 for (n = 0; n < npans; ++n) {
812 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
817 case 5: //square+offcenter center
818 outputs.push_back (Output (0, 0));
819 outputs.push_back (Output (1.0, 0));
820 outputs.push_back (Output (1.0, 1.0));
821 outputs.push_back (Output (0, 1.0));
822 outputs.push_back (Output (0.5, 0.75));
824 for (n = 0; n < npans; ++n) {
825 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
831 /* XXX horrible placement. FIXME */
832 for (n = 0; n < nouts; ++n) {
833 outputs.push_back (Output (0.1 * n, 0.1 * n));
836 for (n = 0; n < npans; ++n) {
837 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
843 for (std::vector<StreamPanner*>::iterator x = _streampanners.begin(); x != _streampanners.end(); ++x) {
847 /* force hard left/right panning in a common case: 2in/2out
850 if (npans == 2 && outputs.size() == 2) {
852 /* Do this only if we changed configuration, or our configuration
853 appears to be the default set up (center).
859 _streampanners.front()->get_position (left);
860 _streampanners.back()->get_position (right);
862 if (changed || ((left == 0.5) && (right == 0.5))) {
864 _streampanners.front()->set_position (0.0);
865 _streampanners.front()->pan_control()->list()->reset_default (0.0);
867 _streampanners.back()->set_position (1.0);
868 _streampanners.back()->pan_control()->list()->reset_default (1.0);
875 Changed (); /* EMIT SIGNAL */
882 Panner::remove (uint32_t which)
884 vector<StreamPanner*>::iterator i;
885 for (i = _streampanners.begin(); i != _streampanners.end() && which; ++i, --which);
887 if (i != _streampanners.end()) {
889 _streampanners.erase (i);
894 Panner::clear_panners ()
896 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
900 _streampanners.clear ();
904 Panner::set_automation_style (AutoStyle style)
906 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
907 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_style (style);
909 _session.set_dirty ();
913 Panner::set_automation_state (AutoState state)
915 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
916 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_state (state);
918 _session.set_dirty ();
922 Panner::automation_state () const
925 return ((AutomationList*)_streampanners.front()->pan_control()->list().get())->automation_state ();
932 Panner::automation_style () const
935 return ((AutomationList*)_streampanners.front()->pan_control()->list().get())->automation_style ();
945 StreamPanner* (*factory)(Panner&, Evoral::Parameter);
948 PanPlugins pan_plugins[] = {
949 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
950 { Multi2dPanner::name, 3, Multi2dPanner::factory },
951 { string (""), 0, 0 }
955 Panner::get_state (void)
961 Panner::state (bool full)
963 XMLNode& node = Processor::state(full);
965 node.add_property ("type", "panner");
969 node.add_property (X_("linked"), (_linked ? "yes" : "no"));
970 node.add_property (X_("link_direction"), enum_2_string (_link_direction));
971 node.add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
973 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
974 XMLNode* onode = new XMLNode (X_("Output"));
975 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
976 onode->add_property (X_("x"), buf);
977 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
978 onode->add_property (X_("y"), buf);
979 node.add_child_nocopy (*onode);
982 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
983 node.add_child_nocopy ((*i)->state (full));
991 Panner::set_state (const XMLNode& node)
994 XMLNodeConstIterator niter;
995 const XMLProperty *prop;
997 uint32_t num_panners = 0;
999 LocaleGuard lg (X_("POSIX"));
1003 Processor::set_state(node);
1005 ChanCount ins = ChanCount::ZERO;
1006 ChanCount outs = ChanCount::ZERO;
1008 // XXX: this might not be necessary anymore
1011 if ((prop = node.property (X_("linked"))) != 0) {
1012 set_linked (prop->value() == "yes");
1016 if ((prop = node.property (X_("bypassed"))) != 0) {
1017 set_bypassed (prop->value() == "yes");
1020 if ((prop = node.property (X_("link_direction"))) != 0) {
1021 LinkDirection ld; /* here to provide type information */
1022 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1025 nlist = node.children();
1027 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1028 if ((*niter)->name() == X_("Output")) {
1032 prop = (*niter)->property (X_("x"));
1033 sscanf (prop->value().c_str(), "%g", &x);
1035 prop = (*niter)->property (X_("y"));
1036 sscanf (prop->value().c_str(), "%g", &y);
1038 outputs.push_back (Output (x, y));
1042 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1044 if ((*niter)->name() == X_("StreamPanner")) {
1046 if ((prop = (*niter)->property (X_("type")))) {
1048 for (i = 0; pan_plugins[i].factory; ++i) {
1049 if (prop->value() == pan_plugins[i].name) {
1052 /* note that we assume that all the stream panners
1053 are of the same type. pretty good
1054 assumption, but its still an assumption.
1057 sp = pan_plugins[i].factory (*this, Evoral::Parameter(PanAutomation, 0, num_panners));
1060 if (sp->set_state (**niter) == 0) {
1061 _streampanners.push_back (sp);
1069 if (!pan_plugins[i].factory) {
1070 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1076 error << _("panner plugin node has no type information!")
1084 reset(num_panners, outputs.size());
1085 /* don't try to do old-school automation loading if it wasn't marked as existing */
1087 if ((prop = node.property (X_("automation")))) {
1089 /* automation path is relative */
1091 automation_path = Glib::build_filename(_session.automation_dir(), prop->value ());
1098 Panner::touching () const
1100 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1101 if (((AutomationList*)(*i)->pan_control()->list().get())->touching ()) {
1110 Panner::set_position (float xpos, StreamPanner& orig)
1116 orig.get_position (xnow);
1117 xdelta = xpos - xnow;
1119 if (_link_direction == SameDirection) {
1121 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1123 (*i)->set_position (xpos, true);
1125 (*i)->get_position (xnow);
1126 xnew = min (1.0f, xnow + xdelta);
1127 xnew = max (0.0f, xnew);
1128 (*i)->set_position (xnew, true);
1134 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1136 (*i)->set_position (xpos, true);
1138 (*i)->get_position (xnow);
1139 xnew = min (1.0f, xnow - xdelta);
1140 xnew = max (0.0f, xnew);
1141 (*i)->set_position (xnew, true);
1148 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1151 float xdelta, ydelta;
1154 orig.get_position (xnow, ynow);
1155 xdelta = xpos - xnow;
1156 ydelta = ypos - ynow;
1158 if (_link_direction == SameDirection) {
1160 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1162 (*i)->set_position (xpos, ypos, true);
1164 (*i)->get_position (xnow, ynow);
1166 xnew = min (1.0f, xnow + xdelta);
1167 xnew = max (0.0f, xnew);
1169 ynew = min (1.0f, ynow + ydelta);
1170 ynew = max (0.0f, ynew);
1172 (*i)->set_position (xnew, ynew, true);
1178 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1180 (*i)->set_position (xpos, ypos, true);
1182 (*i)->get_position (xnow, ynow);
1184 xnew = min (1.0f, xnow - xdelta);
1185 xnew = max (0.0f, xnew);
1187 ynew = min (1.0f, ynow - ydelta);
1188 ynew = max (0.0f, ynew);
1190 (*i)->set_position (xnew, ynew, true);
1197 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1199 float xnow, ynow, znow;
1200 float xdelta, ydelta, zdelta;
1201 float xnew, ynew, znew;
1203 orig.get_position (xnow, ynow, znow);
1204 xdelta = xpos - xnow;
1205 ydelta = ypos - ynow;
1206 zdelta = zpos - znow;
1208 if (_link_direction == SameDirection) {
1210 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1212 (*i)->set_position (xpos, ypos, zpos, true);
1214 (*i)->get_position (xnow, ynow, znow);
1216 xnew = min (1.0f, xnow + xdelta);
1217 xnew = max (0.0f, xnew);
1219 ynew = min (1.0f, ynow + ydelta);
1220 ynew = max (0.0f, ynew);
1222 znew = min (1.0f, znow + zdelta);
1223 znew = max (0.0f, znew);
1225 (*i)->set_position (xnew, ynew, znew, true);
1231 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1233 (*i)->set_position (xpos, ypos, true);
1235 (*i)->get_position (xnow, ynow, znow);
1237 xnew = min (1.0f, xnow - xdelta);
1238 xnew = max (0.0f, xnew);
1240 ynew = min (1.0f, ynow - ydelta);
1241 ynew = max (0.0f, ynew);
1243 znew = min (1.0f, znow + zdelta);
1244 znew = max (0.0f, znew);
1246 (*i)->set_position (xnew, ynew, znew, true);
1253 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, nframes_t offset, gain_t gain_coeff)
1255 if (outbufs.count().n_audio() == 0) {
1256 // Don't want to lose audio...
1257 assert(inbufs.count().n_audio() == 0);
1261 // We shouldn't be called in the first place...
1262 assert(!bypassed());
1266 if (outbufs.count().n_audio() == 1) {
1268 AudioBuffer& dst = outbufs.get_audio(0);
1270 if (gain_coeff == 0.0f) {
1272 /* only one output, and gain was zero, so make it silent */
1274 dst.silence(offset);
1276 } else if (gain_coeff == 1.0f){
1278 /* mix all buffers into the output */
1281 dst.read_from(inbufs.get_audio(0), nframes, offset);
1283 // accumulate starting with the second
1284 if (inbufs.count().n_audio() > 0) {
1285 BufferSet::audio_iterator i = inbufs.audio_begin();
1286 for (++i; i != inbufs.audio_end(); ++i) {
1287 dst.accumulate_from(*i, nframes, offset);
1293 /* mix all buffers into the output, scaling them all by the gain */
1296 dst.read_from(inbufs.get_audio(0), nframes, offset);
1298 // accumulate (with gain) starting with the second
1299 if (inbufs.count().n_audio() > 0) {
1300 BufferSet::audio_iterator i = inbufs.audio_begin();
1301 for (++i; i != inbufs.audio_end(); ++i) {
1302 dst.accumulate_with_gain_from(*i, nframes, offset, gain_coeff);
1311 /* the terrible silence ... */
1312 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1313 i->silence(nframes, offset);
1316 BufferSet::audio_iterator i = inbufs.audio_begin();
1318 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end() && i != inbufs.audio_end(); ++pan, ++i) {
1319 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1324 Panner::run_out_of_place (BufferSet& inbufs, BufferSet& outbufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
1326 if (outbufs.count().n_audio() == 0) {
1327 // Failing to deliver audio we were asked to deliver is a bug
1328 assert(inbufs.count().n_audio() == 0);
1332 // We shouldn't be called in the first place...
1333 assert(!bypassed());
1336 // If we shouldn't play automation defer to distribute_no_automation
1337 if ( !( automation_state() & Play ||
1338 ((automation_state() & Touch) && !touching()) ) ) {
1341 gain_t gain_coeff = 1.0;
1342 if (fabsf(_session.transport_speed()) > 1.5f) {
1343 gain_coeff = speed_quietning;
1346 distribute_no_automation(inbufs, outbufs, nframes, offset, gain_coeff);
1350 // Otherwise.. let the automation flow, baby
1352 if (outbufs.count().n_audio() == 1) {
1354 AudioBuffer& dst = outbufs.get_audio(0);
1356 // FIXME: apply gain automation?
1359 dst.read_from(inbufs.get_audio(0), nframes, offset);
1361 // accumulate starting with the second
1362 BufferSet::audio_iterator i = inbufs.audio_begin();
1363 for (++i; i != inbufs.audio_end(); ++i) {
1364 dst.accumulate_from(*i, nframes, offset);
1370 // More than 1 output, we should have 1 panner for each input
1371 //assert(_streampanners.size() == inbufs.count().n_audio());
1373 /* the terrible silence ... */
1374 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1375 i->silence(nframes, offset);
1378 BufferSet::audio_iterator i = inbufs.audio_begin();
1379 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end(); ++pan, ++i) {
1380 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
1384 /* old school automation handling */
1388 Panner::set_name (string str)
1390 automation_path = Glib::build_filename(_session.automation_dir(),
1391 _session.snap_name() + "-pan-" + legalize_for_path (str) + ".automation");
1399 uint32_t linecnt = 0;
1401 vector<StreamPanner*>::iterator sp;
1402 LocaleGuard lg (X_("POSIX"));
1404 if (automation_path.length() == 0) {
1408 if (access (automation_path.c_str(), F_OK)) {
1412 ifstream in (automation_path.c_str());
1415 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1416 automation_path, strerror (errno))
1421 sp = _streampanners.begin();
1423 while (in.getline (line, sizeof(line), '\n')) {
1425 if (++linecnt == 1) {
1426 if (memcmp (line, X_("version"), 7) == 0) {
1427 if (sscanf (line, "version %f", &version) != 1) {
1428 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1432 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1433 automation_path, line) << endmsg;
1440 if (strlen (line) == 0 || line[0] == '#') {
1444 if (strcmp (line, "begin") == 0) {
1446 if (sp == _streampanners.end()) {
1447 error << string_compose (_("too many panner states found in pan automation file %1"),
1453 if ((*sp)->load (in, automation_path, linecnt)) {