summaryrefslogtreecommitdiff
path: root/libs/ardour/auditioner.cc
blob: aa81c3aa927383313ddc2abecb46e0b4e7c5a89d (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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
    Copyright (C) 2001 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 <glibmm/threads.h>

#include "pbd/error.h"

#include "ardour/amp.h"
#include "ardour/audio_diskstream.h"
#include "ardour/audio_port.h"
#include "ardour/audioengine.h"
#include "ardour/audioplaylist.h"
#include "ardour/audioregion.h"
#include "ardour/auditioner.h"
#include "ardour/data_type.h"
#include "ardour/delivery.h"
#include "ardour/midi_diskstream.h"
#include "ardour/midi_region.h"
#include "ardour/plugin.h"
#include "ardour/plugin_insert.h"
#include "ardour/profile.h"
#include "ardour/region_factory.h"
#include "ardour/route.h"
#include "ardour/session.h"

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

#include "pbd/i18n.h"

Auditioner::Auditioner (Session& s)
	: Track (s, "auditioner", PresentationInfo::Auditioner)
	, current_frame (0)
	, _auditioning (0)
	, length (0)
	, _seek_frame (-1)
	, _seeking (false)
	, _seek_complete (false)
	, via_monitor (false)
	, _midi_audition (false)
	, _synth_added (false)
	, _synth_changed (false)
	, _queue_panic (false)
	, _import_position (0)
{
}

int
Auditioner::init ()
{
	if (Track::init ()) {
		return -1;
	}

	if (connect ()) {
		return -1;
	}

	_output->add_port ("", this, DataType::MIDI);

	lookup_synth();

	_output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
	Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Auditioner::config_changed, this, _1));

	return 0;
}

Auditioner::~Auditioner ()
{
	if (asynth) {
		asynth->drop_references ();
	}
	asynth.reset ();
}

void
Auditioner::lookup_synth ()
{
	string plugin_id = Config->get_midi_audition_synth_uri();
	asynth.reset ();
	if (!plugin_id.empty()) {
		boost::shared_ptr<Plugin> p;
		p = find_plugin (_session, plugin_id, ARDOUR::LV2);
		if (!p) {
			p = find_plugin (_session, "https://community.ardour.org/node/7596", ARDOUR::LV2);
			if (p) {
				warning << _("Falling back to Reasonable Synth for Midi Audition") << endmsg;
			} else {
				warning << _("No synth for midi-audition found.") << endmsg;
				Config->set_midi_audition_synth_uri(""); // Don't check again for Reasonable Synth (ie --no-lv2)
			}
		}
		if (p) {
			asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
		}
	}
}

void
Auditioner::config_changed (std::string p)
{
	if (p == "midi-audition-synth-uri") {
		_synth_changed = true;
	}
}

int
Auditioner::connect ()
{
	string left = Config->get_auditioner_output_left();
	string right = Config->get_auditioner_output_right();

	vector<string> outputs;
	_session.engine().get_physical_outputs (DataType::AUDIO, outputs);

	via_monitor = false;

	if (left.empty() || left == "default") {
                if (_session.monitor_out()) {
                        left = _session.monitor_out()->input()->audio (0)->name();
                        via_monitor = true;
                } else {
			if (outputs.size() > 0) {
				left = outputs[0];
			}
                }
	}

	if (right.empty() || right == "default") {
                if (_session.monitor_out()) {
                        right = _session.monitor_out()->input()->audio (1)->name();
                        via_monitor = true;
                } else {
			if (outputs.size() > 1) {
				right = outputs[1];
			}
                }
	}

	_output->disconnect (this);

	if (left.empty() && right.empty()) {
		if (_output->n_ports().n_audio() == 0) {
			/* ports not set up, so must be during startup */
			warning << _("no outputs available for auditioner - manual connection required") << endmsg;
		}
	} else {

		if (_output->n_ports().n_audio() == 0) {

			/* create (and connect) new ports */

			_main_outs->defer_pan_reset ();

			if (left.length()) {
				_output->add_port (left, this, DataType::AUDIO);
			}

			if (right.length()) {
				_output->add_port (right, this, DataType::AUDIO);
			}

			_main_outs->allow_pan_reset ();
			_main_outs->reset_panner ();

		} else {

			/* reconnect existing ports */

			boost::shared_ptr<Port> oleft (_output->nth (0));
			boost::shared_ptr<Port> oright (_output->nth (1));
			if (oleft) {
				oleft->connect (left);
			}
			if (oright) {
				oright->connect (right);
			}
		}

	}

	return 0;
}


DataType
Auditioner::data_type () const {
	if (_midi_audition) {
		return DataType::MIDI;
	} else {
		return DataType::AUDIO;
	}
}

boost::shared_ptr<Diskstream>
Auditioner::create_diskstream () {

	{
		AudioDiskstream::Flag dflags = AudioDiskstream::Flag (0);
		dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Hidden);
		_diskstream_audio = boost::shared_ptr<AudioDiskstream> (new AudioDiskstream (_session, name(), dflags));
	}

	{
		MidiDiskstream::Flag dflags = MidiDiskstream::Flag (0);
		dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Hidden);
		_diskstream_midi = boost::shared_ptr<Diskstream> (new MidiDiskstream (_session, name(), dflags));
		_diskstream_midi->do_refill_with_alloc ();
		_diskstream_midi->playlist()->set_orig_track_id (id());
	}

	return _diskstream_audio;
}

int
Auditioner::roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler) {
	if (_midi_audition) {
		return roll_midi(nframes, start_frame, end_frame, declick, need_butler);
	} else {
		return roll_audio(nframes, start_frame, end_frame, declick, need_butler);
	}
}

int
Auditioner::roll_midi (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
{
	Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
	if (!lm.locked()) {
		return 0;
	}

	assert(_active);

	framecnt_t playback_distance = nframes;
	boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
	BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
	MidiBuffer& mbuf (bufs.get_midi (0));
	_silent = false;

	ChanCount cnt (DataType::MIDI, 1);
	cnt.set (DataType::AUDIO, bufs.count().n_audio());
	bufs.set_count (cnt);

	if (_queue_panic) {
		_queue_panic = false;
		for (uint8_t chn = 0; chn < 0xf; ++chn) {
			uint8_t buf[3] = { ((uint8_t) (MIDI_CMD_CONTROL | chn)), ((uint8_t) MIDI_CTL_SUSTAIN), 0 };
			mbuf.push_back(0, 3, buf);
			buf[1] = MIDI_CTL_ALL_NOTES_OFF;
			mbuf.push_back(0, 3, buf);
			buf[1] = MIDI_CTL_RESET_CONTROLLERS;
			mbuf.push_back(0, 3, buf);
		}
		process_output_buffers (bufs, start_frame, start_frame+1, 1, false, false);

		for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
			boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
			if (d) {
				d->flush_buffers (nframes);
			}
		}
	}

	diskstream->get_playback (mbuf, nframes);

	process_output_buffers (bufs, start_frame, end_frame, nframes,
				declick, (!diskstream->record_enabled() && !_session.transport_stopped()));

	for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
		boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
		if (d) {
			d->flush_buffers (nframes);
		}
	}

	need_butler = diskstream->commit (playback_distance);
	return 0;
}


int
Auditioner::roll_audio (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler) {
	Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
	if (!lm.locked()) {
		return 0;
	}

	assert(n_outputs().n_total() > 0);
	assert(_active);

	int dret;
	framecnt_t playback_distance;
	framepos_t transport_frame = _session.transport_frame();
	boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
	BufferSet& bufs = _session.get_route_buffers (n_process_buffers ());

	_silent = false;
	_amp->apply_gain_automation(false);

	if ((dret = diskstream->process (bufs, transport_frame, nframes, playback_distance, (monitoring_state() == MonitoringDisk))) != 0) {
		need_butler = diskstream->commit (playback_distance);
		silence (nframes);
		return dret;
	}

	process_output_buffers (bufs, start_frame, end_frame, nframes, declick, (!diskstream->record_enabled() && _session.transport_rolling()));
	need_butler = diskstream->commit (playback_distance);
	return 0;
}

void
Auditioner::set_diskstream (boost::shared_ptr<Diskstream> ds)
{
	Track::set_diskstream (ds);

	_diskstream->set_track (this);
	if (Profile->get_trx()) {
		_diskstream->set_destructive (false);
	} else {
		_diskstream->set_destructive (_mode == Destructive);
	}
	_diskstream->set_non_layered (_mode == NonLayered);
	_diskstream->set_record_enabled (false);
	_diskstream->request_input_monitoring (false);

	DiskstreamChanged (); /* EMIT SIGNAL */
}

AudioPlaylist&
Auditioner::prepare_playlist ()
{
	// used by CrossfadeEditor::audition()

	_midi_audition = false;
	set_diskstream(_diskstream_audio);
	if (_synth_added) {
		remove_processor(asynth);
		_synth_added = false;
	}

	// FIXME auditioner is still audio-only
	boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(_diskstream->playlist());
	assert(apl);

	apl->clear ();
	return *apl;
}

void
Auditioner::audition_region (boost::shared_ptr<Region> region)
{
	if (g_atomic_int_get (&_auditioning)) {
		/* don't go via session for this, because we are going
		   to remain active.
		*/
		cancel_audition ();
	}

	Glib::Threads::Mutex::Lock lm (lock);

	if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {

		_midi_audition = false;
		set_diskstream(_diskstream_audio);
		if (_synth_added) {
			remove_processor(asynth);
			_synth_added = false;
		}
		midi_region.reset();
		_import_position = 0;

		/* copy it */
		the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
		the_region->set_position (0);

		_diskstream->playlist()->drop_regions ();
		_diskstream->playlist()->add_region (the_region, 0, 1);

		if (_diskstream->n_channels().n_audio() < the_region->n_channels()) {
			audio_diskstream()->add_channel (the_region->n_channels() - _diskstream->n_channels().n_audio());
		} else if (_diskstream->n_channels().n_audio() > the_region->n_channels()) {
			audio_diskstream()->remove_channel (_diskstream->n_channels().n_audio() - the_region->n_channels());
		}

		ProcessorStreams ps;
		{
			Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());

			if (configure_processors (&ps)) {
				error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
							 _diskstream->n_channels()) << endmsg;
				return;
			}
		}

	} else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
		_midi_audition = true;
		set_diskstream(_diskstream_midi);
		the_region.reset();
		_import_position = region->position();

		/* copy it */
		midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
		midi_region->set_position (_import_position);

		_diskstream->playlist()->drop_regions ();
		_diskstream->playlist()->add_region (midi_region, _import_position, 1);
		midi_diskstream()->reset_tracker();

		ProcessorStreams ps;

		if (_synth_changed && _synth_added) {
			remove_processor(asynth);
			_synth_added = false;
		}
		if (_synth_changed && !_synth_added) {
			_synth_added = false;
			lookup_synth();
		}


		if (!_synth_added && asynth) {
			int rv = add_processor (asynth, PreFader, &ps, true);
			if (rv) {
				error << _("Failed to load synth for MIDI-Audition.") << endmsg;
			} else {
				_synth_added = true;
			}
		} else {
			_queue_panic = true;
		}

		{
			Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());

			if (configure_processors (&ps)) {
				error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
							 _diskstream->n_channels()) << endmsg;
				return;
			}
		}

	} else {
		error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
		return;
	}

	/* force a panner reset now that we have all channels */
	_main_outs->reset_panner();

	_seek_frame = -1;
	_seeking = false;

	int dir;
	framecnt_t offset;

	if (_midi_audition) {
		length = midi_region->length();
		offset = _import_position + midi_region->sync_offset (dir);
	} else {
		length = the_region->length();
		offset = the_region->sync_offset (dir);
	}

	/* can't audition from a negative sync point */

	if (dir < 0) {
		offset = 0;
	}

	_diskstream->seek (offset, true);
	current_frame = offset;

	g_atomic_int_set (&_auditioning, 1);
}

int
Auditioner::play_audition (framecnt_t nframes)
{
	bool need_butler = false;
	framecnt_t this_nframes;
	int ret;

	if (g_atomic_int_get (&_auditioning) == 0) {
		silence (nframes);
		return 0;
	}

#if 0 // TODO
	if (_seeking && _seek_complete) {
		// set FADE-IN
	} else if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
		// set FADE-OUT -- use/override amp? || use region-gain ?
	}
#endif

	if (_seeking && _seek_complete) {
		_seek_complete = false;
		_seeking = false;
		_seek_frame = -1;
		if (_midi_audition && midi_diskstream()) {
			midi_diskstream()->reset_tracker();
		}
	}

	if(!_seeking) {
		/* process audio */
		this_nframes = min (nframes, length - current_frame + _import_position);

		if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, need_butler)) != 0) {
			silence (nframes);
			return ret;
		}

		current_frame += this_nframes;

	} else {
		silence (nframes);
	}

	if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
		_queue_panic = true;
		_seek_complete = false;
		_seeking = true;
		need_butler = true;
	}

	if (!_seeking) {
		AuditionProgress(current_frame - _import_position, length); /* emit */
	}

	if (current_frame >= length + _import_position) {
		_session.cancel_audition ();
		return 0;
	} else {
		return need_butler ? 1 : 0;
	}
}

void
Auditioner::output_changed (IOChange change, void* /*src*/)
{
	if (change.type & IOChange::ConnectionsChanged) {
		string phys;
		vector<string> connections;
		vector<string> outputs;
		_session.engine().get_physical_outputs (DataType::AUDIO, outputs);
		if (_output->nth (0)->get_connections (connections)) {
			if (outputs.size() > 0) {
				phys = outputs[0];
			}
			if (phys != connections[0]) {
				Config->set_auditioner_output_left (connections[0]);
			} else {
				Config->set_auditioner_output_left ("default");
			}
		} else {
			Config->set_auditioner_output_left ("");
		}

		connections.clear ();

		if (_output->nth (1)->get_connections (connections)) {
			if (outputs.size() > 1) {
				phys = outputs[1];
			}
			if (phys != connections[0]) {
				Config->set_auditioner_output_right (connections[0]);
			} else {
				Config->set_auditioner_output_right ("default");
			}
		} else {
			Config->set_auditioner_output_right ("");
		}
	}
}

ChanCount
Auditioner::input_streams () const
{
	/* auditioner never has any inputs - its channel configuration
		 depends solely on the region we are auditioning.
		 */

	if (!_midi_audition && audio_diskstream()) {
		return audio_diskstream()->n_channels();
	}
	if (_midi_audition && midi_diskstream()) {
		ChanCount cnt (DataType::MIDI, 1);
		return cnt;
	}

	return ChanCount ();
}

MonitorState
Auditioner::monitoring_state () const
{
	return MonitoringDisk;
}

boost::shared_ptr<AudioDiskstream>
Auditioner::audio_diskstream() const
{
	return boost::dynamic_pointer_cast<AudioDiskstream> (_diskstream);
}

boost::shared_ptr<MidiDiskstream>
Auditioner::midi_diskstream() const
{
	return boost::dynamic_pointer_cast<MidiDiskstream> (_diskstream);
}