summaryrefslogtreecommitdiff
path: root/libs/panners/vbap/vbap.cc
blob: 8c1390832fab441ab01ddb1f577f9e49475a4752 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
    Copyright (C) 2012 Paul Davis 

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <cstring>

#include <iostream>
#include <string>

#include "pbd/cartesian.h"
#include "pbd/compose.h"

#include "ardour/amp.h"
#include "ardour/audio_buffer.h"
#include "ardour/buffer_set.h"
#include "ardour/pan_controllable.h"
#include "ardour/pannable.h"
#include "ardour/speakers.h"

#include "vbap.h"
#include "vbap_speakers.h"

#include "i18n.h"

using namespace PBD;
using namespace ARDOUR;
using namespace std;

static PanPluginDescriptor _descriptor = {
        "VBAP 2D panner",
        "http://ardour.org/plugin/panner_vbap",
        "http://ardour.org/plugin/panner_vbap#ui",
        -1, -1,
        1000,
        VBAPanner::factory
};

extern "C" { PanPluginDescriptor* panner_descriptor () { return &_descriptor; } }

VBAPanner::Signal::Signal (Session&, VBAPanner&, uint32_t, uint32_t n_speakers)
{
        resize_gains (n_speakers);

        desired_gains[0] = desired_gains[1] = desired_gains[2] = 0;
        outputs[0] = outputs[1] = outputs[2] = -1;
        desired_outputs[0] = desired_outputs[1] = desired_outputs[2] = -1;
}

void
VBAPanner::Signal::Signal::resize_gains (uint32_t n)
{
        gains.assign (n, 0.0);
}        

VBAPanner::VBAPanner (boost::shared_ptr<Pannable> p, boost::shared_ptr<Speakers> s)
	: Panner (p)
	, _speakers (new VBAPSpeakers (s))
{
        _pannable->pan_azimuth_control->Changed.connect_same_thread (*this, boost::bind (&VBAPanner::update, this));
        _pannable->pan_elevation_control->Changed.connect_same_thread (*this, boost::bind (&VBAPanner::update, this));
        _pannable->pan_width_control->Changed.connect_same_thread (*this, boost::bind (&VBAPanner::update, this));
        if (!_pannable->has_state()) {
                reset();
        }

        update ();
}

VBAPanner::~VBAPanner ()
{
        clear_signals ();
}

void
VBAPanner::clear_signals ()
{
        for (vector<Signal*>::iterator i = _signals.begin(); i != _signals.end(); ++i) {
                delete *i;
        }
        _signals.clear ();
}

void
VBAPanner::configure_io (ChanCount in, ChanCount /* ignored - we use Speakers */)
{
        uint32_t n = in.n_audio();

        clear_signals ();

        for (uint32_t i = 0; i < n; ++i) {
                Signal* s = new Signal (_pannable->session(), *this, i, _speakers->n_speakers());
                _signals.push_back (s);
                
        }

        update ();
}

void
VBAPanner::update ()
{
        /* recompute signal directions based on panner azimuth and, if relevant, width (diffusion) and elevation parameters */
        double elevation = _pannable->pan_elevation_control->get_value() * 90.0;

        if (_signals.size() > 1) {
                double w = - (_pannable->pan_width_control->get_value());
                double signal_direction = 1.0 - (_pannable->pan_azimuth_control->get_value() + (w/2));
                double grd_step_per_signal = w / (_signals.size() - 1);
                for (vector<Signal*>::iterator s = _signals.begin(); s != _signals.end(); ++s) {
                
                        Signal* signal = *s;

                        int over = signal_direction;
                        over -= (signal_direction >= 0) ? 0 : 1;
                        signal_direction -= (double)over;

                        signal->direction = AngularVector (signal_direction * 360.0, elevation);
                        compute_gains (signal->desired_gains, signal->desired_outputs, signal->direction.azi, signal->direction.ele);
                        signal_direction += grd_step_per_signal;
                }
        } else if (_signals.size() == 1) {
                double center = (1.0 - _pannable->pan_azimuth_control->get_value()) * 360.0;

                /* width has no role to play if there is only 1 signal: VBAP does not do "diffusion" of a single channel */

                Signal* s = _signals.front();
                s->direction = AngularVector (center, elevation);
                compute_gains (s->desired_gains, s->desired_outputs, s->direction.azi, s->direction.ele);
        }

        SignalPositionChanged(); /* emit */
}

void 
VBAPanner::compute_gains (double gains[3], int speaker_ids[3], int azi, int ele) 
{
	/* calculates gain factors using loudspeaker setup and given direction */
	double cartdir[3];
	double power;
	int i,j,k;
	double small_g;
	double big_sm_g, gtmp[3];

	spherical_to_cartesian (azi, ele, 1.0, cartdir[0], cartdir[1], cartdir[2]);  
	big_sm_g = -100000.0;

	gains[0] = gains[1] = gains[2] = 0;
	speaker_ids[0] = speaker_ids[1] = speaker_ids[2] = 0;

	for (i = 0; i < _speakers->n_tuples(); i++) {

		small_g = 10000000.0;

		for (j = 0; j < _speakers->dimension(); j++) {

			gtmp[j] = 0.0;

			for (k = 0; k < _speakers->dimension(); k++) {
				gtmp[j] += cartdir[k] * _speakers->matrix(i)[j*_speakers->dimension()+k]; 
			}

			if (gtmp[j] < small_g) {
				small_g = gtmp[j];
			}
		}

		if (small_g > big_sm_g) {

			big_sm_g = small_g;

			gains[0] = gtmp[0]; 
			gains[1] = gtmp[1]; 

			speaker_ids[0] = _speakers->speaker_for_tuple (i, 0);
			speaker_ids[1] = _speakers->speaker_for_tuple (i, 1);

			if (_speakers->dimension() == 3) {
				gains[2] = gtmp[2];
				speaker_ids[2] = _speakers->speaker_for_tuple (i, 2);
			} else {
				gains[2] = 0.0;
				speaker_ids[2] = -1;
			}
		}
	}
        
	power = sqrt (gains[0]*gains[0] + gains[1]*gains[1] + gains[2]*gains[2]);

	if (power > 0) {
		gains[0] /= power; 
		gains[1] /= power;
		gains[2] /= power;
	}
}

void
VBAPanner::distribute (BufferSet& inbufs, BufferSet& obufs, gain_t gain_coefficient, pframes_t nframes)
{
        uint32_t n;
        vector<Signal*>::iterator s;

        assert (inbufs.count().n_audio() == _signals.size());

        for (s = _signals.begin(), n = 0; s != _signals.end(); ++s, ++n) {

                Signal* signal (*s);

                distribute_one (inbufs.get_audio (n), obufs, gain_coefficient, nframes, n);

                memcpy (signal->outputs, signal->desired_outputs, sizeof (signal->outputs));
        }
}

void
VBAPanner::distribute_one (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coefficient, pframes_t nframes, uint32_t which)
{
	Sample* const src = srcbuf.data();
        Signal* signal (_signals[which]);

	/* VBAP may distribute the signal across up to 3 speakers depending on
	   the configuration of the speakers.

           But the set of speakers in use "this time" may be different from
           the set of speakers "the last time". So we have up to 6 speakers
           involved, and we have to interpolate so that those no longer
           in use are rapidly faded to silence and those newly in use
           are rapidly faded to their correct level. This prevents clicks
           as we change the set of speakers used to put the signal in
           a given position.

           However, the speakers are represented by output buffers, and other
           speakers may write to the same buffers, so we cannot use
           anything here that will simply assign new (sample) values
           to the output buffers - everything must be done via mixing
           functions and not assignment/copying.
	*/

        vector<double>::size_type sz = signal->gains.size();

        assert (sz == obufs.count().n_audio());

        int8_t outputs[sz]; // on the stack, no malloc
        
        /* set initial state of each output "record"
         */

        for (uint32_t o = 0; o < sz; ++o) {
                outputs[o] = 0;
        }

        /* for all outputs used this time and last time,
           change the output record to show what has
           happened.
        */


        for (int o = 0; o < 3; ++o) {
                if (signal->outputs[o] != -1) {
                        /* used last time */
                        outputs[signal->outputs[o]] |= 1;
                } 

                if (signal->desired_outputs[o] != -1) {
                        /* used this time */
                        outputs[signal->desired_outputs[o]] |= 1<<1;
                } 
        }

        /* at this point, we can test a speaker's status:

           (outputs[o] & 1)      <= in use before
           (outputs[o] & 2)      <= in use this time
           (outputs[o] & 3) == 3 <= in use both times
            outputs[o] == 0      <= not in use either time
           
        */

	for (int o = 0; o < 3; ++o) {
                pan_t pan;
                int output = signal->desired_outputs[o];

		if (output == -1) {
                        continue;
                }

                pan = gain_coefficient * signal->desired_gains[o];

                if (pan == 0.0 && signal->gains[output] == 0.0) {
                        
                        /* nothing deing delivered to this output */

                        signal->gains[output] = 0.0;
                        
                } else if (fabs (pan - signal->gains[output]) > 0.00001) {
                        
                        /* signal to this output but the gain coefficient has changed, so 
                           interpolate between them.
                        */

                        AudioBuffer& buf (obufs.get_audio (output));
                        buf.accumulate_with_ramped_gain_from (srcbuf.data(), nframes, signal->gains[output], pan, 0);
                        signal->gains[output] = pan;

                } else {
                        
                        /* signal to this output, same gain as before so just copy with gain
                         */
                           
                        mix_buffers_with_gain (obufs.get_audio (output).data(),src,nframes,pan);
                        signal->gains[output] = pan;
                }
	}

        /* clean up the outputs that were used last time but not this time
         */

        for (uint32_t o = 0; o < sz; ++o) {
                if (outputs[o] == 1) {
                        /* take signal and deliver with a rapid fade out
                         */
                        AudioBuffer& buf (obufs.get_audio (o));
                        buf.accumulate_with_ramped_gain_from (srcbuf.data(), nframes, signal->gains[o], 0.0, 0);
                        signal->gains[o] = 0.0;
                }
        }

        /* note that the output buffers were all silenced at some point
           so anything we didn't write to with this signal (or any others)
           is just as it should be.
        */
}

void 
VBAPanner::distribute_one_automated (AudioBuffer& /*src*/, BufferSet& /*obufs*/,
                                     framepos_t /*start*/, framepos_t /*end*/, 
				     pframes_t /*nframes*/, pan_t** /*buffers*/, uint32_t /*which*/)
{
	/* XXX to be implemented */
}

XMLNode&
VBAPanner::get_state ()
{
	XMLNode& node (Panner::get_state());
	node.add_property (X_("uri"), _descriptor.panner_uri);
	/* this is needed to allow new sessions to load with old Ardour: */
	node.add_property (X_("type"), _descriptor.name);
	return node;
}

Panner*
VBAPanner::factory (boost::shared_ptr<Pannable> p, boost::shared_ptr<Speakers> s)
{
	return new VBAPanner (p, s);
}

ChanCount
VBAPanner::in() const
{
        return ChanCount (DataType::AUDIO, _signals.size());
}

ChanCount
VBAPanner::out() const
{
        return ChanCount (DataType::AUDIO, _speakers->n_speakers());
}

std::set<Evoral::Parameter> 
VBAPanner::what_can_be_automated() const
{
        set<Evoral::Parameter> s;
        s.insert (Evoral::Parameter (PanAzimuthAutomation));
        if (_signals.size() > 1) {
                s.insert (Evoral::Parameter (PanWidthAutomation));
        }
        if (_speakers->dimension() == 3) {
                s.insert (Evoral::Parameter (PanElevationAutomation));
        }
        return s;
}
        
string
VBAPanner::describe_parameter (Evoral::Parameter p)
{
        switch (p.type()) {
        case PanAzimuthAutomation:
                return _("Direction");
        case PanWidthAutomation:
                return _("Width");
        case PanElevationAutomation:
                return _("Elevation");
        default:
                return _pannable->describe_parameter (p);
        }
}

string 
VBAPanner::value_as_string (boost::shared_ptr<AutomationControl> ac) const
{
        /* DO NOT USE LocaleGuard HERE */
        double val = ac->get_value();

        switch (ac->parameter().type()) {
        case PanAzimuthAutomation: /* direction */
                return string_compose (_("%1\u00B0"), (int (rint (val * 360.0))+180)%360);
                
        case PanWidthAutomation: /* diffusion */
                return string_compose (_("%1%%"), (int) floor (100.0 * fabs(val)));

        case PanElevationAutomation: /* elevation */
                return string_compose (_("%1\u00B0"), (int) floor (90.0 * fabs(val)));
                
        default:
                return _("unused");
        }
}

AngularVector
VBAPanner::signal_position (uint32_t n) const
{
        if (n < _signals.size()) {
                return _signals[n]->direction;
        }

        return AngularVector();
}

boost::shared_ptr<Speakers>
VBAPanner::get_speakers () const 
{
        return _speakers->parent();
}

void
VBAPanner::set_position (double p)
{
	/* map into 0..1 range */
	int over = p;
	over -= (p >= 0) ? 0 : 1;
	p -= (double)over;
	_pannable->pan_azimuth_control->set_value (p);
}

void
VBAPanner::set_width (double w)
{
        _pannable->pan_width_control->set_value (min (1.0, max (-1.0, w)));
}

void
VBAPanner::set_elevation (double e)
{
        _pannable->pan_elevation_control->set_value (min (1.0, max (0.0, e)));
}

void
VBAPanner::reset ()
{
	set_position (.5);
        if (_signals.size() > 1) {
                set_width (1.0 - (1.0 / (double)_signals.size()));
        } else {
                set_width (1.0);
        }
	set_elevation (0);

	update ();
}