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;
752 bool do_not_and_did_not_need_panning = ((nouts < 2) && (outputs.size() < 2));
754 /* if new and old config don't need panning, or if
755 the config hasn't changed, we're done.
758 if (do_not_and_did_not_need_panning ||
759 ((nouts == outputs.size()) && (npans == _streampanners.size()))) {
763 n = _streampanners.size();
786 fatal << _("programming error:")
787 << X_("Panner::reset() called with a single output")
794 outputs.push_back (Output (0, 0));
795 outputs.push_back (Output (1.0, 0));
797 for (n = 0; n < npans; ++n) {
798 _streampanners.push_back (new EqualPowerStereoPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
803 outputs.push_back (Output (0.5, 0));
804 outputs.push_back (Output (0, 1.0));
805 outputs.push_back (Output (1.0, 1.0));
807 for (n = 0; n < npans; ++n) {
808 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
814 outputs.push_back (Output (0, 0));
815 outputs.push_back (Output (1.0, 0));
816 outputs.push_back (Output (1.0, 1.0));
817 outputs.push_back (Output (0, 1.0));
819 for (n = 0; n < npans; ++n) {
820 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
825 case 5: //square+offcenter center
826 outputs.push_back (Output (0, 0));
827 outputs.push_back (Output (1.0, 0));
828 outputs.push_back (Output (1.0, 1.0));
829 outputs.push_back (Output (0, 1.0));
830 outputs.push_back (Output (0.5, 0.75));
832 for (n = 0; n < npans; ++n) {
833 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
839 /* XXX horrible placement. FIXME */
840 for (n = 0; n < nouts; ++n) {
841 outputs.push_back (Output (0.1 * n, 0.1 * n));
844 for (n = 0; n < npans; ++n) {
845 _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
851 for (std::vector<StreamPanner*>::iterator x = _streampanners.begin(); x != _streampanners.end(); ++x) {
855 /* force hard left/right panning in a common case: 2in/2out
858 if (npans == 2 && outputs.size() == 2) {
860 /* Do this only if we changed configuration, or our configuration
861 appears to be the default set up (center).
867 _streampanners.front()->get_position (left);
868 _streampanners.back()->get_position (right);
870 if (changed || ((left == 0.5) && (right == 0.5))) {
872 _streampanners.front()->set_position (0.0);
873 _streampanners.front()->pan_control()->list()->reset_default (0.0);
875 _streampanners.back()->set_position (1.0);
876 _streampanners.back()->pan_control()->list()->reset_default (1.0);
884 Changed (); /* EMIT SIGNAL */
891 Panner::remove (uint32_t which)
893 vector<StreamPanner*>::iterator i;
894 for (i = _streampanners.begin(); i != _streampanners.end() && which; ++i, --which);
896 if (i != _streampanners.end()) {
898 _streampanners.erase (i);
903 Panner::clear_panners ()
905 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
909 _streampanners.clear ();
913 Panner::set_automation_style (AutoStyle style)
915 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
916 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_style (style);
918 _session.set_dirty ();
922 Panner::set_automation_state (AutoState state)
924 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
925 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_state (state);
927 _session.set_dirty ();
931 Panner::automation_state () const
934 return ((AutomationList*)_streampanners.front()->pan_control()->list().get())->automation_state ();
941 Panner::automation_style () const
944 return ((AutomationList*)_streampanners.front()->pan_control()->list().get())->automation_style ();
954 StreamPanner* (*factory)(Panner&, Evoral::Parameter);
957 PanPlugins pan_plugins[] = {
958 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
959 { Multi2dPanner::name, 3, Multi2dPanner::factory },
960 { string (""), 0, 0 }
964 Panner::get_state (void)
970 Panner::state (bool full)
972 XMLNode& node = Processor::state(full);
974 node.add_property ("type", "panner");
978 node.add_property (X_("linked"), (_linked ? "yes" : "no"));
979 node.add_property (X_("link_direction"), enum_2_string (_link_direction));
980 node.add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
982 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
983 XMLNode* onode = new XMLNode (X_("Output"));
984 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
985 onode->add_property (X_("x"), buf);
986 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
987 onode->add_property (X_("y"), buf);
988 node.add_child_nocopy (*onode);
991 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
992 node.add_child_nocopy ((*i)->state (full));
1000 Panner::set_state (const XMLNode& node)
1003 XMLNodeConstIterator niter;
1004 const XMLProperty *prop;
1006 uint32_t num_panners = 0;
1008 LocaleGuard lg (X_("POSIX"));
1012 Processor::set_state(node);
1014 ChanCount ins = ChanCount::ZERO;
1015 ChanCount outs = ChanCount::ZERO;
1017 // XXX: this might not be necessary anymore
1020 if ((prop = node.property (X_("linked"))) != 0) {
1021 set_linked (prop->value() == "yes");
1025 if ((prop = node.property (X_("bypassed"))) != 0) {
1026 set_bypassed (prop->value() == "yes");
1029 if ((prop = node.property (X_("link_direction"))) != 0) {
1030 LinkDirection ld; /* here to provide type information */
1031 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1034 nlist = node.children();
1036 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1037 if ((*niter)->name() == X_("Output")) {
1041 prop = (*niter)->property (X_("x"));
1042 sscanf (prop->value().c_str(), "%g", &x);
1044 prop = (*niter)->property (X_("y"));
1045 sscanf (prop->value().c_str(), "%g", &y);
1047 outputs.push_back (Output (x, y));
1051 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1053 if ((*niter)->name() == X_("StreamPanner")) {
1055 if ((prop = (*niter)->property (X_("type")))) {
1057 for (i = 0; pan_plugins[i].factory; ++i) {
1058 if (prop->value() == pan_plugins[i].name) {
1061 /* note that we assume that all the stream panners
1062 are of the same type. pretty good
1063 assumption, but its still an assumption.
1066 sp = pan_plugins[i].factory (*this, Evoral::Parameter(PanAutomation, 0, num_panners));
1069 if (sp->set_state (**niter) == 0) {
1070 _streampanners.push_back (sp);
1078 if (!pan_plugins[i].factory) {
1079 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1085 error << _("panner plugin node has no type information!")
1093 reset(num_panners, outputs.size());
1094 /* don't try to do old-school automation loading if it wasn't marked as existing */
1096 if ((prop = node.property (X_("automation")))) {
1098 /* automation path is relative */
1100 automation_path = Glib::build_filename(_session.automation_dir(), prop->value ());
1107 Panner::touching () const
1109 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1110 if (((AutomationList*)(*i)->pan_control()->list().get())->touching ()) {
1119 Panner::set_position (float xpos, StreamPanner& orig)
1125 orig.get_position (xnow);
1126 xdelta = xpos - xnow;
1128 if (_link_direction == SameDirection) {
1130 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1132 (*i)->set_position (xpos, true);
1134 (*i)->get_position (xnow);
1135 xnew = min (1.0f, xnow + xdelta);
1136 xnew = max (0.0f, xnew);
1137 (*i)->set_position (xnew, true);
1143 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1145 (*i)->set_position (xpos, true);
1147 (*i)->get_position (xnow);
1148 xnew = min (1.0f, xnow - xdelta);
1149 xnew = max (0.0f, xnew);
1150 (*i)->set_position (xnew, true);
1157 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1160 float xdelta, ydelta;
1163 orig.get_position (xnow, ynow);
1164 xdelta = xpos - xnow;
1165 ydelta = ypos - ynow;
1167 if (_link_direction == SameDirection) {
1169 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1171 (*i)->set_position (xpos, ypos, true);
1173 (*i)->get_position (xnow, ynow);
1175 xnew = min (1.0f, xnow + xdelta);
1176 xnew = max (0.0f, xnew);
1178 ynew = min (1.0f, ynow + ydelta);
1179 ynew = max (0.0f, ynew);
1181 (*i)->set_position (xnew, ynew, true);
1187 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1189 (*i)->set_position (xpos, ypos, true);
1191 (*i)->get_position (xnow, ynow);
1193 xnew = min (1.0f, xnow - xdelta);
1194 xnew = max (0.0f, xnew);
1196 ynew = min (1.0f, ynow - ydelta);
1197 ynew = max (0.0f, ynew);
1199 (*i)->set_position (xnew, ynew, true);
1206 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1208 float xnow, ynow, znow;
1209 float xdelta, ydelta, zdelta;
1210 float xnew, ynew, znew;
1212 orig.get_position (xnow, ynow, znow);
1213 xdelta = xpos - xnow;
1214 ydelta = ypos - ynow;
1215 zdelta = zpos - znow;
1217 if (_link_direction == SameDirection) {
1219 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1221 (*i)->set_position (xpos, ypos, zpos, true);
1223 (*i)->get_position (xnow, ynow, znow);
1225 xnew = min (1.0f, xnow + xdelta);
1226 xnew = max (0.0f, xnew);
1228 ynew = min (1.0f, ynow + ydelta);
1229 ynew = max (0.0f, ynew);
1231 znew = min (1.0f, znow + zdelta);
1232 znew = max (0.0f, znew);
1234 (*i)->set_position (xnew, ynew, znew, true);
1240 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1242 (*i)->set_position (xpos, ypos, 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);
1262 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, nframes_t offset, gain_t gain_coeff)
1264 if (outbufs.count().n_audio() == 0) {
1265 // Don't want to lose audio...
1266 assert(inbufs.count().n_audio() == 0);
1270 // We shouldn't be called in the first place...
1271 assert(!bypassed());
1275 if (outbufs.count().n_audio() == 1) {
1277 AudioBuffer& dst = outbufs.get_audio(0);
1279 if (gain_coeff == 0.0f) {
1281 /* only one output, and gain was zero, so make it silent */
1283 dst.silence(offset);
1285 } else if (gain_coeff == 1.0f){
1287 /* mix all buffers into the output */
1290 dst.read_from(inbufs.get_audio(0), nframes, offset);
1292 // accumulate starting with the second
1293 if (inbufs.count().n_audio() > 0) {
1294 BufferSet::audio_iterator i = inbufs.audio_begin();
1295 for (++i; i != inbufs.audio_end(); ++i) {
1296 dst.accumulate_from(*i, nframes, offset);
1302 /* mix all buffers into the output, scaling them all by the gain */
1305 dst.read_from(inbufs.get_audio(0), nframes, offset);
1307 // accumulate (with gain) starting with the second
1308 if (inbufs.count().n_audio() > 0) {
1309 BufferSet::audio_iterator i = inbufs.audio_begin();
1310 for (++i; i != inbufs.audio_end(); ++i) {
1311 dst.accumulate_with_gain_from(*i, nframes, offset, gain_coeff);
1320 /* the terrible silence ... */
1321 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1322 i->silence(nframes, offset);
1325 BufferSet::audio_iterator i = inbufs.audio_begin();
1327 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end() && i != inbufs.audio_end(); ++pan, ++i) {
1328 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1333 Panner::run_out_of_place (BufferSet& inbufs, BufferSet& outbufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
1335 if (outbufs.count().n_audio() == 0) {
1336 // Failing to deliver audio we were asked to deliver is a bug
1337 assert(inbufs.count().n_audio() == 0);
1341 // We shouldn't be called in the first place...
1342 assert(!bypassed());
1345 // If we shouldn't play automation defer to distribute_no_automation
1346 if ( !( automation_state() & Play ||
1347 ((automation_state() & Touch) && !touching()) ) ) {
1350 gain_t gain_coeff = 1.0;
1351 if (fabsf(_session.transport_speed()) > 1.5f) {
1352 gain_coeff = speed_quietning;
1355 distribute_no_automation(inbufs, outbufs, nframes, offset, gain_coeff);
1359 // Otherwise.. let the automation flow, baby
1361 if (outbufs.count().n_audio() == 1) {
1363 AudioBuffer& dst = outbufs.get_audio(0);
1365 // FIXME: apply gain automation?
1368 dst.read_from(inbufs.get_audio(0), nframes, offset);
1370 // accumulate starting with the second
1371 BufferSet::audio_iterator i = inbufs.audio_begin();
1372 for (++i; i != inbufs.audio_end(); ++i) {
1373 dst.accumulate_from(*i, nframes, offset);
1379 // More than 1 output, we should have 1 panner for each input
1380 //assert(_streampanners.size() == inbufs.count().n_audio());
1382 /* the terrible silence ... */
1383 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1384 i->silence(nframes, offset);
1387 BufferSet::audio_iterator i = inbufs.audio_begin();
1388 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end(); ++pan, ++i) {
1389 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
1393 /* old school automation handling */
1397 Panner::set_name (string str)
1399 automation_path = Glib::build_filename(_session.automation_dir(),
1400 _session.snap_name() + "-pan-" + legalize_for_path (str) + ".automation");
1408 uint32_t linecnt = 0;
1410 vector<StreamPanner*>::iterator sp;
1411 LocaleGuard lg (X_("POSIX"));
1413 if (automation_path.length() == 0) {
1417 if (access (automation_path.c_str(), F_OK)) {
1421 ifstream in (automation_path.c_str());
1424 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1425 automation_path, strerror (errno))
1430 sp = _streampanners.begin();
1432 while (in.getline (line, sizeof(line), '\n')) {
1434 if (++linecnt == 1) {
1435 if (memcmp (line, X_("version"), 7) == 0) {
1436 if (sscanf (line, "version %f", &version) != 1) {
1437 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1441 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1442 automation_path, line) << endmsg;
1449 if (strlen (line) == 0 || line[0] == '#') {
1453 if (strcmp (line, "begin") == 0) {
1455 if (sp == _streampanners.end()) {
1456 error << string_compose (_("too many panner states found in pan automation file %1"),
1462 if ((*sp)->load (in, automation_path, linecnt)) {