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