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