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