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