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