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