summaryrefslogtreecommitdiff
path: root/libs/ardour/panner_shell.cc
blob: 6c3ae0021cf1c80da7b118e445109ecb01c9a315 (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
488
489
490
/*
    Copyright (C) 2004-2011 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 <inttypes.h>

#include <cmath>
#include <cerrno>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <locale.h>
#include <unistd.h>
#include <float.h>
#include <iomanip>

#include <glibmm.h>

#include "pbd/cartesian.h"
#include "pbd/convert.h"
#include "pbd/error.h"
#include "pbd/failed_constructor.h"
#include "pbd/xml++.h"
#include "pbd/enumwriter.h"

#include "evoral/Curve.hpp"

#include "ardour/audio_buffer.h"
#include "ardour/audioengine.h"
#include "ardour/boost_debug.h"
#include "ardour/buffer_set.h"
#include "ardour/debug.h"
#include "ardour/pannable.h"
#include "ardour/panner.h"
#include "ardour/panner_manager.h"
#include "ardour/panner_shell.h"
#include "ardour/profile.h"
#include "ardour/session.h"
#include "ardour/speakers.h"

#include "pbd/i18n.h"

#include "pbd/mathfix.h"

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

PannerShell::PannerShell (string name, Session& s, boost::shared_ptr<Pannable> p, bool is_send)
	: SessionObject (s, name)
	, _pannable_route (p)
	, _is_send (is_send)
	, _panlinked (true)
	, _bypassed (false)
	, _current_panner_uri("")
	, _user_selected_panner_uri("")
	, _panner_gui_uri("")
	, _force_reselect (false)
{
	if (is_send) {
		_pannable_internal.reset(new Pannable (s));
		if (Config->get_link_send_and_route_panner() && !ARDOUR::Profile->get_mixbus()) {
			_panlinked = true;
		} else {
			_panlinked = false;
		}
	}
	set_name (name);
}

PannerShell::~PannerShell ()
{
	DEBUG_TRACE(DEBUG::Destruction, string_compose ("panner shell %3 for %1 destructor, panner is %4, pannable is %2\n", _name, _pannable_route, this, _panner));
}

void
PannerShell::configure_io (ChanCount in, ChanCount out)
{
	uint32_t nouts = out.n_audio();
	uint32_t nins = in.n_audio();

	/* if new and old config don't need panning, or if
	   the config hasn't changed, we're done.
	*/

	if (!_force_reselect && _panner && (_panner->in().n_audio() == nins) && (_panner->out().n_audio() == nouts)) {
		return;
	}
	_force_reselect = false;

	if (nouts < 2 || nins == 0) {
		/* no need for panning with less than 2 outputs or no inputs */
		if (_panner) {
			_panner.reset ();
			_current_panner_uri = "";
			_panner_gui_uri = "";
			if (!_is_send || !_panlinked) {
				pannable()->set_panner(_panner);
			}
			Changed (); /* EMIT SIGNAL */
		}
		return;
	}

	PannerInfo* pi = PannerManager::instance().select_panner (in, out, _user_selected_panner_uri);
	if (!pi) {
		fatal << _("No panner found: check that panners are being discovered correctly during startup.") << endmsg;
		abort(); /*NOTREACHED*/
	}

	DEBUG_TRACE (DEBUG::Panning, string_compose (_("select panner: %1\n"), pi->descriptor.name.c_str()));

	boost::shared_ptr<Speakers> speakers = _session.get_speakers ();

	if (nouts != speakers->size()) {
		/* hmm, output count doesn't match session speaker count so
		   create a new speaker set.
		*/
		Speakers* s = new Speakers ();
		s->setup_default_speakers (nouts);
		speakers.reset (s);
	}

	/* TODO  don't allow to link  _is_send if internal & route panners are different types */
	Panner* p = pi->descriptor.factory (pannable(), speakers);
	// boost_debug_shared_ptr_mark_interesting (p, "Panner");
	_panner.reset (p);
	_panner->configure_io (in, out);
	_current_panner_uri = pi->descriptor.panner_uri;
	_panner_gui_uri = pi->descriptor.gui_uri;

	if (!_is_send || !_panlinked) {
		pannable()->set_panner(_panner);
	}
	Changed (); /* EMIT SIGNAL */
}

XMLNode&
PannerShell::get_state ()
{
	XMLNode* node = new XMLNode ("PannerShell");

	node->add_property (X_("bypassed"), _bypassed ? X_("yes") : X_("no"));
	node->add_property (X_("user-panner"), _user_selected_panner_uri);
	node->add_property (X_("linked-to-route"), _panlinked ? X_("yes") : X_("no"));

	if (_panner && _is_send) {
		node->add_child_nocopy (_panner->get_state ());
	}

	return *node;
}

int
PannerShell::set_state (const XMLNode& node, int version)
{
	XMLNodeList nlist = node.children ();
	XMLNodeConstIterator niter;
	XMLProperty const * prop;
	LocaleGuard lg;

	if ((prop = node.property (X_("bypassed"))) != 0) {
		set_bypassed (string_is_affirmative (prop->value ()));
	}

	if ((prop = node.property (X_("linked-to-route"))) != 0) {
		if (!ARDOUR::Profile->get_mixbus()) {
			_panlinked = string_is_affirmative (prop->value ());
		}
	}

	if ((prop = node.property (X_("user-panner"))) != 0) {
		_user_selected_panner_uri = prop->value ();
	}

	_panner.reset ();

	for (niter = nlist.begin(); niter != nlist.end(); ++niter) {

		if ((*niter)->name() == X_("Panner")) {

			if ((prop = (*niter)->property (X_("uri")))) {
				PannerInfo* p = PannerManager::instance().get_by_uri(prop->value());
				if (p) {
					_panner.reset (p->descriptor.factory (
								_is_send ? _pannable_internal : _pannable_route, _session.get_speakers ()));
					_current_panner_uri = p->descriptor.panner_uri;
					_panner_gui_uri = p->descriptor.gui_uri;
					if (_is_send) {
						if (!_panlinked) {
							_pannable_internal->set_panner(_panner);
						} else {
							_force_reselect = true;
						}
					} else {
						_pannable_route->set_panner(_panner);
					}
					if (_panner->set_state (**niter, version) == 0) {
						return -1;
					}
				}
			}

			else /* backwards compatibility */
			if ((prop = (*niter)->property (X_("type")))) {

				list<PannerInfo*>::iterator p;
				PannerManager& pm (PannerManager::instance());

				for (p = pm.panner_info.begin(); p != pm.panner_info.end(); ++p) {
					if (prop->value() == (*p)->descriptor.name) {

						/* note that we assume that all the stream panners
						   are of the same type. pretty good
						   assumption, but it's still an assumption.
						*/

						_panner.reset ((*p)->descriptor.factory (
									_is_send ? _pannable_internal : _pannable_route, _session.get_speakers ()));
						_current_panner_uri = (*p)->descriptor.panner_uri;
						_panner_gui_uri = (*p)->descriptor.gui_uri;

						if (_is_send) {
							if (!_panlinked) {
								_pannable_internal->set_panner(_panner);
							} else {
								_force_reselect = true;
							}
						} else {
							_pannable_route->set_panner(_panner);
						}

						if (_panner->set_state (**niter, version) == 0) {
							return -1;
						}

						break;
					}
				}

				if (p == pm.panner_info.end()) {
					error << string_compose (_("Unknown panner plugin \"%1\" found in pan state - ignored"),
					                         prop->value())
					      << endmsg;
				}

			} else {
				error << _("panner plugin node has no type information!")
				      << endmsg;
				return -1;
			}
		}
	}

	return 0;
}


void
PannerShell::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, pframes_t nframes, gain_t gain_coeff)
{
	if (outbufs.count().n_audio() == 0) {
		// Don't want to lose audio...
		assert(inbufs.count().n_audio() == 0);
		return;
	}

	if (outbufs.count().n_audio() == 1) {

		/* just one output: no real panning going on */

		AudioBuffer& dst = outbufs.get_audio(0);

		if (gain_coeff == GAIN_COEFF_ZERO) {

			/* gain was zero, so make it silent */

			dst.silence (nframes);

		} else if (gain_coeff == GAIN_COEFF_UNITY){

			/* mix all input buffers into the output */

			// copy the first
			dst.read_from(inbufs.get_audio(0), nframes);

			// accumulate starting with the second
			if (inbufs.count().n_audio() > 0) {
				BufferSet::audio_iterator i = inbufs.audio_begin();
				for (++i; i != inbufs.audio_end(); ++i) {
					dst.merge_from(*i, nframes);
				}
			}

		} else {

			/* mix all buffers into the output, scaling them all by the gain */

			// copy the first
			dst.read_from(inbufs.get_audio(0), nframes);

			// accumulate (with gain) starting with the second
			if (inbufs.count().n_audio() > 0) {
				BufferSet::audio_iterator i = inbufs.audio_begin();
				for (++i; i != inbufs.audio_end(); ++i) {
					dst.accumulate_with_gain_from(*i, nframes, gain_coeff);
				}
			}

		}

		return;
	}

	/* multiple outputs ... we must have a panner */

	assert (_panner);

	/* setup silent buffers so that we can mix into the outbuffers (slightly suboptimal -
	   better to copy the first set of data then mix after that, but hey, its 2011)
	*/

	for (BufferSet::audio_iterator b = outbufs.audio_begin(); b != outbufs.audio_end(); ++b) {
		(*b).silence (nframes);
	}

	_panner->distribute (inbufs, outbufs, gain_coeff, nframes);
}

void
PannerShell::run (BufferSet& inbufs, BufferSet& outbufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
{
	if (inbufs.count().n_audio() == 0) {
		/* Input has no audio buffers (e.g. Aux Send in a MIDI track at a
		   point with no audio because there is no preceding instrument)
		*/
		outbufs.silence(nframes, 0);
		return;
	}

	if (outbufs.count().n_audio() == 0) {
		// Failing to deliver audio we were asked to deliver is a bug
		assert(inbufs.count().n_audio() == 0);
		return;
	}

	if (outbufs.count().n_audio() == 1) {

		/* one output only: no panner */

		AudioBuffer& dst = outbufs.get_audio(0);

		// FIXME: apply gain automation?

		// copy the first
		dst.read_from (inbufs.get_audio(0), nframes);

		// accumulate starting with the second
		BufferSet::audio_iterator i = inbufs.audio_begin();
		for (++i; i != inbufs.audio_end(); ++i) {
			dst.merge_from (*i, nframes);
		}

		return;
	}

	// More than 1 output

	AutoState as = _panner->automation_state ();

	// If we shouldn't play automation defer to distribute_no_automation

	if (!(as & Play || ((as & Touch) && !_panner->touching()))) {

		distribute_no_automation (inbufs, outbufs, nframes, 1.0);

	} else {

		/* setup the terrible silence so that we can mix into the outbuffers (slightly suboptimal -
		   better to copy the first set of data then mix after that, but hey, its 2011)
		*/
		for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
			i->silence(nframes);
		}

		_panner->distribute_automated (inbufs, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
	}
}

void
PannerShell::set_bypassed (bool yn)
{
	if (yn == _bypassed) {
		return;
	}

	_bypassed = yn;
	_session.set_dirty ();
	Changed (); /* EMIT SIGNAL */
}

bool
PannerShell::bypassed () const
{
	return _bypassed;
}

/* set custom-panner config
 *
 * This function is intended to be only called from
 * Route::set_custom_panner()
 * which will trigger IO-reconfigutaion if this fn return true
 */
bool
PannerShell::set_user_selected_panner_uri (std::string const uri)
{
	if (uri == _user_selected_panner_uri) return false;
	_user_selected_panner_uri = uri;
	if (uri == _current_panner_uri) return false;
	_force_reselect = true;
	return true;
}

bool
PannerShell::select_panner_by_uri (std::string const uri)
{
	if (uri == _user_selected_panner_uri) return false;
	_user_selected_panner_uri = uri;
	if (uri == _current_panner_uri) return false;
	_force_reselect = true;
	if (_panner) {
		Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
			ChanCount in = _panner->in();
			ChanCount out = _panner->out();
			configure_io(in, out);
			if (!_is_send || !_panlinked) {
				pannable()->set_panner(_panner);
			}
			_session.set_dirty ();
	}
	return true;
}

void
PannerShell::set_linked_to_route (bool onoff)
{
	assert(_is_send);
	if (onoff == _panlinked) {
		return;
	}

	/* set _pannable-_has_state = true
	 * this way the panners will pick it up
	 * when it is re-created
	 */
	if (pannable()) {
		XMLNode state = pannable()->get_state();
		pannable()->set_state(state, 3000);
	}

	_panlinked = onoff;

	_force_reselect = true;
	if (_panner) {
		Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
			ChanCount in = _panner->in();
			ChanCount out = _panner->out();
			configure_io(in, out);
			if (!_panlinked) {
				pannable()->set_panner(_panner);
			}
			_session.set_dirty ();
	}
	PannableChanged();
}