disable Midi Audition Synth if it cannot be found
[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/audioregion.h"
26 #include "ardour/audioengine.h"
27 #include "ardour/audioplaylist.h"
28 #include "ardour/auditioner.h"
29 #include "ardour/audio_port.h"
30 #include "ardour/data_type.h"
31 #include "ardour/delivery.h"
32 #include "ardour/plugin.h"
33 #include "ardour/region_factory.h"
34 #include "ardour/route.h"
35 #include "ardour/session.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 #include "i18n.h"
42
43 Auditioner::Auditioner (Session& s)
44         : Track (s, "auditioner", Route::Auditioner)
45         , current_frame (0)
46         , _auditioning (0)
47         , length (0)
48         , _seek_frame (-1)
49         , _seeking (false)
50         , _seek_complete (false)
51         , via_monitor (false)
52         , _midi_audition (false)
53         , _synth_added (false)
54         , _synth_changed (false)
55         , _queue_panic (false)
56         , _import_position (0)
57 {
58 }
59
60 int
61 Auditioner::init ()
62 {
63         if (Track::init ()) {
64                 return -1;
65         }
66
67         if (connect ()) {
68                 return -1;
69         }
70
71         _output->add_port ("", this, DataType::MIDI);
72
73         lookup_synth();
74
75         _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
76         Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Auditioner::config_changed, this, _1));
77
78         return 0;
79 }
80
81 Auditioner::~Auditioner ()
82 {
83 }
84
85 void
86 Auditioner::lookup_synth ()
87 {
88         string plugin_id = Config->get_midi_audition_synth_uri();
89         asynth = boost::shared_ptr<Processor>();
90         if (!plugin_id.empty()) {
91                 boost::shared_ptr<Plugin> p;
92                 p = find_plugin (_session, plugin_id, ARDOUR::LV2);
93                 if (!p) {
94                         p = find_plugin (_session, "https://community.ardour.org/node/7596", ARDOUR::LV2);
95                         if (p) {
96                                 warning << _("Falling back to Reasonable Synth for Midi Audition") << endmsg;
97                         } else {
98                                 warning << _("No synth for midi-audition found.") << endmsg;
99                                 Config->set_midi_audition_synth_uri(""); // Don't check again for Reasonable Synth (ie --no-lv2)
100                         }
101                 }
102                 if (p) {
103                         asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
104                 }
105         }
106 }
107
108 void
109 Auditioner::config_changed (std::string p)
110 {
111         if (p == "midi-audition-synth-uri") {
112                 _synth_changed = true;
113         }
114 }
115
116 int
117 Auditioner::connect ()
118 {
119         string left = Config->get_auditioner_output_left();
120         string right = Config->get_auditioner_output_right();
121
122         vector<string> outputs;
123         _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
124
125         via_monitor = false;
126
127         if (left.empty() || left == "default") {
128                 if (_session.monitor_out()) {
129                         left = _session.monitor_out()->input()->audio (0)->name();
130                         via_monitor = true;
131                 } else {
132                         if (outputs.size() > 0) {
133                                 left = outputs[0];
134                         }
135                 }
136         }
137
138         if (right.empty() || right == "default") {
139                 if (_session.monitor_out()) {
140                         right = _session.monitor_out()->input()->audio (1)->name();
141                         via_monitor = true;
142                 } else {
143                         if (outputs.size() > 1) {
144                                 right = outputs[1];
145                         }
146                 }
147         }
148
149         _output->disconnect (this);
150
151         if (left.empty() && right.empty()) {
152                 if (_output->n_ports().n_audio() == 0) {
153                         /* ports not set up, so must be during startup */
154                         warning << _("no outputs available for auditioner - manual connection required") << endmsg;
155                 }
156         } else {
157
158                 if (_output->n_ports().n_audio() == 0) {
159
160                         /* create (and connect) new ports */
161
162                         _main_outs->defer_pan_reset ();
163                         
164                         if (left.length()) {
165                                 _output->add_port (left, this, DataType::AUDIO);
166                         }
167                         
168                         if (right.length()) {
169                                 _output->add_port (right, this, DataType::AUDIO);
170                         }
171                         
172                         _main_outs->allow_pan_reset ();
173                         _main_outs->reset_panner ();
174
175                 } else {
176                         
177                         /* reconnect existing ports */
178
179                         boost::shared_ptr<Port> oleft (_output->nth (0));
180                         boost::shared_ptr<Port> oright (_output->nth (1));
181                         if (oleft) {
182                                 oleft->connect (left);
183                         }
184                         if (oright) {
185                                 oright->connect (right);
186                         }
187                 }
188                         
189         }
190
191         return 0;
192 }
193
194
195 DataType
196 Auditioner::data_type () const {
197         if (_midi_audition) {
198                 return DataType::MIDI;
199         } else {
200                 return DataType::AUDIO;
201         }
202 }
203
204 boost::shared_ptr<Diskstream>
205 Auditioner::create_diskstream () {
206
207         {
208                 AudioDiskstream::Flag dflags = AudioDiskstream::Flag (0);
209                 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Hidden);
210                 _diskstream_audio = boost::shared_ptr<AudioDiskstream> (new AudioDiskstream (_session, name(), dflags));
211         }
212
213         {
214                 MidiDiskstream::Flag dflags = MidiDiskstream::Flag (0);
215                 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Hidden);
216                 _diskstream_midi = boost::shared_ptr<Diskstream> (new MidiDiskstream (_session, name(), dflags));
217                 _diskstream_midi->do_refill_with_alloc ();
218                 _diskstream_midi->playlist()->set_orig_track_id (id());
219         }
220
221         return _diskstream_audio;
222 }
223
224 int
225 Auditioner::roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler) {
226         if (_midi_audition) {
227                 return roll_midi(nframes, start_frame, end_frame, declick, need_butler);
228         } else {
229                 return roll_audio(nframes, start_frame, end_frame, declick, need_butler);
230         }
231 }
232
233 int
234 Auditioner::roll_midi (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
235 {
236         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
237         if (!lm.locked()) {
238                 return 0;
239         }
240
241         assert(_active);
242
243         framecnt_t playback_distance = nframes;
244         boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
245         BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
246         MidiBuffer& mbuf (bufs.get_midi (0));
247         _silent = false;
248
249         ChanCount cnt (DataType::MIDI, 1);
250         cnt.set (DataType::AUDIO, bufs.count().n_audio());
251         bufs.set_count (cnt);
252
253         if (_queue_panic) {
254                 _queue_panic = false;
255                 for (uint8_t chn = 0; chn < 0xf; ++chn) {
256                         uint8_t buf[3] = { ((uint8_t) (MIDI_CMD_CONTROL | chn)), ((uint8_t) MIDI_CTL_SUSTAIN), 0 };
257                         mbuf.push_back(0, 3, buf);
258                         buf[1] = MIDI_CTL_ALL_NOTES_OFF;
259                         mbuf.push_back(0, 3, buf);
260                         buf[1] = MIDI_CTL_RESET_CONTROLLERS;
261                         mbuf.push_back(0, 3, buf);
262                 }
263                 process_output_buffers (bufs, start_frame, start_frame+1, 1, false, false);
264
265                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
266                         boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
267                         if (d) {
268                                 d->flush_buffers (nframes);
269                         }
270                 }
271         }
272
273         diskstream->get_playback (mbuf, nframes);
274
275         process_output_buffers (bufs, start_frame, end_frame, nframes,
276                                 declick, (!diskstream->record_enabled() && !_session.transport_stopped()));
277
278         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
279                 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
280                 if (d) {
281                         d->flush_buffers (nframes);
282                 }
283         }
284
285         need_butler = diskstream->commit (playback_distance);
286         return 0;
287 }
288
289
290 int
291 Auditioner::roll_audio (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler) {
292         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
293         if (!lm.locked()) {
294                 return 0;
295         }
296
297         assert(n_outputs().n_total() > 0);
298         assert(_active);
299
300         int dret;
301         framecnt_t playback_distance;
302         framepos_t transport_frame = _session.transport_frame();
303         boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
304         BufferSet& bufs = _session.get_route_buffers (n_process_buffers ());
305
306         _silent = false;
307         _amp->apply_gain_automation(false);
308
309         if ((dret = diskstream->process (bufs, transport_frame, nframes, playback_distance, (monitoring_state() == MonitoringDisk))) != 0) {
310                 need_butler = diskstream->commit (playback_distance);
311                 silence (nframes);
312                 return dret;
313         }
314
315         process_output_buffers (bufs, start_frame, end_frame, nframes, declick, (!diskstream->record_enabled() && _session.transport_rolling()));
316         need_butler = diskstream->commit (playback_distance);
317         return 0;
318 }
319
320 void
321 Auditioner::set_diskstream (boost::shared_ptr<Diskstream> ds)
322 {
323         Track::set_diskstream (ds);
324
325         _diskstream->set_track (this);
326         _diskstream->set_destructive (_mode == Destructive);
327         _diskstream->set_non_layered (_mode == NonLayered);
328         _diskstream->set_record_enabled (false);
329         _diskstream->request_input_monitoring (false);
330
331         DiskstreamChanged (); /* EMIT SIGNAL */
332 }
333
334 AudioPlaylist&
335 Auditioner::prepare_playlist ()
336 {
337         // used by CrossfadeEditor::audition()
338
339         _midi_audition = false;
340         set_diskstream(_diskstream_audio);
341         if (_synth_added) {
342                 remove_processor(asynth);
343                 _synth_added = false;
344         }
345
346         // FIXME auditioner is still audio-only
347         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(_diskstream->playlist());
348         assert(apl);
349
350         apl->clear ();
351         return *apl;
352 }
353
354 void
355 Auditioner::audition_region (boost::shared_ptr<Region> region)
356 {
357         if (g_atomic_int_get (&_auditioning)) {
358                 /* don't go via session for this, because we are going
359                    to remain active.
360                 */
361                 cancel_audition ();
362         }
363
364         Glib::Threads::Mutex::Lock lm (lock);
365
366         if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {
367
368                 _midi_audition = false;
369                 set_diskstream(_diskstream_audio);
370                 if (_synth_added) {
371                         remove_processor(asynth);
372                         _synth_added = false;
373                 }
374                 midi_region.reset();
375                 _import_position = 0;
376
377                 /* copy it */
378                 the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
379                 the_region->set_position (0);
380
381                 _diskstream->playlist()->drop_regions ();
382                 _diskstream->playlist()->add_region (the_region, 0, 1);
383
384                 if (_diskstream->n_channels().n_audio() < the_region->n_channels()) {
385                         audio_diskstream()->add_channel (the_region->n_channels() - _diskstream->n_channels().n_audio());
386                 } else if (_diskstream->n_channels().n_audio() > the_region->n_channels()) {
387                         audio_diskstream()->remove_channel (_diskstream->n_channels().n_audio() - the_region->n_channels());
388                 }
389
390                 ProcessorStreams ps;
391                 {
392                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
393
394                         if (configure_processors (&ps)) {
395                                 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
396                                                          _diskstream->n_channels()) << endmsg;
397                                 return;
398                         }
399                 }
400
401         } else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
402                 _midi_audition = true;
403                 set_diskstream(_diskstream_midi);
404                 the_region.reset();
405                 _import_position = region->position();
406
407                 /* copy it */
408                 midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
409                 midi_region->set_position (_import_position);
410
411                 _diskstream->playlist()->drop_regions ();
412                 _diskstream->playlist()->add_region (midi_region, _import_position, 1);
413                 midi_diskstream()->reset_tracker();
414
415                 ProcessorStreams ps;
416
417                 if (_synth_changed && _synth_added) {
418                         remove_processor(asynth);
419                         _synth_added = false;
420                 }
421                 if (_synth_changed && !_synth_added) {
422                         _synth_added = false;
423                         lookup_synth();
424                 }
425
426
427                 if (!_synth_added && asynth) {
428                         int rv = add_processor_by_index(asynth, PreFader, &ps, true);
429                         if (rv) {
430                                 error << _("Failed to load synth for MIDI-Audition.") << endmsg;
431                         } else {
432                                 _synth_added = true;
433                         }
434                 } else {
435                         _queue_panic = true;
436                 }
437
438                 {
439                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
440
441                         if (configure_processors (&ps)) {
442                                 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
443                                                          _diskstream->n_channels()) << endmsg;
444                                 return;
445                         }
446                 }
447
448         } else {
449                 error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
450                 return;
451         }
452
453         /* force a panner reset now that we have all channels */
454         _main_outs->reset_panner();
455
456         _seek_frame = -1;
457         _seeking = false;
458
459         int dir;
460         framecnt_t offset;
461
462         if (_midi_audition) {
463                 length = midi_region->length();
464                 offset = _import_position + midi_region->sync_offset (dir);
465         } else {
466                 length = the_region->length();
467                 offset = the_region->sync_offset (dir);
468         }
469
470         /* can't audition from a negative sync point */
471
472         if (dir < 0) {
473                 offset = 0;
474         }
475
476         _diskstream->seek (offset);
477         current_frame = offset;
478
479         g_atomic_int_set (&_auditioning, 1);
480 }
481
482 int
483 Auditioner::play_audition (framecnt_t nframes)
484 {
485         bool need_butler = false;
486         framecnt_t this_nframes;
487         int ret;
488
489         if (g_atomic_int_get (&_auditioning) == 0) {
490                 silence (nframes);
491                 return 0;
492         }
493
494 #if 0 // TODO
495         if (_seeking && _seek_complete) {
496                 // set FADE-IN
497         } else if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
498                 // set FADE-OUT -- use/override amp? || use region-gain ?
499         }
500 #endif
501
502         if (_seeking && _seek_complete) {
503                 _seek_complete = false;
504                 _seeking = false;
505                 _seek_frame = -1;
506                 if (_midi_audition && midi_diskstream()) {
507                         midi_diskstream()->reset_tracker();
508                 }
509         }
510
511         if(!_seeking) {
512                 /* process audio */
513                 this_nframes = min (nframes, length - current_frame);
514
515                 if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, need_butler)) != 0) {
516                         silence (nframes);
517                         return ret;
518                 }
519
520                 current_frame += this_nframes;
521
522         } else {
523                 silence (nframes);
524         }
525
526         if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
527                 _queue_panic = true;
528                 _seek_complete = false;
529                 _seeking = true;
530                 need_butler = true;
531         }
532
533         if (!_seeking) {
534                 AuditionProgress(current_frame - _import_position, length); /* emit */
535         }
536
537         if (current_frame >= length) {
538                 _session.cancel_audition ();
539                 return 0;
540         } else {
541                 return need_butler ? 1 : 0;
542         }
543 }
544
545 void
546 Auditioner::output_changed (IOChange change, void* /*src*/)
547 {
548         if (change.type & IOChange::ConnectionsChanged) {
549                 string phys;
550                 vector<string> connections;
551                 vector<string> outputs;
552                 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
553                 if (_output->nth (0)->get_connections (connections)) {
554                         if (outputs.size() > 0) {
555                                 phys = outputs[0];
556                         }
557                         if (phys != connections[0]) {
558                                 Config->set_auditioner_output_left (connections[0]);
559                         } else {
560                                 Config->set_auditioner_output_left ("default");
561                         }
562                 } else {
563                         Config->set_auditioner_output_left ("");
564                 }
565
566                 connections.clear ();
567
568                 if (_output->nth (1)->get_connections (connections)) {
569                         if (outputs.size() > 1) {
570                                 phys = outputs[1];
571                         }
572                         if (phys != connections[0]) {
573                                 Config->set_auditioner_output_right (connections[0]);
574                         } else {
575                                 Config->set_auditioner_output_right ("default");
576                         }
577                 } else {
578                         Config->set_auditioner_output_right ("");
579                 }
580         }
581 }
582
583 ChanCount
584 Auditioner::input_streams () const
585 {
586         /* auditioner never has any inputs - its channel configuration
587                  depends solely on the region we are auditioning.
588                  */
589
590         if (!_midi_audition && audio_diskstream()) {
591                 return audio_diskstream()->n_channels();
592         }
593         if (_midi_audition && midi_diskstream()) {
594                 ChanCount cnt (DataType::MIDI, 1);
595                 return cnt;
596         }
597
598         return ChanCount ();
599 }
600
601 MonitorState
602 Auditioner::monitoring_state () const
603 {
604         return MonitoringDisk;
605 }