1 /*
2 * Copyright (C) 2001-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
4 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2014-2018 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2019 Ben Loftis <ben@harrisonconsoles.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include <glibmm/threads.h>
24
25 #include "pbd/error.h"
26
27 #include "ardour/amp.h"
28 #include "ardour/audio_port.h"
29 #include "ardour/audioengine.h"
30 #include "ardour/audioplaylist.h"
31 #include "ardour/audioregion.h"
32 #include "ardour/auditioner.h"
33 #include "ardour/data_type.h"
34 #include "ardour/delivery.h"
35 #include "ardour/disk_reader.h"
36 #include "ardour/midi_playlist.h"
37 #include "ardour/midi_region.h"
38 #include "ardour/plugin_insert.h"
39 #include "ardour/plugin_manager.h"
40 #include "ardour/profile.h"
41 #include "ardour/region_factory.h"
42 #include "ardour/route.h"
43 #include "ardour/session.h"
44
45 using namespace std;
46 using namespace ARDOUR;
47 using namespace PBD;
48
49 #include "pbd/i18n.h"
50
Auditioner(Session & s)51 Auditioner::Auditioner (Session& s)
52 : Track (s, "auditioner", PresentationInfo::Auditioner)
53 , current_sample (0)
54 , length (0)
55 , _seek_sample (-1)
56 , _seeking (false)
57 , _seek_complete (false)
58 , via_monitor (false)
59 , _midi_audition (false)
60 , _queue_panic (false)
61 , _import_position (0)
62 {
63 g_atomic_int_set (&_auditioning, 0);
64 }
65
66 int
init()67 Auditioner::init ()
68 {
69 if (Track::init ()) {
70 return -1;
71 }
72
73 if (connect ()) {
74 return -1;
75 }
76
77 use_new_playlist (DataType::MIDI);
78
79 if (!audition_synth_info) {
80 lookup_fallback_synth ();
81 }
82
83 _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
84
85 return 0;
86 }
87
~Auditioner()88 Auditioner::~Auditioner ()
89 {
90 unload_synth(true);
91 }
92
93 PluginInfoPtr
lookup_fallback_synth_plugin_info(std::string const & uri) const94 Auditioner::lookup_fallback_synth_plugin_info (std::string const& uri) const
95 {
96 PluginManager& mgr (PluginManager::instance());
97 PluginInfoList plugs;
98 plugs = mgr.lv2_plugin_info();
99 for (PluginInfoList::const_iterator i = plugs.begin (); i != plugs.end (); ++i) {
100 if (uri == (*i)->unique_id){
101 return (*i);
102 }
103 }
104 return PluginInfoPtr ();
105 }
106
107 void
lookup_fallback_synth()108 Auditioner::lookup_fallback_synth ()
109 {
110
111 PluginInfoPtr nfo = lookup_fallback_synth_plugin_info ("http://gareus.org/oss/lv2/gmsynth");
112
113 //GMsynth not found: fallback to Reasonable Synth
114 if (!nfo) {
115 nfo = lookup_fallback_synth_plugin_info ("https://community.ardour.org/node/7596");
116 if (nfo) {
117 warning << _("Falling back to Reasonable Synth for Midi Audition") << endmsg;
118 }
119 }
120
121 if (!nfo) {
122 warning << _("No synth for midi-audition found.") << endmsg;
123 return;
124 }
125
126 set_audition_synth_info(nfo);
127 }
128
129 void
load_synth(bool need_lock)130 Auditioner::load_synth (bool need_lock)
131 {
132 unload_synth(need_lock);
133
134 if (!audition_synth_info) {
135 lookup_fallback_synth ();
136 }
137
138 if (!audition_synth_info) {
139 return;
140 }
141
142 boost::shared_ptr<Plugin> p = audition_synth_info->load (_session);
143 if (p) {
144 asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
145 }
146 }
147
148 void
unload_synth(bool need_lock)149 Auditioner::unload_synth (bool need_lock)
150 {
151 if (asynth) {
152 asynth->drop_references ();
153 remove_processor (asynth, NULL, need_lock);
154 }
155 asynth.reset ();
156 }
157
158 int
connect()159 Auditioner::connect ()
160 {
161 string left = Config->get_auditioner_output_left();
162 string right = Config->get_auditioner_output_right();
163
164 vector<string> outputs;
165 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
166
167 via_monitor = false;
168
169 if (left.empty() || left == "default") {
170 if (_session.monitor_out() && _session.monitor_out()->input()->audio (0)) {
171 left = _session.monitor_out()->input()->audio (0)->name();
172 } else {
173 if (outputs.size() > 0) {
174 left = outputs[0];
175 }
176 }
177 }
178
179 if (right.empty() || right == "default") {
180 if (_session.monitor_out() && _session.monitor_out()->input()->audio (1)) {
181 right = _session.monitor_out()->input()->audio (1)->name();
182 } else {
183 if (outputs.size() > 1) {
184 right = outputs[1];
185 }
186 }
187 }
188
189 _output->disconnect (this);
190
191 if (left.empty() && right.empty()) {
192 if (_output->n_ports().n_audio() == 0) {
193 /* ports not set up, so must be during startup */
194 warning << _("no outputs available for auditioner - manual connection required") << endmsg;
195 }
196 } else {
197
198 if (_output->n_ports().n_audio() == 0) {
199
200 /* create (and connect) new ports */
201
202 _main_outs->defer_pan_reset ();
203
204 if (left.length()) {
205 _output->add_port (left, this, DataType::AUDIO);
206 }
207
208 if (right.length()) {
209 _output->add_port (right, this, DataType::AUDIO);
210 }
211
212 _main_outs->allow_pan_reset ();
213 _main_outs->reset_panner ();
214
215 } else {
216
217 /* reconnect existing ports */
218
219 boost::shared_ptr<Port> oleft (_output->nth (0));
220 boost::shared_ptr<Port> oright (_output->nth (1));
221 if (oleft) {
222 oleft->connect (left);
223 }
224 if (oright) {
225 oright->connect (right);
226 }
227 }
228
229 }
230
231 if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input())) {
232 via_monitor = true;
233 }
234
235 return 0;
236 }
237
238 DataType
data_type() const239 Auditioner::data_type () const {
240 if (_midi_audition) {
241 return DataType::MIDI;
242 } else {
243 return DataType::AUDIO;
244 }
245 }
246
247 int
roll(pframes_t nframes,samplepos_t start_sample,samplepos_t end_sample,bool & need_butler)248 Auditioner::roll (pframes_t nframes, samplepos_t start_sample, samplepos_t end_sample, bool& need_butler)
249 {
250 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
251 if (!lm.locked()) {
252 return 0;
253 }
254
255 assert(_active);
256
257 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
258
259 if (_queue_panic) {
260 MidiBuffer& mbuf (bufs.get_midi (0));
261 _queue_panic = false;
262 for (uint8_t chn = 0; chn < 0xf; ++chn) {
263 uint8_t buf[3] = { ((uint8_t) (MIDI_CMD_CONTROL | chn)), ((uint8_t) MIDI_CTL_SUSTAIN), 0 };
264 mbuf.push_back(0, Evoral::MIDI_EVENT, 3, buf);
265 buf[1] = MIDI_CTL_ALL_NOTES_OFF;
266 mbuf.push_back(0, Evoral::MIDI_EVENT, 3, buf);
267 buf[1] = MIDI_CTL_RESET_CONTROLLERS;
268 mbuf.push_back(0, Evoral::MIDI_EVENT, 3, buf);
269 }
270 }
271
272 process_output_buffers (bufs, start_sample, end_sample, nframes, !_session.transport_stopped(), true);
273
274 /* note: auditioner never writes to disk, so we don't care about the
275 * disk writer status (it's buffers will always have no data in them).
276 */
277
278 if (_disk_reader->need_butler()) {
279 need_butler = true;
280 }
281
282 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
283 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
284 if (d) {
285 d->flush_buffers (nframes);
286 }
287 }
288
289 return 0;
290 }
291
292 void
audition_region(boost::shared_ptr<Region> region)293 Auditioner::audition_region (boost::shared_ptr<Region> region)
294 {
295 if (g_atomic_int_get (&_auditioning)) {
296 /* don't go via session for this, because we are going
297 to remain active.
298 */
299 cancel_audition ();
300 }
301
302 Glib::Threads::Mutex::Lock lm (lock);
303
304 if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {
305
306 _midi_audition = false;
307
308 unload_synth (true);
309
310 midi_region.reset();
311 _import_position = 0;
312
313 /* copy it */
314 the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region, false));
315 the_region->set_position (0);
316
317 _disk_reader->midi_playlist()->drop_regions ();
318
319 _disk_reader->audio_playlist()->drop_regions ();
320 _disk_reader->audio_playlist()->add_region (the_region, 0, 1);
321
322 ProcessorStreams ps;
323 {
324 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
325
326 if (configure_processors (&ps)) {
327 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
328 region->n_channels()) << endmsg;
329 return;
330 }
331 }
332
333 } else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
334 _midi_audition = true;
335
336 the_region.reset();
337 _import_position = region->position();
338
339 /* copy it */
340 midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region, false)));
341 midi_region->set_position (_import_position);
342
343 _disk_reader->audio_playlist()->drop_regions();
344
345 _disk_reader->midi_playlist()->drop_regions ();
346 _disk_reader->midi_playlist()->add_region (midi_region, _import_position, 1);
347 _disk_reader->reset_tracker();
348
349 ProcessorStreams ps;
350
351 load_synth (true);
352
353 if (asynth) {
354 int rv = add_processor (asynth, PreFader, &ps, true);
355 if (rv) {
356 error << _("Failed to load synth for MIDI-Audition.") << endmsg;
357 }
358 }
359
360 {
361 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
362
363 if (configure_processors (&ps)) {
364 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
365 region->n_channels()) << endmsg;
366 unload_synth (true);
367 return;
368 }
369 }
370
371 } else {
372 error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
373 return;
374 }
375
376 /* force a panner reset now that we have all channels */
377 _main_outs->reset_panner();
378
379 _seek_sample = -1;
380 _seeking = false;
381
382 int dir;
383 samplecnt_t offset;
384
385 if (_midi_audition) {
386 length = midi_region->length();
387 offset = _import_position + midi_region->sync_offset (dir);
388 } else {
389 length = the_region->length();
390 offset = the_region->sync_offset (dir);
391 }
392
393 if (length == 0) {
394 error << _("Cannot audition empty file.") << endmsg;
395 unload_synth (true);
396 return;
397 }
398
399 /* can't audition from a negative sync point */
400
401 if (dir < 0) {
402 offset = 0;
403 }
404
405 _disk_reader->seek (offset, true);
406
407 if (_midi_audition) {
408 /* Fill MIDI buffers.
409 * This is safe to call from here. ::::audition_region()
410 * is called by the butler thread. Also the session is not
411 * yet auditioning. So Session::non_realtime_overwrite()
412 * does call the auditioner's DR.
413 */
414 set_pending_overwrite (PlaylistModified);
415 _disk_reader->overwrite_existing_buffers ();
416 }
417
418 current_sample = offset;
419
420 g_atomic_int_set (&_auditioning, 1);
421 }
422
423 int
play_audition(samplecnt_t nframes)424 Auditioner::play_audition (samplecnt_t nframes)
425 {
426 bool need_butler = false;
427 samplecnt_t this_nframes;
428 int ret;
429
430 if (g_atomic_int_get (&_auditioning) == 0) {
431 silence (nframes);
432 unload_synth (false);
433 return 0;
434 }
435
436 #if 0 // TODO
437 if (_seeking && _seek_complete) {
438 // set FADE-IN
439 } else if (_seek_sample >= 0 && _seek_sample < length && !_seeking) {
440 // set FADE-OUT -- use/override amp? || use region-gain ?
441 }
442 #endif
443
444 if (_seeking && _seek_complete) {
445 _seek_complete = false;
446 _seeking = false;
447 _seek_sample = -1;
448 if (_midi_audition) {
449 /* Force MIDI note tracker to resolve any notes that are
450 * still playing -> set DR::run_must_resolve */
451 _disk_reader->set_pending_overwrite (PlaylistModified);
452 _disk_reader->overwrite_existing_buffers ();
453 }
454 }
455
456 if(!_seeking) {
457 /* process audio */
458 this_nframes = min (nframes, length - current_sample + _import_position);
459
460 if (this_nframes > 0 && 0 != (ret = roll (this_nframes, current_sample, current_sample + this_nframes, need_butler))) {
461 silence (nframes);
462 return ret;
463 }
464
465 current_sample += this_nframes;
466
467 if (this_nframes < nframes) {
468 if (this_nframes > 0) {
469 _session.engine().split_cycle (this_nframes);
470 }
471 silence (nframes - this_nframes);
472 }
473
474 } else {
475 silence (nframes);
476 }
477
478 if (_seek_sample >= 0 && _seek_sample < length && !_seeking) {
479 _queue_panic = true;
480 _seek_complete = false;
481 _seeking = true;
482 need_butler = true;
483 }
484
485 if (!_seeking) {
486 AuditionProgress(current_sample - _import_position, length); /* emit */
487 }
488
489 if (current_sample >= length + _import_position) {
490 _session.cancel_audition ();
491 unload_synth (false);
492 return 0;
493 } else {
494 return need_butler ? 1 : 0;
495 }
496 }
497
498 void
cancel_audition()499 Auditioner::cancel_audition () {
500 g_atomic_int_set (&_auditioning, 0);
501 }
502
503 bool
auditioning() const504 Auditioner::auditioning() const {
505 return g_atomic_int_get (&_auditioning);
506 }
507
508 void
seek_to_sample(sampleoffset_t pos)509 Auditioner::seek_to_sample (sampleoffset_t pos) {
510 if (_seek_sample < 0 && !_seeking) {
511 _seek_sample = pos;
512 }
513 }
514
515 void
seek_to_percent(float const pos)516 Auditioner::seek_to_percent (float const pos) {
517 if (_seek_sample < 0 && !_seeking) {
518 _seek_sample = floorf(length * pos / 100.0);
519 }
520 }
521
522 void
seek_response(sampleoffset_t pos)523 Auditioner::seek_response (sampleoffset_t pos) {
524 /* called from the butler thread */
525 _seek_complete = true;
526 if (_seeking) {
527 current_sample = pos;
528 _seek_complete = true;
529 }
530 }
531
532
533 void
output_changed(IOChange change,void *)534 Auditioner::output_changed (IOChange change, void* /*src*/)
535 {
536 if (0 == (change.type & IOChange::ConnectionsChanged)) {
537 return;
538 }
539 if (_session.inital_connect_or_deletion_in_progress ()) {
540 return;
541 }
542 if (_session.reconnection_in_progress ()) {
543 return;
544 }
545
546 string phys;
547 vector<string> connections;
548 vector<string> outputs;
549 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
550
551 if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input ())) {
552 Config->set_auditioner_output_left ("default");
553 Config->set_auditioner_output_right ("default");
554 via_monitor = true;
555 return;
556 }
557
558 if (_output->nth (0)->get_connections (connections)) {
559 if (outputs.size() > 0) {
560 phys = outputs[0];
561 }
562 if (phys != connections[0]) {
563 Config->set_auditioner_output_left (connections[0]);
564 } else {
565 Config->set_auditioner_output_left ("default");
566 }
567 } else {
568 Config->set_auditioner_output_left ("");
569 }
570
571 connections.clear ();
572
573 if (_output->nth (1)->get_connections (connections)) {
574 if (outputs.size() > 1) {
575 phys = outputs[1];
576 }
577 if (phys != connections[0]) {
578 Config->set_auditioner_output_right (connections[0]);
579 } else {
580 Config->set_auditioner_output_right ("default");
581 }
582 } else {
583 Config->set_auditioner_output_right ("");
584 }
585 }
586
587 ChanCount
input_streams() const588 Auditioner::input_streams () const
589 {
590 /* auditioner never has any inputs - its channel configuration
591 depends solely on the region we are auditioning.
592 */
593
594 if (_midi_audition) {
595 return ChanCount (DataType::MIDI, 1);
596 } else {
597 if (the_region) {
598 return ChanCount (DataType::AUDIO, the_region->n_channels ());
599 }
600 }
601
602 return ChanCount (DataType::AUDIO, 1);
603 }
604
605 MonitorState
monitoring_state() const606 Auditioner::monitoring_state () const
607 {
608 return MonitoringDisk;
609 }
610
611