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.
21 #define __STDC_FORMAT_MACROS 1
36 #include <pbd/error.h>
37 #include <pbd/failed_constructor.h>
38 #include <pbd/xml++.h>
40 #include <ardour/session.h>
41 #include <ardour/panner.h>
42 #include <ardour/utils.h>
44 #include <ardour/mix.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)
74 _control (X_("panner"), *this)
83 StreamPanner::~StreamPanner ()
88 StreamPanner::PanControllable::set_value (float val)
90 panner.set_position (direct_control_to_pan (val));
94 StreamPanner::PanControllable::get_value (void) const
97 panner.get_effective_position (xpos);
98 return direct_pan_to_control (xpos);
102 StreamPanner::PanControllable::can_send_feedback () const
104 AutoState astate = panner.get_parent().automation_state ();
106 if ((astate == Play) || (astate == Touch && !panner.get_parent().touching())) {
114 StreamPanner::set_muted (bool yn)
123 StreamPanner::set_position (float xpos, bool link_call)
125 if (!link_call && parent.linked()) {
126 parent.set_position (xpos, *this);
138 StreamPanner::set_position (float xpos, float ypos, bool link_call)
140 if (!link_call && parent.linked()) {
141 parent.set_position (xpos, ypos, *this);
144 if (x != xpos || y != ypos) {
154 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
156 if (!link_call && parent.linked()) {
157 parent.set_position (xpos, ypos, zpos, *this);
160 if (x != xpos || y != ypos || z != zpos) {
170 StreamPanner::set_state (const XMLNode& node)
172 const XMLProperty* prop;
173 XMLNodeConstIterator iter;
175 if ((prop = node.property (X_("muted")))) {
176 set_muted (prop->value() == "yes");
183 StreamPanner::add_state (XMLNode& node)
185 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
188 /*---------------------------------------------------------------------- */
190 BaseStereoPanner::BaseStereoPanner (Panner& p)
191 : StreamPanner (p), _automation (0.0, 1.0, 0.5)
195 BaseStereoPanner::~BaseStereoPanner ()
200 BaseStereoPanner::snapshot (nframes_t now)
202 if (_automation.automation_state() == Write || _automation.automation_state() == Touch) {
203 _automation.rt_add (now, x);
208 BaseStereoPanner::transport_stopped (nframes_t frame)
210 _automation.reposition_for_rt_add (frame);
212 if (_automation.automation_state() != Off) {
214 if (_automation.automation_write()) {
215 _automation.save_state (_("automation write pass"));
218 set_position (_automation.eval (frame));
223 BaseStereoPanner::set_automation_style (AutoStyle style)
225 _automation.set_automation_style (style);
229 BaseStereoPanner::set_automation_state (AutoState state)
231 if (state != _automation.automation_state()) {
233 _automation.set_automation_state (state);
236 set_position (_automation.eval (parent.session().transport_frame()));
242 BaseStereoPanner::save (ostream& out) const
244 LocaleGuard lg (X_("POSIX"));
246 /* force a single format for numeric data to ease session interchange
247 across national boundaries.
250 out << "begin" << endl;
252 for (AutomationList::const_iterator i = _automation.const_begin(); i != _automation.const_end(); ++i) {
253 out << '\t' << (nframes_t) floor ((*i)->when) << ' ' << (*i)->value << endl;
255 error << string_compose (_("error writing pan automation file (%s)"), strerror (errno)) << endmsg;
259 out << "end" << endl;
265 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
268 LocaleGuard lg (X_("POSIX"));
270 _automation.clear ();
272 while (in.getline (line, sizeof (line), '\n')) {
278 if (strcmp (line, "end") == 0) {
282 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
283 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
287 _automation.add (when, value, true);
290 /* now that we are done loading */
292 _automation.save_state (_("loaded from disk"));
293 _automation.StateChanged (Change (0));
299 BaseStereoPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
301 assert(obufs.count().get(DataType::AUDIO) == 2);
311 Sample* const src = srcbuf.data(nframes);
315 dst = obufs.get_audio(0).data(nframes);
317 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
319 /* interpolate over 64 frames or nframes, whichever is smaller */
321 nframes_t limit = min ((nframes_t)64, nframes);
324 delta = -(delta / (float) (limit));
326 for (n = 0; n < limit; n++) {
327 left_interp = left_interp + delta;
328 left = left_interp + 0.9 * (left - left_interp);
329 dst[n] += src[n] * left * gain_coeff;
332 pan = left * gain_coeff;
334 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
341 if ((pan = (left * gain_coeff)) != 1.0f) {
345 Session::mix_buffers_with_gain(dst,src,nframes,pan);
347 /* mark that we wrote into the buffer */
355 Session::mix_buffers_no_gain(dst,src,nframes);
357 /* mark that we wrote into the buffer */
365 dst = obufs.get_audio(1).data(nframes);
367 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
369 /* interpolate over 64 frames or nframes, whichever is smaller */
371 nframes_t limit = min ((nframes_t)64, nframes);
374 delta = -(delta / (float) (limit));
376 for (n = 0; n < limit; n++) {
377 right_interp = right_interp + delta;
378 right = right_interp + 0.9 * (right - right_interp);
379 dst[n] += src[n] * right * gain_coeff;
382 pan = right * gain_coeff;
384 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
386 /* XXX it would be nice to mark the buffer as written to */
390 right = desired_right;
391 right_interp = right;
393 if ((pan = (right * gain_coeff)) != 1.0f) {
397 Session::mix_buffers_with_gain(dst,src,nframes,pan);
399 /* XXX it would be nice to mark the buffer as written to */
404 Session::mix_buffers_no_gain(dst,src,nframes);
406 /* XXX it would be nice to mark the buffer as written to */
411 /*---------------------------------------------------------------------- */
413 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p)
414 : BaseStereoPanner (p)
419 right = desired_right;
421 right_interp = right;
424 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
429 EqualPowerStereoPanner::update ()
431 /* it would be very nice to split this out into a virtual function
432 that can be accessed from BaseStereoPanner and used in distribute_automated().
434 but the place where its used in distribute_automated() is a tight inner loop,
435 and making "nframes" virtual function calls to compute values is an absurd
439 /* x == 0 => hard left
444 float panL = 1 - panR;
446 const float pan_law_attenuation = -3.0f;
447 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
449 desired_left = panL * (scale * panL + 1.0f - scale);
450 desired_right = panR * (scale * panR + 1.0f - scale);
456 EqualPowerStereoPanner::distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs,
457 nframes_t start, nframes_t end, nframes_t nframes,
460 assert(obufs.count().get(DataType::AUDIO) == 2);
464 Sample* const src = srcbuf.data(nframes);
466 /* fetch positional data */
468 if (!_automation.rt_safe_get_vector (start, end, buffers[0], nframes)) {
471 distribute (srcbuf, obufs, 1.0, nframes);
476 /* store effective pan position. do this even if we are muted */
479 effective_x = buffers[0][nframes-1];
485 /* apply pan law to convert positional data into pan coefficients for
489 const float pan_law_attenuation = -3.0f;
490 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
492 for (nframes_t n = 0; n < nframes; ++n) {
494 float panR = buffers[0][n];
495 float panL = 1 - panR;
497 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
498 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
503 dst = obufs.get_audio(0).data(nframes);
506 for (nframes_t n = 0; n < nframes; ++n) {
507 dst[n] += src[n] * pbuf[n];
510 /* XXX it would be nice to mark the buffer as written to */
514 dst = obufs.get_audio(1).data(nframes);
517 for (nframes_t n = 0; n < nframes; ++n) {
518 dst[n] += src[n] * pbuf[n];
521 /* XXX it would be nice to mark the buffer as written to */
525 EqualPowerStereoPanner::factory (Panner& parent)
527 return new EqualPowerStereoPanner (parent);
531 EqualPowerStereoPanner::get_state (void)
537 EqualPowerStereoPanner::state (bool full_state)
539 XMLNode* root = new XMLNode ("StreamPanner");
541 LocaleGuard lg (X_("POSIX"));
543 snprintf (buf, sizeof (buf), "%.12g", x);
544 root->add_property (X_("x"), buf);
545 root->add_property (X_("type"), EqualPowerStereoPanner::name);
547 snprintf (buf, sizeof (buf), "0x%x", _automation.automation_state());
549 /* never store automation states other than off in a template */
550 snprintf (buf, sizeof (buf), "0x%x", ARDOUR::Off);
552 root->add_property (X_("automation-state"), buf);
553 snprintf (buf, sizeof (buf), "0x%x", _automation.automation_style());
554 root->add_property (X_("automation-style"), buf);
556 StreamPanner::add_state (*root);
557 root->add_child_nocopy (_control.get_state ());
563 EqualPowerStereoPanner::set_state (const XMLNode& node)
565 const XMLProperty* prop;
568 LocaleGuard lg (X_("POSIX"));
570 if ((prop = node.property (X_("x")))) {
571 pos = atof (prop->value().c_str());
572 set_position (pos, true);
575 if ((prop = node.property (X_("automation-state")))) {
576 sscanf (prop->value().c_str(), "0x%x", &x);
577 _automation.set_automation_state ((AutoState) x);
580 set_position (_automation.eval (parent.session().transport_frame()));
584 if ((prop = node.property (X_("automation-style")))) {
585 sscanf (prop->value().c_str(), "0x%x", &x);
586 _automation.set_automation_style ((AutoStyle) x);
589 StreamPanner::set_state (node);
591 for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
592 if ((*iter)->name() == X_("panner")) {
593 _control.set_state (**iter);
594 parent.session().add_controllable (&_control);
601 /*----------------------------------------------------------------------*/
603 Multi2dPanner::Multi2dPanner (Panner& p)
604 : StreamPanner (p), _automation (0.0, 1.0, 0.5) // XXX useless
609 Multi2dPanner::~Multi2dPanner ()
614 Multi2dPanner::snapshot (nframes_t now)
620 Multi2dPanner::transport_stopped (nframes_t frame)
626 Multi2dPanner::set_automation_style (AutoStyle style)
632 Multi2dPanner::set_automation_state (AutoState state)
638 Multi2dPanner::update ()
640 static const float BIAS = FLT_MIN;
642 uint32_t nouts = parent.outputs.size();
649 for (i = 0; i < nouts; i++) {
650 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
654 f += dsq[i] * dsq[i];
657 // terrible hack to support OSX < 10.3.9 builds
658 fr = (float) (1.0 / sqrt((double)f));
662 for (i = 0; i < nouts; ++i) {
663 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
670 Multi2dPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
674 vector<Panner::Output>::iterator o;
681 Sample* const src = srcbuf.data(nframes);
684 for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
686 dst = obufs.get_audio(n).data(nframes);
689 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
691 /* interpolate over 64 frames or nframes, whichever is smaller */
693 nframes_t limit = min ((nframes_t)64, nframes);
696 delta = -(delta / (float) (limit));
698 for (n = 0; n < limit; n++) {
699 left_interp = left_interp + delta;
700 left = left_interp + 0.9 * (left - left_interp);
701 dst[n] += src[n] * left * gain_coeff;
704 pan = left * gain_coeff;
706 for (; n < nframes; ++n) {
707 dst[n] += src[n] * pan;
713 pan = (*o).desired_pan;
715 if ((pan *= gain_coeff) != 1.0f) {
719 for (nframes_t n = 0; n < nframes; ++n) {
720 dst[n] += src[n] * pan;
728 for (nframes_t n = 0; n < nframes; ++n) {
743 Multi2dPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs,
744 nframes_t start, nframes_t end, nframes_t nframes,
757 Multi2dPanner::factory (Panner& p)
759 return new Multi2dPanner (p);
763 Multi2dPanner::load (istream& in, string path, uint32_t& linecnt)
769 Multi2dPanner::save (ostream& out) const
775 Multi2dPanner::get_state (void)
781 Multi2dPanner::state (bool full_state)
783 XMLNode* root = new XMLNode ("StreamPanner");
785 LocaleGuard lg (X_("POSIX"));
787 snprintf (buf, sizeof (buf), "%.12g", x);
788 root->add_property (X_("x"), buf);
789 snprintf (buf, sizeof (buf), "%.12g", y);
790 root->add_property (X_("y"), buf);
791 root->add_property (X_("type"), Multi2dPanner::name);
797 Multi2dPanner::set_state (const XMLNode& node)
799 const XMLProperty* prop;
801 LocaleGuard lg (X_("POSIX"));
806 if ((prop = node.property (X_("x")))) {
807 newx = atof (prop->value().c_str());
810 if ((prop = node.property (X_("y")))) {
811 newy = atof (prop->value().c_str());
814 if (x < 0 || y < 0) {
815 error << _("badly-formed positional data for Multi2dPanner - ignored")
820 set_position (newx, newy);
824 /*---------------------------------------------------------------------- */
826 Panner::Panner (string name, Session& s)
831 _link_direction = SameDirection;
840 Panner::set_linked (bool yn)
844 _session.set_dirty ();
845 LinkStateChanged (); /* EMIT SIGNAL */
850 Panner::set_link_direction (LinkDirection ld)
852 if (ld != _link_direction) {
853 _link_direction = ld;
854 _session.set_dirty ();
855 LinkStateChanged (); /* EMIT SIGNAL */
860 Panner::set_name (string str)
862 automation_path = _session.automation_dir();
863 automation_path += _session.snap_name();
864 automation_path += "-pan-";
865 automation_path += legalize_for_path (str);
866 automation_path += ".automation";
871 Panner::set_bypassed (bool yn)
873 if (yn != _bypassed) {
881 Panner::reset (uint32_t nouts, uint32_t npans)
884 bool changed = false;
887 if (nouts < 2 || (nouts == outputs.size() && npans == size())) {
910 fatal << _("programming error:")
911 << X_("Panner::reset() called with a single output")
918 outputs.push_back (Output (0, 0));
919 outputs.push_back (Output (1.0, 0));
921 for (n = 0; n < npans; ++n) {
922 push_back (new EqualPowerStereoPanner (*this));
927 outputs.push_back (Output (0.5, 0));
928 outputs.push_back (Output (0, 1.0));
929 outputs.push_back (Output (1.0, 1.0));
931 for (n = 0; n < npans; ++n) {
932 push_back (new Multi2dPanner (*this));
938 outputs.push_back (Output (0, 0));
939 outputs.push_back (Output (1.0, 0));
940 outputs.push_back (Output (1.0, 1.0));
941 outputs.push_back (Output (0, 1.0));
943 for (n = 0; n < npans; ++n) {
944 push_back (new Multi2dPanner (*this));
949 case 5: //square+offcenter center
950 outputs.push_back (Output (0, 0));
951 outputs.push_back (Output (1.0, 0));
952 outputs.push_back (Output (1.0, 1.0));
953 outputs.push_back (Output (0, 1.0));
954 outputs.push_back (Output (0.5, 0.75));
956 for (n = 0; n < npans; ++n) {
957 push_back (new Multi2dPanner (*this));
963 /* XXX horrible placement. FIXME */
964 for (n = 0; n < nouts; ++n) {
965 outputs.push_back (Output (0.1 * n, 0.1 * n));
968 for (n = 0; n < npans; ++n) {
969 push_back (new Multi2dPanner (*this));
975 for (iterator x = begin(); x != end(); ++x) {
979 /* force hard left/right panning in a common case: 2in/2out
982 if (npans == 2 && outputs.size() == 2) {
984 /* Do this only if we changed configuration, or our configuration
985 appears to be the default set up (center).
991 front()->get_position (left);
992 back()->get_position (right);
994 if (changed || ((left == 0.5) && (right == 0.5))) {
996 front()->set_position (0.0);
997 front()->automation().reset_default (0.0);
999 back()->set_position (1.0);
1000 back()->automation().reset_default (1.0);
1007 Changed (); /* EMIT SIGNAL */
1014 Panner::remove (uint32_t which)
1016 vector<StreamPanner*>::iterator i;
1017 for (i = begin(); i != end() && which; ++i, --which);
1028 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1032 vector<StreamPanner*>::clear ();
1036 Panner::set_automation_style (AutoStyle style)
1038 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1039 (*i)->set_automation_style (style);
1041 _session.set_dirty ();
1045 Panner::set_automation_state (AutoState state)
1047 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1048 (*i)->set_automation_state (state);
1050 _session.set_dirty ();
1054 Panner::automation_state () const
1057 return front()->automation().automation_state ();
1064 Panner::automation_style () const
1067 return front()->automation().automation_style ();
1074 Panner::transport_stopped (nframes_t frame)
1076 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1077 (*i)->transport_stopped (frame);
1082 Panner::snapshot (nframes_t now)
1084 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1085 (*i)->snapshot (now);
1090 Panner::clear_automation ()
1092 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1093 (*i)->automation().clear ();
1095 _session.set_dirty ();
1099 Panner::save () const
1101 ofstream out (automation_path.c_str());
1104 error << string_compose (_("cannot open pan automation file \"%1\" for saving (%2)"), automation_path, strerror (errno))
1109 out << X_("version ") << current_automation_version_number << endl;
1111 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1112 if ((*i)->save (out)) {
1124 uint32_t linecnt = 0;
1127 LocaleGuard lg (X_("POSIX"));
1129 if (automation_path.length() == 0) {
1133 if (access (automation_path.c_str(), F_OK)) {
1137 ifstream in (automation_path.c_str());
1140 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1141 automation_path, strerror (errno))
1148 while (in.getline (line, sizeof(line), '\n')) {
1150 if (++linecnt == 1) {
1151 if (memcmp (line, X_("version"), 7) == 0) {
1152 if (sscanf (line, "version %f", &version) != 1) {
1153 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1157 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1158 automation_path, line) << endmsg;
1162 if (version != current_automation_version_number) {
1163 error << string_compose(_("mismatched pan automation event file version (%1)"), version) << endmsg;
1170 if (strlen (line) == 0 || line[0] == '#') {
1174 if (strcmp (line, "begin") == 0) {
1177 error << string_compose (_("too many panner states found in pan automation file %1"),
1183 if ((*sp)->load (in, automation_path, linecnt)) {
1197 StreamPanner* (*factory)(Panner&);
1200 PanPlugins pan_plugins[] = {
1201 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1202 { Multi2dPanner::name, 3, Multi2dPanner::factory },
1203 { string (""), 0, 0 }
1207 Panner::get_state (void)
1209 return state (true);
1213 Panner::state (bool full)
1215 XMLNode* root = new XMLNode (X_("Panner"));
1218 for (iterator p = begin(); p != end(); ++p) {
1219 root->add_child_nocopy ((*p)->state (full));
1222 root->add_property (X_("linked"), (_linked ? "yes" : "no"));
1223 snprintf (buf, sizeof (buf), "%d", _link_direction);
1224 root->add_property (X_("link_direction"), buf);
1225 root->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1227 /* add each output */
1229 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1230 XMLNode* onode = new XMLNode (X_("Output"));
1231 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1232 onode->add_property (X_("x"), buf);
1233 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1234 onode->add_property (X_("y"), buf);
1235 root->add_child_nocopy (*onode);
1240 root->add_property (X_("automation"), Glib::path_get_basename (automation_path));
1248 Panner::set_state (const XMLNode& node)
1251 XMLNodeConstIterator niter;
1252 const XMLProperty *prop;
1255 LocaleGuard lg (X_("POSIX"));
1260 if ((prop = node.property (X_("linked"))) != 0) {
1261 set_linked (prop->value() == "yes");
1265 if ((prop = node.property (X_("bypassed"))) != 0) {
1266 set_bypassed (prop->value() == "yes");
1269 if ((prop = node.property (X_("link_direction"))) != 0) {
1270 sscanf (prop->value().c_str(), "%d", &i);
1271 set_link_direction ((LinkDirection) (i));
1274 nlist = node.children();
1276 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1277 if ((*niter)->name() == X_("Output")) {
1281 prop = (*niter)->property (X_("x"));
1282 sscanf (prop->value().c_str(), "%g", &x);
1284 prop = (*niter)->property (X_("y"));
1285 sscanf (prop->value().c_str(), "%g", &y);
1287 outputs.push_back (Output (x, y));
1291 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1293 if ((*niter)->name() == X_("StreamPanner")) {
1295 if ((prop = (*niter)->property (X_("type")))) {
1297 for (i = 0; pan_plugins[i].factory; ++i) {
1298 if (prop->value() == pan_plugins[i].name) {
1301 /* note that we assume that all the stream panners
1302 are of the same type. pretty good
1303 assumption, but its still an assumption.
1306 sp = pan_plugins[i].factory (*this);
1308 if (sp->set_state (**niter) == 0) {
1317 if (!pan_plugins[i].factory) {
1318 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1324 error << _("panner plugin node has no type information!")
1332 /* don't try to load automation if it wasn't marked as existing */
1334 if ((prop = node.property (X_("automation")))) {
1336 /* automation path is relative */
1338 automation_path = _session.automation_dir();
1339 automation_path += prop->value ();
1348 Panner::touching () const
1350 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1351 if ((*i)->automation().touching ()) {
1360 Panner::set_position (float xpos, StreamPanner& orig)
1366 orig.get_position (xnow);
1367 xdelta = xpos - xnow;
1369 if (_link_direction == SameDirection) {
1371 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1373 (*i)->set_position (xpos, true);
1375 (*i)->get_position (xnow);
1376 xnew = min (1.0f, xnow + xdelta);
1377 xnew = max (0.0f, xnew);
1378 (*i)->set_position (xnew, true);
1384 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1386 (*i)->set_position (xpos, true);
1388 (*i)->get_position (xnow);
1389 xnew = min (1.0f, xnow - xdelta);
1390 xnew = max (0.0f, xnew);
1391 (*i)->set_position (xnew, true);
1398 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1401 float xdelta, ydelta;
1404 orig.get_position (xnow, ynow);
1405 xdelta = xpos - xnow;
1406 ydelta = ypos - ynow;
1408 if (_link_direction == SameDirection) {
1410 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1412 (*i)->set_position (xpos, ypos, true);
1414 (*i)->get_position (xnow, ynow);
1416 xnew = min (1.0f, xnow + xdelta);
1417 xnew = max (0.0f, xnew);
1419 ynew = min (1.0f, ynow + ydelta);
1420 ynew = max (0.0f, ynew);
1422 (*i)->set_position (xnew, ynew, true);
1428 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1430 (*i)->set_position (xpos, ypos, true);
1432 (*i)->get_position (xnow, ynow);
1434 xnew = min (1.0f, xnow - xdelta);
1435 xnew = max (0.0f, xnew);
1437 ynew = min (1.0f, ynow - ydelta);
1438 ynew = max (0.0f, ynew);
1440 (*i)->set_position (xnew, ynew, true);
1447 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1449 float xnow, ynow, znow;
1450 float xdelta, ydelta, zdelta;
1451 float xnew, ynew, znew;
1453 orig.get_position (xnow, ynow, znow);
1454 xdelta = xpos - xnow;
1455 ydelta = ypos - ynow;
1456 zdelta = zpos - znow;
1458 if (_link_direction == SameDirection) {
1460 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1462 (*i)->set_position (xpos, ypos, zpos, true);
1464 (*i)->get_position (xnow, ynow, znow);
1466 xnew = min (1.0f, xnow + xdelta);
1467 xnew = max (0.0f, xnew);
1469 ynew = min (1.0f, ynow + ydelta);
1470 ynew = max (0.0f, ynew);
1472 znew = min (1.0f, znow + zdelta);
1473 znew = max (0.0f, znew);
1475 (*i)->set_position (xnew, ynew, znew, true);
1481 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1483 (*i)->set_position (xpos, ypos, true);
1485 (*i)->get_position (xnow, ynow, znow);
1487 xnew = min (1.0f, xnow - xdelta);
1488 xnew = max (0.0f, xnew);
1490 ynew = min (1.0f, ynow - ydelta);
1491 ynew = max (0.0f, ynew);
1493 znew = min (1.0f, znow + zdelta);
1494 znew = max (0.0f, znew);
1496 (*i)->set_position (xnew, ynew, znew, true);
1503 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, nframes_t offset, gain_t gain_coeff)
1505 if (outbufs.count().get(DataType::AUDIO) == 0) {
1506 // Don't want to lose audio...
1507 assert(inbufs.count().get(DataType::AUDIO) == 0);
1511 // We shouldn't be called in the first place...
1512 assert(!bypassed());
1516 if (outbufs.count().get(DataType::AUDIO) == 1) {
1518 AudioBuffer& dst = outbufs.get_audio(0);
1520 if (gain_coeff == 0.0f) {
1522 /* only one output, and gain was zero, so make it silent */
1524 dst.silence(offset);
1526 } else if (gain_coeff == 1.0f){
1528 /* mix all buffers into the output */
1531 dst.read_from(inbufs.get_audio(0), nframes, offset);
1533 // accumulate starting with the second
1534 BufferSet::audio_iterator i = inbufs.audio_begin();
1535 for (++i; i != inbufs.audio_end(); ++i) {
1536 dst.accumulate_from(*i, nframes, offset);
1541 /* mix all buffers into the output, scaling them all by the gain */
1544 dst.read_from(inbufs.get_audio(0), nframes, offset);
1546 // accumulate (with gain) starting with the second
1547 BufferSet::audio_iterator i = inbufs.audio_begin();
1548 for (++i; i != inbufs.audio_end(); ++i) {
1549 dst.accumulate_with_gain_from(*i, nframes, offset, gain_coeff);
1557 // More than 1 output, we should have 1 panner for each input
1558 assert(size() == inbufs.count().get(DataType::AUDIO));
1560 /* the terrible silence ... */
1561 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1562 i->silence(nframes, offset);
1565 BufferSet::audio_iterator i = inbufs.audio_begin();
1566 for (iterator pan = begin(); pan != end(); ++pan, ++i) {
1567 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1572 Panner::distribute (BufferSet& inbufs, BufferSet& outbufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
1574 if (outbufs.count().get(DataType::AUDIO) == 0) {
1575 // Failing to deliver audio we were asked to deliver is a bug
1576 assert(inbufs.count().get(DataType::AUDIO) == 0);
1580 // We shouldn't be called in the first place...
1581 assert(!bypassed());
1584 // If we shouldn't play automation defer to distribute_no_automation
1585 if ( !( automation_state() & Play ||
1586 ((automation_state() & Touch) && !touching()) ) ) {
1589 gain_t gain_coeff = 1.0;
1590 if (fabsf(_session.transport_speed()) > 1.5f) {
1591 gain_coeff = speed_quietning;
1594 distribute_no_automation(inbufs, outbufs, nframes, offset, gain_coeff);
1598 // Otherwise.. let the automation flow, baby
1600 if (outbufs.count().get(DataType::AUDIO) == 1) {
1602 AudioBuffer& dst = outbufs.get_audio(0);
1604 // FIXME: apply gain automation?
1607 dst.read_from(inbufs.get_audio(0), nframes, offset);
1609 // accumulate starting with the second
1610 BufferSet::audio_iterator i = inbufs.audio_begin();
1611 for (++i; i != inbufs.audio_end(); ++i) {
1612 dst.accumulate_from(*i, nframes, offset);
1618 // More than 1 output, we should have 1 panner for each input
1619 assert(size() == inbufs.count().get(DataType::AUDIO));
1621 /* the terrible silence ... */
1622 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1623 i->silence(nframes, offset);
1626 BufferSet::audio_iterator i = inbufs.audio_begin();
1627 for (iterator pan = begin(); pan != end(); ++pan, ++i) {
1628 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());