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