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>
45 using namespace ARDOUR;
47 float Panner::current_automation_version_number = 1.0;
49 string EqualPowerStereoPanner::name = "Equal Power Stereo";
50 string Multi2dPanner::name = "Multiple (2D)";
52 /* this is a default mapper of MIDI control values to a pan position
53 others can be imagined. see Panner::set_midi_to_pan_function().
56 static pan_t direct_midi_to_pan (double fract) {
60 static double direct_pan_to_midi (pan_t val) {
64 StreamPanner::StreamPanner (Panner& p)
66 _midi_control (*this, (MIDI::Port*) 0)
75 StreamPanner::~StreamPanner ()
79 StreamPanner::MIDIControl::MIDIControl (StreamPanner& s, MIDI::Port* port)
80 : MIDI::Controllable (port, 0), sp (s), setting(false)
82 midi_to_pan = direct_midi_to_pan;
83 pan_to_midi = direct_pan_to_midi;
84 last_written = 0; /* XXX need a good out-of-bound-value */
88 StreamPanner::MIDIControl::set_value (float val)
91 sp.set_position (midi_to_pan (val));
96 StreamPanner::MIDIControl::send_feedback (pan_t value)
99 if (!setting && get_midi_feedback() && pan_to_midi) {
100 MIDI::byte val = (MIDI::byte) (pan_to_midi (value) * 127.0f);
101 MIDI::channel_t ch = 0;
102 MIDI::eventType ev = MIDI::none;
103 MIDI::byte additional = 0;
104 MIDI::EventTwoBytes data;
106 if (get_control_info (ch, ev, additional)) {
107 data.controller_number = additional;
110 sp.get_parent().session().send_midi_message (get_port(), ev, ch, data);
113 // send_midi_feedback (pan_to_midi (val));
119 StreamPanner::MIDIControl::write_feedback (MIDI::byte* buf, int32_t& bufsize, pan_t val, bool force)
121 if (get_midi_feedback() && pan_to_midi && bufsize > 2) {
122 MIDI::channel_t ch = 0;
123 MIDI::eventType ev = MIDI::none;
124 MIDI::byte additional = 0;
126 if (get_control_info (ch, ev, additional)) {
128 pm = (MIDI::byte) (pan_to_midi (val) * 127.0);
130 if (pm != last_written || force) {
131 *buf++ = (0xF0 & ev) | (0xF & ch);
132 *buf++ = additional; /* controller number */
145 StreamPanner::reset_midi_control (MIDI::Port* port, bool on)
151 _midi_control.get_control_info (chn, ev, extra);
155 _midi_control.midi_rebind (port, chn);
159 StreamPanner::set_muted (bool yn)
168 StreamPanner::set_position (float xpos, bool link_call)
170 if (!link_call && parent.linked()) {
171 parent.set_position (xpos, *this);
179 if (parent.session().get_midi_feedback()) {
180 _midi_control.send_feedback (x);
186 StreamPanner::set_position (float xpos, float ypos, bool link_call)
188 if (!link_call && parent.linked()) {
189 parent.set_position (xpos, ypos, *this);
192 if (x != xpos || y != ypos) {
202 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
204 if (!link_call && parent.linked()) {
205 parent.set_position (xpos, ypos, zpos, *this);
208 if (x != xpos || y != ypos || z != zpos) {
218 StreamPanner::set_state (const XMLNode& node)
220 const XMLProperty* prop;
221 XMLNodeConstIterator iter;
222 XMLNodeList midi_kids;
224 if ((prop = node.property (X_("muted")))) {
225 set_muted (prop->value() == "yes");
228 midi_kids = node.children ("MIDI");
230 for (iter = midi_kids.begin(); iter != midi_kids.end(); ++iter) {
233 XMLNodeConstIterator miter;
236 kids = (*iter)->children ();
238 for (miter = kids.begin(); miter != kids.end(); ++miter) {
242 if (child->name() == "pan") {
244 MIDI::eventType ev = MIDI::on; /* initialize to keep gcc happy */
245 MIDI::byte additional = 0; /* ditto */
246 MIDI::channel_t chn = 0; /* ditto */
248 if (get_midi_node_info (child, ev, chn, additional)) {
249 _midi_control.set_control_type (chn, ev, additional);
251 error << _("MIDI pan control specification is incomplete, so it has been ignored") << endmsg;
262 StreamPanner::add_state (XMLNode& node)
264 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
270 MIDI::byte additional;
271 XMLNode* midi_node = 0;
274 if (_midi_control.get_control_info (chn, ev, additional)) {
276 midi_node = node.add_child ("MIDI");
278 child = midi_node->add_child ("pan");
279 set_midi_node_info (child, ev, chn, additional);
286 StreamPanner::get_midi_node_info (XMLNode * node, MIDI::eventType & ev, MIDI::channel_t & chan, MIDI::byte & additional)
289 const XMLProperty* prop;
292 if ((prop = node->property ("event")) != 0) {
293 sscanf (prop->value().c_str(), "0x%x", &xx);
294 ev = (MIDI::eventType) xx;
299 if (ok && ((prop = node->property ("channel")) != 0)) {
300 sscanf (prop->value().c_str(), "%d", &xx);
301 chan = (MIDI::channel_t) xx;
306 if (ok && ((prop = node->property ("additional")) != 0)) {
307 sscanf (prop->value().c_str(), "0x%x", &xx);
308 additional = (MIDI::byte) xx;
315 StreamPanner::set_midi_node_info (XMLNode * node, MIDI::eventType ev, MIDI::channel_t chan, MIDI::byte additional)
319 snprintf (buf, sizeof(buf), "0x%x", ev);
320 node->add_property ("event", buf);
321 snprintf (buf, sizeof(buf), "%d", chan);
322 node->add_property ("channel", buf);
323 snprintf (buf, sizeof(buf), "0x%x", additional);
324 node->add_property ("additional", buf);
329 /*---------------------------------------------------------------------- */
331 BaseStereoPanner::BaseStereoPanner (Panner& p)
332 : StreamPanner (p), _automation (0.0, 1.0, 0.5)
336 BaseStereoPanner::~BaseStereoPanner ()
341 BaseStereoPanner::snapshot (jack_nframes_t now)
343 if (_automation.automation_state() == Write || _automation.automation_state() == Touch) {
344 _automation.rt_add (now, x);
349 BaseStereoPanner::transport_stopped (jack_nframes_t frame)
351 _automation.reposition_for_rt_add (frame);
353 if (_automation.automation_state() != Off) {
355 if (_automation.automation_write()) {
356 _automation.save_state (_("automation write pass"));
359 set_position (_automation.eval (frame));
364 BaseStereoPanner::set_automation_style (AutoStyle style)
366 _automation.set_automation_style (style);
370 BaseStereoPanner::set_automation_state (AutoState state)
372 if (state != _automation.automation_state()) {
374 _automation.set_automation_state (state);
377 set_position (_automation.eval (parent.session().transport_frame()));
383 BaseStereoPanner::save (ostream& out) const
385 LocaleGuard lg (X_("POSIX"));
387 /* force a single format for numeric data to ease session interchange
388 across national boundaries.
391 out << "begin" << endl;
393 for (AutomationList::const_iterator i = _automation.const_begin(); i != _automation.const_end(); ++i) {
394 out << '\t' << (jack_nframes_t) floor ((*i)->when) << ' ' << (*i)->value << endl;
396 error << string_compose (_("error writing pan automation file (%s)"), strerror (errno)) << endmsg;
400 out << "end" << endl;
406 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
409 LocaleGuard lg (X_("POSIX"));
411 _automation.clear ();
413 while (in.getline (line, sizeof (line), '\n')) {
419 if (strcmp (line, "end") == 0) {
423 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
424 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
428 _automation.add (when, value, true);
431 /* now that we are done loading */
433 _automation.save_state (_("loaded from disk"));
434 _automation.StateChanged (Change (0));
440 BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, jack_nframes_t nframes)
454 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
456 /* interpolate over 64 frames or nframes, whichever is smaller */
458 jack_nframes_t limit = min ((jack_nframes_t)64, nframes);
461 delta = -(delta / (float) (limit));
463 for (n = 0; n < limit; n++) {
464 left_interp = left_interp + delta;
465 left = left_interp + 0.9 * (left - left_interp);
466 dst[n] += src[n] * left * gain_coeff;
469 pan = left * gain_coeff;
471 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
478 if ((pan = (left * gain_coeff)) != 1.0f) {
482 Session::mix_buffers_with_gain(dst,src,nframes,pan);
484 /* mark that we wrote into the buffer */
492 Session::mix_buffers_no_gain(dst,src,nframes);
494 /* mark that we wrote into the buffer */
504 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
506 /* interpolate over 64 frames or nframes, whichever is smaller */
508 jack_nframes_t limit = min ((jack_nframes_t)64, nframes);
511 delta = -(delta / (float) (limit));
513 for (n = 0; n < limit; n++) {
514 right_interp = right_interp + delta;
515 right = right_interp + 0.9 * (right - right_interp);
516 dst[n] += src[n] * right * gain_coeff;
519 pan = right * gain_coeff;
521 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
523 /* XXX it would be nice to mark the buffer as written to */
527 right = desired_right;
528 right_interp = right;
530 if ((pan = (right * gain_coeff)) != 1.0f) {
534 Session::mix_buffers_with_gain(dst,src,nframes,pan);
536 /* XXX it would be nice to mark the buffer as written to */
541 Session::mix_buffers_no_gain(dst,src,nframes);
543 /* XXX it would be nice to mark the buffer as written to */
548 /*---------------------------------------------------------------------- */
550 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p)
551 : BaseStereoPanner (p)
556 right = desired_right;
558 right_interp = right;
561 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
566 EqualPowerStereoPanner::update ()
568 /* it would be very nice to split this out into a virtual function
569 that can be accessed from BaseStereoPanner and used in distribute_automated().
571 but the place where its used in distribute_automated() is a tight inner loop,
572 and making "nframes" virtual function calls to compute values is an absurd
576 /* x == 0 => hard left
581 float panL = 1 - panR;
583 const float pan_law_attenuation = -3.0f;
584 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
586 desired_left = panL * (scale * panL + 1.0f - scale);
587 desired_right = panR * (scale * panR + 1.0f - scale);
593 EqualPowerStereoPanner::distribute_automated (Sample* src, Sample** obufs,
594 jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes,
600 /* fetch positional data */
602 if (!_automation.rt_safe_get_vector (start, end, buffers[0], nframes)) {
605 distribute (src, obufs, 1.0, nframes);
610 /* store effective pan position. do this even if we are muted */
612 effective_x = buffers[0][nframes-1];
618 /* apply pan law to convert positional data into pan coefficients for
622 const float pan_law_attenuation = -3.0f;
623 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
625 for (jack_nframes_t n = 0; n < nframes; ++n) {
627 float panR = buffers[0][n];
628 float panL = 1 - panR;
630 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
631 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
639 for (jack_nframes_t n = 0; n < nframes; ++n) {
640 dst[n] += src[n] * pbuf[n];
643 /* XXX it would be nice to mark the buffer as written to */
650 for (jack_nframes_t n = 0; n < nframes; ++n) {
651 dst[n] += src[n] * pbuf[n];
654 /* XXX it would be nice to mark the buffer as written to */
658 EqualPowerStereoPanner::factory (Panner& parent)
660 return new EqualPowerStereoPanner (parent);
664 EqualPowerStereoPanner::get_state (void)
670 EqualPowerStereoPanner::state (bool full_state)
672 XMLNode* root = new XMLNode ("StreamPanner");
674 LocaleGuard lg (X_("POSIX"));
676 snprintf (buf, sizeof (buf), "%f", x);
677 root->add_property (X_("x"), buf);
678 root->add_property (X_("type"), EqualPowerStereoPanner::name);
680 snprintf (buf, sizeof (buf), "0x%x", _automation.automation_state());
682 /* never store automation states other than off in a template */
683 snprintf (buf, sizeof (buf), "0x%x", ARDOUR::Off);
685 root->add_property (X_("automation-state"), buf);
686 snprintf (buf, sizeof (buf), "0x%x", _automation.automation_style());
687 root->add_property (X_("automation-style"), buf);
689 StreamPanner::add_state (*root);
695 EqualPowerStereoPanner::set_state (const XMLNode& node)
697 const XMLProperty* prop;
700 LocaleGuard lg (X_("POSIX"));
702 if ((prop = node.property (X_("x")))) {
703 pos = atof (prop->value().c_str());
704 set_position (pos, true);
707 if ((prop = node.property (X_("automation-state")))) {
708 sscanf (prop->value().c_str(), "0x%x", &x);
709 _automation.set_automation_state ((AutoState) x);
712 set_position (_automation.eval (parent.session().transport_frame()));
716 if ((prop = node.property (X_("automation-style")))) {
717 sscanf (prop->value().c_str(), "0x%x", &x);
718 _automation.set_automation_style ((AutoStyle) x);
721 StreamPanner::set_state (node);
726 /*----------------------------------------------------------------------*/
728 Multi2dPanner::Multi2dPanner (Panner& p)
729 : StreamPanner (p), _automation (0.0, 1.0, 0.5) // XXX useless
734 Multi2dPanner::~Multi2dPanner ()
739 Multi2dPanner::snapshot (jack_nframes_t now)
745 Multi2dPanner::transport_stopped (jack_nframes_t frame)
751 Multi2dPanner::set_automation_style (AutoStyle style)
757 Multi2dPanner::set_automation_state (AutoState state)
763 Multi2dPanner::update ()
765 static const float BIAS = FLT_MIN;
767 uint32_t nouts = parent.outputs.size();
774 for (i = 0; i < nouts; i++) {
775 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
779 f += dsq[i] * dsq[i];
781 fr = 1.0f / sqrtf(f);
783 for (i = 0; i < nouts; ++i) {
784 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
791 Multi2dPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, jack_nframes_t nframes)
795 vector<Panner::Output>::iterator o;
803 for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
808 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
810 /* interpolate over 64 frames or nframes, whichever is smaller */
812 jack_nframes_t limit = min ((jack_nframes_t)64, nframes);
815 delta = -(delta / (float) (limit));
817 for (n = 0; n < limit; n++) {
818 left_interp = left_interp + delta;
819 left = left_interp + 0.9 * (left - left_interp);
820 dst[n] += src[n] * left * gain_coeff;
823 pan = left * gain_coeff;
825 for (; n < nframes; ++n) {
826 dst[n] += src[n] * pan;
832 pan = (*o).desired_pan;
834 if ((pan *= gain_coeff) != 1.0f) {
838 for (jack_nframes_t n = 0; n < nframes; ++n) {
839 dst[n] += src[n] * pan;
847 for (jack_nframes_t n = 0; n < nframes; ++n) {
862 Multi2dPanner::distribute_automated (Sample* src, Sample** obufs,
863 jack_nframes_t start, jack_nframes_t end, jack_nframes_t nframes,
876 Multi2dPanner::factory (Panner& p)
878 return new Multi2dPanner (p);
882 Multi2dPanner::load (istream& in, string path, uint32_t& linecnt)
888 Multi2dPanner::save (ostream& out) const
894 Multi2dPanner::get_state (void)
900 Multi2dPanner::state (bool full_state)
902 XMLNode* root = new XMLNode ("StreamPanner");
904 LocaleGuard lg (X_("POSIX"));
906 snprintf (buf, sizeof (buf), "%f", x);
907 root->add_property (X_("x"), buf);
908 snprintf (buf, sizeof (buf), "%f", y);
909 root->add_property (X_("y"), buf);
910 root->add_property (X_("type"), Multi2dPanner::name);
916 Multi2dPanner::set_state (const XMLNode& node)
918 const XMLProperty* prop;
920 LocaleGuard lg (X_("POSIX"));
925 if ((prop = node.property (X_("x")))) {
926 newx = atof (prop->value().c_str());
929 if ((prop = node.property (X_("y")))) {
930 newy = atof (prop->value().c_str());
933 if (x < 0 || y < 0) {
934 error << _("badly-formed positional data for Multi2dPanner - ignored")
939 set_position (newx, newy);
943 /*---------------------------------------------------------------------- */
945 Panner::Panner (string name, Session& s)
950 _link_direction = SameDirection;
953 reset_midi_control (_session.mmc_port(), _session.get_mmc_control());
961 Panner::set_linked (bool yn)
965 _session.set_dirty ();
966 LinkStateChanged (); /* EMIT SIGNAL */
971 Panner::set_link_direction (LinkDirection ld)
973 if (ld != _link_direction) {
974 _link_direction = ld;
975 _session.set_dirty ();
976 LinkStateChanged (); /* EMIT SIGNAL */
981 Panner::set_name (string str)
983 automation_path = _session.automation_dir();
984 automation_path += _session.snap_name();
985 automation_path += "-pan-";
986 automation_path += legalize_for_path (str);
987 automation_path += ".automation";
992 Panner::set_bypassed (bool yn)
994 if (yn != _bypassed) {
1002 Panner::reset (uint32_t nouts, uint32_t npans)
1005 bool changed = false;
1008 if (nouts < 2 || (nouts == outputs.size() && npans == size())) {
1031 fatal << _("programming error:")
1032 << X_("Panner::reset() called with a single output")
1039 outputs.push_back (Output (0, 0));
1040 outputs.push_back (Output (1.0, 0));
1042 for (n = 0; n < npans; ++n) {
1043 push_back (new EqualPowerStereoPanner (*this));
1048 outputs.push_back (Output (0.5, 0));
1049 outputs.push_back (Output (0, 1.0));
1050 outputs.push_back (Output (1.0, 1.0));
1052 for (n = 0; n < npans; ++n) {
1053 push_back (new Multi2dPanner (*this));
1059 outputs.push_back (Output (0, 0));
1060 outputs.push_back (Output (1.0, 0));
1061 outputs.push_back (Output (1.0, 1.0));
1062 outputs.push_back (Output (0, 1.0));
1064 for (n = 0; n < npans; ++n) {
1065 push_back (new Multi2dPanner (*this));
1070 case 5: //square+offcenter center
1071 outputs.push_back (Output (0, 0));
1072 outputs.push_back (Output (1.0, 0));
1073 outputs.push_back (Output (1.0, 1.0));
1074 outputs.push_back (Output (0, 1.0));
1075 outputs.push_back (Output (0.5, 0.75));
1077 for (n = 0; n < npans; ++n) {
1078 push_back (new Multi2dPanner (*this));
1084 /* XXX horrible placement. FIXME */
1085 for (n = 0; n < nouts; ++n) {
1086 outputs.push_back (Output (0.1 * n, 0.1 * n));
1089 for (n = 0; n < npans; ++n) {
1090 push_back (new Multi2dPanner (*this));
1096 for (iterator x = begin(); x != end(); ++x) {
1100 reset_midi_control (_session.mmc_port(), _session.get_mmc_control());
1102 /* force hard left/right panning in a common case: 2in/2out
1105 if (npans == 2 && outputs.size() == 2) {
1107 /* Do this only if we changed configuration, or our configuration
1108 appears to be the default set up (center).
1114 front()->get_position (left);
1115 back()->get_position (right);
1117 if (changed || ((left == 0.5) && (right == 0.5))) {
1119 front()->set_position (0.0);
1120 front()->automation().reset_default (0.0);
1122 back()->set_position (1.0);
1123 back()->automation().reset_default (1.0);
1130 Changed (); /* EMIT SIGNAL */
1137 Panner::remove (uint32_t which)
1139 vector<StreamPanner*>::iterator i;
1140 for (i = begin(); i != end() && which; ++i, --which);
1151 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1155 vector<StreamPanner*>::clear ();
1159 Panner::set_automation_style (AutoStyle style)
1161 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1162 (*i)->set_automation_style (style);
1164 _session.set_dirty ();
1168 Panner::set_automation_state (AutoState state)
1170 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1171 (*i)->set_automation_state (state);
1173 _session.set_dirty ();
1177 Panner::automation_state () const
1180 return front()->automation().automation_state ();
1187 Panner::automation_style () const
1190 return front()->automation().automation_style ();
1197 Panner::transport_stopped (jack_nframes_t frame)
1199 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1200 (*i)->transport_stopped (frame);
1205 Panner::snapshot (jack_nframes_t now)
1207 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1208 (*i)->snapshot (now);
1213 Panner::clear_automation ()
1215 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1216 (*i)->automation().clear ();
1218 _session.set_dirty ();
1222 Panner::save () const
1224 ofstream out (automation_path.c_str());
1227 error << string_compose (_("cannot open pan automation file \"%1\" for saving (%s)"), automation_path, strerror (errno))
1232 out << X_("version ") << current_automation_version_number << endl;
1234 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1235 if ((*i)->save (out)) {
1247 uint32_t linecnt = 0;
1250 LocaleGuard lg (X_("POSIX"));
1252 if (automation_path.length() == 0) {
1256 if (access (automation_path.c_str(), F_OK)) {
1260 ifstream in (automation_path.c_str());
1263 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1264 automation_path, strerror (errno))
1271 while (in.getline (line, sizeof(line), '\n')) {
1273 if (++linecnt == 1) {
1274 if (memcmp (line, X_("version"), 7) == 0) {
1275 if (sscanf (line, "version %f", &version) != 1) {
1276 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1280 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1281 automation_path, line) << endmsg;
1285 if (version != current_automation_version_number) {
1286 error << string_compose(_("mismatched pan automation event file version (%1)"), version) << endmsg;
1293 if (strlen (line) == 0 || line[0] == '#') {
1297 if (strcmp (line, "begin") == 0) {
1300 error << string_compose (_("too many panner states found in pan automation file %1"),
1306 if ((*sp)->load (in, automation_path, linecnt)) {
1320 StreamPanner* (*factory)(Panner&);
1323 PanPlugins pan_plugins[] = {
1324 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1325 { Multi2dPanner::name, 3, Multi2dPanner::factory },
1330 Panner::get_state (void)
1332 return state (true);
1336 Panner::state (bool full)
1338 XMLNode* root = new XMLNode (X_("Panner"));
1341 for (iterator p = begin(); p != end(); ++p) {
1342 root->add_child_nocopy ((*p)->state (full));
1345 root->add_property (X_("linked"), (_linked ? "yes" : "no"));
1346 snprintf (buf, sizeof (buf), "%d", _link_direction);
1347 root->add_property (X_("link_direction"), buf);
1348 root->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1350 /* add each output */
1352 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1353 XMLNode* onode = new XMLNode (X_("Output"));
1354 snprintf (buf, sizeof (buf), "%f", (*o).x);
1355 onode->add_property (X_("x"), buf);
1356 snprintf (buf, sizeof (buf), "%f", (*o).y);
1357 onode->add_property (X_("y"), buf);
1358 root->add_child_nocopy (*onode);
1363 root->add_property (X_("automation"), PBD::basename (automation_path));
1371 Panner::set_state (const XMLNode& node)
1374 XMLNodeConstIterator niter;
1375 const XMLProperty *prop;
1378 LocaleGuard lg (X_("POSIX"));
1383 if ((prop = node.property (X_("linked"))) != 0) {
1384 set_linked (prop->value() == "yes");
1388 if ((prop = node.property (X_("bypassed"))) != 0) {
1389 set_bypassed (prop->value() == "yes");
1392 if ((prop = node.property (X_("link_direction"))) != 0) {
1393 sscanf (prop->value().c_str(), "%d", &i);
1394 set_link_direction ((LinkDirection) (i));
1397 nlist = node.children();
1399 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1400 if ((*niter)->name() == X_("Output")) {
1404 prop = (*niter)->property (X_("x"));
1405 sscanf (prop->value().c_str(), "%f", &x);
1407 prop = (*niter)->property (X_("y"));
1408 sscanf (prop->value().c_str(), "%f", &y);
1410 outputs.push_back (Output (x, y));
1414 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1416 if ((*niter)->name() == X_("StreamPanner")) {
1418 if ((prop = (*niter)->property (X_("type")))) {
1420 for (i = 0; pan_plugins[i].factory; ++i) {
1421 if (prop->value() == pan_plugins[i].name) {
1424 /* note that we assume that all the stream panners
1425 are of the same type. pretty good
1426 assumption, but its still an assumption.
1429 sp = pan_plugins[i].factory (*this);
1431 if (sp->set_state (**niter) == 0) {
1440 if (!pan_plugins[i].factory) {
1441 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1447 error << _("panner plugin node has no type information!")
1455 /* don't try to load automation if it wasn't marked as existing */
1457 if ((prop = node.property (X_("automation")))) {
1459 /* automation path is relative */
1461 automation_path = _session.automation_dir();
1462 automation_path += prop->value ();
1471 Panner::touching () const
1473 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1474 if ((*i)->automation().touching ()) {
1483 Panner::reset_midi_control (MIDI::Port* port, bool on)
1485 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1486 (*i)->reset_midi_control (port, on);
1491 Panner::set_position (float xpos, StreamPanner& orig)
1497 orig.get_position (xnow);
1498 xdelta = xpos - xnow;
1500 if (_link_direction == SameDirection) {
1502 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1504 (*i)->set_position (xpos, true);
1506 (*i)->get_position (xnow);
1507 xnew = min (1.0f, xnow + xdelta);
1508 xnew = max (0.0f, xnew);
1509 (*i)->set_position (xnew, true);
1515 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1517 (*i)->set_position (xpos, true);
1519 (*i)->get_position (xnow);
1520 xnew = min (1.0f, xnow - xdelta);
1521 xnew = max (0.0f, xnew);
1522 (*i)->set_position (xnew, true);
1529 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1532 float xdelta, ydelta;
1535 orig.get_position (xnow, ynow);
1536 xdelta = xpos - xnow;
1537 ydelta = ypos - ynow;
1539 if (_link_direction == SameDirection) {
1541 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1543 (*i)->set_position (xpos, ypos, true);
1545 (*i)->get_position (xnow, ynow);
1547 xnew = min (1.0f, xnow + xdelta);
1548 xnew = max (0.0f, xnew);
1550 ynew = min (1.0f, ynow + ydelta);
1551 ynew = max (0.0f, ynew);
1553 (*i)->set_position (xnew, ynew, true);
1559 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1561 (*i)->set_position (xpos, ypos, true);
1563 (*i)->get_position (xnow, ynow);
1565 xnew = min (1.0f, xnow - xdelta);
1566 xnew = max (0.0f, xnew);
1568 ynew = min (1.0f, ynow - ydelta);
1569 ynew = max (0.0f, ynew);
1571 (*i)->set_position (xnew, ynew, true);
1578 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1580 float xnow, ynow, znow;
1581 float xdelta, ydelta, zdelta;
1582 float xnew, ynew, znew;
1584 orig.get_position (xnow, ynow, znow);
1585 xdelta = xpos - xnow;
1586 ydelta = ypos - ynow;
1587 zdelta = zpos - znow;
1589 if (_link_direction == SameDirection) {
1591 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1593 (*i)->set_position (xpos, ypos, zpos, true);
1595 (*i)->get_position (xnow, ynow, znow);
1597 xnew = min (1.0f, xnow + xdelta);
1598 xnew = max (0.0f, xnew);
1600 ynew = min (1.0f, ynow + ydelta);
1601 ynew = max (0.0f, ynew);
1603 znew = min (1.0f, znow + zdelta);
1604 znew = max (0.0f, znew);
1606 (*i)->set_position (xnew, ynew, znew, true);
1612 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1614 (*i)->set_position (xpos, ypos, true);
1616 (*i)->get_position (xnow, ynow, znow);
1618 xnew = min (1.0f, xnow - xdelta);
1619 xnew = max (0.0f, xnew);
1621 ynew = min (1.0f, ynow - ydelta);
1622 ynew = max (0.0f, ynew);
1624 znew = min (1.0f, znow + zdelta);
1625 znew = max (0.0f, znew);
1627 (*i)->set_position (xnew, ynew, znew, true);
1634 Panner::send_all_midi_feedback ()
1636 if (_session.get_midi_feedback()) {
1639 // do feedback for all panners
1640 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1641 (*i)->get_effective_position (xpos);
1643 (*i)->midi_control().send_feedback (xpos);
1650 Panner::write_midi_feedback (MIDI::byte* buf, int32_t& bufsize)
1652 AutoState astate = automation_state ();
1654 if (_session.get_midi_feedback() &&
1655 (astate == Play || (astate == Touch && !touching()))) {
1659 // do feedback for all panners
1660 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1661 (*i)->get_effective_position (xpos);
1663 buf = (*i)->midi_control().write_feedback (buf, bufsize, xpos);