massive changes in automation state handling, not entirely complete; some bug fixes...
[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         XMLNode* autonode = new XMLNode (X_("Automation"));
477         autonode->add_child_nocopy (_automation.state (full_state));
478         root->add_child_nocopy (*autonode);
479
480         StreamPanner::add_state (*root);
481
482         root->add_child_nocopy (_control.get_state ());
483
484         return *root;
485 }
486
487 int
488 EqualPowerStereoPanner::set_state (const XMLNode& node)
489 {
490         const XMLProperty* prop;
491         float pos;
492         LocaleGuard lg (X_("POSIX"));
493
494         if ((prop = node.property (X_("x")))) {
495                 pos = atof (prop->value().c_str());
496                 set_position (pos, true);
497         } 
498
499         StreamPanner::set_state (node);
500
501         for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
502
503                 if ((*iter)->name() == X_("panner")) {
504
505                         _control.set_state (**iter);
506
507                 } else if ((*iter)->name() == X_("Automation")) {
508
509                         _automation.set_state (*((*iter)->children().front()));
510
511                         if (_automation.automation_state() != Off) {
512                                 set_position (_automation.eval (parent.session().transport_frame()));
513                         }
514                 }
515         }
516
517         return 0;
518 }
519
520 /*----------------------------------------------------------------------*/
521
522 Multi2dPanner::Multi2dPanner (Panner& p)
523         : StreamPanner (p), _automation (0.0, 1.0, 0.5) // XXX useless
524 {
525         update ();
526 }
527
528 Multi2dPanner::~Multi2dPanner ()
529 {
530 }
531
532 void
533 Multi2dPanner::snapshot (nframes_t now)
534 {
535         // how?
536 }
537
538 void
539 Multi2dPanner::transport_stopped (nframes_t frame)
540 {
541         //what?
542 }
543
544 void
545 Multi2dPanner::set_automation_style (AutoStyle style)
546 {
547         //what?
548 }
549
550 void
551 Multi2dPanner::set_automation_state (AutoState state)
552 {
553         // what?
554 }
555
556 void
557 Multi2dPanner::update ()
558 {
559         static const float BIAS = FLT_MIN;
560         uint32_t i;
561         uint32_t nouts = parent.outputs.size();
562         float dsq[nouts];
563         float f, fr;
564         vector<pan_t> pans;
565
566         f = 0.0f;
567
568         for (i = 0; i < nouts; i++) {
569                 dsq[i] = ((x - parent.outputs[i].x) * (x - parent.outputs[i].x) + (y - parent.outputs[i].y) * (y - parent.outputs[i].y) + BIAS);
570                 if (dsq[i] < 0.0) {
571                         dsq[i] = 0.0;
572                 }
573                 f += dsq[i] * dsq[i];
574         }
575 #ifdef __APPLE__
576         // terrible hack to support OSX < 10.3.9 builds
577         fr = (float) (1.0 / sqrt((double)f));
578 #else
579         fr = 1.0 / sqrtf(f);
580 #endif  
581         for (i = 0; i < nouts; ++i) {
582                 parent.outputs[i].desired_pan = 1.0f - (dsq[i] * fr);
583         }
584
585         effective_x = x;
586 }
587
588 void
589 Multi2dPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nframes_t nframes)
590 {
591         Sample* dst;
592         pan_t pan;
593         vector<Panner::Output>::iterator o;
594         uint32_t n;
595
596         if (_muted) {
597                 return;
598         }
599
600
601         for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
602
603                 dst = obufs[n];
604         
605 #ifdef CAN_INTERP
606                 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc 
607                         
608                         /* interpolate over 64 frames or nframes, whichever is smaller */
609                         
610                         nframes_t limit = min ((nframes_t)64, nframes);
611                         nframes_t n;
612                         
613                         delta = -(delta / (float) (limit));
614                 
615                         for (n = 0; n < limit; n++) {
616                                 left_interp = left_interp + delta;
617                                 left = left_interp + 0.9 * (left - left_interp);
618                                 dst[n] += src[n] * left * gain_coeff;
619                         }
620                         
621                         pan = left * gain_coeff;
622                         
623                         for (; n < nframes; ++n) {
624                                 dst[n] += src[n] * pan;
625                         }
626                         
627                 } else {
628
629 #else                   
630                         pan = (*o).desired_pan;
631                         
632                         if ((pan *= gain_coeff) != 1.0f) {
633                                 
634                                 if (pan != 0.0f) {
635                                         
636                                         for (nframes_t n = 0; n < nframes; ++n) {
637                                                 dst[n] += src[n] * pan;
638                                         }      
639                                         
640                                 } 
641
642                                 
643                         } else {
644                                 
645                                 for (nframes_t n = 0; n < nframes; ++n) {
646                                         dst[n] += src[n];
647                                 }      
648
649                         }
650 #endif
651 #ifdef CAN_INTERP
652                 }
653 #endif
654         }
655         
656         return;
657 }
658
659 void
660 Multi2dPanner::distribute_automated (Sample* src, Sample** obufs, 
661                                      nframes_t start, nframes_t end, nframes_t nframes,
662                                      pan_t** buffers)
663 {
664         if (_muted) {
665                 return;
666         }
667
668         /* what ? */
669
670         return;
671 }
672
673 StreamPanner*
674 Multi2dPanner::factory (Panner& p)
675 {
676         return new Multi2dPanner (p);
677 }
678
679 XMLNode&
680 Multi2dPanner::get_state (void)
681 {
682         return state (true);
683 }
684
685 XMLNode&
686 Multi2dPanner::state (bool full_state)
687 {
688         XMLNode* root = new XMLNode ("StreamPanner");
689         char buf[64];
690         LocaleGuard lg (X_("POSIX"));
691
692         snprintf (buf, sizeof (buf), "%.12g", x); 
693         root->add_property (X_("x"), buf);
694         snprintf (buf, sizeof (buf), "%.12g", y); 
695         root->add_property (X_("y"), buf);
696         root->add_property (X_("type"), Multi2dPanner::name);
697
698         /* XXX no meaningful automation yet */
699
700         return *root;
701 }
702
703 int
704 Multi2dPanner::set_state (const XMLNode& node)
705 {
706         const XMLProperty* prop;
707         float newx,newy;
708         LocaleGuard lg (X_("POSIX"));
709
710         newx = -1;
711         newy = -1;
712
713         if ((prop = node.property (X_("x")))) {
714                 newx = atof (prop->value().c_str());
715         }
716        
717         if ((prop = node.property (X_("y")))) {
718                 newy = atof (prop->value().c_str());
719         }
720         
721         if (x < 0 || y < 0) {
722                 error << _("badly-formed positional data for Multi2dPanner - ignored")
723                       << endmsg;
724                 return -1;
725         } 
726         
727         set_position (newx, newy);
728         return 0;
729 }
730
731 /*---------------------------------------------------------------------- */
732
733 Panner::Panner (string name, Session& s)
734         : _session (s)
735 {
736         _linked = false;
737         _link_direction = SameDirection;
738         _bypassed = false;
739 }
740
741 Panner::~Panner ()
742 {
743 }
744
745 void
746 Panner::set_linked (bool yn)
747 {
748         if (yn != _linked) {
749                 _linked = yn;
750                 _session.set_dirty ();
751                 LinkStateChanged (); /* EMIT SIGNAL */
752         }
753 }
754
755 void
756 Panner::set_link_direction (LinkDirection ld)
757 {
758         if (ld != _link_direction) {
759                 _link_direction = ld;
760                 _session.set_dirty ();
761                 LinkStateChanged (); /* EMIT SIGNAL */
762         }
763 }
764
765 void
766 Panner::set_bypassed (bool yn)
767 {
768         if (yn != _bypassed) {
769                 _bypassed = yn;
770                 StateChanged ();
771         }
772 }
773
774
775 void
776 Panner::reset (uint32_t nouts, uint32_t npans)
777 {
778         uint32_t n;
779         bool changed = false;
780
781         if (nouts < 2 || (nouts == outputs.size() && npans == size())) {
782                 return;
783         } 
784
785         n = size();
786         clear ();
787
788         if (n != npans) {
789                 changed = true;
790         }
791
792         n = outputs.size();
793         outputs.clear ();
794
795         if (n != nouts) {
796                 changed = true;
797         }
798
799         switch (nouts) {
800         case 0:
801                 break;
802
803         case 1:
804                 fatal << _("programming error:")
805                       << X_("Panner::reset() called with a single output")
806                       << endmsg;
807                 /*NOTREACHED*/
808                 break;
809
810         case 2:
811                 /* line */
812                 outputs.push_back (Output (0, 0));
813                 outputs.push_back (Output (1.0, 0));
814
815                 for (n = 0; n < npans; ++n) {
816                         push_back (new EqualPowerStereoPanner (*this));
817                 }
818                 break;
819
820         case 3: // triangle
821                 outputs.push_back (Output  (0.5, 0));
822                 outputs.push_back (Output  (0, 1.0));
823                 outputs.push_back (Output  (1.0, 1.0));
824
825                 for (n = 0; n < npans; ++n) {
826                         push_back (new Multi2dPanner (*this));
827                 }
828
829                 break; 
830
831         case 4: // square
832                 outputs.push_back (Output  (0, 0));
833                 outputs.push_back (Output  (1.0, 0));
834                 outputs.push_back (Output  (1.0, 1.0));
835                 outputs.push_back (Output  (0, 1.0));
836
837                 for (n = 0; n < npans; ++n) {
838                         push_back (new Multi2dPanner (*this));
839                 }
840
841                 break;  
842
843         case 5: //square+offcenter center
844                 outputs.push_back (Output  (0, 0));
845                 outputs.push_back (Output  (1.0, 0));
846                 outputs.push_back (Output  (1.0, 1.0));
847                 outputs.push_back (Output  (0, 1.0));
848                 outputs.push_back (Output  (0.5, 0.75));
849
850                 for (n = 0; n < npans; ++n) {
851                         push_back (new Multi2dPanner (*this));
852                 }
853
854                 break;
855
856         default:
857                 /* XXX horrible placement. FIXME */
858                 for (n = 0; n < nouts; ++n) {
859                         outputs.push_back (Output (0.1 * n, 0.1 * n));
860                 }
861
862                 for (n = 0; n < npans; ++n) {
863                         push_back (new Multi2dPanner (*this));
864                 }
865
866                 break;
867         }
868
869         for (iterator x = begin(); x != end(); ++x) {
870                 (*x)->update ();
871         }
872
873         /* force hard left/right panning in a common case: 2in/2out 
874         */
875         
876         if (npans == 2 && outputs.size() == 2) {
877
878                 /* Do this only if we changed configuration, or our configuration
879                    appears to be the default set up (center).
880                 */
881
882                 float left;
883                 float right;
884
885                 front()->get_position (left);
886                 back()->get_position (right);
887
888                 if (changed || ((left == 0.5) && (right == 0.5))) {
889                 
890                         front()->set_position (0.0);
891                         front()->automation().reset_default (0.0);
892                         
893                         back()->set_position (1.0);
894                         back()->automation().reset_default (1.0);
895                         
896                         changed = true;
897                 }
898         }
899
900         if (changed) {
901                 Changed (); /* EMIT SIGNAL */
902         }
903
904         return;
905 }
906
907 void
908 Panner::remove (uint32_t which)
909 {
910         vector<StreamPanner*>::iterator i;
911         for (i = begin(); i != end() && which; ++i, --which);
912
913         if (i != end()) {
914                 delete *i;
915                 erase (i);
916         }
917 }
918
919 void
920 Panner::clear ()
921 {
922         for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
923                 delete *i;
924         }
925
926         vector<StreamPanner*>::clear ();
927 }
928
929 void
930 Panner::set_automation_style (AutoStyle style)
931 {
932         for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
933                 (*i)->set_automation_style (style);
934         }
935         _session.set_dirty ();
936 }       
937
938 void
939 Panner::set_automation_state (AutoState state)
940 {
941         for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
942                 (*i)->set_automation_state (state);
943         }
944         _session.set_dirty ();
945 }       
946
947 AutoState
948 Panner::automation_state () const
949 {
950         if (!empty()) {
951                 return front()->automation().automation_state ();
952         } else {
953                 return Off;
954         }
955 }
956
957 AutoStyle
958 Panner::automation_style () const
959 {
960         if (!empty()) {
961                 return front()->automation().automation_style ();
962         } else {
963                 return Absolute;
964         }
965 }
966
967 void
968 Panner::transport_stopped (nframes_t frame)
969 {
970         for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
971                 (*i)->transport_stopped (frame);
972         }
973 }       
974
975 void
976 Panner::snapshot (nframes_t now)
977 {
978         for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
979                 (*i)->snapshot (now);
980         }
981 }       
982
983 void
984 Panner::clear_automation ()
985 {
986         for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
987                 (*i)->automation().clear ();
988         }
989         _session.set_dirty ();
990 }       
991
992 struct PanPlugins {
993     string name;
994     uint32_t nouts;
995     StreamPanner* (*factory)(Panner&);
996 };
997
998 PanPlugins pan_plugins[] = {
999         { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1000         { Multi2dPanner::name, 3, Multi2dPanner::factory },
1001         { string (""), 0, 0 }
1002 };
1003
1004 XMLNode&
1005 Panner::get_state (void)
1006 {
1007         return state (true);
1008 }
1009
1010 XMLNode&
1011 Panner::state (bool full)
1012 {
1013         XMLNode* root = new XMLNode (X_("Panner"));
1014         char buf[32];
1015
1016         root->add_property (X_("linked"), (_linked ? "yes" : "no"));
1017         snprintf (buf, sizeof (buf), "%d", _link_direction);
1018         root->add_property (X_("link_direction"), buf);
1019         root->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1020
1021         /* add each output */
1022
1023         for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1024                 XMLNode* onode = new XMLNode (X_("Output"));
1025                 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1026                 onode->add_property (X_("x"), buf);
1027                 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1028                 onode->add_property (X_("y"), buf);
1029                 root->add_child_nocopy (*onode);
1030         }
1031
1032         for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1033                 root->add_child_nocopy ((*i)->state (full));
1034         }
1035
1036         return *root;
1037 }
1038
1039 int
1040 Panner::set_state (const XMLNode& node)
1041 {
1042         XMLNodeList nlist;
1043         XMLNodeConstIterator niter;
1044         const XMLProperty *prop;
1045         uint32_t i;
1046         StreamPanner* sp;
1047         LocaleGuard lg (X_("POSIX"));
1048
1049         clear ();
1050         outputs.clear ();
1051
1052         if ((prop = node.property (X_("linked"))) != 0) {
1053                 set_linked (prop->value() == "yes");
1054         }
1055
1056
1057         if ((prop = node.property (X_("bypassed"))) != 0) {
1058                 set_bypassed (prop->value() == "yes");
1059         }
1060
1061         if ((prop = node.property (X_("link_direction"))) != 0) {
1062                 sscanf (prop->value().c_str(), "%d", &i);
1063                 set_link_direction ((LinkDirection) (i));
1064         }
1065
1066         nlist = node.children();
1067
1068         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1069                 if ((*niter)->name() == X_("Output")) {
1070                         
1071                         float x, y;
1072                         
1073                         prop = (*niter)->property (X_("x"));
1074                         sscanf (prop->value().c_str(), "%g", &x);
1075                         
1076                         prop = (*niter)->property (X_("y"));
1077                         sscanf (prop->value().c_str(), "%g", &y);
1078                         
1079                         outputs.push_back (Output (x, y));
1080                 }
1081         }
1082
1083         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1084
1085                 if ((*niter)->name() == X_("StreamPanner")) {
1086                 
1087                         if ((prop = (*niter)->property (X_("type")))) {
1088                                 
1089                                 for (i = 0; pan_plugins[i].factory; ++i) {
1090                                         if (prop->value() == pan_plugins[i].name) {
1091                                                 
1092                                                 
1093                                                 /* note that we assume that all the stream panners
1094                                                    are of the same type. pretty good
1095                                                    assumption, but its still an assumption.
1096                                                 */
1097                                                 
1098                                                 sp = pan_plugins[i].factory (*this);
1099                                                 
1100                                                 if (sp->set_state (**niter) == 0) {
1101                                                         push_back (sp);
1102                                                 }
1103                                                 
1104                                                 break;
1105                                         }
1106                                 }
1107                                 
1108                                 
1109                                 if (!pan_plugins[i].factory) {
1110                                         error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1111                                                           prop->value())
1112                                               << endmsg;
1113                                 }
1114
1115                         } else {
1116                                 error << _("panner plugin node has no type information!")
1117                                       << endmsg;
1118                                 return -1;
1119                         }
1120
1121                 }       
1122         }
1123
1124         return 0;
1125 }
1126
1127
1128
1129 bool
1130 Panner::touching () const
1131 {
1132         for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
1133                 if ((*i)->automation().touching ()) {
1134                         return true;
1135                 }
1136         }
1137
1138         return false;
1139 }
1140
1141 void
1142 Panner::set_position (float xpos, StreamPanner& orig)
1143 {
1144         float xnow;
1145         float xdelta ;
1146         float xnew;
1147
1148         orig.get_position (xnow);
1149         xdelta = xpos - xnow;
1150         
1151         if (_link_direction == SameDirection) {
1152
1153                 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1154                         if (*i == &orig) {
1155                                 (*i)->set_position (xpos, true);
1156                         } else {
1157                                 (*i)->get_position (xnow);
1158                                 xnew = min (1.0f, xnow + xdelta);
1159                                 xnew = max (0.0f, xnew);
1160                                 (*i)->set_position (xnew, true);
1161                         }
1162                 }
1163
1164         } else {
1165
1166                 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1167                         if (*i == &orig) {
1168                                 (*i)->set_position (xpos, true);
1169                         } else {
1170                                 (*i)->get_position (xnow);
1171                                 xnew = min (1.0f, xnow - xdelta);
1172                                 xnew = max (0.0f, xnew);
1173                                 (*i)->set_position (xnew, true);
1174                         }
1175                 }
1176         }
1177 }
1178
1179 void
1180 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1181 {
1182         float xnow, ynow;
1183         float xdelta, ydelta;
1184         float xnew, ynew;
1185
1186         orig.get_position (xnow, ynow);
1187         xdelta = xpos - xnow;
1188         ydelta = ypos - ynow;
1189         
1190         if (_link_direction == SameDirection) {
1191
1192                 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1193                         if (*i == &orig) {
1194                                 (*i)->set_position (xpos, ypos, true);
1195                         } else {
1196                                 (*i)->get_position (xnow, ynow);
1197
1198                                 xnew = min (1.0f, xnow + xdelta);
1199                                 xnew = max (0.0f, xnew);
1200
1201                                 ynew = min (1.0f, ynow + ydelta);
1202                                 ynew = max (0.0f, ynew);
1203
1204                                 (*i)->set_position (xnew, ynew, true);
1205                         }
1206                 }
1207
1208         } else {
1209
1210                 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1211                         if (*i == &orig) {
1212                                 (*i)->set_position (xpos, ypos, true);
1213                         } else {
1214                                 (*i)->get_position (xnow, ynow);
1215                                 
1216                                 xnew = min (1.0f, xnow - xdelta);
1217                                 xnew = max (0.0f, xnew);
1218
1219                                 ynew = min (1.0f, ynow - ydelta);
1220                                 ynew = max (0.0f, ynew);
1221
1222                                 (*i)->set_position (xnew, ynew, true);
1223                         }
1224                 }
1225         }
1226 }
1227
1228 void
1229 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1230 {
1231         float xnow, ynow, znow;
1232         float xdelta, ydelta, zdelta;
1233         float xnew, ynew, znew;
1234
1235         orig.get_position (xnow, ynow, znow);
1236         xdelta = xpos - xnow;
1237         ydelta = ypos - ynow;
1238         zdelta = zpos - znow;
1239
1240         if (_link_direction == SameDirection) {
1241
1242                 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1243                         if (*i == &orig) {
1244                                 (*i)->set_position (xpos, ypos, zpos, true);
1245                         } else {
1246                                 (*i)->get_position (xnow, ynow, znow);
1247                                 
1248                                 xnew = min (1.0f, xnow + xdelta);
1249                                 xnew = max (0.0f, xnew);
1250
1251                                 ynew = min (1.0f, ynow + ydelta);
1252                                 ynew = max (0.0f, ynew);
1253
1254                                 znew = min (1.0f, znow + zdelta);
1255                                 znew = max (0.0f, znew);
1256
1257                                 (*i)->set_position (xnew, ynew, znew, true);
1258                         }
1259                 }
1260
1261         } else {
1262
1263                 for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
1264                         if (*i == &orig) {
1265                                 (*i)->set_position (xpos, ypos, true);
1266                         } else {
1267                                 (*i)->get_position (xnow, ynow, znow);
1268
1269                                 xnew = min (1.0f, xnow - xdelta);
1270                                 xnew = max (0.0f, xnew);
1271
1272                                 ynew = min (1.0f, ynow - ydelta);
1273                                 ynew = max (0.0f, ynew);
1274
1275                                 znew = min (1.0f, znow + zdelta);
1276                                 znew = max (0.0f, znew);
1277
1278                                 (*i)->set_position (xnew, ynew, znew, true);
1279                         }
1280                 }
1281         }
1282 }