Fixed midi controllable state saving/loading
[ardour.git] / libs / ardour / panner.cc
1 /*
2     Copyright (C) 2004 Paul Davis 
3
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.
8
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.
13
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.
17
18     $Id$
19 */
20
21 #include <cmath>
22 #include <cerrno>
23 #include <fstream>
24 #include <cstdlib>
25 #include <string>
26 #include <cstdio>
27 #include <locale.h>
28 #include <unistd.h>
29 #include <float.h>
30
31 #include <glibmm.h>
32
33 #include <pbd/error.h>
34 #include <pbd/failed_constructor.h>
35 #include <pbd/xml++.h>
36 #include <pbd/enumwriter.h>
37
38 #include <ardour/session.h>
39 #include <ardour/panner.h>
40 #include <ardour/utils.h>
41
42 #include <ardour/mix.h>
43
44 #include "i18n.h"
45
46 #include <pbd/mathfix.h>
47
48 using namespace std;
49 using namespace ARDOUR;
50 using namespace PBD;
51
52 float Panner::current_automation_version_number = 1.0;
53
54 string EqualPowerStereoPanner::name = "Equal Power Stereo";
55 string Multi2dPanner::name = "Multiple (2D)";
56
57 /* this is a default mapper of  control values to a pan position
58    others can be imagined. 
59 */
60
61 static pan_t direct_control_to_pan (double fract) { 
62         return fract;
63 }
64
65 static double direct_pan_to_control (pan_t val) { 
66         return val;
67 }
68
69 StreamPanner::StreamPanner (Panner& p)
70         : parent (p),
71           _control (X_("panner"), *this)
72 {
73         _muted = false;
74
75         parent.session().add_controllable (&_control);
76
77         x = 0.5;
78         y = 0.5;
79         z = 0.5;
80 }
81
82 StreamPanner::~StreamPanner ()
83 {
84 }
85
86 void
87 StreamPanner::PanControllable::set_value (float val)
88 {
89         panner.set_position (direct_control_to_pan (val));
90 }
91
92 float
93 StreamPanner::PanControllable::get_value (void) const
94 {
95         float xpos;
96         panner.get_effective_position (xpos);
97         return direct_pan_to_control (xpos);
98 }
99
100 bool
101 StreamPanner::PanControllable::can_send_feedback () const
102 {
103         AutoState astate = panner.get_parent().automation_state ();
104
105         if ((astate == Play) || (astate == Touch && !panner.get_parent().touching())) {
106                 return true;
107         }
108
109         return false;
110 }
111
112 void
113 StreamPanner::set_muted (bool yn)
114 {
115         if (yn != _muted) {
116                 _muted = yn;
117                 StateChanged ();
118         }
119 }
120
121 void
122 StreamPanner::set_position (float xpos, bool link_call)
123 {
124         if (!link_call && parent.linked()) {
125                 parent.set_position (xpos, *this);
126         }
127
128         if (x != xpos) {
129                 x = xpos;
130                 update ();
131                 Changed ();
132                 _control.Changed ();
133         }
134 }
135
136 void
137 StreamPanner::set_position (float xpos, float ypos, bool link_call)
138 {
139         if (!link_call && parent.linked()) {
140                 parent.set_position (xpos, ypos, *this);
141         }
142
143         if (x != xpos || y != ypos) {
144                 
145                 x = xpos;
146                 y = ypos;
147                 update ();
148                 Changed ();
149         }
150 }
151
152 void
153 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
154 {
155         if (!link_call && parent.linked()) {
156                 parent.set_position (xpos, ypos, zpos, *this);
157         }
158
159         if (x != xpos || y != ypos || z != zpos) {
160                 x = xpos;
161                 y = ypos;
162                 z = zpos;
163                 update ();
164                 Changed ();
165         }
166 }
167
168 int
169 StreamPanner::set_state (const XMLNode& node)
170 {
171         const XMLProperty* prop;
172         XMLNodeConstIterator iter;
173
174         if ((prop = node.property (X_("muted")))) {
175                 set_muted (prop->value() == "yes");
176         }
177
178         return 0;
179 }
180
181 void
182 StreamPanner::add_state (XMLNode& node)
183 {
184         node.add_property (X_("muted"), (muted() ? "yes" : "no"));
185 }
186
187 /*---------------------------------------------------------------------- */
188
189 BaseStereoPanner::BaseStereoPanner (Panner& p)
190         : StreamPanner (p), _automation (0.0, 1.0, 0.5)
191 {
192 }
193
194 BaseStereoPanner::~BaseStereoPanner ()
195 {
196 }
197
198 void
199 BaseStereoPanner::snapshot (nframes_t now)
200 {
201         if (_automation.automation_state() == Write || _automation.automation_state() == Touch) {
202                 _automation.rt_add (now, x);
203         }
204 }
205
206 void
207 BaseStereoPanner::transport_stopped (nframes_t frame)
208 {
209         _automation.reposition_for_rt_add (frame);
210
211         if (_automation.automation_state() != Off) {
212                 set_position (_automation.eval (frame));
213         }
214 }
215
216 void
217 BaseStereoPanner::set_automation_style (AutoStyle style)
218 {
219         _automation.set_automation_style (style);
220 }
221
222 void
223 BaseStereoPanner::set_automation_state (AutoState state)
224 {
225         if (state != _automation.automation_state()) {
226
227                 _automation.set_automation_state (state);
228                 
229                 if (state != Off) {
230                         set_position (_automation.eval (parent.session().transport_frame()));
231                 }
232         }
233 }
234
235 int
236 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
237 {
238         char line[128];
239         LocaleGuard lg (X_("POSIX"));
240         
241         _automation.clear ();
242
243         while (in.getline (line, sizeof (line), '\n')) {
244                 jack_nframes_t when;
245                 double value;
246
247                 ++linecnt;
248
249                 if (strcmp (line, "end") == 0) {
250                         break;
251                 }
252
253                 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
254                         warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
255                         continue;
256                 }
257
258                 _automation.fast_simple_add (when, value);
259         }
260
261         /* now that we are done loading */
262
263         _automation.StateChanged ();
264
265         return 0;
266 }
267
268 void
269 BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nframes_t nframes)
270 {
271         pan_t delta;
272         Sample* dst;
273         pan_t pan;
274
275         if (_muted) {
276                 return;
277         }
278
279         /* LEFT */
280
281         dst = obufs[0];
282
283         if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc 
284                 
285                 /* interpolate over 64 frames or nframes, whichever is smaller */
286                 
287                 nframes_t limit = min ((nframes_t)64, nframes);
288                 nframes_t n;
289
290                 delta = -(delta / (float) (limit));
291                 
292                 for (n = 0; n < limit; n++) {
293                         left_interp = left_interp + delta;
294                         left = left_interp + 0.9 * (left - left_interp);
295                         dst[n] += src[n] * left * gain_coeff;
296                 }
297                 
298                 pan = left * gain_coeff;
299
300                 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
301                 
302         } else {
303                 
304                 left = desired_left;
305                 left_interp = left;
306
307                 if ((pan = (left * gain_coeff)) != 1.0f) {
308                         
309                         if (pan != 0.0f) {
310                                 
311                                 Session::mix_buffers_with_gain(dst,src,nframes,pan);
312
313                                 /* mark that we wrote into the buffer */
314
315                                 // obufs[0] = 0;
316
317                         } 
318                         
319                 } else {
320                         
321                         Session::mix_buffers_no_gain(dst,src,nframes);
322                         
323                         /* mark that we wrote into the buffer */
324                         
325                         // obufs[0] = 0;
326                 }
327         }
328
329         /* RIGHT */
330
331         dst = obufs[1];
332         
333         if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc 
334                 
335                 /* interpolate over 64 frames or nframes, whichever is smaller */
336                 
337                 nframes_t limit = min ((nframes_t)64, nframes);
338                 nframes_t n;
339
340                 delta = -(delta / (float) (limit));
341
342                 for (n = 0; n < limit; n++) {
343                         right_interp = right_interp + delta;
344                         right = right_interp + 0.9 * (right - right_interp);
345                         dst[n] += src[n] * right * gain_coeff;
346                 }
347                 
348                 pan = right * gain_coeff;
349                 
350                 Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
351                 
352                 /* XXX it would be nice to mark the buffer as written to */
353
354         } else {
355
356                 right = desired_right;
357                 right_interp = right;
358                 
359                 if ((pan = (right * gain_coeff)) != 1.0f) {
360                         
361                         if (pan != 0.0f) {
362                                 
363                                 Session::mix_buffers_with_gain(dst,src,nframes,pan);
364                                 
365                                 /* XXX it would be nice to mark the buffer as written to */
366                         }
367                         
368                 } else {
369                         
370                         Session::mix_buffers_no_gain(dst,src,nframes);
371                         
372                         /* XXX it would be nice to mark the buffer as written to */
373                 }
374         }
375 }
376
377 /*---------------------------------------------------------------------- */
378
379 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p)
380         : BaseStereoPanner (p)
381 {
382         update ();
383
384         left = desired_left;
385         right = desired_right;
386         left_interp = left;
387         right_interp = right;
388 }
389
390 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
391 {
392 }
393
394 void
395 EqualPowerStereoPanner::update ()
396 {
397         /* it would be very nice to split this out into a virtual function
398            that can be accessed from BaseStereoPanner and used in distribute_automated().
399            
400            but the place where its used in distribute_automated() is a tight inner loop,
401            and making "nframes" virtual function calls to compute values is an absurd
402            overhead.
403         */
404
405         /* x == 0 => hard left
406            x == 1 => hard right
407         */
408
409         float panR = x;
410         float panL = 1 - panR;
411
412         const float pan_law_attenuation = -3.0f;
413         const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
414         
415         desired_left = panL * (scale * panL + 1.0f - scale);
416         desired_right = panR * (scale * panR + 1.0f - scale);
417
418         effective_x = x;
419 }
420
421 void
422 EqualPowerStereoPanner::distribute_automated (Sample* src, Sample** obufs, 
423                                               nframes_t start, nframes_t end, nframes_t nframes,
424                                               pan_t** buffers)
425 {
426         Sample* dst;
427         pan_t* pbuf;
428
429         /* fetch positional data */
430
431         if (!_automation.rt_safe_get_vector (start, end, buffers[0], nframes)) {
432                 /* fallback */
433                 if (!_muted) {
434                         distribute (src, obufs, 1.0, nframes);
435                 }
436                 return;
437         }
438
439         /* store effective pan position. do this even if we are muted */
440
441         if (nframes > 0) 
442                 effective_x = buffers[0][nframes-1];
443
444         if (_muted) {
445                 return;
446         }
447
448         /* apply pan law to convert positional data into pan coefficients for
449            each buffer (output)
450         */
451
452         const float pan_law_attenuation = -3.0f;
453         const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
454
455         for (nframes_t n = 0; n < nframes; ++n) {
456
457                 float panR = buffers[0][n];
458                 float panL = 1 - panR;
459                 
460                 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
461                 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
462         }
463
464         /* LEFT */
465
466         dst = obufs[0];
467         pbuf = buffers[0];
468         
469         for (nframes_t n = 0; n < nframes; ++n) {
470                 dst[n] += src[n] * pbuf[n];
471         }       
472
473         /* XXX it would be nice to mark the buffer as written to */
474
475         /* RIGHT */
476
477         dst = obufs[1];
478         pbuf = buffers[1];
479
480         for (nframes_t n = 0; n < nframes; ++n) {
481                 dst[n] += src[n] * pbuf[n];
482         }       
483         
484         /* XXX it would be nice to mark the buffer as written to */
485 }
486
487 StreamPanner*
488 EqualPowerStereoPanner::factory (Panner& parent)
489 {
490         return new EqualPowerStereoPanner (parent);
491 }
492
493 XMLNode&
494 EqualPowerStereoPanner::get_state (void)
495 {
496         return state (true);
497 }
498
499 XMLNode&
500 EqualPowerStereoPanner::state (bool full_state)
501 {
502         XMLNode* root = new XMLNode ("StreamPanner");
503         char buf[64];
504         LocaleGuard lg (X_("POSIX"));
505
506         snprintf (buf, sizeof (buf), "%.12g", x); 
507         root->add_property (X_("x"), buf);
508         root->add_property (X_("type"), EqualPowerStereoPanner::name);
509
510         XMLNode* autonode = new XMLNode (X_("Automation"));
511         autonode->add_child_nocopy (_automation.state (full_state));
512         root->add_child_nocopy (*autonode);
513
514         StreamPanner::add_state (*root);
515
516         root->add_child_nocopy (_control.get_state ());
517
518         return *root;
519 }
520
521 int
522 EqualPowerStereoPanner::set_state (const XMLNode& node)
523 {
524         const XMLProperty* prop;
525         float pos;
526         LocaleGuard lg (X_("POSIX"));
527
528         if ((prop = node.property (X_("x")))) {
529                 pos = atof (prop->value().c_str());
530                 set_position (pos, true);
531         } 
532
533         StreamPanner::set_state (node);
534
535         for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
536
537                 if ((*iter)->name() == X_("controllable")) {
538                         if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
539                                 _control.set_state (**iter);
540                         }
541
542                 } else if ((*iter)->name() == X_("Automation")) {
543
544                         _automation.set_state (*((*iter)->children().front()));
545
546                         if (_automation.automation_state() != Off) {
547                                 set_position (_automation.eval (parent.session().transport_frame()));
548                         }
549                 }
550         }
551
552         return 0;
553 }
554
555 /*----------------------------------------------------------------------*/
556
557 Multi2dPanner::Multi2dPanner (Panner& p)
558         : StreamPanner (p), _automation (0.0, 1.0, 0.5) // XXX useless
559 {
560         update ();
561 }
562
563 Multi2dPanner::~Multi2dPanner ()
564 {
565 }
566
567 void
568 Multi2dPanner::snapshot (nframes_t now)
569 {
570         // how?
571 }
572
573 void
574 Multi2dPanner::transport_stopped (nframes_t frame)
575 {
576         //what?
577 }
578
579 void
580 Multi2dPanner::set_automation_style (AutoStyle style)
581 {
582         //what?
583 }
584
585 void
586 Multi2dPanner::set_automation_state (AutoState state)
587 {
588         // what?
589 }
590
591 void
592 Multi2dPanner::update ()
593 {
594         static const float BIAS = FLT_MIN;
595         uint32_t i;
596         uint32_t nouts = parent.outputs.size();
597         float dsq[nouts];
598         float f, fr;
599         vector<pan_t> pans;
600
601         f = 0.0f;
602
603         for (i = 0; i < nouts; i++) {
604                 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
605                 if (dsq[i] < 0.0) {
606                         dsq[i] = 0.0;
607                 }
608                 f += dsq[i] * dsq[i];
609         }
610 #ifdef __APPLE__
611         // terrible hack to support OSX < 10.3.9 builds
612         fr = (float) (1.0 / sqrt((double)f));
613 #else
614         fr = 1.0 / sqrtf(f);
615 #endif  
616         for (i = 0; i < nouts; ++i) {
617                 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
618         }
619
620         effective_x = x;
621 }
622
623 void
624 Multi2dPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nframes_t nframes)
625 {
626         Sample* dst;
627         pan_t pan;
628         vector<Panner::Output>::iterator o;
629         uint32_t n;
630
631         if (_muted) {
632                 return;
633         }
634
635
636         for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
637
638                 dst = obufs[n];
639         
640 #ifdef CAN_INTERP
641                 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc 
642                         
643                         /* interpolate over 64 frames or nframes, whichever is smaller */
644                         
645                         nframes_t limit = min ((nframes_t)64, nframes);
646                         nframes_t n;
647                         
648                         delta = -(delta / (float) (limit));
649                 
650                         for (n = 0; n < limit; n++) {
651                                 left_interp = left_interp + delta;
652                                 left = left_interp + 0.9 * (left - left_interp);
653                                 dst[n] += src[n] * left * gain_coeff;
654                         }
655                         
656                         pan = left * gain_coeff;
657                         
658                         for (; n < nframes; ++n) {
659                                 dst[n] += src[n] * pan;
660                         }
661                         
662                 } else {
663
664 #else                   
665                         pan = (*o).desired_pan;
666                         
667                         if ((pan *= gain_coeff) != 1.0f) {
668                                 
669                                 if (pan != 0.0f) {
670                                         
671                                         for (nframes_t n = 0; n < nframes; ++n) {
672                                                 dst[n] += src[n] * pan;
673                                         }      
674                                         
675                                 } 
676
677                                 
678                         } else {
679                                 
680                                 for (nframes_t n = 0; n < nframes; ++n) {
681                                         dst[n] += src[n];
682                                 }      
683
684                         }
685 #endif
686 #ifdef CAN_INTERP
687                 }
688 #endif
689         }
690         
691         return;
692 }
693
694 void
695 Multi2dPanner::distribute_automated (Sample* src, Sample** obufs, 
696                                      nframes_t start, nframes_t end, nframes_t nframes,
697                                      pan_t** buffers)
698 {
699         if (_muted) {
700                 return;
701         }
702
703         /* what ? */
704
705         return;
706 }
707
708 StreamPanner*
709 Multi2dPanner::factory (Panner& p)
710 {
711         return new Multi2dPanner (p);
712 }
713
714 int
715 Multi2dPanner::load (istream& in, string path, uint32_t& linecnt)
716 {
717         return 0;
718 }
719
720 XMLNode&
721 Multi2dPanner::get_state (void)
722 {
723         return state (true);
724 }
725
726 XMLNode&
727 Multi2dPanner::state (bool full_state)
728 {
729         XMLNode* root = new XMLNode ("StreamPanner");
730         char buf[64];
731         LocaleGuard lg (X_("POSIX"));
732
733         snprintf (buf, sizeof (buf), "%.12g", x); 
734         root->add_property (X_("x"), buf);
735         snprintf (buf, sizeof (buf), "%.12g", y); 
736         root->add_property (X_("y"), buf);
737         root->add_property (X_("type"), Multi2dPanner::name);
738
739         /* XXX no meaningful automation yet */
740
741         return *root;
742 }
743
744 int
745 Multi2dPanner::set_state (const XMLNode& node)
746 {
747         const XMLProperty* prop;
748         float newx,newy;
749         LocaleGuard lg (X_("POSIX"));
750
751         newx = -1;
752         newy = -1;
753
754         if ((prop = node.property (X_("x")))) {
755                 newx = atof (prop->value().c_str());
756         }
757        
758         if ((prop = node.property (X_("y")))) {
759                 newy = atof (prop->value().c_str());
760         }
761         
762         if (x < 0 || y < 0) {
763                 error << _("badly-formed positional data for Multi2dPanner - ignored")
764                       << endmsg;
765                 return -1;
766         } 
767         
768         set_position (newx, newy);
769         return 0;
770 }
771
772 /*---------------------------------------------------------------------- */
773
774 Panner::Panner (string name, Session& s)
775         : _session (s)
776 {
777         set_name (name);
778
779         _linked = false;
780         _link_direction = SameDirection;
781         _bypassed = false;
782 }
783
784 Panner::~Panner ()
785 {
786 }
787
788 void
789 Panner::set_linked (bool yn)
790 {
791         if (yn != _linked) {
792                 _linked = yn;
793                 _session.set_dirty ();
794                 LinkStateChanged (); /* EMIT SIGNAL */
795         }
796 }
797
798 void
799 Panner::set_link_direction (LinkDirection ld)
800 {
801         if (ld != _link_direction) {
802                 _link_direction = ld;
803                 _session.set_dirty ();
804                 LinkStateChanged (); /* EMIT SIGNAL */
805         }
806 }
807
808 void
809 Panner::set_bypassed (bool yn)
810 {
811         if (yn != _bypassed) {
812                 _bypassed = yn;
813                 StateChanged ();
814         }
815 }
816
817
818 void
819 Panner::reset (uint32_t nouts, uint32_t npans)
820 {
821         uint32_t n;
822         bool changed = false;
823
824         if (nouts < 2 || (nouts == outputs.size() && npans == size())) {
825                 return;
826         } 
827
828         n = size();
829         clear ();
830
831         if (n != npans) {
832                 changed = true;
833         }
834
835         n = outputs.size();
836         outputs.clear ();
837
838         if (n != nouts) {
839                 changed = true;
840         }
841
842         switch (nouts) {
843         case 0:
844                 break;
845
846         case 1:
847                 fatal << _("programming error:")
848                       << X_("Panner::reset() called with a single output")
849                       << endmsg;
850                 /*NOTREACHED*/
851                 break;
852
853         case 2:
854                 /* line */
855                 outputs.push_back (Output (0, 0));
856                 outputs.push_back (Output (1.0, 0));
857
858                 for (n = 0; n < npans; ++n) {
859                         push_back (new EqualPowerStereoPanner (*this));
860                 }
861                 break;
862
863         case 3: // triangle
864                 outputs.push_back (Output  (0.5, 0));
865                 outputs.push_back (Output  (0, 1.0));
866                 outputs.push_back (Output  (1.0, 1.0));
867
868                 for (n = 0; n < npans; ++n) {
869                         push_back (new Multi2dPanner (*this));
870                 }
871
872                 break; 
873
874         case 4: // square
875                 outputs.push_back (Output  (0, 0));
876                 outputs.push_back (Output  (1.0, 0));
877                 outputs.push_back (Output  (1.0, 1.0));
878                 outputs.push_back (Output  (0, 1.0));
879
880                 for (n = 0; n < npans; ++n) {
881                         push_back (new Multi2dPanner (*this));
882                 }
883
884                 break;  
885
886         case 5: //square+offcenter center
887                 outputs.push_back (Output  (0, 0));
888                 outputs.push_back (Output  (1.0, 0));
889                 outputs.push_back (Output  (1.0, 1.0));
890                 outputs.push_back (Output  (0, 1.0));
891                 outputs.push_back (Output  (0.5, 0.75));
892
893                 for (n = 0; n < npans; ++n) {
894                         push_back (new Multi2dPanner (*this));
895                 }
896
897                 break;
898
899         default:
900                 /* XXX horrible placement. FIXME */
901                 for (n = 0; n < nouts; ++n) {
902                         outputs.push_back (Output (0.1 * n, 0.1 * n));
903                 }
904
905                 for (n = 0; n < npans; ++n) {
906                         push_back (new Multi2dPanner (*this));
907                 }
908
909                 break;
910         }
911
912         for (iterator x = begin(); x != end(); ++x) {
913                 (*x)->update ();
914         }
915
916         /* force hard left/right panning in a common case: 2in/2out 
917         */
918         
919         if (npans == 2 && outputs.size() == 2) {
920
921                 /* Do this only if we changed configuration, or our configuration
922                    appears to be the default set up (center).
923                 */
924
925                 float left;
926                 float right;
927
928                 front()->get_position (left);
929                 back()->get_position (right);
930
931                 if (changed || ((left == 0.5) && (right == 0.5))) {
932                 
933                         front()->set_position (0.0);
934                         front()->automation().reset_default (0.0);
935                         
936                         back()->set_position (1.0);
937                         back()->automation().reset_default (1.0);
938                         
939                         changed = true;
940                 }
941         }
942
943         if (changed) {
944                 Changed (); /* EMIT SIGNAL */
945         }
946
947         return;
948 }
949
950 void
951 Panner::remove (uint32_t which)
952 {
953         vector<StreamPanner*>::iterator i;
954         for (i = begin(); i != end() && which; ++i, --which);
955
956         if (i != end()) {
957                 delete *i;
958                 erase (i);
959         }
960 }
961
962 void
963 Panner::clear ()
964 {
965         for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
966                 delete *i;
967         }
968
969         vector<StreamPanner*>::clear ();
970 }
971
972 void
973 Panner::set_automation_style (AutoStyle style)
974 {
975         for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
976                 (*i)->set_automation_style (style);
977         }
978         _session.set_dirty ();
979 }       
980
981 void
982 Panner::set_automation_state (AutoState state)
983 {
984         for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
985                 (*i)->set_automation_state (state);
986         }
987         _session.set_dirty ();
988 }       
989
990 AutoState
991 Panner::automation_state () const
992 {
993         if (!empty()) {
994                 return front()->automation().automation_state ();
995         } else {
996                 return Off;
997         }
998 }
999
1000 AutoStyle
1001 Panner::automation_style () const
1002 {
1003         if (!empty()) {
1004                 return front()->automation().automation_style ();
1005         } else {
1006                 return Absolute;
1007         }
1008 }
1009
1010 void
1011 Panner::transport_stopped (nframes_t frame)
1012 {
1013         for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1014                 (*i)->transport_stopped (frame);
1015         }
1016 }       
1017
1018 void
1019 Panner::snapshot (nframes_t now)
1020 {
1021         for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1022                 (*i)->snapshot (now);
1023         }
1024 }       
1025
1026 void
1027 Panner::clear_automation ()
1028 {
1029         for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1030                 (*i)->automation().clear ();
1031         }
1032         _session.set_dirty ();
1033 }       
1034
1035 struct PanPlugins {
1036     string name;
1037     uint32_t nouts;
1038     StreamPanner* (*factory)(Panner&);
1039 };
1040
1041 PanPlugins pan_plugins[] = {
1042         { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1043         { Multi2dPanner::name, 3, Multi2dPanner::factory },
1044         { string (""), 0, 0 }
1045 };
1046
1047 XMLNode&
1048 Panner::get_state (void)
1049 {
1050         return state (true);
1051 }
1052
1053 XMLNode&
1054 Panner::state (bool full)
1055 {
1056         XMLNode* root = new XMLNode (X_("Panner"));
1057         char buf[32];
1058
1059         root->add_property (X_("linked"), (_linked ? "yes" : "no"));
1060         root->add_property (X_("link_direction"), enum_2_string (_link_direction));
1061         root->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1062
1063         /* add each output */
1064
1065         for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1066                 XMLNode* onode = new XMLNode (X_("Output"));
1067                 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1068                 onode->add_property (X_("x"), buf);
1069                 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1070                 onode->add_property (X_("y"), buf);
1071                 root->add_child_nocopy (*onode);
1072         }
1073
1074         for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1075                 root->add_child_nocopy ((*i)->state (full));
1076         }
1077
1078         return *root;
1079 }
1080
1081 int
1082 Panner::set_state (const XMLNode& node)
1083 {
1084         XMLNodeList nlist;
1085         XMLNodeConstIterator niter;
1086         const XMLProperty *prop;
1087         uint32_t i;
1088         StreamPanner* sp;
1089         LocaleGuard lg (X_("POSIX"));
1090
1091         clear ();
1092         outputs.clear ();
1093
1094         if ((prop = node.property (X_("linked"))) != 0) {
1095                 set_linked (prop->value() == "yes");
1096         }
1097
1098
1099         if ((prop = node.property (X_("bypassed"))) != 0) {
1100                 set_bypassed (prop->value() == "yes");
1101         }
1102
1103         if ((prop = node.property (X_("link_direction"))) != 0) {
1104                 LinkDirection ld; /* here to provide type information */
1105                 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1106         }
1107
1108         nlist = node.children();
1109
1110         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1111                 if ((*niter)->name() == X_("Output")) {
1112                         
1113                         float x, y;
1114                         
1115                         prop = (*niter)->property (X_("x"));
1116                         sscanf (prop->value().c_str(), "%g", &x);
1117                         
1118                         prop = (*niter)->property (X_("y"));
1119                         sscanf (prop->value().c_str(), "%g", &y);
1120                         
1121                         outputs.push_back (Output (x, y));
1122                 }
1123         }
1124
1125         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1126
1127                 if ((*niter)->name() == X_("StreamPanner")) {
1128                 
1129                         if ((prop = (*niter)->property (X_("type")))) {
1130                                 
1131                                 for (i = 0; pan_plugins[i].factory; ++i) {
1132                                         if (prop->value() == pan_plugins[i].name) {
1133                                                 
1134                                                 
1135                                                 /* note that we assume that all the stream panners
1136                                                    are of the same type. pretty good
1137                                                    assumption, but its still an assumption.
1138                                                 */
1139                                                 
1140                                                 sp = pan_plugins[i].factory (*this);
1141                                                 
1142                                                 if (sp->set_state (**niter) == 0) {
1143                                                         push_back (sp);
1144                                                 }
1145                                                 
1146                                                 break;
1147                                         }
1148                                 }
1149                                 
1150                                 
1151                                 if (!pan_plugins[i].factory) {
1152                                         error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1153                                                           prop->value())
1154                                               << endmsg;
1155                                 }
1156
1157                         } else {
1158                                 error << _("panner plugin node has no type information!")
1159                                       << endmsg;
1160                                 return -1;
1161                         }
1162
1163                 }       
1164         }
1165
1166         /* don't try to do old-school automation loading if it wasn't marked as existing */
1167
1168         if ((prop = node.property (X_("automation")))) {
1169
1170                 /* automation path is relative */
1171                 
1172                 automation_path = _session.automation_dir();
1173                 automation_path += prop->value ();
1174         } 
1175
1176         return 0;
1177 }
1178
1179
1180
1181 bool
1182 Panner::touching () const
1183 {
1184         for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1185                 if ((*i)->automation().touching ()) {
1186                         return true;
1187                 }
1188         }
1189
1190         return false;
1191 }
1192
1193 void
1194 Panner::set_position (float xpos, StreamPanner& orig)
1195 {
1196         float xnow;
1197         float xdelta ;
1198         float xnew;
1199
1200         orig.get_position (xnow);
1201         xdelta = xpos - xnow;
1202         
1203         if (_link_direction == SameDirection) {
1204
1205                 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1206                         if (*i == &orig) {
1207                                 (*i)->set_position (xpos, true);
1208                         } else {
1209                                 (*i)->get_position (xnow);
1210                                 xnew = min (1.0f, xnow + xdelta);
1211                                 xnew = max (0.0f, xnew);
1212                                 (*i)->set_position (xnew, true);
1213                         }
1214                 }
1215
1216         } else {
1217
1218                 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1219                         if (*i == &orig) {
1220                                 (*i)->set_position (xpos, true);
1221                         } else {
1222                                 (*i)->get_position (xnow);
1223                                 xnew = min (1.0f, xnow - xdelta);
1224                                 xnew = max (0.0f, xnew);
1225                                 (*i)->set_position (xnew, true);
1226                         }
1227                 }
1228         }
1229 }
1230
1231 void
1232 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1233 {
1234         float xnow, ynow;
1235         float xdelta, ydelta;
1236         float xnew, ynew;
1237
1238         orig.get_position (xnow, ynow);
1239         xdelta = xpos - xnow;
1240         ydelta = ypos - ynow;
1241         
1242         if (_link_direction == SameDirection) {
1243
1244                 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1245                         if (*i == &orig) {
1246                                 (*i)->set_position (xpos, ypos, true);
1247                         } else {
1248                                 (*i)->get_position (xnow, ynow);
1249
1250                                 xnew = min (1.0f, xnow + xdelta);
1251                                 xnew = max (0.0f, xnew);
1252
1253                                 ynew = min (1.0f, ynow + ydelta);
1254                                 ynew = max (0.0f, ynew);
1255
1256                                 (*i)->set_position (xnew, ynew, true);
1257                         }
1258                 }
1259
1260         } else {
1261
1262                 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1263                         if (*i == &orig) {
1264                                 (*i)->set_position (xpos, ypos, true);
1265                         } else {
1266                                 (*i)->get_position (xnow, ynow);
1267                                 
1268                                 xnew = min (1.0f, xnow - xdelta);
1269                                 xnew = max (0.0f, xnew);
1270
1271                                 ynew = min (1.0f, ynow - ydelta);
1272                                 ynew = max (0.0f, ynew);
1273
1274                                 (*i)->set_position (xnew, ynew, true);
1275                         }
1276                 }
1277         }
1278 }
1279
1280 void
1281 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1282 {
1283         float xnow, ynow, znow;
1284         float xdelta, ydelta, zdelta;
1285         float xnew, ynew, znew;
1286
1287         orig.get_position (xnow, ynow, znow);
1288         xdelta = xpos - xnow;
1289         ydelta = ypos - ynow;
1290         zdelta = zpos - znow;
1291
1292         if (_link_direction == SameDirection) {
1293
1294                 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1295                         if (*i == &orig) {
1296                                 (*i)->set_position (xpos, ypos, zpos, true);
1297                         } else {
1298                                 (*i)->get_position (xnow, ynow, znow);
1299                                 
1300                                 xnew = min (1.0f, xnow + xdelta);
1301                                 xnew = max (0.0f, xnew);
1302
1303                                 ynew = min (1.0f, ynow + ydelta);
1304                                 ynew = max (0.0f, ynew);
1305
1306                                 znew = min (1.0f, znow + zdelta);
1307                                 znew = max (0.0f, znew);
1308
1309                                 (*i)->set_position (xnew, ynew, znew, true);
1310                         }
1311                 }
1312
1313         } else {
1314
1315                 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1316                         if (*i == &orig) {
1317                                 (*i)->set_position (xpos, ypos, true);
1318                         } else {
1319                                 (*i)->get_position (xnow, ynow, znow);
1320
1321                                 xnew = min (1.0f, xnow - xdelta);
1322                                 xnew = max (0.0f, xnew);
1323
1324                                 ynew = min (1.0f, ynow - ydelta);
1325                                 ynew = max (0.0f, ynew);
1326
1327                                 znew = min (1.0f, znow + zdelta);
1328                                 znew = max (0.0f, znew);
1329
1330                                 (*i)->set_position (xnew, ynew, znew, true);
1331                         }
1332                 }
1333         }
1334 }
1335
1336 /* old school automation handling */
1337
1338 void
1339 Panner::set_name (string str)
1340 {
1341         automation_path = _session.automation_dir();
1342         automation_path += _session.snap_name();
1343         automation_path += "-pan-";
1344         automation_path += legalize_for_path (str);
1345         automation_path += ".automation";
1346 }
1347
1348 int
1349 Panner::load ()
1350 {
1351         char line[128];
1352         uint32_t linecnt = 0;
1353         float version;
1354         iterator sp;
1355         LocaleGuard lg (X_("POSIX"));
1356
1357         if (automation_path.length() == 0) {
1358                 return 0;
1359         }
1360         
1361         if (access (automation_path.c_str(), F_OK)) {
1362                 return 0;
1363         }
1364
1365         ifstream in (automation_path.c_str());
1366
1367         if (!in) {
1368                 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1369                                   automation_path, strerror (errno))
1370                       << endmsg;
1371                 return -1;
1372         }
1373
1374         sp = begin();
1375
1376         while (in.getline (line, sizeof(line), '\n')) {
1377
1378                 if (++linecnt == 1) {
1379                         if (memcmp (line, X_("version"), 7) == 0) {
1380                                 if (sscanf (line, "version %f", &version) != 1) {
1381                                         error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1382                                         return -1;
1383                                 }
1384                         } else {
1385                                 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"), 
1386                                                  automation_path, line) << endmsg;
1387                                 return -1;
1388                         }
1389
1390                         continue;
1391                 }
1392
1393                 if (strlen (line) == 0 || line[0] == '#') {
1394                         continue;
1395                 }
1396
1397                 if (strcmp (line, "begin") == 0) {
1398                         
1399                         if (sp == end()) {
1400                                 error << string_compose (_("too many panner states found in pan automation file %1"),
1401                                                   automation_path)
1402                                       << endmsg;
1403                                 return -1;
1404                         }
1405
1406                         if ((*sp)->load (in, automation_path, linecnt)) {
1407                                 return -1;
1408                         }
1409                         
1410                         ++sp;
1411                 }
1412         }
1413
1414         return 0;
1415 }