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