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