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;
114 sp.get_parent().session().send_midi_message (get_port(), ev, ch, data);
117 // send_midi_feedback (pan_to_midi (val));
123 StreamPanner::MIDIControl::write_feedback (MIDI::byte* buf, int32_t& bufsize, pan_t val, bool force)
125 if (get_midi_feedback() && pan_to_midi && bufsize > 2) {
126 MIDI::channel_t ch = 0;
127 MIDI::eventType ev = MIDI::none;
128 MIDI::byte additional = 0;
130 if (get_control_info (ch, ev, additional)) {
132 pm = (MIDI::byte) (pan_to_midi (val) * 127.0);
134 if (pm != last_written || force) {
135 *buf++ = (0xF0 & ev) | (0xF & ch);
136 *buf++ = additional; /* controller number */
149 StreamPanner::reset_midi_control (MIDI::Port* port, bool on)
155 _midi_control.get_control_info (chn, ev, extra);
159 _midi_control.midi_rebind (port, chn);
163 StreamPanner::set_muted (bool yn)
172 StreamPanner::set_position (float xpos, bool link_call)
174 if (!link_call && parent.linked()) {
175 parent.set_position (xpos, *this);
183 if (parent.session().get_midi_feedback()) {
184 _midi_control.send_feedback (x);
190 StreamPanner::set_position (float xpos, float ypos, bool link_call)
192 if (!link_call && parent.linked()) {
193 parent.set_position (xpos, ypos, *this);
196 if (x != xpos || y != ypos) {
206 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
208 if (!link_call && parent.linked()) {
209 parent.set_position (xpos, ypos, zpos, *this);
212 if (x != xpos || y != ypos || z != zpos) {
222 StreamPanner::set_state (const XMLNode& node)
224 const XMLProperty* prop;
225 XMLNodeConstIterator iter;
226 XMLNodeList midi_kids;
228 if ((prop = node.property (X_("muted")))) {
229 set_muted (prop->value() == "yes");
232 midi_kids = node.children ("MIDI");
234 for (iter = midi_kids.begin(); iter != midi_kids.end(); ++iter) {
237 XMLNodeConstIterator miter;
240 kids = (*iter)->children ();
242 for (miter = kids.begin(); miter != kids.end(); ++miter) {
246 if (child->name() == "pan") {
248 MIDI::eventType ev = MIDI::on; /* initialize to keep gcc happy */
249 MIDI::byte additional = 0; /* ditto */
250 MIDI::channel_t chn = 0; /* ditto */
252 if (get_midi_node_info (child, ev, chn, additional)) {
253 _midi_control.set_control_type (chn, ev, additional);
255 error << _("MIDI pan control specification is incomplete, so it has been ignored") << endmsg;
266 StreamPanner::add_state (XMLNode& node)
268 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
274 MIDI::byte additional;
275 XMLNode* midi_node = 0;
278 if (_midi_control.get_control_info (chn, ev, additional)) {
280 midi_node = node.add_child ("MIDI");
282 child = midi_node->add_child ("pan");
283 set_midi_node_info (child, ev, chn, additional);
290 StreamPanner::get_midi_node_info (XMLNode * node, MIDI::eventType & ev, MIDI::channel_t & chan, MIDI::byte & additional)
293 const XMLProperty* prop;
296 if ((prop = node->property ("event")) != 0) {
297 sscanf (prop->value().c_str(), "0x%x", &xx);
298 ev = (MIDI::eventType) xx;
303 if (ok && ((prop = node->property ("channel")) != 0)) {
304 sscanf (prop->value().c_str(), "%d", &xx);
305 chan = (MIDI::channel_t) xx;
310 if (ok && ((prop = node->property ("additional")) != 0)) {
311 sscanf (prop->value().c_str(), "0x%x", &xx);
312 additional = (MIDI::byte) xx;
319 StreamPanner::set_midi_node_info (XMLNode * node, MIDI::eventType ev, MIDI::channel_t chan, MIDI::byte additional)
323 snprintf (buf, sizeof(buf), "0x%x", ev);
324 node->add_property ("event", buf);
325 snprintf (buf, sizeof(buf), "%d", chan);
326 node->add_property ("channel", buf);
327 snprintf (buf, sizeof(buf), "0x%x", additional);
328 node->add_property ("additional", buf);
333 /*---------------------------------------------------------------------- */
335 BaseStereoPanner::BaseStereoPanner (Panner& p)
336 : StreamPanner (p), _automation (0.0, 1.0, 0.5)
340 BaseStereoPanner::~BaseStereoPanner ()
345 BaseStereoPanner::snapshot (jack_nframes_t now)
347 if (_automation.automation_state() == Write || _automation.automation_state() == Touch) {
348 _automation.rt_add (now, x);
353 BaseStereoPanner::transport_stopped (jack_nframes_t frame)
355 _automation.reposition_for_rt_add (frame);
357 if (_automation.automation_state() != Off) {
359 if (_automation.automation_write()) {
360 _automation.save_state (_("automation write pass"));
363 set_position (_automation.eval (frame));
368 BaseStereoPanner::set_automation_style (AutoStyle style)
370 _automation.set_automation_style (style);
374 BaseStereoPanner::set_automation_state (AutoState state)
376 if (state != _automation.automation_state()) {
378 _automation.set_automation_state (state);
381 set_position (_automation.eval (parent.session().transport_frame()));
387 BaseStereoPanner::save (ostream& out) const
389 LocaleGuard lg (X_("POSIX"));
391 /* force a single format for numeric data to ease session interchange
392 across national boundaries.
395 out << "begin" << endl;
397 for (AutomationList::const_iterator i = _automation.const_begin(); i != _automation.const_end(); ++i) {
398 out << '\t' << (jack_nframes_t) floor ((*i)->when) << ' ' << (*i)->value << endl;
400 error << string_compose (_("error writing pan automation file (%s)"), strerror (errno)) << endmsg;
404 out << "end" << endl;
410 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
413 LocaleGuard lg (X_("POSIX"));
415 _automation.clear ();
417 while (in.getline (line, sizeof (line), '\n')) {
423 if (strcmp (line, "end") == 0) {
427 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
428 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
432 _automation.add (when, value, true);
435 /* now that we are done loading */
437 _automation.save_state (_("loaded from disk"));
438 _automation.StateChanged (Change (0));
444 BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, jack_nframes_t nframes)
458 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
460 /* interpolate over 64 frames or nframes, whichever is smaller */
462 jack_nframes_t limit = min ((jack_nframes_t)64, nframes);
465 delta = -(delta / (float) (limit));
467 for (n = 0; n < limit; n++) {
468 left_interp = left_interp + delta;
469 left = left_interp + 0.9 * (left - left_interp);
470 dst[n] += src[n] * left * gain_coeff;
473 pan = left * gain_coeff;
475 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
482 if ((pan = (left * gain_coeff)) != 1.0f) {
486 Session::mix_buffers_with_gain(dst,src,nframes,pan);
488 /* mark that we wrote into the buffer */
496 Session::mix_buffers_no_gain(dst,src,nframes);
498 /* mark that we wrote into the buffer */
508 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
510 /* interpolate over 64 frames or nframes, whichever is smaller */
512 jack_nframes_t limit = min ((jack_nframes_t)64, nframes);
515 delta = -(delta / (float) (limit));
517 for (n = 0; n < limit; n++) {
518 right_interp = right_interp + delta;
519 right = right_interp + 0.9 * (right - right_interp);
520 dst[n] += src[n] * right * gain_coeff;
523 pan = right * gain_coeff;
525 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
527 /* XXX it would be nice to mark the buffer as written to */
531 right = desired_right;
532 right_interp = right;
534 if ((pan = (right * gain_coeff)) != 1.0f) {
538 Session::mix_buffers_with_gain(dst,src,nframes,pan);
540 /* XXX it would be nice to mark the buffer as written to */
545 Session::mix_buffers_no_gain(dst,src,nframes);
547 /* XXX it would be nice to mark the buffer as written to */
552 /*---------------------------------------------------------------------- */
554 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p)
555 : BaseStereoPanner (p)
560 right = desired_right;
562 right_interp = right;
565 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
570 EqualPowerStereoPanner::update ()
572 /* it would be very nice to split this out into a virtual function
573 that can be accessed from BaseStereoPanner and used in distribute_automated().
575 but the place where its used in distribute_automated() is a tight inner loop,
576 and making "nframes" virtual function calls to compute values is an absurd
580 /* x == 0 => hard left
585 float panL = 1 - panR;
587 const float pan_law_attenuation = -3.0f;
588 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
590 desired_left = panL * (scale * panL + 1.0f - scale);
591 desired_right = panR * (scale * panR + 1.0f - scale);
597 EqualPowerStereoPanner::distribute_automated (Sample* src, Sample** obufs,
598 jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes,
604 /* fetch positional data */
606 if (!_automation.rt_safe_get_vector (start, end, buffers[0], nframes)) {
609 distribute (src, obufs, 1.0, nframes);
614 /* store effective pan position. do this even if we are muted */
616 effective_x = buffers[0][nframes-1];
622 /* apply pan law to convert positional data into pan coefficients for
626 const float pan_law_attenuation = -3.0f;
627 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
629 for (jack_nframes_t n = 0; n < nframes; ++n) {
631 float panR = buffers[0][n];
632 float panL = 1 - panR;
634 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
635 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
643 for (jack_nframes_t n = 0; n < nframes; ++n) {
644 dst[n] += src[n] * pbuf[n];
647 /* XXX it would be nice to mark the buffer as written to */
654 for (jack_nframes_t n = 0; n < nframes; ++n) {
655 dst[n] += src[n] * pbuf[n];
658 /* XXX it would be nice to mark the buffer as written to */
662 EqualPowerStereoPanner::factory (Panner& parent)
664 return new EqualPowerStereoPanner (parent);
668 EqualPowerStereoPanner::get_state (void)
674 EqualPowerStereoPanner::state (bool full_state)
676 XMLNode* root = new XMLNode ("StreamPanner");
678 LocaleGuard lg (X_("POSIX"));
680 snprintf (buf, sizeof (buf), "%f", x);
681 root->add_property (X_("x"), buf);
682 root->add_property (X_("type"), EqualPowerStereoPanner::name);
684 snprintf (buf, sizeof (buf), "0x%x", _automation.automation_state());
686 /* never store automation states other than off in a template */
687 snprintf (buf, sizeof (buf), "0x%x", ARDOUR::Off);
689 root->add_property (X_("automation-state"), buf);
690 snprintf (buf, sizeof (buf), "0x%x", _automation.automation_style());
691 root->add_property (X_("automation-style"), buf);
693 StreamPanner::add_state (*root);
699 EqualPowerStereoPanner::set_state (const XMLNode& node)
701 const XMLProperty* prop;
704 LocaleGuard lg (X_("POSIX"));
706 if ((prop = node.property (X_("x")))) {
707 pos = atof (prop->value().c_str());
708 set_position (pos, true);
711 if ((prop = node.property (X_("automation-state")))) {
712 sscanf (prop->value().c_str(), "0x%x", &x);
713 _automation.set_automation_state ((AutoState) x);
716 set_position (_automation.eval (parent.session().transport_frame()));
720 if ((prop = node.property (X_("automation-style")))) {
721 sscanf (prop->value().c_str(), "0x%x", &x);
722 _automation.set_automation_style ((AutoStyle) x);
725 StreamPanner::set_state (node);
730 /*----------------------------------------------------------------------*/
732 Multi2dPanner::Multi2dPanner (Panner& p)
733 : StreamPanner (p), _automation (0.0, 1.0, 0.5) // XXX useless
738 Multi2dPanner::~Multi2dPanner ()
743 Multi2dPanner::snapshot (jack_nframes_t now)
749 Multi2dPanner::transport_stopped (jack_nframes_t frame)
755 Multi2dPanner::set_automation_style (AutoStyle style)
761 Multi2dPanner::set_automation_state (AutoState state)
767 Multi2dPanner::update ()
769 static const float BIAS = FLT_MIN;
771 uint32_t nouts = parent.outputs.size();
778 for (i = 0; i < nouts; i++) {
779 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
783 f += dsq[i] * dsq[i];
786 // terrible hack to support OSX < 10.3.9 builds
787 fr = (float) (1.0 / sqrt((double)f));
791 for (i = 0; i < nouts; ++i) {
792 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
799 Multi2dPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, jack_nframes_t nframes)
803 vector<Panner::Output>::iterator o;
811 for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
816 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
818 /* interpolate over 64 frames or nframes, whichever is smaller */
820 jack_nframes_t limit = min ((jack_nframes_t)64, nframes);
823 delta = -(delta / (float) (limit));
825 for (n = 0; n < limit; n++) {
826 left_interp = left_interp + delta;
827 left = left_interp + 0.9 * (left - left_interp);
828 dst[n] += src[n] * left * gain_coeff;
831 pan = left * gain_coeff;
833 for (; n < nframes; ++n) {
834 dst[n] += src[n] * pan;
840 pan = (*o).desired_pan;
842 if ((pan *= gain_coeff) != 1.0f) {
846 for (jack_nframes_t n = 0; n < nframes; ++n) {
847 dst[n] += src[n] * pan;
855 for (jack_nframes_t n = 0; n < nframes; ++n) {
870 Multi2dPanner::distribute_automated (Sample* src, Sample** obufs,
871 jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes,
884 Multi2dPanner::factory (Panner& p)
886 return new Multi2dPanner (p);
890 Multi2dPanner::load (istream& in, string path, uint32_t& linecnt)
896 Multi2dPanner::save (ostream& out) const
902 Multi2dPanner::get_state (void)
908 Multi2dPanner::state (bool full_state)
910 XMLNode* root = new XMLNode ("StreamPanner");
912 LocaleGuard lg (X_("POSIX"));
914 snprintf (buf, sizeof (buf), "%f", x);
915 root->add_property (X_("x"), buf);
916 snprintf (buf, sizeof (buf), "%f", y);
917 root->add_property (X_("y"), buf);
918 root->add_property (X_("type"), Multi2dPanner::name);
924 Multi2dPanner::set_state (const XMLNode& node)
926 const XMLProperty* prop;
928 LocaleGuard lg (X_("POSIX"));
933 if ((prop = node.property (X_("x")))) {
934 newx = atof (prop->value().c_str());
937 if ((prop = node.property (X_("y")))) {
938 newy = atof (prop->value().c_str());
941 if (x < 0 || y < 0) {
942 error << _("badly-formed positional data for Multi2dPanner - ignored")
947 set_position (newx, newy);
951 /*---------------------------------------------------------------------- */
953 Panner::Panner (string name, Session& s)
958 _link_direction = SameDirection;
961 reset_midi_control (_session.mmc_port(), _session.get_mmc_control());
969 Panner::set_linked (bool yn)
973 _session.set_dirty ();
974 LinkStateChanged (); /* EMIT SIGNAL */
979 Panner::set_link_direction (LinkDirection ld)
981 if (ld != _link_direction) {
982 _link_direction = ld;
983 _session.set_dirty ();
984 LinkStateChanged (); /* EMIT SIGNAL */
989 Panner::set_name (string str)
991 automation_path = _session.automation_dir();
992 automation_path += _session.snap_name();
993 automation_path += "-pan-";
994 automation_path += legalize_for_path (str);
995 automation_path += ".automation";
1000 Panner::set_bypassed (bool yn)
1002 if (yn != _bypassed) {
1010 Panner::reset (uint32_t nouts, uint32_t npans)
1013 bool changed = false;
1016 if (nouts < 2 || (nouts == outputs.size() && npans == size())) {
1039 fatal << _("programming error:")
1040 << X_("Panner::reset() called with a single output")
1047 outputs.push_back (Output (0, 0));
1048 outputs.push_back (Output (1.0, 0));
1050 for (n = 0; n < npans; ++n) {
1051 push_back (new EqualPowerStereoPanner (*this));
1056 outputs.push_back (Output (0.5, 0));
1057 outputs.push_back (Output (0, 1.0));
1058 outputs.push_back (Output (1.0, 1.0));
1060 for (n = 0; n < npans; ++n) {
1061 push_back (new Multi2dPanner (*this));
1067 outputs.push_back (Output (0, 0));
1068 outputs.push_back (Output (1.0, 0));
1069 outputs.push_back (Output (1.0, 1.0));
1070 outputs.push_back (Output (0, 1.0));
1072 for (n = 0; n < npans; ++n) {
1073 push_back (new Multi2dPanner (*this));
1078 case 5: //square+offcenter center
1079 outputs.push_back (Output (0, 0));
1080 outputs.push_back (Output (1.0, 0));
1081 outputs.push_back (Output (1.0, 1.0));
1082 outputs.push_back (Output (0, 1.0));
1083 outputs.push_back (Output (0.5, 0.75));
1085 for (n = 0; n < npans; ++n) {
1086 push_back (new Multi2dPanner (*this));
1092 /* XXX horrible placement. FIXME */
1093 for (n = 0; n < nouts; ++n) {
1094 outputs.push_back (Output (0.1 * n, 0.1 * n));
1097 for (n = 0; n < npans; ++n) {
1098 push_back (new Multi2dPanner (*this));
1104 for (iterator x = begin(); x != end(); ++x) {
1108 reset_midi_control (_session.mmc_port(), _session.get_mmc_control());
1110 /* force hard left/right panning in a common case: 2in/2out
1113 if (npans == 2 && outputs.size() == 2) {
1115 /* Do this only if we changed configuration, or our configuration
1116 appears to be the default set up (center).
1122 front()->get_position (left);
1123 back()->get_position (right);
1125 if (changed || ((left == 0.5) && (right == 0.5))) {
1127 front()->set_position (0.0);
1128 front()->automation().reset_default (0.0);
1130 back()->set_position (1.0);
1131 back()->automation().reset_default (1.0);
1138 Changed (); /* EMIT SIGNAL */
1145 Panner::remove (uint32_t which)
1147 vector<StreamPanner*>::iterator i;
1148 for (i = begin(); i != end() && which; ++i, --which);
1159 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1163 vector<StreamPanner*>::clear ();
1167 Panner::set_automation_style (AutoStyle style)
1169 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1170 (*i)->set_automation_style (style);
1172 _session.set_dirty ();
1176 Panner::set_automation_state (AutoState state)
1178 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1179 (*i)->set_automation_state (state);
1181 _session.set_dirty ();
1185 Panner::automation_state () const
1188 return front()->automation().automation_state ();
1195 Panner::automation_style () const
1198 return front()->automation().automation_style ();
1205 Panner::transport_stopped (jack_nframes_t frame)
1207 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1208 (*i)->transport_stopped (frame);
1213 Panner::snapshot (jack_nframes_t now)
1215 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1216 (*i)->snapshot (now);
1221 Panner::clear_automation ()
1223 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1224 (*i)->automation().clear ();
1226 _session.set_dirty ();
1230 Panner::save () const
1232 ofstream out (automation_path.c_str());
1235 error << string_compose (_("cannot open pan automation file \"%1\" for saving (%s)"), automation_path, strerror (errno))
1240 out << X_("version ") << current_automation_version_number << endl;
1242 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1243 if ((*i)->save (out)) {
1255 uint32_t linecnt = 0;
1258 LocaleGuard lg (X_("POSIX"));
1260 if (automation_path.length() == 0) {
1264 if (access (automation_path.c_str(), F_OK)) {
1268 ifstream in (automation_path.c_str());
1271 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1272 automation_path, strerror (errno))
1279 while (in.getline (line, sizeof(line), '\n')) {
1281 if (++linecnt == 1) {
1282 if (memcmp (line, X_("version"), 7) == 0) {
1283 if (sscanf (line, "version %f", &version) != 1) {
1284 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1288 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1289 automation_path, line) << endmsg;
1293 if (version != current_automation_version_number) {
1294 error << string_compose(_("mismatched pan automation event file version (%1)"), version) << endmsg;
1301 if (strlen (line) == 0 || line[0] == '#') {
1305 if (strcmp (line, "begin") == 0) {
1308 error << string_compose (_("too many panner states found in pan automation file %1"),
1314 if ((*sp)->load (in, automation_path, linecnt)) {
1328 StreamPanner* (*factory)(Panner&);
1331 PanPlugins pan_plugins[] = {
1332 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1333 { Multi2dPanner::name, 3, Multi2dPanner::factory },
1338 Panner::get_state (void)
1340 return state (true);
1344 Panner::state (bool full)
1346 XMLNode* root = new XMLNode (X_("Panner"));
1349 for (iterator p = begin(); p != end(); ++p) {
1350 root->add_child_nocopy ((*p)->state (full));
1353 root->add_property (X_("linked"), (_linked ? "yes" : "no"));
1354 snprintf (buf, sizeof (buf), "%d", _link_direction);
1355 root->add_property (X_("link_direction"), buf);
1356 root->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1358 /* add each output */
1360 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1361 XMLNode* onode = new XMLNode (X_("Output"));
1362 snprintf (buf, sizeof (buf), "%f", (*o).x);
1363 onode->add_property (X_("x"), buf);
1364 snprintf (buf, sizeof (buf), "%f", (*o).y);
1365 onode->add_property (X_("y"), buf);
1366 root->add_child_nocopy (*onode);
1371 root->add_property (X_("automation"), PBD::basename (automation_path));
1379 Panner::set_state (const XMLNode& node)
1382 XMLNodeConstIterator niter;
1383 const XMLProperty *prop;
1386 LocaleGuard lg (X_("POSIX"));
1391 if ((prop = node.property (X_("linked"))) != 0) {
1392 set_linked (prop->value() == "yes");
1396 if ((prop = node.property (X_("bypassed"))) != 0) {
1397 set_bypassed (prop->value() == "yes");
1400 if ((prop = node.property (X_("link_direction"))) != 0) {
1401 sscanf (prop->value().c_str(), "%d", &i);
1402 set_link_direction ((LinkDirection) (i));
1405 nlist = node.children();
1407 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1408 if ((*niter)->name() == X_("Output")) {
1412 prop = (*niter)->property (X_("x"));
1413 sscanf (prop->value().c_str(), "%f", &x);
1415 prop = (*niter)->property (X_("y"));
1416 sscanf (prop->value().c_str(), "%f", &y);
1418 outputs.push_back (Output (x, y));
1422 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1424 if ((*niter)->name() == X_("StreamPanner")) {
1426 if ((prop = (*niter)->property (X_("type")))) {
1428 for (i = 0; pan_plugins[i].factory; ++i) {
1429 if (prop->value() == pan_plugins[i].name) {
1432 /* note that we assume that all the stream panners
1433 are of the same type. pretty good
1434 assumption, but its still an assumption.
1437 sp = pan_plugins[i].factory (*this);
1439 if (sp->set_state (**niter) == 0) {
1448 if (!pan_plugins[i].factory) {
1449 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1455 error << _("panner plugin node has no type information!")
1463 /* don't try to load automation if it wasn't marked as existing */
1465 if ((prop = node.property (X_("automation")))) {
1467 /* automation path is relative */
1469 automation_path = _session.automation_dir();
1470 automation_path += prop->value ();
1479 Panner::touching () const
1481 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1482 if ((*i)->automation().touching ()) {
1491 Panner::reset_midi_control (MIDI::Port* port, bool on)
1493 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1494 (*i)->reset_midi_control (port, on);
1499 Panner::set_position (float xpos, StreamPanner& orig)
1505 orig.get_position (xnow);
1506 xdelta = xpos - xnow;
1508 if (_link_direction == SameDirection) {
1510 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1512 (*i)->set_position (xpos, true);
1514 (*i)->get_position (xnow);
1515 xnew = min (1.0f, xnow + xdelta);
1516 xnew = max (0.0f, xnew);
1517 (*i)->set_position (xnew, true);
1523 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1525 (*i)->set_position (xpos, true);
1527 (*i)->get_position (xnow);
1528 xnew = min (1.0f, xnow - xdelta);
1529 xnew = max (0.0f, xnew);
1530 (*i)->set_position (xnew, true);
1537 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1540 float xdelta, ydelta;
1543 orig.get_position (xnow, ynow);
1544 xdelta = xpos - xnow;
1545 ydelta = ypos - ynow;
1547 if (_link_direction == SameDirection) {
1549 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1551 (*i)->set_position (xpos, ypos, true);
1553 (*i)->get_position (xnow, ynow);
1555 xnew = min (1.0f, xnow + xdelta);
1556 xnew = max (0.0f, xnew);
1558 ynew = min (1.0f, ynow + ydelta);
1559 ynew = max (0.0f, ynew);
1561 (*i)->set_position (xnew, ynew, true);
1567 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1569 (*i)->set_position (xpos, ypos, true);
1571 (*i)->get_position (xnow, ynow);
1573 xnew = min (1.0f, xnow - xdelta);
1574 xnew = max (0.0f, xnew);
1576 ynew = min (1.0f, ynow - ydelta);
1577 ynew = max (0.0f, ynew);
1579 (*i)->set_position (xnew, ynew, true);
1586 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1588 float xnow, ynow, znow;
1589 float xdelta, ydelta, zdelta;
1590 float xnew, ynew, znew;
1592 orig.get_position (xnow, ynow, znow);
1593 xdelta = xpos - xnow;
1594 ydelta = ypos - ynow;
1595 zdelta = zpos - znow;
1597 if (_link_direction == SameDirection) {
1599 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1601 (*i)->set_position (xpos, ypos, zpos, true);
1603 (*i)->get_position (xnow, ynow, znow);
1605 xnew = min (1.0f, xnow + xdelta);
1606 xnew = max (0.0f, xnew);
1608 ynew = min (1.0f, ynow + ydelta);
1609 ynew = max (0.0f, ynew);
1611 znew = min (1.0f, znow + zdelta);
1612 znew = max (0.0f, znew);
1614 (*i)->set_position (xnew, ynew, znew, true);
1620 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1622 (*i)->set_position (xpos, ypos, true);
1624 (*i)->get_position (xnow, ynow, znow);
1626 xnew = min (1.0f, xnow - xdelta);
1627 xnew = max (0.0f, xnew);
1629 ynew = min (1.0f, ynow - ydelta);
1630 ynew = max (0.0f, ynew);
1632 znew = min (1.0f, znow + zdelta);
1633 znew = max (0.0f, znew);
1635 (*i)->set_position (xnew, ynew, znew, true);
1642 Panner::send_all_midi_feedback ()
1644 if (_session.get_midi_feedback()) {
1647 // do feedback for all panners
1648 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1649 (*i)->get_effective_position (xpos);
1651 (*i)->midi_control().send_feedback (xpos);
1658 Panner::write_midi_feedback (MIDI::byte* buf, int32_t& bufsize)
1660 AutoState astate = automation_state ();
1662 if (_session.get_midi_feedback() &&
1663 (astate == Play || (astate == Touch && !touching()))) {
1667 // do feedback for all panners
1668 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1669 (*i)->get_effective_position (xpos);
1671 buf = (*i)->midi_control().write_feedback (buf, bufsize, xpos);