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