change MidiPlaylist::dump() into ::render(); change type of initial argument
[ardour.git] / libs / ardour / auditioner.cc
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
51 Auditioner::Auditioner (Session& s)
52         : Track (s, "auditioner", PresentationInfo::Auditioner)
53         , current_sample (0)
54         , _auditioning (0)
55         , length (0)
56         , _seek_sample (-1)
57         , _seeking (false)
58         , _seek_complete (false)
59         , via_monitor (false)
60         , _midi_audition (false)
61         , _queue_panic (false)
62         , _import_position (0)
63 {
64 }
65
66 int
67 Auditioner::init ()
68 {
69         if (Track::init ()) {
70                 return -1;
71         }
72
73         if (connect ()) {
74                 return -1;
75         }
76
77         _output->add_port ("", this, DataType::MIDI);
78         use_new_playlist (DataType::MIDI);
79
80         if (!audition_synth_info) {
81                 lookup_fallback_synth ();
82         } 
83
84         _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
85
86         return 0;
87 }
88
89 Auditioner::~Auditioner ()
90 {
91         unload_synth(true);
92 }
93
94 PluginInfoPtr
95 Auditioner::lookup_fallback_synth_plugin_info (std::string const& uri) const
96 {
97         PluginManager& mgr (PluginManager::instance());
98         PluginInfoList plugs;
99 #ifdef LV2_SUPPORT
100         plugs = mgr.lv2_plugin_info();
101 #endif
102         for (PluginInfoList::const_iterator i = plugs.begin (); i != plugs.end (); ++i) {
103                 if (uri == (*i)->unique_id){
104                         return (*i);
105                 }
106         }
107         return PluginInfoPtr ();
108 }
109
110 void
111 Auditioner::lookup_fallback_synth ()
112 {
113         
114         PluginInfoPtr nfo = lookup_fallback_synth_plugin_info ("http://gareus.org/oss/lv2/gmsynth");
115
116         //GMsynth not found: fallback to Reasonable Synth
117         if (!nfo) {
118                 nfo = lookup_fallback_synth_plugin_info ("https://community.ardour.org/node/7596");
119                 if (nfo) {
120                         warning << _("Falling back to Reasonable Synth for Midi Audition") << endmsg;
121                 }
122         }
123
124         if (!nfo) {
125                 warning << _("No synth for midi-audition found.") << endmsg;
126                 return;
127         }
128
129         set_audition_synth_info(nfo);
130 }
131
132 void
133 Auditioner::load_synth (bool need_lock)
134 {
135         unload_synth(need_lock);
136         
137         boost::shared_ptr<Plugin> p = audition_synth_info->load (_session);
138         asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
139 }
140
141 void
142 Auditioner::unload_synth (bool need_lock)
143 {
144         if (asynth) {
145                 asynth->drop_references ();
146         }
147         if (0 == remove_processor (asynth, NULL, need_lock)) {
148                 asynth.reset ();
149         }
150 }
151
152 int
153 Auditioner::connect ()
154 {
155         string left = Config->get_auditioner_output_left();
156         string right = Config->get_auditioner_output_right();
157
158         vector<string> outputs;
159         _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
160
161         via_monitor = false;
162
163         if (left.empty() || left == "default") {
164                 if (_session.monitor_out() && _session.monitor_out()->input()->audio (0)) {
165                         left = _session.monitor_out()->input()->audio (0)->name();
166                 } else {
167                         if (outputs.size() > 0) {
168                                 left = outputs[0];
169                         }
170                 }
171         }
172
173         if (right.empty() || right == "default") {
174                 if (_session.monitor_out() && _session.monitor_out()->input()->audio (1)) {
175                         right = _session.monitor_out()->input()->audio (1)->name();
176                 } else {
177                         if (outputs.size() > 1) {
178                                 right = outputs[1];
179                         }
180                 }
181         }
182
183         _output->disconnect (this);
184
185         if (left.empty() && right.empty()) {
186                 if (_output->n_ports().n_audio() == 0) {
187                         /* ports not set up, so must be during startup */
188                         warning << _("no outputs available for auditioner - manual connection required") << endmsg;
189                 }
190         } else {
191
192                 if (_output->n_ports().n_audio() == 0) {
193
194                         /* create (and connect) new ports */
195
196                         _main_outs->defer_pan_reset ();
197
198                         if (left.length()) {
199                                 _output->add_port (left, this, DataType::AUDIO);
200                         }
201
202                         if (right.length()) {
203                                 _output->add_port (right, this, DataType::AUDIO);
204                         }
205
206                         _main_outs->allow_pan_reset ();
207                         _main_outs->reset_panner ();
208
209                 } else {
210
211                         /* reconnect existing ports */
212
213                         boost::shared_ptr<Port> oleft (_output->nth (0));
214                         boost::shared_ptr<Port> oright (_output->nth (1));
215                         if (oleft) {
216                                 oleft->connect (left);
217                         }
218                         if (oright) {
219                                 oright->connect (right);
220                         }
221                 }
222
223         }
224
225         if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input())) {
226                 via_monitor = true;
227         }
228
229         return 0;
230 }
231
232 DataType
233 Auditioner::data_type () const {
234         if (_midi_audition) {
235                 return DataType::MIDI;
236         } else {
237                 return DataType::AUDIO;
238         }
239 }
240
241 int
242 Auditioner::roll (pframes_t nframes, samplepos_t start_sample, samplepos_t end_sample, bool& need_butler)
243 {
244         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
245         if (!lm.locked()) {
246                 return 0;
247         }
248
249         assert(_active);
250
251         BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
252
253         if (_queue_panic) {
254                 MidiBuffer& mbuf (bufs.get_midi (0));
255                 _queue_panic = false;
256                 for (uint8_t chn = 0; chn < 0xf; ++chn) {
257                         uint8_t buf[3] = { ((uint8_t) (MIDI_CMD_CONTROL | chn)), ((uint8_t) MIDI_CTL_SUSTAIN), 0 };
258                         mbuf.push_back(0, 3, buf);
259                         buf[1] = MIDI_CTL_ALL_NOTES_OFF;
260                         mbuf.push_back(0, 3, buf);
261                         buf[1] = MIDI_CTL_RESET_CONTROLLERS;
262                         mbuf.push_back(0, 3, buf);
263                 }
264         }
265
266         process_output_buffers (bufs, start_sample, end_sample, nframes, !_session.transport_stopped(), true);
267
268         /* note: auditioner never writes to disk, so we don't care about the
269          * disk writer status (it's buffers will always have no data in them).
270          */
271
272         if (_disk_reader->need_butler()) {
273                 need_butler = true;
274         }
275
276         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
277                 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
278                 if (d) {
279                         d->flush_buffers (nframes);
280                 }
281         }
282
283         return 0;
284 }
285
286 void
287 Auditioner::audition_region (boost::shared_ptr<Region> region)
288 {
289         if (g_atomic_int_get (&_auditioning)) {
290                 /* don't go via session for this, because we are going
291                    to remain active.
292                 */
293                 cancel_audition ();
294         }
295
296         Glib::Threads::Mutex::Lock lm (lock);
297
298         if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {
299
300                 _midi_audition = false;
301
302                 unload_synth (true);
303
304                 midi_region.reset();
305                 _import_position = 0;
306
307                 /* copy it */
308                 the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
309                 the_region->set_position (0);
310
311                 _disk_reader->midi_playlist()->drop_regions ();
312
313                 _disk_reader->audio_playlist()->drop_regions ();
314                 _disk_reader->audio_playlist()->add_region (the_region, 0, 1);
315
316                 ProcessorStreams ps;
317                 {
318                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
319
320                         if (configure_processors (&ps)) {
321                                 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
322                                                          region->n_channels()) << endmsg;
323                                 return;
324                         }
325                 }
326
327         } else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
328                 _midi_audition = true;
329
330                 the_region.reset();
331                 _import_position = region->position();
332
333                 /* copy it */
334                 midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
335                 midi_region->set_position (_import_position);
336
337                 _disk_reader->audio_playlist()->drop_regions();
338
339                 _disk_reader->midi_playlist()->drop_regions ();
340                 _disk_reader->midi_playlist()->add_region (midi_region, _import_position, 1);
341                 _disk_reader->reset_tracker();
342
343                 ProcessorStreams ps;
344
345                 load_synth (true);
346
347                 if (asynth) {
348                         int rv = add_processor (asynth, PreFader, &ps, true);
349                         if (rv) {
350                                 error << _("Failed to load synth for MIDI-Audition.") << endmsg;
351                         }
352                 }
353
354                 {
355                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
356
357                         if (configure_processors (&ps)) {
358                                 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
359                                                          region->n_channels()) << endmsg;
360                                 unload_synth (true);
361                                 return;
362                         }
363                 }
364
365         } else {
366                 error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
367                 return;
368         }
369
370         /* force a panner reset now that we have all channels */
371         _main_outs->reset_panner();
372
373         _seek_sample = -1;
374         _seeking = false;
375
376         int dir;
377         samplecnt_t offset;
378
379         if (_midi_audition) {
380                 length = midi_region->length();
381                 offset = _import_position + midi_region->sync_offset (dir);
382         } else {
383                 length = the_region->length();
384                 offset = the_region->sync_offset (dir);
385         }
386
387         if (length == 0) {
388                 error << _("Cannot audition empty file.") << endmsg;
389                 unload_synth (true);
390                 return;
391         }
392
393         /* can't audition from a negative sync point */
394
395         if (dir < 0) {
396                 offset = 0;
397         }
398
399         _disk_reader->seek (offset, true);
400         current_sample = offset;
401
402         g_atomic_int_set (&_auditioning, 1);
403 }
404
405 int
406 Auditioner::play_audition (samplecnt_t nframes)
407 {
408         bool need_butler = false;
409         samplecnt_t this_nframes;
410         int ret;
411
412         if (g_atomic_int_get (&_auditioning) == 0) {
413                 silence (nframes);
414                 unload_synth (false);
415                 return 0;
416         }
417
418 #if 0 // TODO
419         if (_seeking && _seek_complete) {
420                 // set FADE-IN
421         } else if (_seek_sample >= 0 && _seek_sample < length && !_seeking) {
422                 // set FADE-OUT -- use/override amp? || use region-gain ?
423         }
424 #endif
425
426         if (_seeking && _seek_complete) {
427                 _seek_complete = false;
428                 _seeking = false;
429                 _seek_sample = -1;
430                 _disk_reader->reset_tracker();
431         }
432
433         if(!_seeking) {
434                 /* process audio */
435                 this_nframes = min (nframes, length - current_sample + _import_position);
436
437                 if (this_nframes > 0 && 0 != (ret = roll (this_nframes, current_sample, current_sample + this_nframes, need_butler))) {
438                         silence (nframes);
439                         return ret;
440                 }
441
442                 current_sample += this_nframes;
443
444                 if (this_nframes < nframes) {
445                         if (this_nframes > 0) {
446                                 _session.engine().split_cycle (this_nframes);
447                         }
448                         silence (nframes - this_nframes);
449                 }
450
451         } else {
452                 silence (nframes);
453         }
454
455         if (_seek_sample >= 0 && _seek_sample < length && !_seeking) {
456                 _queue_panic = true;
457                 _seek_complete = false;
458                 _seeking = true;
459                 need_butler = true;
460         }
461
462         if (!_seeking) {
463                 AuditionProgress(current_sample - _import_position, length); /* emit */
464         }
465
466         if (current_sample >= length + _import_position) {
467                 _session.cancel_audition ();
468                 unload_synth (false);
469                 return 0;
470         } else {
471                 return need_butler ? 1 : 0;
472         }
473 }
474
475 void
476 Auditioner::cancel_audition () {
477         g_atomic_int_set (&_auditioning, 0);
478 }
479
480 bool
481 Auditioner::auditioning() const {
482         return g_atomic_int_get (&_auditioning);
483 }
484
485 void
486 Auditioner::seek_to_sample (sampleoffset_t pos) {
487         if (_seek_sample < 0 && !_seeking) {
488                 _seek_sample = pos;
489         }
490 }
491
492 void
493 Auditioner::seek_to_percent (float const pos) {
494         if (_seek_sample < 0 && !_seeking) {
495                 _seek_sample = floorf(length * pos / 100.0);
496         }
497 }
498
499 void
500 Auditioner::seek_response (sampleoffset_t pos) {
501         /* called from the butler thread */
502         _seek_complete = true;
503         if (_seeking) {
504                 current_sample = pos;
505                 _seek_complete = true;
506         }
507 }
508
509
510 void
511 Auditioner::output_changed (IOChange change, void* /*src*/)
512 {
513         if (change.type & IOChange::ConnectionsChanged) {
514                 string phys;
515                 vector<string> connections;
516                 vector<string> outputs;
517                 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
518
519                 if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input ())) {
520                         Config->set_auditioner_output_left ("default");
521                         Config->set_auditioner_output_right ("default");
522                         via_monitor = true;
523                         return;
524                 }
525
526                 if (_output->nth (0)->get_connections (connections)) {
527                         if (outputs.size() > 0) {
528                                 phys = outputs[0];
529                         }
530                         if (phys != connections[0]) {
531                                 Config->set_auditioner_output_left (connections[0]);
532                         } else {
533                                 Config->set_auditioner_output_left ("default");
534                         }
535                 } else {
536                         Config->set_auditioner_output_left ("");
537                 }
538
539                 connections.clear ();
540
541                 if (_output->nth (1)->get_connections (connections)) {
542                         if (outputs.size() > 1) {
543                                 phys = outputs[1];
544                         }
545                         if (phys != connections[0]) {
546                                 Config->set_auditioner_output_right (connections[0]);
547                         } else {
548                                 Config->set_auditioner_output_right ("default");
549                         }
550                 } else {
551                         Config->set_auditioner_output_right ("");
552                 }
553         }
554 }
555
556 ChanCount
557 Auditioner::input_streams () const
558 {
559         /* auditioner never has any inputs - its channel configuration
560            depends solely on the region we are auditioning.
561         */
562
563         if (_midi_audition) {
564                 return ChanCount (DataType::MIDI, 1);
565         } else {
566                 if (the_region) {
567                         return ChanCount (DataType::AUDIO, the_region->n_channels ());
568                 }
569         }
570
571         return ChanCount (DataType::AUDIO, 1);
572 }
573
574 MonitorState
575 Auditioner::monitoring_state () const
576 {
577         return MonitoringDisk;
578 }
579