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.
35 #include "pbd/cartesian.h"
36 #include "pbd/error.h"
37 #include "pbd/failed_constructor.h"
38 #include "pbd/xml++.h"
39 #include "pbd/enumwriter.h"
41 #include "evoral/Curve.hpp"
43 #include "ardour/session.h"
44 #include "ardour/panner.h"
45 #include "ardour/utils.h"
46 #include "ardour/audio_buffer.h"
48 #include "ardour/runtime_functions.h"
49 #include "ardour/buffer_set.h"
50 #include "ardour/audio_buffer.h"
51 #include "ardour/vbap.h"
55 #include "pbd/mathfix.h"
58 using namespace ARDOUR;
61 float Panner::current_automation_version_number = 1.0;
63 string EqualPowerStereoPanner::name = "Equal Power Stereo";
64 string Multi2dPanner::name = "Multiple (2D)";
66 /* this is a default mapper of control values to a pan position
67 others can be imagined.
70 static pan_t direct_control_to_pan (double fract)
75 StreamPanner::StreamPanner (Panner& p, Evoral::Parameter param)
78 assert (param.type() != NullAutomation);
83 /* get our AutomationControl from our parent Panner, creating it if required */
84 _control = boost::dynamic_pointer_cast<AutomationControl> (parent.control (param, true));
91 StreamPanner::~StreamPanner ()
96 StreamPanner::set_mono (bool yn)
105 Panner::PanControllable::set_value (double val)
107 panner.streampanner (parameter().id()).set_position (direct_control_to_pan (val));
108 AutomationControl::set_value(val);
112 Panner::PanControllable::get_value (void) const
114 return AutomationControl::get_value();
118 StreamPanner::set_muted (bool yn)
127 StreamPanner::set_position (float xpos, bool link_call)
129 if (!link_call && parent.linked()) {
130 parent.set_position (xpos, *this);
137 _control->Changed ();
142 StreamPanner::set_position (float xpos, float ypos, bool link_call)
144 if (!link_call && parent.linked()) {
145 parent.set_position (xpos, ypos, *this);
148 if (_x != xpos || _y != ypos) {
153 _control->Changed ();
158 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
160 if (!link_call && parent.linked()) {
161 parent.set_position (xpos, ypos, zpos, *this);
164 if (_x != xpos || _y != ypos || _z != zpos) {
170 _control->Changed ();
175 StreamPanner::set_state (const XMLNode& node, int /*version*/)
177 const XMLProperty* prop;
178 XMLNodeConstIterator iter;
180 if ((prop = node.property (X_("muted")))) {
181 set_muted (string_is_affirmative (prop->value()));
184 if ((prop = node.property (X_("mono")))) {
185 set_mono (string_is_affirmative (prop->value()));
192 StreamPanner::add_state (XMLNode& node)
194 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
195 node.add_property (X_("mono"), (_mono ? "yes" : "no"));
199 StreamPanner::distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
202 /* we're in mono mode, so just pan the input to all outputs equally */
203 int const N = parent.nouts ();
204 for (int i = 0; i < N; ++i) {
205 mix_buffers_with_gain (obufs.get_audio(i).data(), src.data(), nframes, gain_coeff);
208 /* normal mode, call the `real' distribute method */
209 do_distribute (src, obufs, gain_coeff, nframes);
214 StreamPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs,
215 nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers)
218 /* we're in mono mode, so just pan the input to all outputs equally */
219 int const N = parent.nouts ();
220 for (int i = 0; i < N; ++i) {
221 mix_buffers_with_gain (obufs.get_audio(i).data(), src.data(), nframes, 1.0);
224 /* normal mode, call the `real' distribute method */
225 do_distribute_automated (src, obufs, start, end, nframes, buffers);
231 /*---------------------------------------------------------------------- */
233 BaseStereoPanner::BaseStereoPanner (Panner& p, Evoral::Parameter param)
234 : StreamPanner (p, param)
238 , right_interp (right)
242 BaseStereoPanner::~BaseStereoPanner ()
247 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
250 LocaleGuard lg (X_("POSIX"));
252 _control->list()->clear ();
254 while (in.getline (line, sizeof (line), '\n')) {
260 if (strcmp (line, "end") == 0) {
264 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
265 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
269 _control->list()->fast_simple_add (when, value);
272 /* now that we are done loading */
274 ((AutomationList*)_control->list().get())->StateChanged ();
280 BaseStereoPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
282 assert(obufs.count().n_audio() == 2);
292 Sample* const src = srcbuf.data();
296 dst = obufs.get_audio(0).data();
298 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
300 /* we've moving the pan by an appreciable amount, so we must
301 interpolate over 64 frames or nframes, whichever is smaller */
303 nframes_t const limit = min ((nframes_t)64, nframes);
306 delta = -(delta / (float) (limit));
308 for (n = 0; n < limit; n++) {
309 left_interp = left_interp + delta;
310 left = left_interp + 0.9 * (left - left_interp);
311 dst[n] += src[n] * left * gain_coeff;
314 /* then pan the rest of the buffer; no need for interpolation for this bit */
316 pan = left * gain_coeff;
318 mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
325 if ((pan = (left * gain_coeff)) != 1.0f) {
329 /* pan is 1 but also not 0, so we must do it "properly" */
331 mix_buffers_with_gain(dst,src,nframes,pan);
333 /* mark that we wrote into the buffer */
341 /* pan is 1 so we can just copy the input samples straight in */
343 mix_buffers_no_gain(dst,src,nframes);
345 /* mark that we wrote into the buffer */
353 dst = obufs.get_audio(1).data();
355 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
357 /* we're moving the pan by an appreciable amount, so we must
358 interpolate over 64 frames or nframes, whichever is smaller */
360 nframes_t const limit = min ((nframes_t)64, nframes);
363 delta = -(delta / (float) (limit));
365 for (n = 0; n < limit; n++) {
366 right_interp = right_interp + delta;
367 right = right_interp + 0.9 * (right - right_interp);
368 dst[n] += src[n] * right * gain_coeff;
371 /* then pan the rest of the buffer, no need for interpolation for this bit */
373 pan = right * gain_coeff;
375 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
377 /* XXX it would be nice to mark the buffer as written to */
381 right = desired_right;
382 right_interp = right;
384 if ((pan = (right * gain_coeff)) != 1.0f) {
388 /* pan is not 1 but also not 0, so we must do it "properly" */
390 mix_buffers_with_gain(dst,src,nframes,pan);
392 /* XXX it would be nice to mark the buffer as written to */
397 /* pan is 1 so we can just copy the input samples straight in */
399 mix_buffers_no_gain(dst,src,nframes);
401 /* XXX it would be nice to mark the buffer as written to */
406 /*---------------------------------------------------------------------- */
408 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p, Evoral::Parameter param)
409 : BaseStereoPanner (p, param)
414 right = desired_right;
416 right_interp = right;
419 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
424 EqualPowerStereoPanner::update ()
426 /* it would be very nice to split this out into a virtual function
427 that can be accessed from BaseStereoPanner and used in do_distribute_automated().
429 but the place where its used in do_distribute_automated() is a tight inner loop,
430 and making "nframes" virtual function calls to compute values is an absurd
434 /* x == 0 => hard left
438 float const panR = _x;
439 float const panL = 1 - panR;
441 float const pan_law_attenuation = -3.0f;
442 float const scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
444 desired_left = panL * (scale * panL + 1.0f - scale);
445 desired_right = panR * (scale * panR + 1.0f - scale);
448 //_control->set_value(x);
452 EqualPowerStereoPanner::do_distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs,
453 nframes_t start, nframes_t end, nframes_t nframes,
456 assert (obufs.count().n_audio() == 2);
460 Sample* const src = srcbuf.data();
462 /* fetch positional data */
464 if (!_control->list()->curve().rt_safe_get_vector (start, end, buffers[0], nframes)) {
467 do_distribute (srcbuf, obufs, 1.0, nframes);
472 /* store effective pan position. do this even if we are muted */
475 effective_x = buffers[0][nframes-1];
482 /* apply pan law to convert positional data into pan coefficients for
486 const float pan_law_attenuation = -3.0f;
487 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
489 for (nframes_t n = 0; n < nframes; ++n) {
491 float const panR = buffers[0][n];
492 float const panL = 1 - panR;
494 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
495 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
500 dst = obufs.get_audio(0).data();
503 for (nframes_t n = 0; n < nframes; ++n) {
504 dst[n] += src[n] * pbuf[n];
507 /* XXX it would be nice to mark the buffer as written to */
511 dst = obufs.get_audio(1).data();
514 for (nframes_t n = 0; n < nframes; ++n) {
515 dst[n] += src[n] * pbuf[n];
518 /* XXX it would be nice to mark the buffer as written to */
522 EqualPowerStereoPanner::factory (Panner& parent, Evoral::Parameter param)
524 return new EqualPowerStereoPanner (parent, param);
528 EqualPowerStereoPanner::get_state (void)
534 EqualPowerStereoPanner::state (bool /*full_state*/)
536 XMLNode* root = new XMLNode ("StreamPanner");
538 LocaleGuard lg (X_("POSIX"));
540 snprintf (buf, sizeof (buf), "%.12g", _x);
541 root->add_property (X_("x"), buf);
542 root->add_property (X_("type"), EqualPowerStereoPanner::name);
544 // XXX: dont save automation here... its part of the automatable panner now.
546 StreamPanner::add_state (*root);
548 root->add_child_nocopy (_control->get_state ());
554 EqualPowerStereoPanner::set_state (const XMLNode& node, int version)
556 const XMLProperty* prop;
557 LocaleGuard lg (X_("POSIX"));
559 if ((prop = node.property (X_("x")))) {
560 const float pos = atof (prop->value().c_str());
561 set_position (pos, true);
564 StreamPanner::set_state (node, version);
566 for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
568 if ((*iter)->name() == X_("Controllable")) {
569 if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
570 _control->set_state (**iter, version);
573 } else if ((*iter)->name() == X_("Automation")) {
575 _control->alist()->set_state (*((*iter)->children().front()), version);
577 if (_control->alist()->automation_state() != Off) {
578 set_position (_control->list()->eval (parent.session().transport_frame()));
586 /*----------------------------------------------------------------------*/
588 Multi2dPanner::Multi2dPanner (Panner& p, Evoral::Parameter param)
589 : StreamPanner (p, param)
594 Multi2dPanner::~Multi2dPanner ()
599 Multi2dPanner::update ()
601 static const float BIAS = FLT_MIN;
603 uint32_t const nouts = parent.nouts ();
610 for (i = 0; i < nouts; i++) {
611 dsq[i] = ((_x - parent.output(i).x) * (_x - parent.output(i).x) + (_y - parent.output(i).y) * (_y - parent.output(i).y) + BIAS);
615 f += dsq[i] * dsq[i];
618 // terrible hack to support OSX < 10.3.9 builds
619 fr = (float) (1.0 / sqrt((double)f));
623 for (i = 0; i < nouts; ++i) {
624 parent.output(i).desired_pan = 1.0f - (dsq[i] * fr);
631 Multi2dPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
640 Sample* const src = srcbuf.data();
642 uint32_t const N = parent.nouts ();
643 for (uint32_t n = 0; n < N; ++n) {
644 Panner::Output& o = parent.output (n);
646 dst = obufs.get_audio(n).data();
649 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
651 /* interpolate over 64 frames or nframes, whichever is smaller */
653 nframes_t limit = min ((nframes_t)64, nframes);
656 delta = -(delta / (float) (limit));
658 for (n = 0; n < limit; n++) {
659 left_interp = left_interp + delta;
660 left = left_interp + 0.9 * (left - left_interp);
661 dst[n] += src[n] * left * gain_coeff;
664 pan = left * gain_coeff;
665 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
672 if ((pan *= gain_coeff) != 1.0f) {
675 mix_buffers_with_gain(dst,src,nframes,pan);
679 mix_buffers_no_gain(dst,src,nframes);
691 Multi2dPanner::do_distribute_automated (AudioBuffer& /*src*/, BufferSet& /*obufs*/,
692 nframes_t /*start*/, nframes_t /*end*/, nframes_t /*nframes*/,
705 Multi2dPanner::factory (Panner& p, Evoral::Parameter param)
707 return new Multi2dPanner (p, param);
711 Multi2dPanner::load (istream& /*in*/, string /*path*/, uint32_t& /*linecnt*/)
717 Multi2dPanner::get_state (void)
723 Multi2dPanner::state (bool /*full_state*/)
725 XMLNode* root = new XMLNode ("StreamPanner");
727 LocaleGuard lg (X_("POSIX"));
729 snprintf (buf, sizeof (buf), "%.12g", _x);
730 root->add_property (X_("x"), buf);
731 snprintf (buf, sizeof (buf), "%.12g", _y);
732 root->add_property (X_("y"), buf);
733 root->add_property (X_("type"), Multi2dPanner::name);
735 /* XXX no meaningful automation yet */
741 Multi2dPanner::set_state (const XMLNode& node, int /*version*/)
743 const XMLProperty* prop;
745 LocaleGuard lg (X_("POSIX"));
750 if ((prop = node.property (X_("x")))) {
751 newx = atof (prop->value().c_str());
754 if ((prop = node.property (X_("y")))) {
755 newy = atof (prop->value().c_str());
758 if (_x < 0 || _y < 0) {
759 error << _("badly-formed positional data for Multi2dPanner - ignored")
764 set_position (newx, newy);
768 /*---------------------------------------------------------------------- */
770 Panner::Panner (string name, Session& s)
771 : SessionObject (s, name)
774 //set_name_old_auto (name);
778 _link_direction = SameDirection;
788 Panner::set_linked (bool yn)
792 _session.set_dirty ();
793 LinkStateChanged (); /* EMIT SIGNAL */
798 Panner::set_link_direction (LinkDirection ld)
800 if (ld != _link_direction) {
801 _link_direction = ld;
802 _session.set_dirty ();
803 LinkStateChanged (); /* EMIT SIGNAL */
809 Panner::set_bypassed (bool yn)
811 if (yn != _bypassed) {
819 Panner::reset_to_default ()
821 vector<float> positions;
823 switch (outputs.size()) {
829 if (outputs.size() == 2) {
830 switch (_streampanners.size()) {
832 _streampanners.front()->set_position (0.5);
833 _streampanners.front()->pan_control()->list()->reset_default (0.5);
837 _streampanners.front()->set_position (0.0);
838 _streampanners.front()->pan_control()->list()->reset_default (0.0);
839 _streampanners.back()->set_position (1.0);
840 _streampanners.back()->pan_control()->list()->reset_default (1.0);
847 vector<Output>::iterator o;
848 vector<StreamPanner*>::iterator p;
850 for (o = outputs.begin(), p = _streampanners.begin(); o != outputs.end() && p != _streampanners.end(); ++o, ++p) {
851 (*p)->set_position ((*o).x, (*o).y);
856 Panner::reset_streampanner (uint32_t which)
858 if (which >= _streampanners.size() || which >= outputs.size()) {
862 switch (outputs.size()) {
868 switch (_streampanners.size()) {
870 /* stereo out, 1 stream, default = middle */
871 _streampanners.front()->set_position (0.5);
872 _streampanners.front()->pan_control()->list()->reset_default (0.5);
875 /* stereo out, 2 streams, default = hard left/right */
877 _streampanners.front()->set_position (0.0);
878 _streampanners.front()->pan_control()->list()->reset_default (0.0);
880 _streampanners.back()->set_position (1.0);
881 _streampanners.back()->pan_control()->list()->reset_default (1.0);
888 _streampanners[which]->set_position (outputs[which].x, outputs[which].y);
893 * Reset the panner with a given number of outs and panners (and hence inputs)
895 * \param nouts Number of outputs.
896 * \param npans Number of panners.
899 Panner::reset (uint32_t nouts, uint32_t npans)
902 bool changed = false;
903 bool do_not_and_did_not_need_panning = ((nouts < 2) && (outputs.size() < 2));
905 /* if new and old config don't need panning, or if
906 the config hasn't changed, we're done.
909 if (do_not_and_did_not_need_panning ||
910 ((nouts == outputs.size()) && (npans == _streampanners.size()))) {
914 n = _streampanners.size();
929 /* no need for panning with less than 2 outputs */
931 Changed (); /* EMIT SIGNAL */
938 /* XXX: this can never happen */
942 /* XXX: this can never happen */
943 fatal << _("programming error:")
944 << X_("Panner::reset() called with a single output")
950 outputs.push_back (Output (0, 0));
951 outputs.push_back (Output (1.0, 0));
952 for (n = 0; n < npans; ++n) {
953 _streampanners.push_back (new EqualPowerStereoPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
961 setup_speakers (nouts);
962 for (n = 0; n < npans; ++n) {
963 _streampanners.push_back (new VBAPanner (*this, Evoral::Parameter(PanAutomation, 0, n), _session.get_speakers()));
968 for (std::vector<StreamPanner*>::iterator x = _streampanners.begin(); x != _streampanners.end(); ++x) {
972 /* must emit Changed here, otherwise the changes to the pan_control below raise further
973 signals which the GUI is not prepared for until it has seen the Changed here.
977 Changed (); /* EMIT SIGNAL */
980 /* force hard left/right panning in a common case: 2in/2out
983 if (npans == 2 && outputs.size() == 2) {
985 /* Do this only if we changed configuration, or our configuration
986 appears to be the default set up (center).
992 _streampanners.front()->get_position (left);
993 _streampanners.back()->get_position (right);
995 if (changed || ((left == 0.5) && (right == 0.5))) {
997 _streampanners.front()->set_position (0.0);
998 _streampanners.front()->pan_control()->list()->reset_default (0.0);
1000 _streampanners.back()->set_position (1.0);
1001 _streampanners.back()->pan_control()->list()->reset_default (1.0);
1007 Panner::remove (uint32_t which)
1009 vector<StreamPanner*>::iterator i;
1010 for (i = _streampanners.begin(); i != _streampanners.end() && which; ++i, --which) {}
1012 if (i != _streampanners.end()) {
1014 _streampanners.erase (i);
1019 /** Remove all our StreamPanners */
1021 Panner::clear_panners ()
1023 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1027 _streampanners.clear ();
1031 Panner::set_automation_style (AutoStyle style)
1033 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1034 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_style (style);
1036 _session.set_dirty ();
1040 Panner::set_automation_state (AutoState state)
1042 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1043 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_state (state);
1045 _session.set_dirty ();
1049 Panner::automation_state () const
1051 boost::shared_ptr<AutomationList> l;
1053 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1055 l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1059 return l ? l->automation_state() : Off;
1063 Panner::automation_style () const
1065 boost::shared_ptr<AutomationList> l;
1067 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1069 l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1073 return l ? l->automation_style() : Absolute;
1079 StreamPanner* (*factory)(Panner&, Evoral::Parameter);
1082 PanPlugins pan_plugins[] = {
1083 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1084 { Multi2dPanner::name, 3, Multi2dPanner::factory },
1085 { string (""), 0, 0 }
1089 Panner::get_state (void)
1091 return state (true);
1095 Panner::state (bool full)
1097 XMLNode* node = new XMLNode ("Panner");
1101 node->add_property (X_("linked"), (_linked ? "yes" : "no"));
1102 node->add_property (X_("link_direction"), enum_2_string (_link_direction));
1103 node->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1105 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1106 XMLNode* onode = new XMLNode (X_("Output"));
1107 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1108 onode->add_property (X_("x"), buf);
1109 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1110 onode->add_property (X_("y"), buf);
1111 node->add_child_nocopy (*onode);
1114 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1115 node->add_child_nocopy ((*i)->state (full));
1118 node->add_child_nocopy (get_automation_xml_state ());
1124 Panner::set_state (const XMLNode& node, int version)
1127 XMLNodeConstIterator niter;
1128 const XMLProperty *prop;
1130 uint32_t num_panners = 0;
1132 LocaleGuard lg (X_("POSIX"));
1136 ChanCount ins = ChanCount::ZERO;
1137 ChanCount outs = ChanCount::ZERO;
1139 // XXX: this might not be necessary anymore
1142 if ((prop = node.property (X_("linked"))) != 0) {
1143 set_linked (string_is_affirmative (prop->value()));
1147 if ((prop = node.property (X_("bypassed"))) != 0) {
1148 set_bypassed (string_is_affirmative (prop->value()));
1151 if ((prop = node.property (X_("link_direction"))) != 0) {
1152 LinkDirection ld; /* here to provide type information */
1153 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1156 nlist = node.children();
1158 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1159 if ((*niter)->name() == X_("Output")) {
1163 prop = (*niter)->property (X_("x"));
1164 sscanf (prop->value().c_str(), "%g", &x);
1166 prop = (*niter)->property (X_("y"));
1167 sscanf (prop->value().c_str(), "%g", &y);
1169 outputs.push_back (Output (x, y));
1173 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1175 if ((*niter)->name() == X_("StreamPanner")) {
1177 if ((prop = (*niter)->property (X_("type")))) {
1179 for (i = 0; pan_plugins[i].factory; ++i) {
1180 if (prop->value() == pan_plugins[i].name) {
1183 /* note that we assume that all the stream panners
1184 are of the same type. pretty good
1185 assumption, but it's still an assumption.
1188 sp = pan_plugins[i].factory (*this, Evoral::Parameter(PanAutomation, 0, num_panners));
1191 if (sp->set_state (**niter, version) == 0) {
1192 _streampanners.push_back (sp);
1200 if (!pan_plugins[i].factory) {
1201 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1207 error << _("panner plugin node has no type information!")
1215 reset (outputs.size (), num_panners);
1216 /* don't try to do old-school automation loading if it wasn't marked as existing */
1218 if ((prop = node.property (X_("automation")))) {
1220 /* automation path is relative */
1222 automation_path = Glib::build_filename(_session.automation_dir(), prop->value ());
1225 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1226 if ((*niter)->name() == X_("Automation")) {
1227 set_automation_xml_state (**niter, Evoral::Parameter (PanAutomation));
1235 Panner::touching () const
1237 for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1238 if (((AutomationList*)(*i)->pan_control()->list().get())->touching ()) {
1247 Panner::set_position (float xpos, StreamPanner& orig)
1253 orig.get_position (xnow);
1254 xdelta = xpos - xnow;
1256 if (_link_direction == SameDirection) {
1258 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1260 (*i)->set_position (xpos, true);
1262 (*i)->get_position (xnow);
1263 xnew = min (1.0f, xnow + xdelta);
1264 xnew = max (0.0f, xnew);
1265 (*i)->set_position (xnew, true);
1271 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1273 (*i)->set_position (xpos, true);
1275 (*i)->get_position (xnow);
1276 xnew = min (1.0f, xnow - xdelta);
1277 xnew = max (0.0f, xnew);
1278 (*i)->set_position (xnew, true);
1285 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1288 float xdelta, ydelta;
1291 orig.get_position (xnow, ynow);
1292 xdelta = xpos - xnow;
1293 ydelta = ypos - ynow;
1295 if (_link_direction == SameDirection) {
1297 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1299 (*i)->set_position (xpos, ypos, true);
1301 (*i)->get_position (xnow, ynow);
1303 xnew = min (1.0f, xnow + xdelta);
1304 xnew = max (0.0f, xnew);
1306 ynew = min (1.0f, ynow + ydelta);
1307 ynew = max (0.0f, ynew);
1309 (*i)->set_position (xnew, ynew, true);
1315 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1317 (*i)->set_position (xpos, ypos, true);
1319 (*i)->get_position (xnow, ynow);
1321 xnew = min (1.0f, xnow - xdelta);
1322 xnew = max (0.0f, xnew);
1324 ynew = min (1.0f, ynow - ydelta);
1325 ynew = max (0.0f, ynew);
1327 (*i)->set_position (xnew, ynew, true);
1334 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1336 float xnow, ynow, znow;
1337 float xdelta, ydelta, zdelta;
1338 float xnew, ynew, znew;
1340 orig.get_position (xnow, ynow, znow);
1341 xdelta = xpos - xnow;
1342 ydelta = ypos - ynow;
1343 zdelta = zpos - znow;
1345 if (_link_direction == SameDirection) {
1347 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1349 (*i)->set_position (xpos, ypos, zpos, true);
1351 (*i)->get_position (xnow, ynow, znow);
1353 xnew = min (1.0f, xnow + xdelta);
1354 xnew = max (0.0f, xnew);
1356 ynew = min (1.0f, ynow + ydelta);
1357 ynew = max (0.0f, ynew);
1359 znew = min (1.0f, znow + zdelta);
1360 znew = max (0.0f, znew);
1362 (*i)->set_position (xnew, ynew, znew, true);
1368 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1370 (*i)->set_position (xpos, ypos, true);
1372 (*i)->get_position (xnow, ynow, znow);
1374 xnew = min (1.0f, xnow - xdelta);
1375 xnew = max (0.0f, xnew);
1377 ynew = min (1.0f, ynow - ydelta);
1378 ynew = max (0.0f, ynew);
1380 znew = min (1.0f, znow + zdelta);
1381 znew = max (0.0f, znew);
1383 (*i)->set_position (xnew, ynew, znew, true);
1390 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, gain_t gain_coeff)
1392 if (outbufs.count().n_audio() == 0) {
1393 // Don't want to lose audio...
1394 assert(inbufs.count().n_audio() == 0);
1398 // We shouldn't be called in the first place...
1399 assert(!bypassed());
1403 if (outbufs.count().n_audio() == 1) {
1405 AudioBuffer& dst = outbufs.get_audio(0);
1407 if (gain_coeff == 0.0f) {
1409 /* only one output, and gain was zero, so make it silent */
1411 dst.silence (nframes);
1413 } else if (gain_coeff == 1.0f){
1415 /* mix all buffers into the output */
1418 dst.read_from(inbufs.get_audio(0), nframes);
1420 // accumulate starting with the second
1421 if (inbufs.count().n_audio() > 0) {
1422 BufferSet::audio_iterator i = inbufs.audio_begin();
1423 for (++i; i != inbufs.audio_end(); ++i) {
1424 dst.merge_from(*i, nframes);
1430 /* mix all buffers into the output, scaling them all by the gain */
1433 dst.read_from(inbufs.get_audio(0), nframes);
1435 // accumulate (with gain) starting with the second
1436 if (inbufs.count().n_audio() > 0) {
1437 BufferSet::audio_iterator i = inbufs.audio_begin();
1438 for (++i; i != inbufs.audio_end(); ++i) {
1439 dst.accumulate_with_gain_from(*i, nframes, gain_coeff);
1448 /* the terrible silence ... */
1449 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1450 i->silence(nframes);
1453 BufferSet::audio_iterator i = inbufs.audio_begin();
1455 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end() && i != inbufs.audio_end(); ++pan, ++i) {
1456 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1461 Panner::run (BufferSet& inbufs, BufferSet& outbufs, framepos_t start_frame, framepos_t end_frame, nframes_t nframes)
1463 if (outbufs.count().n_audio() == 0) {
1464 // Failing to deliver audio we were asked to deliver is a bug
1465 assert(inbufs.count().n_audio() == 0);
1469 // We shouldn't be called in the first place...
1470 assert(!bypassed());
1473 // If we shouldn't play automation defer to distribute_no_automation
1474 if (!(automation_state() & Play || ((automation_state() & Touch) && !touching()))) {
1477 gain_t gain_coeff = 1.0;
1479 if (fabsf(_session.transport_speed()) > 1.5f && Config->get_quieten_at_speed ()) {
1480 gain_coeff = speed_quietning;
1483 distribute_no_automation (inbufs, outbufs, nframes, gain_coeff);
1487 // Otherwise.. let the automation flow, baby
1489 if (outbufs.count().n_audio() == 1) {
1491 AudioBuffer& dst = outbufs.get_audio(0);
1493 // FIXME: apply gain automation?
1496 dst.read_from(inbufs.get_audio(0), nframes);
1498 // accumulate starting with the second
1499 BufferSet::audio_iterator i = inbufs.audio_begin();
1500 for (++i; i != inbufs.audio_end(); ++i) {
1501 dst.merge_from(*i, nframes);
1507 // More than 1 output, we should have 1 panner for each input
1508 //assert(_streampanners.size() == inbufs.count().n_audio());
1510 /* the terrible silence ... */
1511 for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1512 i->silence(nframes);
1515 BufferSet::audio_iterator i = inbufs.audio_begin();
1516 for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end(); ++pan, ++i) {
1517 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
1521 /* old school automation handling */
1525 Panner::set_name (string str)
1527 automation_path = Glib::build_filename(_session.automation_dir(),
1528 _session.snap_name() + "-pan-" + legalize_for_path (str) + ".automation");
1536 uint32_t linecnt = 0;
1538 vector<StreamPanner*>::iterator sp;
1539 LocaleGuard lg (X_("POSIX"));
1541 if (automation_path.length() == 0) {
1545 if (access (automation_path.c_str(), F_OK)) {
1549 ifstream in (automation_path.c_str());
1552 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1553 automation_path, strerror (errno))
1558 sp = _streampanners.begin();
1560 while (in.getline (line, sizeof(line), '\n')) {
1562 if (++linecnt == 1) {
1563 if (memcmp (line, X_("version"), 7) == 0) {
1564 if (sscanf (line, "version %f", &version) != 1) {
1565 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1569 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1570 automation_path, line) << endmsg;
1577 if (strlen (line) == 0 || line[0] == '#') {
1581 if (strcmp (line, "begin") == 0) {
1583 if (sp == _streampanners.end()) {
1584 error << string_compose (_("too many panner states found in pan automation file %1"),
1590 if ((*sp)->load (in, automation_path, linecnt)) {
1602 Panner::set_mono (bool yn)
1609 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1610 (*i)->set_mono (yn);
1615 Panner::value_as_string (double v)
1617 if (Panner::equivalent (v, 0.5)) {
1619 } else if (equivalent (v, 0)) {
1621 } else if (equivalent (v, 1)) {
1623 } else if (v < 0.5) {
1625 s << fixed << setprecision (0) << _("L") << ((0.5 - v) * 200) << "%";
1629 s << fixed << setprecision (0) << _("R") << ((v -0.5) * 200) << "%";
1637 Panner::setup_speakers (uint32_t nouts)
1641 outputs.push_back (Output (0.5, 0));
1642 outputs.push_back (Output (0, 1.0));
1643 outputs.push_back (Output (1.0, 1.0));
1646 /* clockwise from top left */
1647 outputs.push_back (Output (0, 1.0));
1648 outputs.push_back (Output (1.0, 1.0));
1649 outputs.push_back (Output (1.0, 0));
1650 outputs.push_back (Output (0, 0));
1653 case 5: //square+offcenter center
1654 outputs.push_back (Output (0, 0));
1655 outputs.push_back (Output (1.0, 0));
1656 outputs.push_back (Output (1.0, 1.0));
1657 outputs.push_back (Output (0, 1.0));
1658 outputs.push_back (Output (0.5, 0.75));
1662 /* XXX horrible placement. FIXME */
1663 for (uint32_t n = 0; n < nouts; ++n) {
1664 outputs.push_back (Output (0.1 * n, 0.1 * n));
1668 VBAPSpeakers& speakers (_session.get_speakers());
1670 speakers.clear_speakers ();
1672 for (vector<Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1679 tz = 0.0; // XXX change this if we ever do actual 3D
1681 cart_to_azi_ele (tx, ty, tz, azimuth, elevation);
1682 speakers.add_speaker (azimuth, elevation);