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