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