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>
43 #include <ardour/audio_buffer.h>
45 #include <ardour/runtime_functions.h>
46 #include <ardour/buffer_set.h>
47 #include <ardour/audio_buffer.h>
51 #include <pbd/mathfix.h>
54 using namespace ARDOUR;
57 float Panner::current_automation_version_number = 1.0;
59 string EqualPowerStereoPanner::name = "Equal Power Stereo";
60 string Multi2dPanner::name = "Multiple (2D)";
62 /* this is a default mapper of control values to a pan position
63 others can be imagined.
66 static pan_t direct_control_to_pan (double fract) {
71 //static double direct_pan_to_control (pan_t val) {
75 StreamPanner::StreamPanner (Panner& p, Evoral::Parameter param)
78 assert(param.type() != NullAutomation);
82 _control = boost::dynamic_pointer_cast<AutomationControl>( parent.control( param, true ) );
89 StreamPanner::~StreamPanner ()
94 Panner::PanControllable::set_value (float val)
96 panner.streampanner(parameter().id()).set_position (direct_control_to_pan (val));
97 AutomationControl::set_value(val);
101 Panner::PanControllable::get_value (void) const
103 return AutomationControl::get_value();
107 StreamPanner::set_muted (bool yn)
116 StreamPanner::set_position (float xpos, bool link_call)
118 if (!link_call && parent.linked()) {
119 parent.set_position (xpos, *this);
126 _control->Changed ();
131 StreamPanner::set_position (float xpos, float ypos, bool link_call)
133 if (!link_call && parent.linked()) {
134 parent.set_position (xpos, ypos, *this);
137 if (x != xpos || y != ypos) {
147 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
149 if (!link_call && parent.linked()) {
150 parent.set_position (xpos, ypos, zpos, *this);
153 if (x != xpos || y != ypos || z != zpos) {
163 StreamPanner::set_state (const XMLNode& node)
165 const XMLProperty* prop;
166 XMLNodeConstIterator iter;
168 if ((prop = node.property (X_("muted")))) {
169 set_muted (prop->value() == "yes");
176 StreamPanner::add_state (XMLNode& node)
178 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
181 /*---------------------------------------------------------------------- */
183 BaseStereoPanner::BaseStereoPanner (Panner& p, Evoral::Parameter param)
184 : StreamPanner (p, param)
188 BaseStereoPanner::~BaseStereoPanner ()
193 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
196 LocaleGuard lg (X_("POSIX"));
198 _control->list()->clear ();
200 while (in.getline (line, sizeof (line), '\n')) {
206 if (strcmp (line, "end") == 0) {
210 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
211 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
215 _control->list()->fast_simple_add (when, value);
218 /* now that we are done loading */
220 ((AutomationList*)_control->list().get())->StateChanged ();
226 BaseStereoPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
228 assert(obufs.count().n_audio() == 2);
238 Sample* const src = srcbuf.data();
242 dst = obufs.get_audio(0).data();
244 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
246 /* interpolate over 64 frames or nframes, whichever is smaller */
248 nframes_t limit = min ((nframes_t)64, nframes);
251 delta = -(delta / (float) (limit));
253 for (n = 0; n < limit; n++) {
254 left_interp = left_interp + delta;
255 left = left_interp + 0.9 * (left - left_interp);
256 dst[n] += src[n] * left * gain_coeff;
259 pan = left * gain_coeff;
261 mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
268 if ((pan = (left * gain_coeff)) != 1.0f) {
272 mix_buffers_with_gain(dst,src,nframes,pan);
274 /* mark that we wrote into the buffer */
282 mix_buffers_no_gain(dst,src,nframes);
284 /* mark that we wrote into the buffer */
292 dst = obufs.get_audio(1).data();
294 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
296 /* interpolate over 64 frames or nframes, whichever is smaller */
298 nframes_t limit = min ((nframes_t)64, nframes);
301 delta = -(delta / (float) (limit));
303 for (n = 0; n < limit; n++) {
304 right_interp = right_interp + delta;
305 right = right_interp + 0.9 * (right - right_interp);
306 dst[n] += src[n] * right * gain_coeff;
309 pan = right * gain_coeff;
311 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
313 /* XXX it would be nice to mark the buffer as written to */
317 right = desired_right;
318 right_interp = right;
320 if ((pan = (right * gain_coeff)) != 1.0f) {
324 mix_buffers_with_gain(dst,src,nframes,pan);
326 /* XXX it would be nice to mark the buffer as written to */
331 mix_buffers_no_gain(dst,src,nframes);
333 /* XXX it would be nice to mark the buffer as written to */
338 /*---------------------------------------------------------------------- */
340 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p, Evoral::Parameter param)
341 : BaseStereoPanner (p, param)
346 right = desired_right;
348 right_interp = right;
351 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
356 EqualPowerStereoPanner::update ()
358 /* it would be very nice to split this out into a virtual function
359 that can be accessed from BaseStereoPanner and used in distribute_automated().
361 but the place where its used in distribute_automated() is a tight inner loop,
362 and making "nframes" virtual function calls to compute values is an absurd
366 /* x == 0 => hard left
371 float panL = 1 - panR;
373 const float pan_law_attenuation = -3.0f;
374 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
376 desired_left = panL * (scale * panL + 1.0f - scale);
377 desired_right = panR * (scale * panR + 1.0f - scale);
380 //_control->set_value(x);
384 EqualPowerStereoPanner::distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs,
385 nframes_t start, nframes_t end, nframes_t nframes,
388 assert(obufs.count().n_audio() == 2);
392 Sample* const src = srcbuf.data();
394 /* fetch positional data */
396 if (!_control->list()->curve().rt_safe_get_vector (start, end, buffers[0], nframes)) {
399 distribute (srcbuf, obufs, 1.0, nframes);
404 /* store effective pan position. do this even if we are muted */
407 effective_x = buffers[0][nframes-1];
414 /* apply pan law to convert positional data into pan coefficients for
418 const float pan_law_attenuation = -3.0f;
419 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
421 for (nframes_t n = 0; n < nframes; ++n) {
423 float panR = buffers[0][n];
424 float panL = 1 - panR;
426 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
427 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
432 dst = obufs.get_audio(0).data();
435 for (nframes_t n = 0; n < nframes; ++n) {
436 dst[n] += src[n] * pbuf[n];
439 /* XXX it would be nice to mark the buffer as written to */
443 dst = obufs.get_audio(1).data();
446 for (nframes_t n = 0; n < nframes; ++n) {
447 dst[n] += src[n] * pbuf[n];
450 /* XXX it would be nice to mark the buffer as written to */
454 EqualPowerStereoPanner::factory (Panner& parent, Evoral::Parameter param)
456 return new EqualPowerStereoPanner (parent, param);
460 EqualPowerStereoPanner::get_state (void)
466 EqualPowerStereoPanner::state (bool full_state)
468 XMLNode* root = new XMLNode ("StreamPanner");
470 LocaleGuard lg (X_("POSIX"));
472 snprintf (buf, sizeof (buf), "%.12g", x);
473 root->add_property (X_("x"), buf);
474 root->add_property (X_("type"), EqualPowerStereoPanner::name);
476 // XXX: dont save automation here... its part of the automatable panner now.
478 StreamPanner::add_state (*root);
480 root->add_child_nocopy (_control->get_state ());
486 EqualPowerStereoPanner::set_state (const XMLNode& node)
488 const XMLProperty* prop;
490 LocaleGuard lg (X_("POSIX"));
492 if ((prop = node.property (X_("x")))) {
493 pos = atof (prop->value().c_str());
494 set_position (pos, true);
497 StreamPanner::set_state (node);
499 for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
501 if ((*iter)->name() == X_("Controllable")) {
502 if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
503 _control->set_state (**iter);
506 } else if ((*iter)->name() == X_("Automation")) {
508 _control->alist()->set_state (*((*iter)->children().front()));
510 if (_control->alist()->automation_state() != Off) {
511 set_position (_control->list()->eval (parent.session().transport_frame()));
519 /*----------------------------------------------------------------------*/
521 Multi2dPanner::Multi2dPanner (Panner& p, Evoral::Parameter param)
522 : StreamPanner (p, param)
527 Multi2dPanner::~Multi2dPanner ()
532 Multi2dPanner::update ()
534 static const float BIAS = FLT_MIN;
536 uint32_t nouts = parent.outputs.size();
543 for (i = 0; i < nouts; i++) {
544 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
548 f += dsq[i] * dsq[i];
551 // terrible hack to support OSX < 10.3.9 builds
552 fr = (float) (1.0 / sqrt((double)f));
556 for (i = 0; i < nouts; ++i) {
557 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
564 Multi2dPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
568 vector<Panner::Output>::iterator o;
575 Sample* const src = srcbuf.data();
578 for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
580 dst = obufs.get_audio(n).data();
583 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
585 /* interpolate over 64 frames or nframes, whichever is smaller */
587 nframes_t limit = min ((nframes_t)64, nframes);
590 delta = -(delta / (float) (limit));
592 for (n = 0; n < limit; n++) {
593 left_interp = left_interp + delta;
594 left = left_interp + 0.9 * (left - left_interp);
595 dst[n] += src[n] * left * gain_coeff;
598 pan = left * gain_coeff;
599 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
604 pan = (*o).desired_pan;
606 if ((pan *= gain_coeff) != 1.0f) {
609 mix_buffers_with_gain(dst,src,nframes,pan);
612 mix_buffers_no_gain(dst,src,nframes);
624 Multi2dPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs,
625 nframes_t start, nframes_t end, nframes_t nframes,
638 Multi2dPanner::factory (Panner& p, Evoral::Parameter param)
640 return new Multi2dPanner (p, param);
644 Multi2dPanner::load (istream& in, string path, uint32_t& linecnt)
650 Multi2dPanner::get_state (void)
656 Multi2dPanner::state (bool full_state)
658 XMLNode* root = new XMLNode ("StreamPanner");
660 LocaleGuard lg (X_("POSIX"));
662 snprintf (buf, sizeof (buf), "%.12g", x);
663 root->add_property (X_("x"), buf);
664 snprintf (buf, sizeof (buf), "%.12g", y);
665 root->add_property (X_("y"), buf);
666 root->add_property (X_("type"), Multi2dPanner::name);
668 /* XXX no meaningful automation yet */
674 Multi2dPanner::set_state (const XMLNode& node)
676 const XMLProperty* prop;
678 LocaleGuard lg (X_("POSIX"));
683 if ((prop = node.property (X_("x")))) {
684 newx = atof (prop->value().c_str());
687 if ((prop = node.property (X_("y")))) {
688 newy = atof (prop->value().c_str());
691 if (x < 0 || y < 0) {
692 error << _("badly-formed positional data for Multi2dPanner - ignored")
697 set_position (newx, newy);
701 /*---------------------------------------------------------------------- */
703 Panner::Panner (string name, Session& s)
704 : Processor(s, name, PostFader)
706 //set_name_old_auto (name);
710 _link_direction = SameDirection;
719 Panner::set_linked (bool yn)
723 _session.set_dirty ();
724 LinkStateChanged (); /* EMIT SIGNAL */
729 Panner::set_link_direction (LinkDirection ld)
731 if (ld != _link_direction) {
732 _link_direction = ld;
733 _session.set_dirty ();
734 LinkStateChanged (); /* EMIT SIGNAL */
740 Panner::set_bypassed (bool yn)
742 if (yn != _bypassed) {
750 Panner::reset (uint32_t nouts, uint32_t npans)
753 bool changed = false;
754 bool do_not_and_did_not_need_panning = ((nouts < 2) && (outputs.size() < 2));
756 /* if new and old config don't need panning, or if
757 the config hasn't changed, we're done.
760 if (do_not_and_did_not_need_panning ||
761 ((nouts == outputs.size()) && (npans == _streampanners.size()))) {
765 n = _streampanners.size();
780 /* no need for panning with less than 2 outputs */
786 /* XXX: this can never happen */
790 /* XXX: this can never happen */
791 fatal << _("programming error:")
792 << X_("Panner::reset() called with a single output")
798 outputs.push_back (Output (0, 0));
799 outputs.push_back (Output (1.0, 0));
801 for (n = 0; n < npans; ++n) {
802 _streampanners.push_back (new EqualPowerStereoPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
807 outputs.push_back (Output (0.5, 0));
808 outputs.push_back (Output (0, 1.0));
809 outputs.push_back (Output (1.0, 1.0));
811 for (n = 0; n < npans; ++n) {
812 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
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));
823 for (n = 0; n < npans; ++n) {
824 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
829 case 5: //square+offcenter center
830 outputs.push_back (Output (0, 0));
831 outputs.push_back (Output (1.0, 0));
832 outputs.push_back (Output (1.0, 1.0));
833 outputs.push_back (Output (0, 1.0));
834 outputs.push_back (Output (0.5, 0.75));
836 for (n = 0; n < npans; ++n) {
837 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
843 /* XXX horrible placement. FIXME */
844 for (n = 0; n < nouts; ++n) {
845 outputs.push_back (Output (0.1 * n, 0.1 * n));
848 for (n = 0; n < npans; ++n) {
849 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
855 for (std::vector<StreamPanner*>::iterator x = _streampanners.begin(); x != _streampanners.end(); ++x) {
859 /* force hard left/right panning in a common case: 2in/2out
862 if (npans == 2 && outputs.size() == 2) {
864 /* Do this only if we changed configuration, or our configuration
865 appears to be the default set up (center).
871 _streampanners.front()->get_position (left);
872 _streampanners.back()->get_position (right);
874 if (changed || ((left == 0.5) && (right == 0.5))) {
876 _streampanners.front()->set_position (0.0);
877 _streampanners.front()->pan_control()->list()->reset_default (0.0);
879 _streampanners.back()->set_position (1.0);
880 _streampanners.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 = _streampanners.begin(); i != _streampanners.end() && which; ++i, --which);
900 if (i != _streampanners.end()) {
902 _streampanners.erase (i);
907 /** Remove all our StreamPanners */
909 Panner::clear_panners ()
911 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
915 _streampanners.clear ();
919 Panner::set_automation_style (AutoStyle style)
921 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
922 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_style (style);
924 _session.set_dirty ();
928 Panner::set_automation_state (AutoState state)
930 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
931 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_state (state);
933 _session.set_dirty ();
937 Panner::automation_state () const
940 return ((AutomationList*)_streampanners.front()->pan_control()->list().get())->automation_state ();
947 Panner::automation_style () const
950 return ((AutomationList*)_streampanners.front()->pan_control()->list().get())->automation_style ();
960 StreamPanner* (*factory)(Panner&, Evoral::Parameter);
963 PanPlugins pan_plugins[] = {
964 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
965 { Multi2dPanner::name, 3, Multi2dPanner::factory },
966 { string (""), 0, 0 }
970 Panner::get_state (void)
976 Panner::state (bool full)
978 XMLNode& node = Processor::state(full);
980 node.add_property ("type", "panner");
984 node.add_property (X_("linked"), (_linked ? "yes" : "no"));
985 node.add_property (X_("link_direction"), enum_2_string (_link_direction));
986 node.add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
988 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
989 XMLNode* onode = new XMLNode (X_("Output"));
990 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
991 onode->add_property (X_("x"), buf);
992 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
993 onode->add_property (X_("y"), buf);
994 node.add_child_nocopy (*onode);
997 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
998 node.add_child_nocopy ((*i)->state (full));
1006 Panner::set_state (const XMLNode& node)
1009 XMLNodeConstIterator niter;
1010 const XMLProperty *prop;
1012 uint32_t num_panners = 0;
1014 LocaleGuard lg (X_("POSIX"));
1018 Processor::set_state(node);
1020 ChanCount ins = ChanCount::ZERO;
1021 ChanCount outs = ChanCount::ZERO;
1023 // XXX: this might not be necessary anymore
1026 if ((prop = node.property (X_("linked"))) != 0) {
1027 set_linked (prop->value() == "yes");
1031 if ((prop = node.property (X_("bypassed"))) != 0) {
1032 set_bypassed (prop->value() == "yes");
1035 if ((prop = node.property (X_("link_direction"))) != 0) {
1036 LinkDirection ld; /* here to provide type information */
1037 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1040 nlist = node.children();
1042 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1043 if ((*niter)->name() == X_("Output")) {
1047 prop = (*niter)->property (X_("x"));
1048 sscanf (prop->value().c_str(), "%g", &x);
1050 prop = (*niter)->property (X_("y"));
1051 sscanf (prop->value().c_str(), "%g", &y);
1053 outputs.push_back (Output (x, y));
1057 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1059 if ((*niter)->name() == X_("StreamPanner")) {
1061 if ((prop = (*niter)->property (X_("type")))) {
1063 for (i = 0; pan_plugins[i].factory; ++i) {
1064 if (prop->value() == pan_plugins[i].name) {
1067 /* note that we assume that all the stream panners
1068 are of the same type. pretty good
1069 assumption, but it's still an assumption.
1072 sp = pan_plugins[i].factory (*this, Evoral::Parameter(PanAutomation, 0, num_panners));
1075 if (sp->set_state (**niter) == 0) {
1076 _streampanners.push_back (sp);
1084 if (!pan_plugins[i].factory) {
1085 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1091 error << _("panner plugin node has no type information!")
1099 reset (outputs.size (), num_panners);
1100 /* don't try to do old-school automation loading if it wasn't marked as existing */
1102 if ((prop = node.property (X_("automation")))) {
1104 /* automation path is relative */
1106 automation_path = Glib::build_filename(_session.automation_dir(), prop->value ());
1113 Panner::touching () const
1115 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1116 if (((AutomationList*)(*i)->pan_control()->list().get())->touching ()) {
1125 Panner::set_position (float xpos, StreamPanner& orig)
1131 orig.get_position (xnow);
1132 xdelta = xpos - xnow;
1134 if (_link_direction == SameDirection) {
1136 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1138 (*i)->set_position (xpos, true);
1140 (*i)->get_position (xnow);
1141 xnew = min (1.0f, xnow + xdelta);
1142 xnew = max (0.0f, xnew);
1143 (*i)->set_position (xnew, true);
1149 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.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);
1163 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1166 float xdelta, ydelta;
1169 orig.get_position (xnow, ynow);
1170 xdelta = xpos - xnow;
1171 ydelta = ypos - ynow;
1173 if (_link_direction == SameDirection) {
1175 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1177 (*i)->set_position (xpos, ypos, true);
1179 (*i)->get_position (xnow, ynow);
1181 xnew = min (1.0f, xnow + xdelta);
1182 xnew = max (0.0f, xnew);
1184 ynew = min (1.0f, ynow + ydelta);
1185 ynew = max (0.0f, ynew);
1187 (*i)->set_position (xnew, ynew, true);
1193 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1195 (*i)->set_position (xpos, ypos, true);
1197 (*i)->get_position (xnow, ynow);
1199 xnew = min (1.0f, xnow - xdelta);
1200 xnew = max (0.0f, xnew);
1202 ynew = min (1.0f, ynow - ydelta);
1203 ynew = max (0.0f, ynew);
1205 (*i)->set_position (xnew, ynew, true);
1212 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1214 float xnow, ynow, znow;
1215 float xdelta, ydelta, zdelta;
1216 float xnew, ynew, znew;
1218 orig.get_position (xnow, ynow, znow);
1219 xdelta = xpos - xnow;
1220 ydelta = ypos - ynow;
1221 zdelta = zpos - znow;
1223 if (_link_direction == SameDirection) {
1225 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1227 (*i)->set_position (xpos, ypos, zpos, true);
1229 (*i)->get_position (xnow, ynow, znow);
1231 xnew = min (1.0f, xnow + xdelta);
1232 xnew = max (0.0f, xnew);
1234 ynew = min (1.0f, ynow + ydelta);
1235 ynew = max (0.0f, ynew);
1237 znew = min (1.0f, znow + zdelta);
1238 znew = max (0.0f, znew);
1240 (*i)->set_position (xnew, ynew, znew, true);
1246 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1248 (*i)->set_position (xpos, ypos, true);
1250 (*i)->get_position (xnow, ynow, znow);
1252 xnew = min (1.0f, xnow - xdelta);
1253 xnew = max (0.0f, xnew);
1255 ynew = min (1.0f, ynow - ydelta);
1256 ynew = max (0.0f, ynew);
1258 znew = min (1.0f, znow + zdelta);
1259 znew = max (0.0f, znew);
1261 (*i)->set_position (xnew, ynew, znew, true);
1268 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, nframes_t offset, gain_t gain_coeff)
1270 if (outbufs.count().n_audio() == 0) {
1271 // Don't want to lose audio...
1272 assert(inbufs.count().n_audio() == 0);
1276 // We shouldn't be called in the first place...
1277 assert(!bypassed());
1281 if (outbufs.count().n_audio() == 1) {
1283 AudioBuffer& dst = outbufs.get_audio(0);
1285 if (gain_coeff == 0.0f) {
1287 /* only one output, and gain was zero, so make it silent */
1289 dst.silence(offset);
1291 } else if (gain_coeff == 1.0f){
1293 /* mix all buffers into the output */
1296 dst.read_from(inbufs.get_audio(0), nframes, offset);
1298 // accumulate 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_from(*i, nframes, offset);
1308 /* mix all buffers into the output, scaling them all by the gain */
1311 dst.read_from(inbufs.get_audio(0), nframes, offset);
1313 // accumulate (with gain) starting with the second
1314 if (inbufs.count().n_audio() > 0) {
1315 BufferSet::audio_iterator i = inbufs.audio_begin();
1316 for (++i; i != inbufs.audio_end(); ++i) {
1317 dst.accumulate_with_gain_from(*i, nframes, offset, gain_coeff);
1326 /* the terrible silence ... */
1327 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1328 i->silence(nframes, offset);
1331 BufferSet::audio_iterator i = inbufs.audio_begin();
1333 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end() && i != inbufs.audio_end(); ++pan, ++i) {
1334 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1339 Panner::run_out_of_place (BufferSet& inbufs, BufferSet& outbufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
1341 if (outbufs.count().n_audio() == 0) {
1342 // Failing to deliver audio we were asked to deliver is a bug
1343 assert(inbufs.count().n_audio() == 0);
1347 // We shouldn't be called in the first place...
1348 assert(!bypassed());
1351 // If we shouldn't play automation defer to distribute_no_automation
1352 if ( !( automation_state() & Play ||
1353 ((automation_state() & Touch) && !touching()) ) ) {
1356 gain_t gain_coeff = 1.0;
1357 if (fabsf(_session.transport_speed()) > 1.5f) {
1358 gain_coeff = speed_quietning;
1361 distribute_no_automation(inbufs, outbufs, nframes, offset, gain_coeff);
1365 // Otherwise.. let the automation flow, baby
1367 if (outbufs.count().n_audio() == 1) {
1369 AudioBuffer& dst = outbufs.get_audio(0);
1371 // FIXME: apply gain automation?
1374 dst.read_from(inbufs.get_audio(0), nframes, offset);
1376 // accumulate starting with the second
1377 BufferSet::audio_iterator i = inbufs.audio_begin();
1378 for (++i; i != inbufs.audio_end(); ++i) {
1379 dst.accumulate_from(*i, nframes, offset);
1385 // More than 1 output, we should have 1 panner for each input
1386 //assert(_streampanners.size() == inbufs.count().n_audio());
1388 /* the terrible silence ... */
1389 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1390 i->silence(nframes, offset);
1393 BufferSet::audio_iterator i = inbufs.audio_begin();
1394 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end(); ++pan, ++i) {
1395 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
1399 /* old school automation handling */
1403 Panner::set_name (string str)
1405 automation_path = Glib::build_filename(_session.automation_dir(),
1406 _session.snap_name() + "-pan-" + legalize_for_path (str) + ".automation");
1414 uint32_t linecnt = 0;
1416 vector<StreamPanner*>::iterator sp;
1417 LocaleGuard lg (X_("POSIX"));
1419 if (automation_path.length() == 0) {
1423 if (access (automation_path.c_str(), F_OK)) {
1427 ifstream in (automation_path.c_str());
1430 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1431 automation_path, strerror (errno))
1436 sp = _streampanners.begin();
1438 while (in.getline (line, sizeof(line), '\n')) {
1440 if (++linecnt == 1) {
1441 if (memcmp (line, X_("version"), 7) == 0) {
1442 if (sscanf (line, "version %f", &version) != 1) {
1443 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1447 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1448 automation_path, line) << endmsg;
1455 if (strlen (line) == 0 || line[0] == '#') {
1459 if (strcmp (line, "begin") == 0) {
1461 if (sp == _streampanners.end()) {
1462 error << string_compose (_("too many panner states found in pan automation file %1"),
1468 if ((*sp)->load (in, automation_path, linecnt)) {