Fix save/reload of pan automation.
[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 */
19
20 #define __STDC_FORMAT_MACROS 1
21 #include <inttypes.h>
22
23 #include <cmath>
24 #include <cerrno>
25 #include <fstream>
26 #include <cstdlib>
27 #include <string>
28 #include <cstdio>
29 #include <locale.h>
30 #include <unistd.h>
31 #include <float.h>
32 #include <iomanip>
33
34 #include <glibmm.h>
35
36 #include "pbd/error.h"
37 #include "pbd/failed_constructor.h"
38 #include "pbd/xml++.h"
39 #include "pbd/enumwriter.h"
40
41 #include "evoral/Curve.hpp"
42
43 #include "ardour/session.h"
44 #include "ardour/panner.h"
45 #include "ardour/utils.h"
46 #include "ardour/audio_buffer.h"
47
48 #include "ardour/runtime_functions.h"
49 #include "ardour/buffer_set.h"
50 #include "ardour/audio_buffer.h"
51
52 #include "i18n.h"
53
54 #include "pbd/mathfix.h"
55
56 using namespace std;
57 using namespace ARDOUR;
58 using namespace PBD;
59
60 float Panner::current_automation_version_number = 1.0;
61
62 string EqualPowerStereoPanner::name = "Equal Power Stereo";
63 string Multi2dPanner::name = "Multiple (2D)";
64
65 /* this is a default mapper of control values to a pan position
66    others can be imagined.
67 */
68
69 static pan_t direct_control_to_pan (double fract)
70 {
71         return fract;
72 }
73
74 StreamPanner::StreamPanner (Panner& p, Evoral::Parameter param)
75         : parent (p)
76 {
77         assert (param.type() != NullAutomation);
78
79         _muted = false;
80         _mono = false;
81
82         /* get our AutomationControl from our parent Panner, creating it if required */
83         _control = boost::dynamic_pointer_cast<AutomationControl> (parent.control (param, true));
84
85         _x = 0.5;
86         _y = 0.5;
87         _z = 0.5;
88 }
89
90 StreamPanner::~StreamPanner ()
91 {
92 }
93
94 void
95 StreamPanner::set_mono (bool yn)
96 {
97         if (yn != _mono) {
98                 _mono = yn;
99                 StateChanged ();
100         }
101 }
102
103 void
104 Panner::PanControllable::set_value (double val)
105 {
106         panner.streampanner (parameter().id()).set_position (direct_control_to_pan (val));
107         AutomationControl::set_value(val);
108 }
109
110 double
111 Panner::PanControllable::get_value (void) const
112 {
113         return AutomationControl::get_value();
114 }
115
116 void
117 StreamPanner::set_muted (bool yn)
118 {
119         if (yn != _muted) {
120                 _muted = yn;
121                 StateChanged ();
122         }
123 }
124
125 void
126 StreamPanner::set_position (float xpos, bool link_call)
127 {
128         if (!link_call && parent.linked()) {
129                 parent.set_position (xpos, *this);
130         }
131
132         if (_x != xpos) {
133                 _x = xpos;
134                 update ();
135                 Changed ();
136                 _control->Changed ();
137         }
138 }
139
140 void
141 StreamPanner::set_position (float xpos, float ypos, bool link_call)
142 {
143         if (!link_call && parent.linked()) {
144                 parent.set_position (xpos, ypos, *this);
145         }
146
147         if (_x != xpos || _y != ypos) {
148                 _x = xpos;
149                 _y = ypos;
150                 update ();
151                 Changed ();
152                 _control->Changed ();
153         }
154 }
155
156 void
157 StreamPanner::set_position (float xpos, float ypos, float zpos, bool link_call)
158 {
159         if (!link_call && parent.linked()) {
160                 parent.set_position (xpos, ypos, zpos, *this);
161         }
162
163         if (_x != xpos || _y != ypos || _z != zpos) {
164                 _x = xpos;
165                 _y = ypos;
166                 _z = zpos;
167                 update ();
168                 Changed ();
169                 _control->Changed ();
170         }
171 }
172
173 int
174 StreamPanner::set_state (const XMLNode& node, int /*version*/)
175 {
176         const XMLProperty* prop;
177         XMLNodeConstIterator iter;
178
179         if ((prop = node.property (X_("muted")))) {
180                 set_muted (string_is_affirmative (prop->value()));
181         }
182
183         if ((prop = node.property (X_("mono")))) {
184                 set_mono (string_is_affirmative (prop->value()));
185         }
186
187         return 0;
188 }
189
190 void
191 StreamPanner::add_state (XMLNode& node)
192 {
193         node.add_property (X_("muted"), (muted() ? "yes" : "no"));
194         node.add_property (X_("mono"), (_mono ? "yes" : "no"));
195 }
196
197 void
198 StreamPanner::distribute (AudioBuffer& src, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
199 {
200         if (_mono) {
201                 /* we're in mono mode, so just pan the input to all outputs equally */
202                 int const N = parent.nouts ();
203                 for (int i = 0; i < N; ++i) {
204                         mix_buffers_with_gain (obufs.get_audio(i).data(), src.data(), nframes, gain_coeff);
205                 }
206         } else {
207                 /* normal mode, call the `real' distribute method */
208                 do_distribute (src, obufs, gain_coeff, nframes);
209         }
210 }
211
212 void
213 StreamPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs,
214                                     nframes_t start, nframes_t end, nframes_t nframes, pan_t** buffers)
215 {
216         if (_mono) {
217                 /* we're in mono mode, so just pan the input to all outputs equally */
218                 int const N = parent.nouts ();
219                 for (int i = 0; i < N; ++i) {
220                         mix_buffers_with_gain (obufs.get_audio(i).data(), src.data(), nframes, 1.0);
221                 }
222         } else {
223                 /* normal mode, call the `real' distribute method */
224                 do_distribute_automated (src, obufs, start, end, nframes, buffers);
225         }
226         
227 }
228
229
230 /*---------------------------------------------------------------------- */
231
232 BaseStereoPanner::BaseStereoPanner (Panner& p, Evoral::Parameter param)
233         : StreamPanner (p, param)
234         , left (0.5)
235         , right (0.5)
236         , left_interp (left)
237         , right_interp (right)
238 {
239 }
240
241 BaseStereoPanner::~BaseStereoPanner ()
242 {
243 }
244
245 int
246 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
247 {
248         char line[128];
249         LocaleGuard lg (X_("POSIX"));
250
251         _control->list()->clear ();
252
253         while (in.getline (line, sizeof (line), '\n')) {
254                 nframes_t when;
255                 double value;
256
257                 ++linecnt;
258
259                 if (strcmp (line, "end") == 0) {
260                         break;
261                 }
262
263                 if (sscanf (line, "%" PRIu32 " %lf", &when, &value) != 2) {
264                         warning << string_compose(_("badly formatted pan automation event record at line %1 of %2 (ignored) [%3]"), linecnt, path, line) << endmsg;
265                         continue;
266                 }
267
268                 _control->list()->fast_simple_add (when, value);
269         }
270
271         /* now that we are done loading */
272
273         ((AutomationList*)_control->list().get())->StateChanged ();
274
275         return 0;
276 }
277
278 void
279 BaseStereoPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
280 {
281         assert(obufs.count().n_audio() == 2);
282
283         pan_t delta;
284         Sample* dst;
285         pan_t pan;
286
287         if (_muted) {
288                 return;
289         }
290
291         Sample* const src = srcbuf.data();
292
293         /* LEFT */
294
295         dst = obufs.get_audio(0).data();
296
297         if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc
298
299                 /* we've moving the pan by an appreciable amount, so we must
300                    interpolate over 64 frames or nframes, whichever is smaller */
301
302                 nframes_t const limit = min ((nframes_t)64, nframes);
303                 nframes_t n;
304
305                 delta = -(delta / (float) (limit));
306
307                 for (n = 0; n < limit; n++) {
308                         left_interp = left_interp + delta;
309                         left = left_interp + 0.9 * (left - left_interp);
310                         dst[n] += src[n] * left * gain_coeff;
311                 }
312
313                 /* then pan the rest of the buffer; no need for interpolation for this bit */
314
315                 pan = left * gain_coeff;
316
317                 mix_buffers_with_gain (dst+n,src+n,nframes-n,pan);
318
319         } else {
320
321                 left = desired_left;
322                 left_interp = left;
323
324                 if ((pan = (left * gain_coeff)) != 1.0f) {
325
326                         if (pan != 0.0f) {
327
328                                 /* pan is 1 but also not 0, so we must do it "properly" */
329
330                                 mix_buffers_with_gain(dst,src,nframes,pan);
331
332                                 /* mark that we wrote into the buffer */
333
334                                 // obufs[0] = 0;
335
336                         }
337
338                 } else {
339
340                         /* pan is 1 so we can just copy the input samples straight in */
341
342                         mix_buffers_no_gain(dst,src,nframes);
343
344                         /* mark that we wrote into the buffer */
345
346                         // obufs[0] = 0;
347                 }
348         }
349
350         /* RIGHT */
351
352         dst = obufs.get_audio(1).data();
353
354         if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc
355
356                 /* we're moving the pan by an appreciable amount, so we must
357                    interpolate over 64 frames or nframes, whichever is smaller */
358
359                 nframes_t const limit = min ((nframes_t)64, nframes);
360                 nframes_t n;
361
362                 delta = -(delta / (float) (limit));
363
364                 for (n = 0; n < limit; n++) {
365                         right_interp = right_interp + delta;
366                         right = right_interp + 0.9 * (right - right_interp);
367                         dst[n] += src[n] * right * gain_coeff;
368                 }
369
370                 /* then pan the rest of the buffer, no need for interpolation for this bit */
371
372                 pan = right * gain_coeff;
373
374                 mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
375
376                 /* XXX it would be nice to mark the buffer as written to */
377
378         } else {
379
380                 right = desired_right;
381                 right_interp = right;
382
383                 if ((pan = (right * gain_coeff)) != 1.0f) {
384
385                         if (pan != 0.0f) {
386
387                                 /* pan is not 1 but also not 0, so we must do it "properly" */
388                                 
389                                 mix_buffers_with_gain(dst,src,nframes,pan);
390
391                                 /* XXX it would be nice to mark the buffer as written to */
392                         }
393
394                 } else {
395
396                         /* pan is 1 so we can just copy the input samples straight in */
397                         
398                         mix_buffers_no_gain(dst,src,nframes);
399
400                         /* XXX it would be nice to mark the buffer as written to */
401                 }
402         }
403 }
404
405 /*---------------------------------------------------------------------- */
406
407 EqualPowerStereoPanner::EqualPowerStereoPanner (Panner& p, Evoral::Parameter param)
408         : BaseStereoPanner (p, param)
409 {
410         update ();
411
412         left = desired_left;
413         right = desired_right;
414         left_interp = left;
415         right_interp = right;
416 }
417
418 EqualPowerStereoPanner::~EqualPowerStereoPanner ()
419 {
420 }
421
422 void
423 EqualPowerStereoPanner::update ()
424 {
425         /* it would be very nice to split this out into a virtual function
426            that can be accessed from BaseStereoPanner and used in do_distribute_automated().
427
428            but the place where its used in do_distribute_automated() is a tight inner loop,
429            and making "nframes" virtual function calls to compute values is an absurd
430            overhead.
431         */
432
433         /* x == 0 => hard left
434            x == 1 => hard right
435         */
436
437         float const panR = _x;
438         float const panL = 1 - panR;
439
440         float const pan_law_attenuation = -3.0f;
441         float const scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
442
443         desired_left = panL * (scale * panL + 1.0f - scale);
444         desired_right = panR * (scale * panR + 1.0f - scale);
445
446         effective_x = _x;
447         //_control->set_value(x);
448 }
449
450 void
451 EqualPowerStereoPanner::do_distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs,
452                                                  nframes_t start, nframes_t end, nframes_t nframes,
453                                                  pan_t** buffers)
454 {
455         assert (obufs.count().n_audio() == 2);
456
457         Sample* dst;
458         pan_t* pbuf;
459         Sample* const src = srcbuf.data();
460
461         /* fetch positional data */
462
463         if (!_control->list()->curve().rt_safe_get_vector (start, end, buffers[0], nframes)) {
464                 /* fallback */
465                 if (!_muted) {
466                         do_distribute (srcbuf, obufs, 1.0, nframes);
467                 }
468                 return;
469         }
470
471         /* store effective pan position. do this even if we are muted */
472
473         if (nframes > 0) {
474                 effective_x = buffers[0][nframes-1];
475         }
476
477         if (_muted) {
478                 return;
479         }
480
481         /* apply pan law to convert positional data into pan coefficients for
482            each buffer (output)
483         */
484
485         const float pan_law_attenuation = -3.0f;
486         const float scale = 2.0f - 4.0f * powf (10.0f,pan_law_attenuation/20.0f);
487
488         for (nframes_t n = 0; n < nframes; ++n) {
489
490                 float const panR = buffers[0][n];
491                 float const panL = 1 - panR;
492
493                 buffers[0][n] = panL * (scale * panL + 1.0f - scale);
494                 buffers[1][n] = panR * (scale * panR + 1.0f - scale);
495         }
496
497         /* LEFT */
498
499         dst = obufs.get_audio(0).data();
500         pbuf = buffers[0];
501
502         for (nframes_t n = 0; n < nframes; ++n) {
503                 dst[n] += src[n] * pbuf[n];
504         }
505
506         /* XXX it would be nice to mark the buffer as written to */
507
508         /* RIGHT */
509
510         dst = obufs.get_audio(1).data();
511         pbuf = buffers[1];
512
513         for (nframes_t n = 0; n < nframes; ++n) {
514                 dst[n] += src[n] * pbuf[n];
515         }
516
517         /* XXX it would be nice to mark the buffer as written to */
518 }
519
520 StreamPanner*
521 EqualPowerStereoPanner::factory (Panner& parent, Evoral::Parameter param)
522 {
523         return new EqualPowerStereoPanner (parent, param);
524 }
525
526 XMLNode&
527 EqualPowerStereoPanner::get_state (void)
528 {
529         return state (true);
530 }
531
532 XMLNode&
533 EqualPowerStereoPanner::state (bool /*full_state*/)
534 {
535         XMLNode* root = new XMLNode ("StreamPanner");
536         char buf[64];
537         LocaleGuard lg (X_("POSIX"));
538
539         snprintf (buf, sizeof (buf), "%.12g", _x);
540         root->add_property (X_("x"), buf);
541         root->add_property (X_("type"), EqualPowerStereoPanner::name);
542
543         // XXX: dont save automation here... its part of the automatable panner now.
544
545         StreamPanner::add_state (*root);
546
547         root->add_child_nocopy (_control->get_state ());
548
549         return *root;
550 }
551
552 int
553 EqualPowerStereoPanner::set_state (const XMLNode& node, int version)
554 {
555         const XMLProperty* prop;
556         LocaleGuard lg (X_("POSIX"));
557
558         if ((prop = node.property (X_("x")))) {
559                 const float pos = atof (prop->value().c_str());
560                 set_position (pos, true);
561         }
562
563         StreamPanner::set_state (node, version);
564
565         for (XMLNodeConstIterator iter = node.children().begin(); iter != node.children().end(); ++iter) {
566
567                 if ((*iter)->name() == X_("Controllable")) {
568                         if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
569                                 _control->set_state (**iter, version);
570                         }
571
572                 } else if ((*iter)->name() == X_("Automation")) {
573
574                         _control->alist()->set_state (*((*iter)->children().front()), version);
575
576                         if (_control->alist()->automation_state() != Off) {
577                                 set_position (_control->list()->eval (parent.session().transport_frame()));
578                         }
579                 }
580         }
581
582         return 0;
583 }
584
585 /*----------------------------------------------------------------------*/
586
587 Multi2dPanner::Multi2dPanner (Panner& p, Evoral::Parameter param)
588         : StreamPanner (p, param)
589 {
590         update ();
591 }
592
593 Multi2dPanner::~Multi2dPanner ()
594 {
595 }
596
597 void
598 Multi2dPanner::update ()
599 {
600         static const float BIAS = FLT_MIN;
601         uint32_t i;
602         uint32_t const nouts = parent.nouts ();
603         float dsq[nouts];
604         float f, fr;
605         vector<pan_t> pans;
606
607         f = 0.0f;
608
609         for (i = 0; i < nouts; i++) {
610                 dsq[i] = ((_x - parent.output(i).x) * (_x - parent.output(i).x) + (_y - parent.output(i).y) * (_y - parent.output(i).y) + BIAS);
611                 if (dsq[i] < 0.0) {
612                         dsq[i] = 0.0;
613                 }
614                 f += dsq[i] * dsq[i];
615         }
616 #ifdef __APPLE__
617         // terrible hack to support OSX < 10.3.9 builds
618         fr = (float) (1.0 / sqrt((double)f));
619 #else
620         fr = 1.0 / sqrtf(f);
621 #endif
622         for (i = 0; i < nouts; ++i) {
623                 parent.output(i).desired_pan = 1.0f - (dsq[i] * fr);
624         }
625
626         effective_x = _x;
627 }
628
629 void
630 Multi2dPanner::do_distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
631 {
632         Sample* dst;
633         pan_t pan;
634
635         if (_muted) {
636                 return;
637         }
638
639         Sample* const src = srcbuf.data();
640
641         uint32_t const N = parent.nouts ();
642         for (uint32_t n = 0; n < N; ++n) {
643                 Panner::Output& o = parent.output (n);
644
645                 dst = obufs.get_audio(n).data();
646
647 #ifdef CAN_INTERP
648                 if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc
649
650                         /* interpolate over 64 frames or nframes, whichever is smaller */
651
652                         nframes_t limit = min ((nframes_t)64, nframes);
653                         nframes_t n;
654
655                         delta = -(delta / (float) (limit));
656
657                         for (n = 0; n < limit; n++) {
658                                 left_interp = left_interp + delta;
659                                 left = left_interp + 0.9 * (left - left_interp);
660                                 dst[n] += src[n] * left * gain_coeff;
661                         }
662
663                         pan = left * gain_coeff;
664                         mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
665
666                 } else {
667
668 #else
669                         pan = o.desired_pan;
670
671                         if ((pan *= gain_coeff) != 1.0f) {
672
673                                 if (pan != 0.0f) {
674                                         mix_buffers_with_gain(dst,src,nframes,pan);
675                                 }
676                                 
677                         } else {
678                                         mix_buffers_no_gain(dst,src,nframes);
679                         }
680 #endif
681 #ifdef CAN_INTERP
682                 }
683 #endif
684         }
685
686         return;
687 }
688
689 void
690 Multi2dPanner::do_distribute_automated (AudioBuffer& /*src*/, BufferSet& /*obufs*/,
691                                         nframes_t /*start*/, nframes_t /*end*/, nframes_t /*nframes*/,
692                                         pan_t** /*buffers*/)
693 {
694         if (_muted) {
695                 return;
696         }
697
698         /* what ? */
699
700         return;
701 }
702
703 StreamPanner*
704 Multi2dPanner::factory (Panner& p, Evoral::Parameter param)
705 {
706         return new Multi2dPanner (p, param);
707 }
708
709 int
710 Multi2dPanner::load (istream& /*in*/, string /*path*/, uint32_t& /*linecnt*/)
711 {
712         return 0;
713 }
714
715 XMLNode&
716 Multi2dPanner::get_state (void)
717 {
718         return state (true);
719 }
720
721 XMLNode&
722 Multi2dPanner::state (bool /*full_state*/)
723 {
724         XMLNode* root = new XMLNode ("StreamPanner");
725         char buf[64];
726         LocaleGuard lg (X_("POSIX"));
727
728         snprintf (buf, sizeof (buf), "%.12g", _x);
729         root->add_property (X_("x"), buf);
730         snprintf (buf, sizeof (buf), "%.12g", _y);
731         root->add_property (X_("y"), buf);
732         root->add_property (X_("type"), Multi2dPanner::name);
733
734         /* XXX no meaningful automation yet */
735
736         return *root;
737 }
738
739 int
740 Multi2dPanner::set_state (const XMLNode& node, int /*version*/)
741 {
742         const XMLProperty* prop;
743         float newx,newy;
744         LocaleGuard lg (X_("POSIX"));
745
746         newx = -1;
747         newy = -1;
748
749         if ((prop = node.property (X_("x")))) {
750                 newx = atof (prop->value().c_str());
751         }
752
753         if ((prop = node.property (X_("y")))) {
754                 newy = atof (prop->value().c_str());
755         }
756
757         if (_x < 0 || _y < 0) {
758                 error << _("badly-formed positional data for Multi2dPanner - ignored")
759                       << endmsg;
760                 return -1;
761         }
762
763         set_position (newx, newy);
764         return 0;
765 }
766
767 /*---------------------------------------------------------------------- */
768
769 Panner::Panner (string name, Session& s)
770         : SessionObject (s, name)
771         , Automatable (s)
772 {
773         //set_name_old_auto (name);
774         set_name (name);
775
776         _linked = false;
777         _link_direction = SameDirection;
778         _bypassed = false;
779         _mono = 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
807 void
808 Panner::set_bypassed (bool yn)
809 {
810         if (yn != _bypassed) {
811                 _bypassed = yn;
812                 StateChanged ();
813         }
814 }
815
816
817 void
818 Panner::reset_to_default ()
819 {
820         vector<float> positions;
821
822         switch (outputs.size()) {
823         case 0:
824         case 1:
825                 return;
826         }
827
828         if (outputs.size() == 2) {
829                 switch (_streampanners.size()) {
830                 case 1:
831                         _streampanners.front()->set_position (0.5);
832                         _streampanners.front()->pan_control()->list()->reset_default (0.5);
833                         return;
834                         break;
835                 case 2:
836                         _streampanners.front()->set_position (0.0);
837                         _streampanners.front()->pan_control()->list()->reset_default (0.0);
838                         _streampanners.back()->set_position (1.0);
839                         _streampanners.back()->pan_control()->list()->reset_default (1.0);
840                         return;
841                 default:
842                         break;
843                 }
844         }
845
846         vector<Output>::iterator o;
847         vector<StreamPanner*>::iterator p;
848
849         for (o = outputs.begin(), p = _streampanners.begin(); o != outputs.end() && p != _streampanners.end(); ++o, ++p) {
850                 (*p)->set_position ((*o).x, (*o).y);
851         }
852 }
853
854 void
855 Panner::reset_streampanner (uint32_t which)
856 {
857         if (which >= _streampanners.size() || which >= outputs.size()) {
858                 return;
859         }
860
861         switch (outputs.size()) {
862         case 0:
863         case 1:
864                 return;
865
866         case 2:
867                 switch (_streampanners.size()) {
868                 case 1:
869                         /* stereo out, 1 stream, default = middle */
870                         _streampanners.front()->set_position (0.5);
871                         _streampanners.front()->pan_control()->list()->reset_default (0.5);
872                         break;
873                 case 2:
874                         /* stereo out, 2 streams, default = hard left/right */
875                         if (which == 0) {
876                                 _streampanners.front()->set_position (0.0);
877                                 _streampanners.front()->pan_control()->list()->reset_default (0.0);
878                         } else {
879                                 _streampanners.back()->set_position (1.0);
880                                 _streampanners.back()->pan_control()->list()->reset_default (1.0);
881                         }
882                         break;
883                 }
884                 return;
885
886         default:
887                 _streampanners[which]->set_position (outputs[which].x, outputs[which].y);
888         }
889 }
890
891 /**
892  *    Reset the panner with a given number of outs and panners (and hence inputs)
893  *
894  *    \param nouts Number of outputs.
895  *    \param npans Number of panners.
896  */
897 void
898 Panner::reset (uint32_t nouts, uint32_t npans)
899 {
900         uint32_t n;
901         bool changed = false;
902         bool do_not_and_did_not_need_panning = ((nouts < 2) && (outputs.size() < 2));
903
904         /* if new and old config don't need panning, or if
905            the config hasn't changed, we're done.
906         */
907
908         if (do_not_and_did_not_need_panning ||
909             ((nouts == outputs.size()) && (npans == _streampanners.size()))) {
910                 return;
911         }
912
913         n = _streampanners.size();
914         clear_panners ();
915
916         if (n != npans) {
917                 changed = true;
918         }
919
920         n = outputs.size();
921         outputs.clear ();
922
923         if (n != nouts) {
924                 changed = true;
925         }
926
927         if (nouts < 2) {
928                 /* no need for panning with less than 2 outputs */
929                 if (changed) {
930                         Changed (); /* EMIT SIGNAL */
931                 }
932                 return;
933         }
934
935         switch (nouts) {
936         case 0:
937                 /* XXX: this can never happen */
938                 break;
939
940         case 1:
941                 /* XXX: this can never happen */
942                 fatal << _("programming error:")
943                       << X_("Panner::reset() called with a single output")
944                       << endmsg;
945                 /*NOTREACHED*/
946                 break;
947
948         case 2: // line
949                 outputs.push_back (Output (0, 0));
950                 outputs.push_back (Output (1.0, 0));
951
952                 for (n = 0; n < npans; ++n) {
953                         _streampanners.push_back (new EqualPowerStereoPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
954                 }
955                 break;
956
957         case 3: // triangle
958                 outputs.push_back (Output  (0.5, 0));
959                 outputs.push_back (Output  (0, 1.0));
960                 outputs.push_back (Output  (1.0, 1.0));
961
962                 for (n = 0; n < npans; ++n) {
963                         _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
964                 }
965
966                 break;
967
968         case 4: // square
969                 outputs.push_back (Output  (0, 0));
970                 outputs.push_back (Output  (1.0, 0));
971                 outputs.push_back (Output  (1.0, 1.0));
972                 outputs.push_back (Output  (0, 1.0));
973
974                 for (n = 0; n < npans; ++n) {
975                         _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
976                 }
977
978                 break;
979
980         case 5: //square+offcenter center
981                 outputs.push_back (Output  (0, 0));
982                 outputs.push_back (Output  (1.0, 0));
983                 outputs.push_back (Output  (1.0, 1.0));
984                 outputs.push_back (Output  (0, 1.0));
985                 outputs.push_back (Output  (0.5, 0.75));
986
987                 for (n = 0; n < npans; ++n) {
988                         _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
989                 }
990
991                 break;
992
993         default:
994                 /* XXX horrible placement. FIXME */
995                 for (n = 0; n < nouts; ++n) {
996                         outputs.push_back (Output (0.1 * n, 0.1 * n));
997                 }
998
999                 for (n = 0; n < npans; ++n) {
1000                         _streampanners.push_back (new Multi2dPanner (*this, Evoral::Parameter(PanAutomation, 0, n)));
1001                 }
1002
1003                 break;
1004         }
1005
1006         for (std::vector<StreamPanner*>::iterator x = _streampanners.begin(); x != _streampanners.end(); ++x) {
1007                 (*x)->update ();
1008         }
1009
1010         /* must emit Changed here, otherwise the changes to the pan_control below raise further
1011            signals which the GUI is not prepared for until it has seen the Changed here.
1012         */
1013         
1014         if (changed) {
1015                 Changed (); /* EMIT SIGNAL */
1016         }
1017
1018         /* force hard left/right panning in a common case: 2in/2out
1019         */
1020
1021         if (npans == 2 && outputs.size() == 2) {
1022
1023                 /* Do this only if we changed configuration, or our configuration
1024                    appears to be the default set up (center).
1025                 */
1026
1027                 float left;
1028                 float right;
1029
1030                 _streampanners.front()->get_position (left);
1031                 _streampanners.back()->get_position (right);
1032
1033                 if (changed || ((left == 0.5) && (right == 0.5))) {
1034
1035                         _streampanners.front()->set_position (0.0);
1036                         _streampanners.front()->pan_control()->list()->reset_default (0.0);
1037
1038                         _streampanners.back()->set_position (1.0);
1039                         _streampanners.back()->pan_control()->list()->reset_default (1.0);
1040                 }
1041         }
1042 }
1043
1044 void
1045 Panner::remove (uint32_t which)
1046 {
1047         vector<StreamPanner*>::iterator i;
1048         for (i = _streampanners.begin(); i != _streampanners.end() && which; ++i, --which) {}
1049
1050         if (i != _streampanners.end()) {
1051                 delete *i;
1052                 _streampanners.erase (i);
1053         }
1054 }
1055
1056
1057 /** Remove all our StreamPanners */
1058 void
1059 Panner::clear_panners ()
1060 {
1061         for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1062                 delete *i;
1063         }
1064
1065         _streampanners.clear ();
1066 }
1067
1068 void
1069 Panner::set_automation_style (AutoStyle style)
1070 {
1071         for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1072                 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_style (style);
1073         }
1074         _session.set_dirty ();
1075 }
1076
1077 void
1078 Panner::set_automation_state (AutoState state)
1079 {
1080         for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1081                 ((AutomationList*)(*i)->pan_control()->list().get())->set_automation_state (state);
1082         }
1083         _session.set_dirty ();
1084 }
1085
1086 AutoState
1087 Panner::automation_state () const
1088 {
1089         boost::shared_ptr<AutomationList> l;
1090         if (!empty()) {
1091                 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1092                 if (control) {
1093                         l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1094                 }
1095         }
1096
1097         return l ? l->automation_state() : Off;
1098 }
1099
1100 AutoStyle
1101 Panner::automation_style () const
1102 {
1103         boost::shared_ptr<AutomationList> l;
1104         if (!empty()) {
1105                 boost::shared_ptr<AutomationControl> control = _streampanners.front()->pan_control();
1106                 if (control) {
1107                         l = boost::dynamic_pointer_cast<AutomationList>(control->list());
1108                 }
1109         }
1110
1111         return l ? l->automation_style() : Absolute;
1112 }
1113
1114 struct PanPlugins {
1115     string name;
1116     uint32_t nouts;
1117     StreamPanner* (*factory)(Panner&, Evoral::Parameter);
1118 };
1119
1120 PanPlugins pan_plugins[] = {
1121         { EqualPowerStereoPanner::name, 2, EqualPowerStereoPanner::factory },
1122         { Multi2dPanner::name, 3, Multi2dPanner::factory },
1123         { string (""), 0, 0 }
1124 };
1125
1126 XMLNode&
1127 Panner::get_state (void)
1128 {
1129         return state (true);
1130 }
1131
1132 XMLNode&
1133 Panner::state (bool full)
1134 {
1135         XMLNode* node = new XMLNode ("Panner");
1136
1137         char buf[32];
1138
1139         node->add_property (X_("linked"), (_linked ? "yes" : "no"));
1140         node->add_property (X_("link_direction"), enum_2_string (_link_direction));
1141         node->add_property (X_("bypassed"), (bypassed() ? "yes" : "no"));
1142
1143         for (vector<Panner::Output>::iterator o = outputs.begin(); o != outputs.end(); ++o) {
1144                 XMLNode* onode = new XMLNode (X_("Output"));
1145                 snprintf (buf, sizeof (buf), "%.12g", (*o).x);
1146                 onode->add_property (X_("x"), buf);
1147                 snprintf (buf, sizeof (buf), "%.12g", (*o).y);
1148                 onode->add_property (X_("y"), buf);
1149                 node->add_child_nocopy (*onode);
1150         }
1151
1152         for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1153                 node->add_child_nocopy ((*i)->state (full));
1154         }
1155
1156         node->add_child_nocopy (get_automation_xml_state ());
1157
1158         return *node;
1159 }
1160
1161 int
1162 Panner::set_state (const XMLNode& node, int version)
1163 {
1164         XMLNodeList nlist;
1165         XMLNodeConstIterator niter;
1166         const XMLProperty *prop;
1167         uint32_t i;
1168         uint32_t num_panners = 0;
1169         StreamPanner* sp;
1170         LocaleGuard lg (X_("POSIX"));
1171
1172         clear_panners ();
1173
1174         ChanCount ins = ChanCount::ZERO;
1175         ChanCount outs = ChanCount::ZERO;
1176
1177         // XXX: this might not be necessary anymore
1178         outputs.clear ();
1179
1180         if ((prop = node.property (X_("linked"))) != 0) {
1181                 set_linked (string_is_affirmative (prop->value()));
1182         }
1183
1184
1185         if ((prop = node.property (X_("bypassed"))) != 0) {
1186                 set_bypassed (string_is_affirmative (prop->value()));
1187         }
1188
1189         if ((prop = node.property (X_("link_direction"))) != 0) {
1190                 LinkDirection ld; /* here to provide type information */
1191                 set_link_direction (LinkDirection (string_2_enum (prop->value(), ld)));
1192         }
1193
1194         nlist = node.children();
1195
1196         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1197                 if ((*niter)->name() == X_("Output")) {
1198
1199                         float x, y;
1200
1201                         prop = (*niter)->property (X_("x"));
1202                         sscanf (prop->value().c_str(), "%g", &x);
1203
1204                         prop = (*niter)->property (X_("y"));
1205                         sscanf (prop->value().c_str(), "%g", &y);
1206
1207                         outputs.push_back (Output (x, y));
1208                 }
1209         }
1210
1211         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1212
1213                 if ((*niter)->name() == X_("StreamPanner")) {
1214
1215                         if ((prop = (*niter)->property (X_("type")))) {
1216
1217                                 for (i = 0; pan_plugins[i].factory; ++i) {
1218                                         if (prop->value() == pan_plugins[i].name) {
1219
1220
1221                                                 /* note that we assume that all the stream panners
1222                                                    are of the same type. pretty good
1223                                                    assumption, but it's still an assumption.
1224                                                 */
1225
1226                                                 sp = pan_plugins[i].factory (*this, Evoral::Parameter(PanAutomation, 0, num_panners));
1227                                                 num_panners++;
1228
1229                                                 if (sp->set_state (**niter, version) == 0) {
1230                                                         _streampanners.push_back (sp);
1231                                                 }
1232
1233                                                 break;
1234                                         }
1235                                 }
1236
1237
1238                                 if (!pan_plugins[i].factory) {
1239                                         error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
1240                                                           prop->value())
1241                                               << endmsg;
1242                                 }
1243
1244                         } else {
1245                                 error << _("panner plugin node has no type information!")
1246                                       << endmsg;
1247                                 return -1;
1248                         }
1249
1250                 }
1251         }
1252
1253         reset (outputs.size (), num_panners);
1254         /* don't try to do old-school automation loading if it wasn't marked as existing */
1255
1256         if ((prop = node.property (X_("automation")))) {
1257
1258                 /* automation path is relative */
1259
1260                 automation_path = Glib::build_filename(_session.automation_dir(), prop->value ());
1261         }
1262
1263         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1264                 if ((*niter)->name() == X_("Automation")) {
1265                         set_automation_xml_state (**niter, Evoral::Parameter (PanAutomation));
1266                 }
1267         }
1268         
1269         return 0;
1270 }
1271
1272 bool
1273 Panner::touching () const
1274 {
1275         for (vector<StreamPanner*>::const_iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1276                 if (((AutomationList*)(*i)->pan_control()->list().get())->touching ()) {
1277                         return true;
1278                 }
1279         }
1280
1281         return false;
1282 }
1283
1284 void
1285 Panner::set_position (float xpos, StreamPanner& orig)
1286 {
1287         float xnow;
1288         float xdelta ;
1289         float xnew;
1290
1291         orig.get_position (xnow);
1292         xdelta = xpos - xnow;
1293
1294         if (_link_direction == SameDirection) {
1295
1296                 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1297                         if (*i == &orig) {
1298                                 (*i)->set_position (xpos, true);
1299                         } else {
1300                                 (*i)->get_position (xnow);
1301                                 xnew = min (1.0f, xnow + xdelta);
1302                                 xnew = max (0.0f, xnew);
1303                                 (*i)->set_position (xnew, true);
1304                         }
1305                 }
1306
1307         } else {
1308
1309                 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1310                         if (*i == &orig) {
1311                                 (*i)->set_position (xpos, true);
1312                         } else {
1313                                 (*i)->get_position (xnow);
1314                                 xnew = min (1.0f, xnow - xdelta);
1315                                 xnew = max (0.0f, xnew);
1316                                 (*i)->set_position (xnew, true);
1317                         }
1318                 }
1319         }
1320 }
1321
1322 void
1323 Panner::set_position (float xpos, float ypos, StreamPanner& orig)
1324 {
1325         float xnow, ynow;
1326         float xdelta, ydelta;
1327         float xnew, ynew;
1328
1329         orig.get_position (xnow, ynow);
1330         xdelta = xpos - xnow;
1331         ydelta = ypos - ynow;
1332
1333         if (_link_direction == SameDirection) {
1334
1335                 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1336                         if (*i == &orig) {
1337                                 (*i)->set_position (xpos, ypos, true);
1338                         } else {
1339                                 (*i)->get_position (xnow, ynow);
1340
1341                                 xnew = min (1.0f, xnow + xdelta);
1342                                 xnew = max (0.0f, xnew);
1343
1344                                 ynew = min (1.0f, ynow + ydelta);
1345                                 ynew = max (0.0f, ynew);
1346
1347                                 (*i)->set_position (xnew, ynew, true);
1348                         }
1349                 }
1350
1351         } else {
1352
1353                 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1354                         if (*i == &orig) {
1355                                 (*i)->set_position (xpos, ypos, true);
1356                         } else {
1357                                 (*i)->get_position (xnow, ynow);
1358
1359                                 xnew = min (1.0f, xnow - xdelta);
1360                                 xnew = max (0.0f, xnew);
1361
1362                                 ynew = min (1.0f, ynow - ydelta);
1363                                 ynew = max (0.0f, ynew);
1364
1365                                 (*i)->set_position (xnew, ynew, true);
1366                         }
1367                 }
1368         }
1369 }
1370
1371 void
1372 Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
1373 {
1374         float xnow, ynow, znow;
1375         float xdelta, ydelta, zdelta;
1376         float xnew, ynew, znew;
1377
1378         orig.get_position (xnow, ynow, znow);
1379         xdelta = xpos - xnow;
1380         ydelta = ypos - ynow;
1381         zdelta = zpos - znow;
1382
1383         if (_link_direction == SameDirection) {
1384
1385                 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1386                         if (*i == &orig) {
1387                                 (*i)->set_position (xpos, ypos, zpos, true);
1388                         } else {
1389                                 (*i)->get_position (xnow, ynow, znow);
1390
1391                                 xnew = min (1.0f, xnow + xdelta);
1392                                 xnew = max (0.0f, xnew);
1393
1394                                 ynew = min (1.0f, ynow + ydelta);
1395                                 ynew = max (0.0f, ynew);
1396
1397                                 znew = min (1.0f, znow + zdelta);
1398                                 znew = max (0.0f, znew);
1399
1400                                 (*i)->set_position (xnew, ynew, znew, true);
1401                         }
1402                 }
1403
1404         } else {
1405
1406                 for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1407                         if (*i == &orig) {
1408                                 (*i)->set_position (xpos, ypos, true);
1409                         } else {
1410                                 (*i)->get_position (xnow, ynow, znow);
1411
1412                                 xnew = min (1.0f, xnow - xdelta);
1413                                 xnew = max (0.0f, xnew);
1414
1415                                 ynew = min (1.0f, ynow - ydelta);
1416                                 ynew = max (0.0f, ynew);
1417
1418                                 znew = min (1.0f, znow + zdelta);
1419                                 znew = max (0.0f, znew);
1420
1421                                 (*i)->set_position (xnew, ynew, znew, true);
1422                         }
1423                 }
1424         }
1425 }
1426
1427 void
1428 Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, gain_t gain_coeff)
1429 {
1430         if (outbufs.count().n_audio() == 0) {
1431                 // Don't want to lose audio...
1432                 assert(inbufs.count().n_audio() == 0);
1433                 return;
1434         }
1435
1436         // We shouldn't be called in the first place...
1437         assert(!bypassed());
1438         assert(!empty());
1439
1440
1441         if (outbufs.count().n_audio() == 1) {
1442
1443                 AudioBuffer& dst = outbufs.get_audio(0);
1444
1445                 if (gain_coeff == 0.0f) {
1446
1447                         /* only one output, and gain was zero, so make it silent */
1448
1449                         dst.silence (nframes);
1450
1451                 } else if (gain_coeff == 1.0f){
1452
1453                         /* mix all buffers into the output */
1454
1455                         // copy the first
1456                         dst.read_from(inbufs.get_audio(0), nframes);
1457
1458                         // accumulate starting with the second
1459                         if (inbufs.count().n_audio() > 0) {
1460                                 BufferSet::audio_iterator i = inbufs.audio_begin();
1461                                 for (++i; i != inbufs.audio_end(); ++i) {
1462                                         dst.merge_from(*i, nframes);
1463                                 }
1464                         }
1465
1466                 } else {
1467
1468                         /* mix all buffers into the output, scaling them all by the gain */
1469
1470                         // copy the first
1471                         dst.read_from(inbufs.get_audio(0), nframes);
1472
1473                         // accumulate (with gain) starting with the second
1474                         if (inbufs.count().n_audio() > 0) {
1475                                 BufferSet::audio_iterator i = inbufs.audio_begin();
1476                                 for (++i; i != inbufs.audio_end(); ++i) {
1477                                         dst.accumulate_with_gain_from(*i, nframes, gain_coeff);
1478                                 }
1479                         }
1480
1481                 }
1482
1483                 return;
1484         }
1485
1486         /* the terrible silence ... */
1487         for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1488                 i->silence(nframes);
1489         }
1490
1491         BufferSet::audio_iterator i = inbufs.audio_begin();
1492
1493         for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end() && i != inbufs.audio_end(); ++pan, ++i) {
1494                 (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
1495         }
1496 }
1497
1498 void
1499 Panner::run (BufferSet& inbufs, BufferSet& outbufs, framepos_t start_frame, framepos_t end_frame, nframes_t nframes)
1500 {
1501         if (outbufs.count().n_audio() == 0) {
1502                 // Failing to deliver audio we were asked to deliver is a bug
1503                 assert(inbufs.count().n_audio() == 0);
1504                 return;
1505         }
1506
1507         // We shouldn't be called in the first place...
1508         assert(!bypassed());
1509         assert(!empty());
1510
1511         // If we shouldn't play automation defer to distribute_no_automation
1512         if (!(automation_state() & Play || ((automation_state() & Touch) && !touching()))) {
1513
1514                 // Speed quietning
1515                 gain_t gain_coeff = 1.0;
1516
1517                 if (fabsf(_session.transport_speed()) > 1.5f && Config->get_quieten_at_speed ()) {
1518                         gain_coeff = speed_quietning;
1519                 }
1520
1521                 distribute_no_automation (inbufs, outbufs, nframes, gain_coeff);
1522                 return;
1523         }
1524
1525         // Otherwise.. let the automation flow, baby
1526
1527         if (outbufs.count().n_audio() == 1) {
1528
1529                 AudioBuffer& dst = outbufs.get_audio(0);
1530
1531                 // FIXME: apply gain automation?
1532
1533                 // copy the first
1534                 dst.read_from(inbufs.get_audio(0), nframes);
1535
1536                 // accumulate starting with the second
1537                 BufferSet::audio_iterator i = inbufs.audio_begin();
1538                 for (++i; i != inbufs.audio_end(); ++i) {
1539                         dst.merge_from(*i, nframes);
1540                 }
1541
1542                 return;
1543         }
1544
1545         // More than 1 output, we should have 1 panner for each input
1546         //assert(_streampanners.size() == inbufs.count().n_audio());
1547
1548         /* the terrible silence ... */
1549         for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
1550                 i->silence(nframes);
1551         }
1552
1553         BufferSet::audio_iterator i = inbufs.audio_begin();
1554         for (vector<StreamPanner*>::iterator pan = _streampanners.begin(); pan != _streampanners.end(); ++pan, ++i) {
1555                 (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
1556         }
1557 }
1558
1559 /* old school automation handling */
1560
1561 /*
1562 void
1563 Panner::set_name (string str)
1564 {
1565         automation_path = Glib::build_filename(_session.automation_dir(),
1566                 _session.snap_name() + "-pan-" + legalize_for_path (str) + ".automation");
1567 }
1568 */
1569
1570 int
1571 Panner::load ()
1572 {
1573         char line[128];
1574         uint32_t linecnt = 0;
1575         float version;
1576         vector<StreamPanner*>::iterator sp;
1577         LocaleGuard lg (X_("POSIX"));
1578
1579         if (automation_path.length() == 0) {
1580                 return 0;
1581         }
1582
1583         if (access (automation_path.c_str(), F_OK)) {
1584                 return 0;
1585         }
1586
1587         ifstream in (automation_path.c_str());
1588
1589         if (!in) {
1590                 error << string_compose (_("cannot open pan automation file %1 (%2)"),
1591                                   automation_path, strerror (errno))
1592                       << endmsg;
1593                 return -1;
1594         }
1595
1596         sp = _streampanners.begin();
1597
1598         while (in.getline (line, sizeof(line), '\n')) {
1599
1600                 if (++linecnt == 1) {
1601                         if (memcmp (line, X_("version"), 7) == 0) {
1602                                 if (sscanf (line, "version %f", &version) != 1) {
1603                                         error << string_compose(_("badly formed version number in pan automation event file \"%1\""), automation_path) << endmsg;
1604                                         return -1;
1605                                 }
1606                         } else {
1607                                 error << string_compose(_("no version information in pan automation event file \"%1\" (first line = %2)"),
1608                                                  automation_path, line) << endmsg;
1609                                 return -1;
1610                         }
1611
1612                         continue;
1613                 }
1614
1615                 if (strlen (line) == 0 || line[0] == '#') {
1616                         continue;
1617                 }
1618
1619                 if (strcmp (line, "begin") == 0) {
1620
1621                         if (sp == _streampanners.end()) {
1622                                 error << string_compose (_("too many panner states found in pan automation file %1"),
1623                                                   automation_path)
1624                                       << endmsg;
1625                                 return -1;
1626                         }
1627
1628                         if ((*sp)->load (in, automation_path, linecnt)) {
1629                                 return -1;
1630                         }
1631
1632                         ++sp;
1633                 }
1634         }
1635
1636         return 0;
1637 }
1638
1639 void
1640 Panner::set_mono (bool yn)
1641 {
1642         if (yn != _mono) {
1643                 _mono = yn;
1644                 StateChanged ();
1645         }
1646
1647         for (vector<StreamPanner*>::iterator i = _streampanners.begin(); i != _streampanners.end(); ++i) {
1648                 (*i)->set_mono (yn);
1649         }
1650 }
1651
1652 string
1653 Panner::value_as_string (double v)
1654 {
1655         if (Panner::equivalent (v, 0.5)) {
1656                 return _("C");
1657         } else if (equivalent (v, 0)) {
1658                 return _("L");
1659         } else if (equivalent (v, 1)) {
1660                 return _("R");
1661         } else if (v < 0.5) {
1662                 stringstream s;
1663                 s << fixed << setprecision (0) << _("L") << ((0.5 - v) * 200) << "%";
1664                 return s.str();
1665         } else {
1666                 stringstream s;
1667                 s << fixed << setprecision (0) << _("R") << ((v -0.5) * 200) << "%";
1668                 return s.str ();
1669         }
1670
1671         return "";
1672 }