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