merge from thirdparty rubberband 1.3 @ 4901
[ardour2.git] / libs / ardour / panner.cc
blob4848f559cc8d4315a96fdc30a32fb024ecceabf5
1 /*
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.
20 #include <cmath>
21 #include <cerrno>
22 #include <fstream>
23 #include <cstdlib>
24 #include <string>
25 #include <cstdio>
26 #include <locale.h>
27 #include <unistd.h>
28 #include <float.h>
30 #include <glibmm.h>
32 #include <pbd/error.h>
33 #include <pbd/failed_constructor.h>
34 #include <pbd/xml++.h>
35 #include <pbd/enumwriter.h>
37 #include <ardour/session.h>
38 #include <ardour/panner.h>
39 #include <ardour/utils.h>
41 #include <ardour/mix.h>
43 #include "i18n.h"
45 #include <pbd/mathfix.h>
47 using namespace std;
48 using namespace ARDOUR;
49 using namespace PBD;
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 control values to a pan position
57 others can be imagined.
60 static pan_t direct_control_to_pan (double fract) {
61 return fract;
64 static double direct_pan_to_control (pan_t val) {
65 return val;
68 StreamPanner::StreamPanner (Panner& p)
69 : parent (p),
70 _control (X_("panner"), *this)
72 _muted = false;
74 parent.session().add_controllable (&_control);
76 x = 0.5;
77 y = 0.5;
78 z = 0.5;
81 StreamPanner::~StreamPanner ()
85 void
86 StreamPanner::PanControllable::set_value (float val)
88 panner.set_position (direct_control_to_pan (val));
91 float
92 StreamPanner::PanControllable::get_value (void) const
94 float xpos;
95 panner.get_effective_position (xpos);
96 return direct_pan_to_control (xpos);
99 bool
100 StreamPanner::PanControllable::can_send_feedback () const
102 AutoState astate = panner.get_parent().automation_state ();
104 if ((astate == Play) || (astate == Touch && !panner.get_parent().touching())) {
105 return true;
108 return false;
111 void
112 StreamPanner::set_muted (bool yn)
114 if (yn != _muted) {
115 _muted = yn;
116 StateChanged ();
120 void
121 StreamPanner::set_position (float xpos, bool link_call)
123 if (!link_call && parent.linked()) {
124 parent.set_position (xpos, *this);
127 if (x != xpos) {
128 x = xpos;
129 update ();
130 Changed ();
131 _control.Changed ();
135 void
136 StreamPanner::set_position (float xpos, float ypos, bool link_call)
138 if (!link_call && parent.linked()) {
139 parent.set_position (xpos, ypos, *this);
142 if (x != xpos || y != ypos) {
144 x = xpos;
145 y = ypos;
146 update ();
147 Changed ();
151 void
152 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
154 if (!link_call && parent.linked()) {
155 parent.set_position (xpos, ypos, zpos, *this);
158 if (x != xpos || y != ypos || z != zpos) {
159 x = xpos;
160 y = ypos;
161 z = zpos;
162 update ();
163 Changed ();
168 StreamPanner::set_state (const XMLNode& node)
170 const XMLProperty* prop;
171 XMLNodeConstIterator iter;
173 if ((prop = node.property (X_("muted")))) {
174 set_muted (prop->value() == "yes");
177 return 0;
180 void
181 StreamPanner::add_state (XMLNode& node)
183 node.add_property (X_("muted"), (muted() ? "yes" : "no"));
186 /*---------------------------------------------------------------------- */
188 BaseStereoPanner::BaseStereoPanner (Panner& p)
189 : StreamPanner (p), _automation (0.0, 1.0, 0.5)
193 BaseStereoPanner::~BaseStereoPanner ()
197 void
198 BaseStereoPanner::snapshot (nframes_t now)
200 if (_automation.automation_state() == Write || _automation.automation_state() == Touch) {
201 _automation.rt_add (now, x);
205 void
206 BaseStereoPanner::transport_stopped (nframes_t frame)
208 if (_automation.automation_state() != Off) {
209 set_position (_automation.eval (frame));
212 _automation.reposition_for_rt_add (frame);
215 void
216 BaseStereoPanner::set_automation_style (AutoStyle style)
218 _automation.set_automation_style (style);
221 void
222 BaseStereoPanner::set_automation_state (AutoState state)
224 if (state != _automation.automation_state()) {
226 _automation.set_automation_state (state);
228 if (state != Off) {
229 set_position (_automation.eval (parent.session().transport_frame()));
235 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
237 char line[128];
238 LocaleGuard lg (X_("POSIX"));
240 _automation.clear ();
242 while (in.getline (line, sizeof (line), '\n')) {
243 jack_nframes_t when;
244 double value;
246 ++linecnt;
248 if (strcmp (line, "end") == 0) {
249 break;
252 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
253 warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
254 continue;
257 _automation.fast_simple_add (when, value);
260 /* now that we are done loading */
262 _automation.StateChanged ();
264 return 0;
267 void
268 BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nframes_t nframes)
270 pan_t delta;
271 Sample* dst;
272 pan_t pan;
274 if (_muted) {
275 return;
278 /* LEFT */
280 dst = obufs[0];
282 if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
284 /* interpolate over 64 frames or nframes, whichever is smaller */
286 nframes_t limit = min ((nframes_t)64, nframes);
287 nframes_t n;
289 delta = -(delta / (float) (limit));
291 for (n = 0; n < limit; n++) {
292 left_interp = left_interp + delta;
293 left = left_interp + 0.9 * (left - left_interp);
294 dst[n] += src[n] * left * gain_coeff;
297 pan = left * gain_coeff;
299 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
301 } else {
303 left = desired_left;
304 left_interp = left;
306 if ((pan = (left * gain_coeff)) != 1.0f) {
308 if (pan != 0.0f) {
310 Session::mix_buffers_with_gain(dst,src,nframes,pan);
312 /* mark that we wrote into the buffer */
314 // obufs[0] = 0;
318 } else {
320 Session::mix_buffers_no_gain(dst,src,nframes);
322 /* mark that we wrote into the buffer */
324 // obufs[0] = 0;
328 /* RIGHT */
330 dst = obufs[1];
332 if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
334 /* interpolate over 64 frames or nframes, whichever is smaller */
336 nframes_t limit = min ((nframes_t)64, nframes);
337 nframes_t n;
339 delta = -(delta / (float) (limit));
341 for (n = 0; n < limit; n++) {
342 right_interp = right_interp + delta;
343 right = right_interp + 0.9 * (right - right_interp);
344 dst[n] += src[n] * right * gain_coeff;
347 pan = right * gain_coeff;
349 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
351 /* XXX it would be nice to mark the buffer as written to */
353 } else {
355 right = desired_right;
356 right_interp = right;
358 if ((pan = (right * gain_coeff)) != 1.0f) {
360 if (pan != 0.0f) {
362 Session::mix_buffers_with_gain(dst,src,nframes,pan);
364 /* XXX it would be nice to mark the buffer as written to */
367 } else {
369 Session::mix_buffers_no_gain(dst,src,nframes);
371 /* XXX it would be nice to mark the buffer as written to */
376 /*---------------------------------------------------------------------- */
378 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p)
379 : BaseStereoPanner (p)
381 update ();
383 left = desired_left;
384 right = desired_right;
385 left_interp = left;
386 right_interp = right;
389 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
393 void
394 EqualPowerStereoPanner::update ()
396 /* it would be very nice to split this out into a virtual function
397 that can be accessed from BaseStereoPanner and used in distribute_automated().
399 but the place where its used in distribute_automated() is a tight inner loop,
400 and making "nframes" virtual function calls to compute values is an absurd
401 overhead.
404 /* x == 0 => hard left
405 x == 1 => hard right
408 float panR = x;
409 float panL = 1 - panR;
411 const float pan_law_attenuation = -3.0f;
412 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
414 desired_left = panL * (scale * panL + 1.0f - scale);
415 desired_right = panR * (scale * panR + 1.0f - scale);
417 effective_x = x;
420 void
421 EqualPowerStereoPanner::distribute_automated (Sample* src, Sample** obufs,
422 nframes_t start, nframes_t end, nframes_t nframes,
423 pan_t** buffers)
425 Sample* dst;
426 pan_t* pbuf;
428 /* fetch positional data */
430 if (!_automation.rt_safe_get_vector (start, end, buffers[0], nframes)) {
431 /* fallback */
432 if (!_muted) {
433 distribute (src, obufs, 1.0, nframes);
435 return;
438 /* store effective pan position. do this even if we are muted */
440 if (nframes > 0)
441 effective_x = buffers[0][nframes-1];
443 if (_muted) {
444 return;
447 /* apply pan law to convert positional data into pan coefficients for
448 each buffer (output)
451 const float pan_law_attenuation = -3.0f;
452 const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
454 for (nframes_t n = 0; n < nframes; ++n) {
456 float panR = buffers[0][n];
457 float panL = 1 - panR;
459 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
460 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
463 /* LEFT */
465 dst = obufs[0];
466 pbuf = buffers[0];
468 for (nframes_t n = 0; n < nframes; ++n) {
469 dst[n] += src[n] * pbuf[n];
472 /* XXX it would be nice to mark the buffer as written to */
474 /* RIGHT */
476 dst = obufs[1];
477 pbuf = buffers[1];
479 for (nframes_t n = 0; n < nframes; ++n) {
480 dst[n] += src[n] * pbuf[n];
483 /* XXX it would be nice to mark the buffer as written to */
486 StreamPanner*
487 EqualPowerStereoPanner::factory (Panner& parent)
489 return new EqualPowerStereoPanner (parent);
492 XMLNode&
493 EqualPowerStereoPanner::get_state (void)
495 return state (true);
498 XMLNode&
499 EqualPowerStereoPanner::state (bool full_state)
501 XMLNode* root = new XMLNode ("StreamPanner");
502 char buf[64];
503 LocaleGuard lg (X_("POSIX"));
505 snprintf (buf, sizeof (buf), "%.12g", x);
506 root->add_property (X_("x"), buf);
507 root->add_property (X_("type"), EqualPowerStereoPanner::name);
509 XMLNode* autonode = new XMLNode (X_("Automation"));
510 autonode->add_child_nocopy (_automation.state (full_state));
511 root->add_child_nocopy (*autonode);
513 StreamPanner::add_state (*root);
515 root->add_child_nocopy (_control.get_state ());
517 return *root;
521 EqualPowerStereoPanner::set_state (const XMLNode& node)
523 const XMLProperty* prop;
524 float pos;
525 LocaleGuard lg (X_("POSIX"));
527 if ((prop = node.property (X_("x")))) {
528 pos = atof (prop->value().c_str());
529 set_position (pos, true);
532 StreamPanner::set_state (node);
534 for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
536 if ((*iter)->name() == X_("controllable")) {
537 if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
538 _control.set_state (**iter);
541 } else if ((*iter)->name() == X_("Automation")) {
543 _automation.set_state (*((*iter)->children().front()));
545 if (_automation.automation_state() != Off) {
546 set_position (_automation.eval (parent.session().transport_frame()));
551 return 0;
554 /*----------------------------------------------------------------------*/
556 Multi2dPanner::Multi2dPanner (Panner& p)
557 : StreamPanner (p), _automation (0.0, 1.0, 0.5) // XXX useless
559 update ();
562 Multi2dPanner::~Multi2dPanner ()
566 void
567 Multi2dPanner::snapshot (nframes_t now)
569 // how?
572 void
573 Multi2dPanner::transport_stopped (nframes_t frame)
575 //what?
578 void
579 Multi2dPanner::set_automation_style (AutoStyle style)
581 //what?
584 void
585 Multi2dPanner::set_automation_state (AutoState state)
587 // what?
590 void
591 Multi2dPanner::update ()
593 static const float BIAS = FLT_MIN;
594 uint32_t i;
595 uint32_t nouts = parent.outputs.size();
596 float dsq[nouts];
597 float f, fr;
598 vector<pan_t> pans;
600 f = 0.0f;
602 for (i = 0; i < nouts; i++) {
603 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
604 if (dsq[i] < 0.0) {
605 dsq[i] = 0.0;
607 f += dsq[i] * dsq[i];
609 #ifdef __APPLE__
610 // terrible hack to support OSX < 10.3.9 builds
611 fr = (float) (1.0 / sqrt((double)f));
612 #else
613 fr = 1.0 / sqrtf(f);
614 #endif
615 for (i = 0; i < nouts; ++i) {
616 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
619 effective_x = x;
622 void
623 Multi2dPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nframes_t nframes)
625 Sample* dst;
626 pan_t pan;
627 vector<Panner::Output>::iterator o;
628 uint32_t n;
630 if (_muted) {
631 return;
635 for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
637 dst = obufs[n];
639 #ifdef CAN_INTERP
640 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
642 /* interpolate over 64 frames or nframes, whichever is smaller */
644 nframes_t limit = min ((nframes_t)64, nframes);
645 nframes_t n;
647 delta = -(delta / (float) (limit));
649 for (n = 0; n < limit; n++) {
650 left_interp = left_interp + delta;
651 left = left_interp + 0.9 * (left - left_interp);
652 dst[n] += src[n] * left * gain_coeff;
655 pan = left * gain_coeff;
656 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
658 } else {
660 #else
661 pan = (*o).desired_pan;
663 if ((pan *= gain_coeff) != 1.0f) {
665 if (pan != 0.0f) {
666 Session::mix_buffers_with_gain(dst,src,nframes,pan);
668 } else {
669 Session::mix_buffers_no_gain(dst,src,nframes);
671 #endif
672 #ifdef CAN_INTERP
674 #endif
677 return;
680 void
681 Multi2dPanner::distribute_automated (Sample* src, Sample** obufs,
682 nframes_t start, nframes_t end, nframes_t nframes,
683 pan_t** buffers)
685 if (_muted) {
686 return;
689 /* what ? */
691 return;
694 StreamPanner*
695 Multi2dPanner::factory (Panner& p)
697 return new Multi2dPanner (p);
701 Multi2dPanner::load (istream& in, string path, uint32_t& linecnt)
703 return 0;
706 XMLNode&
707 Multi2dPanner::get_state (void)
709 return state (true);
712 XMLNode&
713 Multi2dPanner::state (bool full_state)
715 XMLNode* root = new XMLNode ("StreamPanner");
716 char buf[64];
717 LocaleGuard lg (X_("POSIX"));
719 snprintf (buf, sizeof (buf), "%.12g", x);
720 root->add_property (X_("x"), buf);
721 snprintf (buf, sizeof (buf), "%.12g", y);
722 root->add_property (X_("y"), buf);
723 root->add_property (X_("type"), Multi2dPanner::name);
725 /* XXX no meaningful automation yet */
727 return *root;
731 Multi2dPanner::set_state (const XMLNode& node)
733 const XMLProperty* prop;
734 float newx,newy;
735 LocaleGuard lg (X_("POSIX"));
737 newx = -1;
738 newy = -1;
740 if ((prop = node.property (X_("x")))) {
741 newx = atof (prop->value().c_str());
744 if ((prop = node.property (X_("y")))) {
745 newy = atof (prop->value().c_str());
748 if (x < 0 || y < 0) {
749 error << _("badly-formed positional data for Multi2dPanner - ignored")
750 << endmsg;
751 return -1;
754 set_position (newx, newy);
755 return 0;
758 /*---------------------------------------------------------------------- */
760 Panner::Panner (string name, Session& s)
761 : _session (s)
763 set_name (name);
765 _linked = false;
766 _link_direction = SameDirection;
767 _bypassed = false;
770 Panner::~Panner ()
774 void
775 Panner::set_linked (bool yn)
777 if (yn != _linked) {
778 _linked = yn;
779 _session.set_dirty ();
780 LinkStateChanged (); /* EMIT SIGNAL */
784 void
785 Panner::set_link_direction (LinkDirection ld)
787 if (ld != _link_direction) {
788 _link_direction = ld;
789 _session.set_dirty ();
790 LinkStateChanged (); /* EMIT SIGNAL */
794 void
795 Panner::set_bypassed (bool yn)
797 if (yn != _bypassed) {
798 _bypassed = yn;
799 StateChanged ();
804 void
805 Panner::reset (uint32_t nouts, uint32_t npans)
807 uint32_t n;
808 bool changed = false;
809 bool do_not_and_did_not_need_panning = ((nouts < 2) && (outputs.size() < 2));
811 /* if new and old config don't need panning, or if
812 the config hasn't changed, we're done.
815 if (do_not_and_did_not_need_panning ||
816 ((nouts == outputs.size()) && (npans == size()))) {
817 return;
820 n = size();
821 clear ();
823 if (n != npans) {
824 changed = true;
827 n = outputs.size();
828 outputs.clear ();
830 if (n != nouts) {
831 changed = true;
834 if (nouts < 2) {
835 goto send_changed;
838 switch (nouts) {
839 case 0:
840 break;
842 case 1:
843 fatal << _("programming error:")
844 << X_("Panner::reset() called with a single output")
845 << endmsg;
846 /*NOTREACHED*/
847 break;
849 case 2:
850 /* line */
851 outputs.push_back (Output (0, 0));
852 outputs.push_back (Output (1.0, 0));
854 for (n = 0; n < npans; ++n) {
855 push_back (new EqualPowerStereoPanner (*this));
857 break;
859 case 3: // triangle
860 outputs.push_back (Output (0.5, 0));
861 outputs.push_back (Output (0, 1.0));
862 outputs.push_back (Output (1.0, 1.0));
864 for (n = 0; n < npans; ++n) {
865 push_back (new Multi2dPanner (*this));
868 break;
870 case 4: // square
871 outputs.push_back (Output (0, 0));
872 outputs.push_back (Output (1.0, 0));
873 outputs.push_back (Output (1.0, 1.0));
874 outputs.push_back (Output (0, 1.0));
876 for (n = 0; n < npans; ++n) {
877 push_back (new Multi2dPanner (*this));
880 break;
882 case 5: //square+offcenter center
883 outputs.push_back (Output (0, 0));
884 outputs.push_back (Output (1.0, 0));
885 outputs.push_back (Output (1.0, 1.0));
886 outputs.push_back (Output (0, 1.0));
887 outputs.push_back (Output (0.5, 0.75));
889 for (n = 0; n < npans; ++n) {
890 push_back (new Multi2dPanner (*this));
893 break;
895 default:
896 /* XXX horrible placement. FIXME */
897 for (n = 0; n < nouts; ++n) {
898 outputs.push_back (Output (0.1 * n, 0.1 * n));
901 for (n = 0; n < npans; ++n) {
902 push_back (new Multi2dPanner (*this));
905 break;
908 for (iterator x = begin(); x != end(); ++x) {
909 (*x)->update ();
912 /* force hard left/right panning in a common case: 2in/2out
915 if (npans == 2 && outputs.size() == 2) {
917 /* Do this only if we changed configuration, or our configuration
918 appears to be the default set up (center).
921 float left;
922 float right;
924 front()->get_position (left);
925 back()->get_position (right);
927 if (changed || ((left == 0.5) && (right == 0.5))) {
929 front()->set_position (0.0);
930 front()->automation().reset_default (0.0);
932 back()->set_position (1.0);
933 back()->automation().reset_default (1.0);
935 changed = true;
939 send_changed:
940 if (changed) {
941 Changed (); /* EMIT SIGNAL */
944 return;
947 void
948 Panner::remove (uint32_t which)
950 vector<StreamPanner*>::iterator i;
951 for (i = begin(); i != end() && which; ++i, --which);
953 if (i != end()) {
954 delete *i;
955 erase (i);
959 void
960 Panner::clear ()
962 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
963 delete *i;
966 vector<StreamPanner*>::clear ();
969 void
970 Panner::set_automation_style (AutoStyle style)
972 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
973 (*i)->set_automation_style (style);
975 _session.set_dirty ();
978 void
979 Panner::set_automation_state (AutoState state)
981 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
982 (*i)->set_automation_state (state);
984 _session.set_dirty ();
987 AutoState
988 Panner::automation_state () const
990 if (!empty()) {
991 return front()->automation().automation_state ();
992 } else {
993 return Off;
997 AutoStyle
998 Panner::automation_style () const
1000 if (!empty()) {
1001 return front()->automation().automation_style ();
1002 } else {
1003 return Absolute;
1007 void
1008 Panner::transport_stopped (nframes_t frame)
1010 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1011 (*i)->transport_stopped (frame);
1015 void
1016 Panner::snapshot (nframes_t now)
1018 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1019 (*i)->snapshot (now);
1023 void
1024 Panner::clear_automation ()
1026 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1027 (*i)->automation().clear ();
1029 _session.set_dirty ();
1032 struct PanPlugins {
1033 string name;
1034 uint32_t nouts;
1035 StreamPanner* (*factory)(Panner&);
1038 PanPlugins pan_plugins[] = {
1039 { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1040 { Multi2dPanner::name, 3, Multi2dPanner::factory },
1041 { string (""), 0, 0 }
1044 XMLNode&
1045 Panner::get_state (void)
1047 return state (true);
1050 XMLNode&
1051 Panner::state (bool full)
1053 XMLNode* root = new XMLNode (X_("Panner"));
1054 char buf[32];
1056 root->add_property (X_("linked"), (_linked ? "yes" : "no"));
1057 root->add_property (X_("link_direction"), enum_2_string (_link_direction));
1058 root->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1060 /* add each output */
1062 for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1063 XMLNode* onode = new XMLNode (X_("Output"));
1064 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1065 onode->add_property (X_("x"), buf);
1066 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1067 onode->add_property (X_("y"), buf);
1068 root->add_child_nocopy (*onode);
1071 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1072 root->add_child_nocopy ((*i)->state (full));
1075 return *root;
1079 Panner::set_state (const XMLNode& node)
1081 XMLNodeList nlist;
1082 XMLNodeConstIterator niter;
1083 const XMLProperty *prop;
1084 uint32_t i;
1085 StreamPanner* sp;
1086 LocaleGuard lg (X_("POSIX"));
1088 clear ();
1089 outputs.clear ();
1091 if ((prop = node.property (X_("linked"))) != 0) {
1092 set_linked (prop->value() == "yes");
1096 if ((prop = node.property (X_("bypassed"))) != 0) {
1097 set_bypassed (prop->value() == "yes");
1100 if ((prop = node.property (X_("link_direction"))) != 0) {
1101 LinkDirection ld; /* here to provide type information */
1102 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1105 nlist = node.children();
1107 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1108 if ((*niter)->name() == X_("Output")) {
1110 float x, y;
1112 prop = (*niter)->property (X_("x"));
1113 sscanf (prop->value().c_str(), "%g", &x);
1115 prop = (*niter)->property (X_("y"));
1116 sscanf (prop->value().c_str(), "%g", &y);
1118 outputs.push_back (Output (x, y));
1122 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1124 if ((*niter)->name() == X_("StreamPanner")) {
1126 if ((prop = (*niter)->property (X_("type")))) {
1128 for (i = 0; pan_plugins[i].factory; ++i) {
1129 if (prop->value() == pan_plugins[i].name) {
1132 /* note that we assume that all the stream panners
1133 are of the same type. pretty good
1134 assumption, but its still an assumption.
1137 sp = pan_plugins[i].factory (*this);
1139 if (sp->set_state (**niter) == 0) {
1140 push_back (sp);
1143 break;
1148 if (!pan_plugins[i].factory) {
1149 error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1150 prop->value())
1151 << endmsg;
1154 } else {
1155 error << _("panner plugin node has no type information!")
1156 << endmsg;
1157 return -1;
1163 /* don't try to do old-school automation loading if it wasn't marked as existing */
1165 if ((prop = node.property (X_("automation")))) {
1167 /* automation path is relative */
1169 automation_path = Glib::build_filename(_session.automation_dir(), prop->value ());
1172 return 0;
1177 bool
1178 Panner::touching () const
1180 for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1181 if ((*i)->automation().touching ()) {
1182 return true;
1186 return false;
1189 void
1190 Panner::set_position (float xpos, StreamPanner& orig)
1192 float xnow;
1193 float xdelta ;
1194 float xnew;
1196 orig.get_position (xnow);
1197 xdelta = xpos - xnow;
1199 if (_link_direction == SameDirection) {
1201 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1202 if (*i == &orig) {
1203 (*i)->set_position (xpos, true);
1204 } else {
1205 (*i)->get_position (xnow);
1206 xnew = min (1.0f, xnow + xdelta);
1207 xnew = max (0.0f, xnew);
1208 (*i)->set_position (xnew, true);
1212 } else {
1214 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1215 if (*i == &orig) {
1216 (*i)->set_position (xpos, true);
1217 } else {
1218 (*i)->get_position (xnow);
1219 xnew = min (1.0f, xnow - xdelta);
1220 xnew = max (0.0f, xnew);
1221 (*i)->set_position (xnew, true);
1227 void
1228 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1230 float xnow, ynow;
1231 float xdelta, ydelta;
1232 float xnew, ynew;
1234 orig.get_position (xnow, ynow);
1235 xdelta = xpos - xnow;
1236 ydelta = ypos - ynow;
1238 if (_link_direction == SameDirection) {
1240 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1241 if (*i == &orig) {
1242 (*i)->set_position (xpos, ypos, true);
1243 } else {
1244 (*i)->get_position (xnow, ynow);
1246 xnew = min (1.0f, xnow + xdelta);
1247 xnew = max (0.0f, xnew);
1249 ynew = min (1.0f, ynow + ydelta);
1250 ynew = max (0.0f, ynew);
1252 (*i)->set_position (xnew, ynew, true);
1256 } else {
1258 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1259 if (*i == &orig) {
1260 (*i)->set_position (xpos, ypos, true);
1261 } else {
1262 (*i)->get_position (xnow, ynow);
1264 xnew = min (1.0f, xnow - xdelta);
1265 xnew = max (0.0f, xnew);
1267 ynew = min (1.0f, ynow - ydelta);
1268 ynew = max (0.0f, ynew);
1270 (*i)->set_position (xnew, ynew, true);
1276 void
1277 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1279 float xnow, ynow, znow;
1280 float xdelta, ydelta, zdelta;
1281 float xnew, ynew, znew;
1283 orig.get_position (xnow, ynow, znow);
1284 xdelta = xpos - xnow;
1285 ydelta = ypos - ynow;
1286 zdelta = zpos - znow;
1288 if (_link_direction == SameDirection) {
1290 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1291 if (*i == &orig) {
1292 (*i)->set_position (xpos, ypos, zpos, true);
1293 } else {
1294 (*i)->get_position (xnow, ynow, znow);
1296 xnew = min (1.0f, xnow + xdelta);
1297 xnew = max (0.0f, xnew);
1299 ynew = min (1.0f, ynow + ydelta);
1300 ynew = max (0.0f, ynew);
1302 znew = min (1.0f, znow + zdelta);
1303 znew = max (0.0f, znew);
1305 (*i)->set_position (xnew, ynew, znew, true);
1309 } else {
1311 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1312 if (*i == &orig) {
1313 (*i)->set_position (xpos, ypos, true);
1314 } else {
1315 (*i)->get_position (xnow, ynow, znow);
1317 xnew = min (1.0f, xnow - xdelta);
1318 xnew = max (0.0f, xnew);
1320 ynew = min (1.0f, ynow - ydelta);
1321 ynew = max (0.0f, ynew);
1323 znew = min (1.0f, znow + zdelta);
1324 znew = max (0.0f, znew);
1326 (*i)->set_position (xnew, ynew, znew, true);
1332 /* old school automation handling */
1334 void
1335 Panner::set_name (string str)
1337 automation_path = Glib::build_filename(_session.automation_dir(),
1338 _session.snap_name() + "-pan-" + legalize_for_path (str) + ".automation");
1342 Panner::load ()
1344 char line[128];
1345 uint32_t linecnt = 0;
1346 float version;
1347 iterator sp;
1348 LocaleGuard lg (X_("POSIX"));
1350 if (automation_path.length() == 0) {
1351 return 0;
1354 if (access (automation_path.c_str(), F_OK)) {
1355 return 0;
1358 ifstream in (automation_path.c_str());
1360 if (!in) {
1361 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1362 automation_path, strerror (errno))
1363 << endmsg;
1364 return -1;
1367 sp = begin();
1369 while (in.getline (line, sizeof(line), '\n')) {
1371 if (++linecnt == 1) {
1372 if (memcmp (line, X_("version"), 7) == 0) {
1373 if (sscanf (line, "version %f", &version) != 1) {
1374 error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1375 return -1;
1377 } else {
1378 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1379 automation_path, line) << endmsg;
1380 return -1;
1383 continue;
1386 if (strlen (line) == 0 || line[0] == '#') {
1387 continue;
1390 if (strcmp (line, "begin") == 0) {
1392 if (sp == end()) {
1393 error << string_compose (_("too many panner states found in pan automation file %1"),
1394 automation_path)
1395 << endmsg;
1396 return -1;
1399 if ((*sp)->load (in, automation_path, linecnt)) {
1400 return -1;
1403 ++sp;
1407 return 0;