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