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.
31 #include <pbd/error.h>
32 #include <pbd/failed_constructor.h>
33 #include <pbd/basename.h>
34 #include <pbd/xml++.h>
36 #include <ardour/session.h>
37 #include <ardour/panner.h>
38 #include <ardour/utils.h>
40 #include <ardour/mix.h>
44 #include <pbd/mathfix.h>
48 using namespace ARDOUR;
50 float Panner::current_automation_version_number = 1.0;
52 string EqualPowerStereoPanner::name = "Equal Power Stereo";
53 string Multi2dPanner::name = "Multiple (2D)";
55 /* this is a default mapper of MIDI control values to a pan position
56 others can be imagined. see Panner::set_midi_to_pan_function().
59 static pan_t direct_midi_to_pan (double fract) {
63 static double direct_pan_to_midi (pan_t val) {
67 StreamPanner::StreamPanner (Panner& p)
69 _midi_control (*this, (MIDI::Port*) 0)
78 StreamPanner::~StreamPanner ()
82 StreamPanner::MIDIControl::MIDIControl (StreamPanner& s, MIDI::Port* port)
83 : MIDI::Controllable (port, 0), sp (s), setting(false)
85 midi_to_pan = direct_midi_to_pan;
86 pan_to_midi = direct_pan_to_midi;
87 last_written = 0; /* XXX need a good out-of-bound-value */
91 StreamPanner::MIDIControl::set_value (float val)
94 sp.set_position (midi_to_pan (val));
99 StreamPanner::MIDIControl::send_feedback (pan_t value)
102 if (!setting && get_midi_feedback() && pan_to_midi) {
103 MIDI::byte val = (MIDI::byte) (pan_to_midi (value) * 127.0f);
104 MIDI::channel_t ch = 0;
105 MIDI::eventType ev = MIDI::none;
106 MIDI::byte additional = 0;
107 MIDI::EventTwoBytes data;
109 if (get_control_info (ch, ev, additional)) {
110 data.controller_number = additional;
113 sp.get_parent().session().send_midi_message (get_port(), ev, ch, data);
116 // send_midi_feedback (pan_to_midi (val));
122 StreamPanner::MIDIControl::write_feedback (MIDI::byte* buf, int32_t& bufsize, pan_t val, bool force)
124 if (get_midi_feedback() && pan_to_midi && bufsize > 2) {
125 MIDI::channel_t ch = 0;
126 MIDI::eventType ev = MIDI::none;
127 MIDI::byte additional = 0;
129 if (get_control_info (ch, ev, additional)) {
131 pm = (MIDI::byte) (pan_to_midi (val) * 127.0);
133 if (pm != last_written || force) {
134 *buf++ = (0xF0 & ev) | (0xF & ch);
135 *buf++ = additional; /* controller number */
148 StreamPanner::reset_midi_control (MIDI::Port* port, bool on)
154 _midi_control.get_control_info (chn, ev, extra);
158 _midi_control.midi_rebind (port, chn);
162 StreamPanner::set_muted (bool yn)
171 StreamPanner::set_position (float xpos, bool link_call)
173 if (!link_call && parent.linked()) {
174 parent.set_position (xpos, *this);
182 if (parent.session().get_midi_feedback()) {
183 _midi_control.send_feedback (x);
189 StreamPanner::set_position (float xpos, float ypos, bool link_call)
191 if (!link_call && parent.linked()) {
192 parent.set_position (xpos, ypos, *this);
195 if (x != xpos || y != ypos) {
205 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
207 if (!link_call && parent.linked()) {
208 parent.set_position (xpos, ypos, zpos, *this);
211 if (x != xpos || y != ypos || z != zpos) {
221 StreamPanner::set_state (const XMLNode& node)
223 const XMLProperty* prop;
224 XMLNodeConstIterator iter;
225 XMLNodeList midi_kids;
227 if ((prop = node.property (X_("muted")))) {
228 set_muted (prop->value() == "yes");
231 midi_kids = node.children ("MIDI");
233 for (iter = midi_kids.begin(); iter != midi_kids.end(); ++iter) {
236 XMLNodeConstIterator miter;
239 kids = (*iter)->children ();
241 for (miter = kids.begin(); miter != kids.end(); ++miter) {
245 if (child->name() == "pan") {
247 MIDI::eventType ev = MIDI::on; /* initialize to keep gcc happy */
248 MIDI::byte additional = 0; /* ditto */
249 MIDI::channel_t chn = 0; /* ditto */
251 if (get_midi_node_info (child, ev, chn, additional)) {
252 _midi_control.set_control_type (chn, ev, additional);
254 error << _("MIDI pan control specification is incomplete, so it has been ignored") << endmsg;
265 StreamPanner::add_state (XMLNode& node)
267 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
273 MIDI::byte additional;
274 XMLNode* midi_node = 0;
277 if (_midi_control.get_control_info (chn, ev, additional)) {
279 midi_node = node.add_child ("MIDI");
281 child = midi_node->add_child ("pan");
282 set_midi_node_info (child, ev, chn, additional);
289 StreamPanner::get_midi_node_info (XMLNode * node, MIDI::eventType & ev, MIDI::channel_t & chan, MIDI::byte & additional)
292 const XMLProperty* prop;
295 if ((prop = node->property ("event")) != 0) {
296 sscanf (prop->value().c_str(), "0x%x", &xx);
297 ev = (MIDI::eventType) xx;
302 if (ok && ((prop = node->property ("channel")) != 0)) {
303 sscanf (prop->value().c_str(), "%d", &xx);
304 chan = (MIDI::channel_t) xx;
309 if (ok && ((prop = node->property ("additional")) != 0)) {
310 sscanf (prop->value().c_str(), "0x%x", &xx);
311 additional = (MIDI::byte) xx;
318 StreamPanner::set_midi_node_info (XMLNode * node, MIDI::eventType ev, MIDI::channel_t chan, MIDI::byte additional)
322 snprintf (buf, sizeof(buf), "0x%x", ev);
323 node->add_property ("event", buf);
324 snprintf (buf, sizeof(buf), "%d", chan);
325 node->add_property ("channel", buf);
326 snprintf (buf, sizeof(buf), "0x%x", additional);
327 node->add_property ("additional", buf);
332 /*---------------------------------------------------------------------- */
334 BaseStereoPanner::BaseStereoPanner (Panner& p)
335 : StreamPanner (p), _automation (0.0, 1.0, 0.5)
339 BaseStereoPanner::~BaseStereoPanner ()
344 BaseStereoPanner::snapshot (jack_nframes_t now)
346 if (_automation.automation_state() == Write || _automation.automation_state() == Touch) {
347 _automation.rt_add (now, x);
352 BaseStereoPanner::transport_stopped (jack_nframes_t frame)
354 _automation.reposition_for_rt_add (frame);
356 if (_automation.automation_state() != Off) {
358 if (_automation.automation_write()) {
359 _automation.save_state (_("automation write pass"));
362 set_position (_automation.eval (frame));
367 BaseStereoPanner::set_automation_style (AutoStyle style)
369 _automation.set_automation_style (style);
373 BaseStereoPanner::set_automation_state (AutoState state)
375 if (state != _automation.automation_state()) {
377 _automation.set_automation_state (state);
380 set_position (_automation.eval (parent.session().transport_frame()));
386 BaseStereoPanner::save (ostream& out) const
388 LocaleGuard lg (X_("POSIX"));
390 /* force a single format for numeric data to ease session interchange
391 across national boundaries.
394 out << "begin" << endl;
396 for (AutomationList::const_iterator i = _automation.const_begin(); i != _automation.const_end(); ++i) {
397 out << '\t' << (jack_nframes_t) floor ((*i)->when) << ' ' << (*i)->value << endl;
399 error << string_compose (_("error writing pan automation file (%s)"), strerror (errno)) << endmsg;
403 out << "end" << endl;
409 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
412 LocaleGuard lg (X_("POSIX"));
414 _automation.clear ();
416 while (in.getline (line, sizeof (line), '\n')) {
422 if (strcmp (line, "end") == 0) {
426 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
427 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
431 _automation.add (when, value, true);
434 /* now that we are done loading */
436 _automation.save_state (_("loaded from disk"));
437 _automation.StateChanged (Change (0));
443 BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, jack_nframes_t nframes)
457 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
459 /* interpolate over 64 frames or nframes, whichever is smaller */
461 jack_nframes_t limit = min ((jack_nframes_t)64, nframes);
464 delta = -(delta / (float) (limit));
466 for (n = 0; n < limit; n++) {
467 left_interp = left_interp + delta;
468 left = left_interp + 0.9 * (left - left_interp);
469 dst[n] += src[n] * left * gain_coeff;
472 pan = left * gain_coeff;
474 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
481 if ((pan = (left * gain_coeff)) != 1.0f) {
485 Session::mix_buffers_with_gain(dst,src,nframes,pan);
487 /* mark that we wrote into the buffer */
495 Session::mix_buffers_no_gain(dst,src,nframes);
497 /* mark that we wrote into the buffer */
507 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
509 /* interpolate over 64 frames or nframes, whichever is smaller */
511 jack_nframes_t limit = min ((jack_nframes_t)64, nframes);
514 delta = -(delta / (float) (limit));
516 for (n = 0; n < limit; n++) {
517 right_interp = right_interp + delta;
518 right = right_interp + 0.9 * (right - right_interp);
519 dst[n] += src[n] * right * gain_coeff;
522 pan = right * gain_coeff;
524 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
526 /* XXX it would be nice to mark the buffer as written to */
530 right = desired_right;
531 right_interp = right;
533 if ((pan = (right * gain_coeff)) != 1.0f) {
537 Session::mix_buffers_with_gain(dst,src,nframes,pan);
539 /* XXX it would be nice to mark the buffer as written to */
544 Session::mix_buffers_no_gain(dst,src,nframes);
546 /* XXX it would be nice to mark the buffer as written to */
551 /*---------------------------------------------------------------------- */
553 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p)
554 : BaseStereoPanner (p)
559 right = desired_right;
561 right_interp = right;
564 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
569 EqualPowerStereoPanner::update ()
571 /* it would be very nice to split this out into a virtual function
572 that can be accessed from BaseStereoPanner and used in distribute_automated().
574 but the place where its used in distribute_automated() is a tight inner loop,
575 and making "nframes" virtual function calls to compute values is an absurd
579 /* x == 0 => hard left
584 float panL = 1 - panR;
586 const float pan_law_attenuation = -3.0f;
587 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
589 desired_left = panL * (scale * panL + 1.0f - scale);
590 desired_right = panR * (scale * panR + 1.0f - scale);
596 EqualPowerStereoPanner::distribute_automated (Sample* src, Sample** obufs,
597 jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes,
603 /* fetch positional data */
605 if (!_automation.rt_safe_get_vector (start, end, buffers[0], nframes)) {
608 distribute (src, obufs, 1.0, nframes);
613 /* store effective pan position. do this even if we are muted */
615 effective_x = buffers[0][nframes-1];
621 /* apply pan law to convert positional data into pan coefficients for
625 const float pan_law_attenuation = -3.0f;
626 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
628 for (jack_nframes_t n = 0; n < nframes; ++n) {
630 float panR = buffers[0][n];
631 float panL = 1 - panR;
633 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
634 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
642 for (jack_nframes_t n = 0; n < nframes; ++n) {
643 dst[n] += src[n] * pbuf[n];
646 /* XXX it would be nice to mark the buffer as written to */
653 for (jack_nframes_t n = 0; n < nframes; ++n) {
654 dst[n] += src[n] * pbuf[n];
657 /* XXX it would be nice to mark the buffer as written to */
661 EqualPowerStereoPanner::factory (Panner& parent)
663 return new EqualPowerStereoPanner (parent);
667 EqualPowerStereoPanner::get_state (void)
673 EqualPowerStereoPanner::state (bool full_state)
675 XMLNode* root = new XMLNode ("StreamPanner");
677 LocaleGuard lg (X_("POSIX"));
679 snprintf (buf, sizeof (buf), "%f", x);
680 root->add_property (X_("x"), buf);
681 root->add_property (X_("type"), EqualPowerStereoPanner::name);
683 snprintf (buf, sizeof (buf), "0x%x", _automation.automation_state());
685 /* never store automation states other than off in a template */
686 snprintf (buf, sizeof (buf), "0x%x", ARDOUR::Off);
688 root->add_property (X_("automation-state"), buf);
689 snprintf (buf, sizeof (buf), "0x%x", _automation.automation_style());
690 root->add_property (X_("automation-style"), buf);
692 StreamPanner::add_state (*root);
698 EqualPowerStereoPanner::set_state (const XMLNode& node)
700 const XMLProperty* prop;
703 LocaleGuard lg (X_("POSIX"));
705 if ((prop = node.property (X_("x")))) {
706 pos = atof (prop->value().c_str());
707 set_position (pos, true);
710 if ((prop = node.property (X_("automation-state")))) {
711 sscanf (prop->value().c_str(), "0x%x", &x);
712 _automation.set_automation_state ((AutoState) x);
715 set_position (_automation.eval (parent.session().transport_frame()));
719 if ((prop = node.property (X_("automation-style")))) {
720 sscanf (prop->value().c_str(), "0x%x", &x);
721 _automation.set_automation_style ((AutoStyle) x);
724 StreamPanner::set_state (node);
729 /*----------------------------------------------------------------------*/
731 Multi2dPanner::Multi2dPanner (Panner& p)
732 : StreamPanner (p), _automation (0.0, 1.0, 0.5) // XXX useless
737 Multi2dPanner::~Multi2dPanner ()
742 Multi2dPanner::snapshot (jack_nframes_t now)
748 Multi2dPanner::transport_stopped (jack_nframes_t frame)
754 Multi2dPanner::set_automation_style (AutoStyle style)
760 Multi2dPanner::set_automation_state (AutoState state)
766 Multi2dPanner::update ()
768 static const float BIAS = FLT_MIN;
770 uint32_t nouts = parent.outputs.size();
777 for (i = 0; i < nouts; i++) {
778 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
782 f += dsq[i] * dsq[i];
785 // terrible hack to support OSX < 10.3.9 builds
786 fr = (float) (1.0 / sqrt((double)f));
790 for (i = 0; i < nouts; ++i) {
791 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
798 Multi2dPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, jack_nframes_t nframes)
802 vector<Panner::Output>::iterator o;
810 for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
815 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
817 /* interpolate over 64 frames or nframes, whichever is smaller */
819 jack_nframes_t limit = min ((jack_nframes_t)64, nframes);
822 delta = -(delta / (float) (limit));
824 for (n = 0; n < limit; n++) {
825 left_interp = left_interp + delta;
826 left = left_interp + 0.9 * (left - left_interp);
827 dst[n] += src[n] * left * gain_coeff;
830 pan = left * gain_coeff;
832 for (; n < nframes; ++n) {
833 dst[n] += src[n] * pan;
839 pan = (*o).desired_pan;
841 if ((pan *= gain_coeff) != 1.0f) {
845 for (jack_nframes_t n = 0; n < nframes; ++n) {
846 dst[n] += src[n] * pan;
854 for (jack_nframes_t n = 0; n < nframes; ++n) {
869 Multi2dPanner::distribute_automated (Sample* src, Sample** obufs,
870 jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes,
883 Multi2dPanner::factory (Panner& p)
885 return new Multi2dPanner (p);
889 Multi2dPanner::load (istream& in, string path, uint32_t& linecnt)
895 Multi2dPanner::save (ostream& out) const
901 Multi2dPanner::get_state (void)
907 Multi2dPanner::state (bool full_state)
909 XMLNode* root = new XMLNode ("StreamPanner");
911 LocaleGuard lg (X_("POSIX"));
913 snprintf (buf, sizeof (buf), "%f", x);
914 root->add_property (X_("x"), buf);
915 snprintf (buf, sizeof (buf), "%f", y);
916 root->add_property (X_("y"), buf);
917 root->add_property (X_("type"), Multi2dPanner::name);
923 Multi2dPanner::set_state (const XMLNode& node)
925 const XMLProperty* prop;
927 LocaleGuard lg (X_("POSIX"));
932 if ((prop = node.property (X_("x")))) {
933 newx = atof (prop->value().c_str());
936 if ((prop = node.property (X_("y")))) {
937 newy = atof (prop->value().c_str());
940 if (x < 0 || y < 0) {
941 error << _("badly-formed positional data for Multi2dPanner - ignored")
946 set_position (newx, newy);
950 /*---------------------------------------------------------------------- */
952 Panner::Panner (string name, Session& s)
957 _link_direction = SameDirection;
960 reset_midi_control (_session.mmc_port(), _session.get_mmc_control());
968 Panner::set_linked (bool yn)
972 _session.set_dirty ();
973 LinkStateChanged (); /* EMIT SIGNAL */
978 Panner::set_link_direction (LinkDirection ld)
980 if (ld != _link_direction) {
981 _link_direction = ld;
982 _session.set_dirty ();
983 LinkStateChanged (); /* EMIT SIGNAL */
988 Panner::set_name (string str)
990 automation_path = _session.automation_dir();
991 automation_path += _session.snap_name();
992 automation_path += "-pan-";
993 automation_path += legalize_for_path (str);
994 automation_path += ".automation";
999 Panner::set_bypassed (bool yn)
1001 if (yn != _bypassed) {
1009 Panner::reset (uint32_t nouts, uint32_t npans)
1012 bool changed = false;
1015 if (nouts < 2 || (nouts == outputs.size() && npans == size())) {
1038 fatal << _("programming error:")
1039 << X_("Panner::reset() called with a single output")
1046 outputs.push_back (Output (0, 0));
1047 outputs.push_back (Output (1.0, 0));
1049 for (n = 0; n < npans; ++n) {
1050 push_back (new EqualPowerStereoPanner (*this));
1055 outputs.push_back (Output (0.5, 0));
1056 outputs.push_back (Output (0, 1.0));
1057 outputs.push_back (Output (1.0, 1.0));
1059 for (n = 0; n < npans; ++n) {
1060 push_back (new Multi2dPanner (*this));
1066 outputs.push_back (Output (0, 0));
1067 outputs.push_back (Output (1.0, 0));
1068 outputs.push_back (Output (1.0, 1.0));
1069 outputs.push_back (Output (0, 1.0));
1071 for (n = 0; n < npans; ++n) {
1072 push_back (new Multi2dPanner (*this));
1077 case 5: //square+offcenter center
1078 outputs.push_back (Output (0, 0));
1079 outputs.push_back (Output (1.0, 0));
1080 outputs.push_back (Output (1.0, 1.0));
1081 outputs.push_back (Output (0, 1.0));
1082 outputs.push_back (Output (0.5, 0.75));
1084 for (n = 0; n < npans; ++n) {
1085 push_back (new Multi2dPanner (*this));
1091 /* XXX horrible placement. FIXME */
1092 for (n = 0; n < nouts; ++n) {
1093 outputs.push_back (Output (0.1 * n, 0.1 * n));
1096 for (n = 0; n < npans; ++n) {
1097 push_back (new Multi2dPanner (*this));
1103 for (iterator x = begin(); x != end(); ++x) {
1107 reset_midi_control (_session.mmc_port(), _session.get_mmc_control());
1109 /* force hard left/right panning in a common case: 2in/2out
1112 if (npans == 2 && outputs.size() == 2) {
1114 /* Do this only if we changed configuration, or our configuration
1115 appears to be the default set up (center).
1121 front()->get_position (left);
1122 back()->get_position (right);
1124 if (changed || ((left == 0.5) && (right == 0.5))) {
1126 front()->set_position (0.0);
1127 front()->automation().reset_default (0.0);
1129 back()->set_position (1.0);
1130 back()->automation().reset_default (1.0);
1137 Changed (); /* EMIT SIGNAL */
1144 Panner::remove (uint32_t which)
1146 vector<StreamPanner*>::iterator i;
1147 for (i = begin(); i != end() && which; ++i, --which);
1158 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1162 vector<StreamPanner*>::clear ();
1166 Panner::set_automation_style (AutoStyle style)
1168 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1169 (*i)->set_automation_style (style);
1171 _session.set_dirty ();
1175 Panner::set_automation_state (AutoState state)
1177 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1178 (*i)->set_automation_state (state);
1180 _session.set_dirty ();
1184 Panner::automation_state () const
1187 return front()->automation().automation_state ();
1194 Panner::automation_style () const
1197 return front()->automation().automation_style ();
1204 Panner::transport_stopped (jack_nframes_t frame)
1206 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1207 (*i)->transport_stopped (frame);
1212 Panner::snapshot (jack_nframes_t now)
1214 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1215 (*i)->snapshot (now);
1220 Panner::clear_automation ()
1222 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1223 (*i)->automation().clear ();
1225 _session.set_dirty ();
1229 Panner::save () const
1231 ofstream out (automation_path.c_str());
1234 error << string_compose (_("cannot open pan automation file \"%1\" for saving (%s)"), automation_path, strerror (errno))
1239 out << X_("version ") << current_automation_version_number << endl;
1241 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1242 if ((*i)->save (out)) {
1254 uint32_t linecnt = 0;
1257 LocaleGuard lg (X_("POSIX"));
1259 if (automation_path.length() == 0) {
1263 if (access (automation_path.c_str(), F_OK)) {
1267 ifstream in (automation_path.c_str());
1270 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1271 automation_path, strerror (errno))
1278 while (in.getline (line, sizeof(line), '\n')) {
1280 if (++linecnt == 1) {
1281 if (memcmp (line, X_("version"), 7) == 0) {
1282 if (sscanf (line, "version %f", &version) != 1) {
1283 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1287 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1288 automation_path, line) << endmsg;
1292 if (version != current_automation_version_number) {
1293 error << string_compose(_("mismatched pan automation event file version (%1)"), version) << endmsg;
1300 if (strlen (line) == 0 || line[0] == '#') {
1304 if (strcmp (line, "begin") == 0) {
1307 error << string_compose (_("too many panner states found in pan automation file %1"),
1313 if ((*sp)->load (in, automation_path, linecnt)) {
1327 StreamPanner* (*factory)(Panner&);
1330 PanPlugins pan_plugins[] = {
1331 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1332 { Multi2dPanner::name, 3, Multi2dPanner::factory },
1337 Panner::get_state (void)
1339 return state (true);
1343 Panner::state (bool full)
1345 XMLNode* root = new XMLNode (X_("Panner"));
1348 for (iterator p = begin(); p != end(); ++p) {
1349 root->add_child_nocopy ((*p)->state (full));
1352 root->add_property (X_("linked"), (_linked ? "yes" : "no"));
1353 snprintf (buf, sizeof (buf), "%d", _link_direction);
1354 root->add_property (X_("link_direction"), buf);
1355 root->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1357 /* add each output */
1359 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1360 XMLNode* onode = new XMLNode (X_("Output"));
1361 snprintf (buf, sizeof (buf), "%f", (*o).x);
1362 onode->add_property (X_("x"), buf);
1363 snprintf (buf, sizeof (buf), "%f", (*o).y);
1364 onode->add_property (X_("y"), buf);
1365 root->add_child_nocopy (*onode);
1370 root->add_property (X_("automation"), PBD::basename (automation_path));
1378 Panner::set_state (const XMLNode& node)
1381 XMLNodeConstIterator niter;
1382 const XMLProperty *prop;
1385 LocaleGuard lg (X_("POSIX"));
1390 if ((prop = node.property (X_("linked"))) != 0) {
1391 set_linked (prop->value() == "yes");
1395 if ((prop = node.property (X_("bypassed"))) != 0) {
1396 set_bypassed (prop->value() == "yes");
1399 if ((prop = node.property (X_("link_direction"))) != 0) {
1400 sscanf (prop->value().c_str(), "%d", &i);
1401 set_link_direction ((LinkDirection) (i));
1404 nlist = node.children();
1406 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1407 if ((*niter)->name() == X_("Output")) {
1411 prop = (*niter)->property (X_("x"));
1412 sscanf (prop->value().c_str(), "%f", &x);
1414 prop = (*niter)->property (X_("y"));
1415 sscanf (prop->value().c_str(), "%f", &y);
1417 outputs.push_back (Output (x, y));
1421 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1423 if ((*niter)->name() == X_("StreamPanner")) {
1425 if ((prop = (*niter)->property (X_("type")))) {
1427 for (i = 0; pan_plugins[i].factory; ++i) {
1428 if (prop->value() == pan_plugins[i].name) {
1431 /* note that we assume that all the stream panners
1432 are of the same type. pretty good
1433 assumption, but its still an assumption.
1436 sp = pan_plugins[i].factory (*this);
1438 if (sp->set_state (**niter) == 0) {
1447 if (!pan_plugins[i].factory) {
1448 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1454 error << _("panner plugin node has no type information!")
1462 /* don't try to load automation if it wasn't marked as existing */
1464 if ((prop = node.property (X_("automation")))) {
1466 /* automation path is relative */
1468 automation_path = _session.automation_dir();
1469 automation_path += prop->value ();
1478 Panner::touching () const
1480 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1481 if ((*i)->automation().touching ()) {
1490 Panner::reset_midi_control (MIDI::Port* port, bool on)
1492 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1493 (*i)->reset_midi_control (port, on);
1498 Panner::set_position (float xpos, StreamPanner& orig)
1504 orig.get_position (xnow);
1505 xdelta = xpos - xnow;
1507 if (_link_direction == SameDirection) {
1509 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1511 (*i)->set_position (xpos, true);
1513 (*i)->get_position (xnow);
1514 xnew = min (1.0f, xnow + xdelta);
1515 xnew = max (0.0f, xnew);
1516 (*i)->set_position (xnew, true);
1522 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1524 (*i)->set_position (xpos, true);
1526 (*i)->get_position (xnow);
1527 xnew = min (1.0f, xnow - xdelta);
1528 xnew = max (0.0f, xnew);
1529 (*i)->set_position (xnew, true);
1536 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1539 float xdelta, ydelta;
1542 orig.get_position (xnow, ynow);
1543 xdelta = xpos - xnow;
1544 ydelta = ypos - ynow;
1546 if (_link_direction == SameDirection) {
1548 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1550 (*i)->set_position (xpos, ypos, true);
1552 (*i)->get_position (xnow, ynow);
1554 xnew = min (1.0f, xnow + xdelta);
1555 xnew = max (0.0f, xnew);
1557 ynew = min (1.0f, ynow + ydelta);
1558 ynew = max (0.0f, ynew);
1560 (*i)->set_position (xnew, ynew, true);
1566 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1568 (*i)->set_position (xpos, ypos, true);
1570 (*i)->get_position (xnow, ynow);
1572 xnew = min (1.0f, xnow - xdelta);
1573 xnew = max (0.0f, xnew);
1575 ynew = min (1.0f, ynow - ydelta);
1576 ynew = max (0.0f, ynew);
1578 (*i)->set_position (xnew, ynew, true);
1585 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1587 float xnow, ynow, znow;
1588 float xdelta, ydelta, zdelta;
1589 float xnew, ynew, znew;
1591 orig.get_position (xnow, ynow, znow);
1592 xdelta = xpos - xnow;
1593 ydelta = ypos - ynow;
1594 zdelta = zpos - znow;
1596 if (_link_direction == SameDirection) {
1598 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1600 (*i)->set_position (xpos, ypos, zpos, true);
1602 (*i)->get_position (xnow, ynow, znow);
1604 xnew = min (1.0f, xnow + xdelta);
1605 xnew = max (0.0f, xnew);
1607 ynew = min (1.0f, ynow + ydelta);
1608 ynew = max (0.0f, ynew);
1610 znew = min (1.0f, znow + zdelta);
1611 znew = max (0.0f, znew);
1613 (*i)->set_position (xnew, ynew, znew, true);
1619 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1621 (*i)->set_position (xpos, ypos, true);
1623 (*i)->get_position (xnow, ynow, znow);
1625 xnew = min (1.0f, xnow - xdelta);
1626 xnew = max (0.0f, xnew);
1628 ynew = min (1.0f, ynow - ydelta);
1629 ynew = max (0.0f, ynew);
1631 znew = min (1.0f, znow + zdelta);
1632 znew = max (0.0f, znew);
1634 (*i)->set_position (xnew, ynew, znew, true);
1641 Panner::send_all_midi_feedback ()
1643 if (_session.get_midi_feedback()) {
1646 // do feedback for all panners
1647 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1648 (*i)->get_effective_position (xpos);
1650 (*i)->midi_control().send_feedback (xpos);
1657 Panner::write_midi_feedback (MIDI::byte* buf, int32_t& bufsize)
1659 AutoState astate = automation_state ();
1661 if (_session.get_midi_feedback() &&
1662 (astate == Play || (astate == Touch && !touching()))) {
1666 // do feedback for all panners
1667 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1668 (*i)->get_effective_position (xpos);
1670 buf = (*i)->midi_control().write_feedback (buf, bufsize, xpos);