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.
33 #include <pbd/error.h>
34 #include <pbd/failed_constructor.h>
35 #include <pbd/xml++.h>
37 #include <ardour/session.h>
38 #include <ardour/panner.h>
39 #include <ardour/utils.h>
41 #include <ardour/mix.h>
45 #include <pbd/mathfix.h>
48 using namespace ARDOUR;
51 float Panner::current_automation_version_number = 1.0;
53 string EqualPowerStereoPanner::name = "Equal Power Stereo";
54 string Multi2dPanner::name = "Multiple (2D)";
56 /* this is a default mapper of MIDI control values to a pan position
57 others can be imagined. see Panner::set_midi_to_pan_function().
60 static pan_t direct_midi_to_pan (double fract) {
64 static double direct_pan_to_midi (pan_t val) {
68 StreamPanner::StreamPanner (Panner& p)
70 _midi_control (*this, (MIDI::Port*) 0)
79 StreamPanner::~StreamPanner ()
83 StreamPanner::MIDIControl::MIDIControl (StreamPanner& s, MIDI::Port* port)
84 : MIDI::Controllable (port, 0), sp (s), setting(false)
86 midi_to_pan = direct_midi_to_pan;
87 pan_to_midi = direct_pan_to_midi;
88 last_written = 0; /* XXX need a good out-of-bound-value */
92 StreamPanner::MIDIControl::set_value (float val)
95 sp.set_position (midi_to_pan (val));
100 StreamPanner::MIDIControl::send_feedback (pan_t value)
103 if (!setting && get_midi_feedback() && pan_to_midi) {
104 MIDI::byte val = (MIDI::byte) (pan_to_midi (value) * 127.0f);
105 MIDI::channel_t ch = 0;
106 MIDI::eventType ev = MIDI::none;
107 MIDI::byte additional = 0;
108 MIDI::EventTwoBytes data;
110 if (get_control_info (ch, ev, additional)) {
111 data.controller_number = additional;
115 sp.get_parent().session().send_midi_message (get_port(), ev, ch, data);
118 // send_midi_feedback (pan_to_midi (val));
124 StreamPanner::MIDIControl::write_feedback (MIDI::byte* buf, int32_t& bufsize, pan_t val, bool force)
126 if (get_midi_feedback() && pan_to_midi && bufsize > 2) {
127 MIDI::channel_t ch = 0;
128 MIDI::eventType ev = MIDI::none;
129 MIDI::byte additional = 0;
131 if (get_control_info (ch, ev, additional)) {
133 pm = (MIDI::byte) (pan_to_midi (val) * 127.0);
135 if (pm != last_written || force) {
136 *buf++ = (0xF0 & ev) | (0xF & ch);
137 *buf++ = additional; /* controller number */
150 StreamPanner::reset_midi_control (MIDI::Port* port, bool on)
156 _midi_control.get_control_info (chn, ev, extra);
160 _midi_control.midi_rebind (port, chn);
164 StreamPanner::set_muted (bool yn)
173 StreamPanner::set_position (float xpos, bool link_call)
175 if (!link_call && parent.linked()) {
176 parent.set_position (xpos, *this);
184 if (parent.session().get_midi_feedback()) {
185 _midi_control.send_feedback (x);
191 StreamPanner::set_position (float xpos, float ypos, bool link_call)
193 if (!link_call && parent.linked()) {
194 parent.set_position (xpos, ypos, *this);
197 if (x != xpos || y != ypos) {
207 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
209 if (!link_call && parent.linked()) {
210 parent.set_position (xpos, ypos, zpos, *this);
213 if (x != xpos || y != ypos || z != zpos) {
223 StreamPanner::set_state (const XMLNode& node)
225 const XMLProperty* prop;
226 XMLNodeConstIterator iter;
227 XMLNodeList midi_kids;
229 if ((prop = node.property (X_("muted")))) {
230 set_muted (prop->value() == "yes");
233 midi_kids = node.children ("MIDI");
235 for (iter = midi_kids.begin(); iter != midi_kids.end(); ++iter) {
238 XMLNodeConstIterator miter;
241 kids = (*iter)->children ();
243 for (miter = kids.begin(); miter != kids.end(); ++miter) {
247 if (child->name() == "pan") {
249 MIDI::eventType ev = MIDI::on; /* initialize to keep gcc happy */
250 MIDI::byte additional = 0; /* ditto */
251 MIDI::channel_t chn = 0; /* ditto */
253 if (get_midi_node_info (child, ev, chn, additional)) {
254 _midi_control.set_control_type (chn, ev, additional);
256 error << _("MIDI pan control specification is incomplete, so it has been ignored") << endmsg;
267 StreamPanner::add_state (XMLNode& node)
269 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
275 MIDI::byte additional;
276 XMLNode* midi_node = 0;
279 if (_midi_control.get_control_info (chn, ev, additional)) {
281 midi_node = node.add_child ("MIDI");
283 child = midi_node->add_child ("pan");
284 set_midi_node_info (child, ev, chn, additional);
291 StreamPanner::get_midi_node_info (XMLNode * node, MIDI::eventType & ev, MIDI::channel_t & chan, MIDI::byte & additional)
294 const XMLProperty* prop;
297 if ((prop = node->property ("event")) != 0) {
298 sscanf (prop->value().c_str(), "0x%x", &xx);
299 ev = (MIDI::eventType) xx;
304 if (ok && ((prop = node->property ("channel")) != 0)) {
305 sscanf (prop->value().c_str(), "%d", &xx);
306 chan = (MIDI::channel_t) xx;
311 if (ok && ((prop = node->property ("additional")) != 0)) {
312 sscanf (prop->value().c_str(), "0x%x", &xx);
313 additional = (MIDI::byte) xx;
320 StreamPanner::set_midi_node_info (XMLNode * node, MIDI::eventType ev, MIDI::channel_t chan, MIDI::byte additional)
324 snprintf (buf, sizeof(buf), "0x%x", ev);
325 node->add_property ("event", buf);
326 snprintf (buf, sizeof(buf), "%d", chan);
327 node->add_property ("channel", buf);
328 snprintf (buf, sizeof(buf), "0x%x", additional);
329 node->add_property ("additional", buf);
334 /*---------------------------------------------------------------------- */
336 BaseStereoPanner::BaseStereoPanner (Panner& p)
337 : StreamPanner (p), _automation (0.0, 1.0, 0.5)
341 BaseStereoPanner::~BaseStereoPanner ()
346 BaseStereoPanner::snapshot (jack_nframes_t now)
348 if (_automation.automation_state() == Write || _automation.automation_state() == Touch) {
349 _automation.rt_add (now, x);
354 BaseStereoPanner::transport_stopped (jack_nframes_t frame)
356 _automation.reposition_for_rt_add (frame);
358 if (_automation.automation_state() != Off) {
360 if (_automation.automation_write()) {
361 _automation.save_state (_("automation write pass"));
364 set_position (_automation.eval (frame));
369 BaseStereoPanner::set_automation_style (AutoStyle style)
371 _automation.set_automation_style (style);
375 BaseStereoPanner::set_automation_state (AutoState state)
377 if (state != _automation.automation_state()) {
379 _automation.set_automation_state (state);
382 set_position (_automation.eval (parent.session().transport_frame()));
388 BaseStereoPanner::save (ostream& out) const
390 LocaleGuard lg (X_("POSIX"));
392 /* force a single format for numeric data to ease session interchange
393 across national boundaries.
396 out << "begin" << endl;
398 for (AutomationList::const_iterator i = _automation.const_begin(); i != _automation.const_end(); ++i) {
399 out << '\t' << (jack_nframes_t) floor ((*i)->when) << ' ' << (*i)->value << endl;
401 error << string_compose (_("error writing pan automation file (%s)"), strerror (errno)) << endmsg;
405 out << "end" << endl;
411 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
414 LocaleGuard lg (X_("POSIX"));
416 _automation.clear ();
418 while (in.getline (line, sizeof (line), '\n')) {
424 if (strcmp (line, "end") == 0) {
428 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
429 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
433 _automation.add (when, value, true);
436 /* now that we are done loading */
438 _automation.save_state (_("loaded from disk"));
439 _automation.StateChanged (Change (0));
445 BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, jack_nframes_t nframes)
459 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
461 /* interpolate over 64 frames or nframes, whichever is smaller */
463 jack_nframes_t limit = min ((jack_nframes_t)64, nframes);
466 delta = -(delta / (float) (limit));
468 for (n = 0; n < limit; n++) {
469 left_interp = left_interp + delta;
470 left = left_interp + 0.9 * (left - left_interp);
471 dst[n] += src[n] * left * gain_coeff;
474 pan = left * gain_coeff;
476 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
483 if ((pan = (left * gain_coeff)) != 1.0f) {
487 Session::mix_buffers_with_gain(dst,src,nframes,pan);
489 /* mark that we wrote into the buffer */
497 Session::mix_buffers_no_gain(dst,src,nframes);
499 /* mark that we wrote into the buffer */
509 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
511 /* interpolate over 64 frames or nframes, whichever is smaller */
513 jack_nframes_t limit = min ((jack_nframes_t)64, nframes);
516 delta = -(delta / (float) (limit));
518 for (n = 0; n < limit; n++) {
519 right_interp = right_interp + delta;
520 right = right_interp + 0.9 * (right - right_interp);
521 dst[n] += src[n] * right * gain_coeff;
524 pan = right * gain_coeff;
526 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
528 /* XXX it would be nice to mark the buffer as written to */
532 right = desired_right;
533 right_interp = right;
535 if ((pan = (right * gain_coeff)) != 1.0f) {
539 Session::mix_buffers_with_gain(dst,src,nframes,pan);
541 /* XXX it would be nice to mark the buffer as written to */
546 Session::mix_buffers_no_gain(dst,src,nframes);
548 /* XXX it would be nice to mark the buffer as written to */
553 /*---------------------------------------------------------------------- */
555 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p)
556 : BaseStereoPanner (p)
561 right = desired_right;
563 right_interp = right;
566 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
571 EqualPowerStereoPanner::update ()
573 /* it would be very nice to split this out into a virtual function
574 that can be accessed from BaseStereoPanner and used in distribute_automated().
576 but the place where its used in distribute_automated() is a tight inner loop,
577 and making "nframes" virtual function calls to compute values is an absurd
581 /* x == 0 => hard left
586 float panL = 1 - panR;
588 const float pan_law_attenuation = -3.0f;
589 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
591 desired_left = panL * (scale * panL + 1.0f - scale);
592 desired_right = panR * (scale * panR + 1.0f - scale);
598 EqualPowerStereoPanner::distribute_automated (Sample* src, Sample** obufs,
599 jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes,
605 /* fetch positional data */
607 if (!_automation.rt_safe_get_vector (start, end, buffers[0], nframes)) {
610 distribute (src, obufs, 1.0, nframes);
615 /* store effective pan position. do this even if we are muted */
618 effective_x = buffers[0][nframes-1];
624 /* apply pan law to convert positional data into pan coefficients for
628 const float pan_law_attenuation = -3.0f;
629 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
631 for (jack_nframes_t n = 0; n < nframes; ++n) {
633 float panR = buffers[0][n];
634 float panL = 1 - panR;
636 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
637 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
645 for (jack_nframes_t n = 0; n < nframes; ++n) {
646 dst[n] += src[n] * pbuf[n];
649 /* XXX it would be nice to mark the buffer as written to */
656 for (jack_nframes_t n = 0; n < nframes; ++n) {
657 dst[n] += src[n] * pbuf[n];
660 /* XXX it would be nice to mark the buffer as written to */
664 EqualPowerStereoPanner::factory (Panner& parent)
666 return new EqualPowerStereoPanner (parent);
670 EqualPowerStereoPanner::get_state (void)
676 EqualPowerStereoPanner::state (bool full_state)
678 XMLNode* root = new XMLNode ("StreamPanner");
680 LocaleGuard lg (X_("POSIX"));
682 snprintf (buf, sizeof (buf), "%f", x);
683 root->add_property (X_("x"), buf);
684 root->add_property (X_("type"), EqualPowerStereoPanner::name);
686 snprintf (buf, sizeof (buf), "0x%x", _automation.automation_state());
688 /* never store automation states other than off in a template */
689 snprintf (buf, sizeof (buf), "0x%x", ARDOUR::Off);
691 root->add_property (X_("automation-state"), buf);
692 snprintf (buf, sizeof (buf), "0x%x", _automation.automation_style());
693 root->add_property (X_("automation-style"), buf);
695 StreamPanner::add_state (*root);
701 EqualPowerStereoPanner::set_state (const XMLNode& node)
703 const XMLProperty* prop;
706 LocaleGuard lg (X_("POSIX"));
708 if ((prop = node.property (X_("x")))) {
709 pos = atof (prop->value().c_str());
710 set_position (pos, true);
713 if ((prop = node.property (X_("automation-state")))) {
714 sscanf (prop->value().c_str(), "0x%x", &x);
715 _automation.set_automation_state ((AutoState) x);
718 set_position (_automation.eval (parent.session().transport_frame()));
722 if ((prop = node.property (X_("automation-style")))) {
723 sscanf (prop->value().c_str(), "0x%x", &x);
724 _automation.set_automation_style ((AutoStyle) x);
727 StreamPanner::set_state (node);
732 /*----------------------------------------------------------------------*/
734 Multi2dPanner::Multi2dPanner (Panner& p)
735 : StreamPanner (p), _automation (0.0, 1.0, 0.5) // XXX useless
740 Multi2dPanner::~Multi2dPanner ()
745 Multi2dPanner::snapshot (jack_nframes_t now)
751 Multi2dPanner::transport_stopped (jack_nframes_t frame)
757 Multi2dPanner::set_automation_style (AutoStyle style)
763 Multi2dPanner::set_automation_state (AutoState state)
769 Multi2dPanner::update ()
771 static const float BIAS = FLT_MIN;
773 uint32_t nouts = parent.outputs.size();
780 for (i = 0; i < nouts; i++) {
781 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
785 f += dsq[i] * dsq[i];
788 // terrible hack to support OSX < 10.3.9 builds
789 fr = (float) (1.0 / sqrt((double)f));
793 for (i = 0; i < nouts; ++i) {
794 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
801 Multi2dPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, jack_nframes_t nframes)
805 vector<Panner::Output>::iterator o;
813 for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
818 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
820 /* interpolate over 64 frames or nframes, whichever is smaller */
822 jack_nframes_t limit = min ((jack_nframes_t)64, nframes);
825 delta = -(delta / (float) (limit));
827 for (n = 0; n < limit; n++) {
828 left_interp = left_interp + delta;
829 left = left_interp + 0.9 * (left - left_interp);
830 dst[n] += src[n] * left * gain_coeff;
833 pan = left * gain_coeff;
835 for (; n < nframes; ++n) {
836 dst[n] += src[n] * pan;
842 pan = (*o).desired_pan;
844 if ((pan *= gain_coeff) != 1.0f) {
848 for (jack_nframes_t n = 0; n < nframes; ++n) {
849 dst[n] += src[n] * pan;
857 for (jack_nframes_t n = 0; n < nframes; ++n) {
872 Multi2dPanner::distribute_automated (Sample* src, Sample** obufs,
873 jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes,
886 Multi2dPanner::factory (Panner& p)
888 return new Multi2dPanner (p);
892 Multi2dPanner::load (istream& in, string path, uint32_t& linecnt)
898 Multi2dPanner::save (ostream& out) const
904 Multi2dPanner::get_state (void)
910 Multi2dPanner::state (bool full_state)
912 XMLNode* root = new XMLNode ("StreamPanner");
914 LocaleGuard lg (X_("POSIX"));
916 snprintf (buf, sizeof (buf), "%f", x);
917 root->add_property (X_("x"), buf);
918 snprintf (buf, sizeof (buf), "%f", y);
919 root->add_property (X_("y"), buf);
920 root->add_property (X_("type"), Multi2dPanner::name);
926 Multi2dPanner::set_state (const XMLNode& node)
928 const XMLProperty* prop;
930 LocaleGuard lg (X_("POSIX"));
935 if ((prop = node.property (X_("x")))) {
936 newx = atof (prop->value().c_str());
939 if ((prop = node.property (X_("y")))) {
940 newy = atof (prop->value().c_str());
943 if (x < 0 || y < 0) {
944 error << _("badly-formed positional data for Multi2dPanner - ignored")
949 set_position (newx, newy);
953 /*---------------------------------------------------------------------- */
955 Panner::Panner (string name, Session& s)
960 _link_direction = SameDirection;
963 reset_midi_control (_session.mmc_port(), _session.get_mmc_control());
971 Panner::set_linked (bool yn)
975 _session.set_dirty ();
976 LinkStateChanged (); /* EMIT SIGNAL */
981 Panner::set_link_direction (LinkDirection ld)
983 if (ld != _link_direction) {
984 _link_direction = ld;
985 _session.set_dirty ();
986 LinkStateChanged (); /* EMIT SIGNAL */
991 Panner::set_name (string str)
993 automation_path = _session.automation_dir();
994 automation_path += _session.snap_name();
995 automation_path += "-pan-";
996 automation_path += legalize_for_path (str);
997 automation_path += ".automation";
1002 Panner::set_bypassed (bool yn)
1004 if (yn != _bypassed) {
1012 Panner::reset (uint32_t nouts, uint32_t npans)
1015 bool changed = false;
1018 if (nouts < 2 || (nouts == outputs.size() && npans == size())) {
1041 fatal << _("programming error:")
1042 << X_("Panner::reset() called with a single output")
1049 outputs.push_back (Output (0, 0));
1050 outputs.push_back (Output (1.0, 0));
1052 for (n = 0; n < npans; ++n) {
1053 push_back (new EqualPowerStereoPanner (*this));
1058 outputs.push_back (Output (0.5, 0));
1059 outputs.push_back (Output (0, 1.0));
1060 outputs.push_back (Output (1.0, 1.0));
1062 for (n = 0; n < npans; ++n) {
1063 push_back (new Multi2dPanner (*this));
1069 outputs.push_back (Output (0, 0));
1070 outputs.push_back (Output (1.0, 0));
1071 outputs.push_back (Output (1.0, 1.0));
1072 outputs.push_back (Output (0, 1.0));
1074 for (n = 0; n < npans; ++n) {
1075 push_back (new Multi2dPanner (*this));
1080 case 5: //square+offcenter center
1081 outputs.push_back (Output (0, 0));
1082 outputs.push_back (Output (1.0, 0));
1083 outputs.push_back (Output (1.0, 1.0));
1084 outputs.push_back (Output (0, 1.0));
1085 outputs.push_back (Output (0.5, 0.75));
1087 for (n = 0; n < npans; ++n) {
1088 push_back (new Multi2dPanner (*this));
1094 /* XXX horrible placement. FIXME */
1095 for (n = 0; n < nouts; ++n) {
1096 outputs.push_back (Output (0.1 * n, 0.1 * n));
1099 for (n = 0; n < npans; ++n) {
1100 push_back (new Multi2dPanner (*this));
1106 for (iterator x = begin(); x != end(); ++x) {
1110 reset_midi_control (_session.mmc_port(), _session.get_mmc_control());
1112 /* force hard left/right panning in a common case: 2in/2out
1115 if (npans == 2 && outputs.size() == 2) {
1117 /* Do this only if we changed configuration, or our configuration
1118 appears to be the default set up (center).
1124 front()->get_position (left);
1125 back()->get_position (right);
1127 if (changed || ((left == 0.5) && (right == 0.5))) {
1129 front()->set_position (0.0);
1130 front()->automation().reset_default (0.0);
1132 back()->set_position (1.0);
1133 back()->automation().reset_default (1.0);
1140 Changed (); /* EMIT SIGNAL */
1147 Panner::remove (uint32_t which)
1149 vector<StreamPanner*>::iterator i;
1150 for (i = begin(); i != end() && which; ++i, --which);
1161 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1165 vector<StreamPanner*>::clear ();
1169 Panner::set_automation_style (AutoStyle style)
1171 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1172 (*i)->set_automation_style (style);
1174 _session.set_dirty ();
1178 Panner::set_automation_state (AutoState state)
1180 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1181 (*i)->set_automation_state (state);
1183 _session.set_dirty ();
1187 Panner::automation_state () const
1190 return front()->automation().automation_state ();
1197 Panner::automation_style () const
1200 return front()->automation().automation_style ();
1207 Panner::transport_stopped (jack_nframes_t frame)
1209 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1210 (*i)->transport_stopped (frame);
1215 Panner::snapshot (jack_nframes_t now)
1217 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1218 (*i)->snapshot (now);
1223 Panner::clear_automation ()
1225 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1226 (*i)->automation().clear ();
1228 _session.set_dirty ();
1232 Panner::save () const
1234 ofstream out (automation_path.c_str());
1237 error << string_compose (_("cannot open pan automation file \"%1\" for saving (%s)"), automation_path, strerror (errno))
1242 out << X_("version ") << current_automation_version_number << endl;
1244 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1245 if ((*i)->save (out)) {
1257 uint32_t linecnt = 0;
1260 LocaleGuard lg (X_("POSIX"));
1262 if (automation_path.length() == 0) {
1266 if (access (automation_path.c_str(), F_OK)) {
1270 ifstream in (automation_path.c_str());
1273 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1274 automation_path, strerror (errno))
1281 while (in.getline (line, sizeof(line), '\n')) {
1283 if (++linecnt == 1) {
1284 if (memcmp (line, X_("version"), 7) == 0) {
1285 if (sscanf (line, "version %f", &version) != 1) {
1286 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1290 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1291 automation_path, line) << endmsg;
1295 if (version != current_automation_version_number) {
1296 error << string_compose(_("mismatched pan automation event file version (%1)"), version) << endmsg;
1303 if (strlen (line) == 0 || line[0] == '#') {
1307 if (strcmp (line, "begin") == 0) {
1310 error << string_compose (_("too many panner states found in pan automation file %1"),
1316 if ((*sp)->load (in, automation_path, linecnt)) {
1330 StreamPanner* (*factory)(Panner&);
1333 PanPlugins pan_plugins[] = {
1334 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1335 { Multi2dPanner::name, 3, Multi2dPanner::factory },
1340 Panner::get_state (void)
1342 return state (true);
1346 Panner::state (bool full)
1348 XMLNode* root = new XMLNode (X_("Panner"));
1351 for (iterator p = begin(); p != end(); ++p) {
1352 root->add_child_nocopy ((*p)->state (full));
1355 root->add_property (X_("linked"), (_linked ? "yes" : "no"));
1356 snprintf (buf, sizeof (buf), "%d", _link_direction);
1357 root->add_property (X_("link_direction"), buf);
1358 root->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1360 /* add each output */
1362 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1363 XMLNode* onode = new XMLNode (X_("Output"));
1364 snprintf (buf, sizeof (buf), "%f", (*o).x);
1365 onode->add_property (X_("x"), buf);
1366 snprintf (buf, sizeof (buf), "%f", (*o).y);
1367 onode->add_property (X_("y"), buf);
1368 root->add_child_nocopy (*onode);
1373 root->add_property (X_("automation"), Glib::path_get_basename (automation_path));
1381 Panner::set_state (const XMLNode& node)
1384 XMLNodeConstIterator niter;
1385 const XMLProperty *prop;
1388 LocaleGuard lg (X_("POSIX"));
1393 if ((prop = node.property (X_("linked"))) != 0) {
1394 set_linked (prop->value() == "yes");
1398 if ((prop = node.property (X_("bypassed"))) != 0) {
1399 set_bypassed (prop->value() == "yes");
1402 if ((prop = node.property (X_("link_direction"))) != 0) {
1403 sscanf (prop->value().c_str(), "%d", &i);
1404 set_link_direction ((LinkDirection) (i));
1407 nlist = node.children();
1409 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1410 if ((*niter)->name() == X_("Output")) {
1414 prop = (*niter)->property (X_("x"));
1415 sscanf (prop->value().c_str(), "%f", &x);
1417 prop = (*niter)->property (X_("y"));
1418 sscanf (prop->value().c_str(), "%f", &y);
1420 outputs.push_back (Output (x, y));
1424 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1426 if ((*niter)->name() == X_("StreamPanner")) {
1428 if ((prop = (*niter)->property (X_("type")))) {
1430 for (i = 0; pan_plugins[i].factory; ++i) {
1431 if (prop->value() == pan_plugins[i].name) {
1434 /* note that we assume that all the stream panners
1435 are of the same type. pretty good
1436 assumption, but its still an assumption.
1439 sp = pan_plugins[i].factory (*this);
1441 if (sp->set_state (**niter) == 0) {
1450 if (!pan_plugins[i].factory) {
1451 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1457 error << _("panner plugin node has no type information!")
1465 /* don't try to load automation if it wasn't marked as existing */
1467 if ((prop = node.property (X_("automation")))) {
1469 /* automation path is relative */
1471 automation_path = _session.automation_dir();
1472 automation_path += prop->value ();
1481 Panner::touching () const
1483 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1484 if ((*i)->automation().touching ()) {
1493 Panner::reset_midi_control (MIDI::Port* port, bool on)
1495 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1496 (*i)->reset_midi_control (port, on);
1501 Panner::set_position (float xpos, StreamPanner& orig)
1507 orig.get_position (xnow);
1508 xdelta = xpos - xnow;
1510 if (_link_direction == SameDirection) {
1512 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1514 (*i)->set_position (xpos, true);
1516 (*i)->get_position (xnow);
1517 xnew = min (1.0f, xnow + xdelta);
1518 xnew = max (0.0f, xnew);
1519 (*i)->set_position (xnew, true);
1525 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1527 (*i)->set_position (xpos, true);
1529 (*i)->get_position (xnow);
1530 xnew = min (1.0f, xnow - xdelta);
1531 xnew = max (0.0f, xnew);
1532 (*i)->set_position (xnew, true);
1539 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1542 float xdelta, ydelta;
1545 orig.get_position (xnow, ynow);
1546 xdelta = xpos - xnow;
1547 ydelta = ypos - ynow;
1549 if (_link_direction == SameDirection) {
1551 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1553 (*i)->set_position (xpos, ypos, true);
1555 (*i)->get_position (xnow, ynow);
1557 xnew = min (1.0f, xnow + xdelta);
1558 xnew = max (0.0f, xnew);
1560 ynew = min (1.0f, ynow + ydelta);
1561 ynew = max (0.0f, ynew);
1563 (*i)->set_position (xnew, ynew, true);
1569 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1571 (*i)->set_position (xpos, ypos, true);
1573 (*i)->get_position (xnow, ynow);
1575 xnew = min (1.0f, xnow - xdelta);
1576 xnew = max (0.0f, xnew);
1578 ynew = min (1.0f, ynow - ydelta);
1579 ynew = max (0.0f, ynew);
1581 (*i)->set_position (xnew, ynew, true);
1588 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1590 float xnow, ynow, znow;
1591 float xdelta, ydelta, zdelta;
1592 float xnew, ynew, znew;
1594 orig.get_position (xnow, ynow, znow);
1595 xdelta = xpos - xnow;
1596 ydelta = ypos - ynow;
1597 zdelta = zpos - znow;
1599 if (_link_direction == SameDirection) {
1601 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1603 (*i)->set_position (xpos, ypos, zpos, true);
1605 (*i)->get_position (xnow, ynow, znow);
1607 xnew = min (1.0f, xnow + xdelta);
1608 xnew = max (0.0f, xnew);
1610 ynew = min (1.0f, ynow + ydelta);
1611 ynew = max (0.0f, ynew);
1613 znew = min (1.0f, znow + zdelta);
1614 znew = max (0.0f, znew);
1616 (*i)->set_position (xnew, ynew, znew, true);
1622 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1624 (*i)->set_position (xpos, ypos, true);
1626 (*i)->get_position (xnow, ynow, znow);
1628 xnew = min (1.0f, xnow - xdelta);
1629 xnew = max (0.0f, xnew);
1631 ynew = min (1.0f, ynow - ydelta);
1632 ynew = max (0.0f, ynew);
1634 znew = min (1.0f, znow + zdelta);
1635 znew = max (0.0f, znew);
1637 (*i)->set_position (xnew, ynew, znew, true);
1644 Panner::send_all_midi_feedback ()
1646 if (_session.get_midi_feedback()) {
1649 // do feedback for all panners
1650 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1651 (*i)->get_effective_position (xpos);
1653 (*i)->midi_control().send_feedback (xpos);
1660 Panner::write_midi_feedback (MIDI::byte* buf, int32_t& bufsize)
1662 AutoState astate = automation_state ();
1664 if (_session.get_midi_feedback() &&
1665 (astate == Play || (astate == Touch && !touching()))) {
1669 // do feedback for all panners
1670 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1671 (*i)->get_effective_position (xpos);
1673 buf = (*i)->midi_control().write_feedback (buf, bufsize, xpos);