58c7cd8c8f52221b80c44b0535c40bd27992d739
[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_port.h"
26 #include "ardour/audioengine.h"
27 #include "ardour/audioplaylist.h"
28 #include "ardour/audioregion.h"
29 #include "ardour/auditioner.h"
30 #include "ardour/data_type.h"
31 #include "ardour/delivery.h"
32 #include "ardour/disk_reader.h"
33 #include "ardour/midi_playlist.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_sample (0)
51         , _auditioning (0)
52         , length (0)
53         , _seek_sample (-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         use_new_playlist (DataType::MIDI);
78
79         lookup_synth();
80
81         _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
82         Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Auditioner::config_changed, this, _1));
83
84         return 0;
85 }
86
87 Auditioner::~Auditioner ()
88 {
89         if (asynth) {
90                 asynth->drop_references ();
91         }
92         asynth.reset ();
93 }
94
95 void
96 Auditioner::lookup_synth ()
97 {
98         string plugin_id = Config->get_midi_audition_synth_uri();
99         asynth.reset ();
100         if (!plugin_id.empty() || plugin_id == X_("@default@")) {
101                 boost::shared_ptr<Plugin> p;
102                 p = find_plugin (_session, plugin_id, ARDOUR::LV2);
103                 if (!p) {
104                         p = find_plugin (_session, "http://gareus.org/oss/lv2/gmsynth", ARDOUR::LV2);
105                         if (!p) {
106                                 p = find_plugin (_session, "https://community.ardour.org/node/7596", ARDOUR::LV2);
107                         }
108                         if (p) {
109                                 warning << _("Falling back to Reasonable Synth for Midi Audition") << endmsg;
110                         } else {
111                                 warning << _("No synth for midi-audition found.") << endmsg;
112                                 Config->set_midi_audition_synth_uri(""); // Don't check again for Reasonable Synth (ie --no-lv2)
113                         }
114                 }
115                 if (p) {
116                         if (plugin_id == X_("@default@")) {
117                                 Config->set_midi_audition_synth_uri (p->get_info()->unique_id);
118                         }
119                         asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
120                 }
121         }
122 }
123
124 void
125 Auditioner::config_changed (std::string p)
126 {
127         if (p == "midi-audition-synth-uri") {
128                 _synth_changed = true;
129         }
130 }
131
132 int
133 Auditioner::connect ()
134 {
135         string left = Config->get_auditioner_output_left();
136         string right = Config->get_auditioner_output_right();
137
138         vector<string> outputs;
139         _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
140
141         via_monitor = false;
142
143         if (left.empty() || left == "default") {
144                 if (_session.monitor_out() && _session.monitor_out()->input()->audio (0)) {
145                         left = _session.monitor_out()->input()->audio (0)->name();
146                 } else {
147                         if (outputs.size() > 0) {
148                                 left = outputs[0];
149                         }
150                 }
151         }
152
153         if (right.empty() || right == "default") {
154                 if (_session.monitor_out() && _session.monitor_out()->input()->audio (1)) {
155                         right = _session.monitor_out()->input()->audio (1)->name();
156                 } else {
157                         if (outputs.size() > 1) {
158                                 right = outputs[1];
159                         }
160                 }
161         }
162
163         _output->disconnect (this);
164
165         if (left.empty() && right.empty()) {
166                 if (_output->n_ports().n_audio() == 0) {
167                         /* ports not set up, so must be during startup */
168                         warning << _("no outputs available for auditioner - manual connection required") << endmsg;
169                 }
170         } else {
171
172                 if (_output->n_ports().n_audio() == 0) {
173
174                         /* create (and connect) new ports */
175
176                         _main_outs->defer_pan_reset ();
177
178                         if (left.length()) {
179                                 _output->add_port (left, this, DataType::AUDIO);
180                         }
181
182                         if (right.length()) {
183                                 _output->add_port (right, this, DataType::AUDIO);
184                         }
185
186                         _main_outs->allow_pan_reset ();
187                         _main_outs->reset_panner ();
188
189                 } else {
190
191                         /* reconnect existing ports */
192
193                         boost::shared_ptr<Port> oleft (_output->nth (0));
194                         boost::shared_ptr<Port> oright (_output->nth (1));
195                         if (oleft) {
196                                 oleft->connect (left);
197                         }
198                         if (oright) {
199                                 oright->connect (right);
200                         }
201                 }
202
203         }
204
205         if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input())) {
206                 via_monitor = true;
207         }
208
209         return 0;
210 }
211
212
213 DataType
214 Auditioner::data_type () const {
215         if (_midi_audition) {
216                 return DataType::MIDI;
217         } else {
218                 return DataType::AUDIO;
219         }
220 }
221
222 int
223 Auditioner::roll (pframes_t nframes, samplepos_t start_sample, samplepos_t end_sample, int declick, bool& need_butler)
224 {
225         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
226         if (!lm.locked()) {
227                 return 0;
228         }
229
230         assert(_active);
231
232         BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
233
234         _silent = false;
235         _amp->apply_gain_automation(false);
236
237         if (_queue_panic) {
238                 MidiBuffer& mbuf (bufs.get_midi (0));
239                 _queue_panic = false;
240                 for (uint8_t chn = 0; chn < 0xf; ++chn) {
241                         uint8_t buf[3] = { ((uint8_t) (MIDI_CMD_CONTROL | chn)), ((uint8_t) MIDI_CTL_SUSTAIN), 0 };
242                         mbuf.push_back(0, 3, buf);
243                         buf[1] = MIDI_CTL_ALL_NOTES_OFF;
244                         mbuf.push_back(0, 3, buf);
245                         buf[1] = MIDI_CTL_RESET_CONTROLLERS;
246                         mbuf.push_back(0, 3, buf);
247                 }
248         }
249
250         process_output_buffers (bufs, start_sample, end_sample, nframes, declick, !_session.transport_stopped());
251
252         /* note: auditioner never writes to disk, so we don't care about the
253          * disk writer status (it's buffers will always have no data in them).
254          */
255
256         if (_disk_reader->need_butler()) {
257                 need_butler = true;
258         }
259
260         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
261                 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
262                 if (d) {
263                         d->flush_buffers (nframes);
264                 }
265         }
266
267         return 0;
268 }
269
270 void
271 Auditioner::audition_region (boost::shared_ptr<Region> region)
272 {
273         if (g_atomic_int_get (&_auditioning)) {
274                 /* don't go via session for this, because we are going
275                    to remain active.
276                 */
277                 cancel_audition ();
278         }
279
280         Glib::Threads::Mutex::Lock lm (lock);
281
282         if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {
283
284                 _midi_audition = false;
285
286                 if (_synth_added) {
287                         remove_processor(asynth);
288                         _synth_added = false;
289                 }
290                 midi_region.reset();
291                 _import_position = 0;
292
293                 /* copy it */
294                 the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
295                 the_region->set_position (0);
296
297                 _disk_reader->midi_playlist()->drop_regions ();
298
299                 _disk_reader->audio_playlist()->drop_regions ();
300                 _disk_reader->audio_playlist()->add_region (the_region, 0, 1);
301
302                 ProcessorStreams ps;
303                 {
304                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
305
306                         if (configure_processors (&ps)) {
307                                 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
308                                                          region->n_channels()) << endmsg;
309                                 return;
310                         }
311                 }
312
313         } else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
314                 _midi_audition = true;
315
316                 the_region.reset();
317                 _import_position = region->position();
318
319                 /* copy it */
320                 midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
321                 midi_region->set_position (_import_position);
322
323                 _disk_reader->audio_playlist()->drop_regions();
324
325                 _disk_reader->midi_playlist()->drop_regions ();
326                 _disk_reader->midi_playlist()->add_region (midi_region, _import_position, 1);
327                 _disk_reader->reset_tracker();
328
329                 ProcessorStreams ps;
330
331                 if (_synth_changed && _synth_added) {
332                         remove_processor(asynth);
333                         _synth_added = false;
334                 }
335                 if (_synth_changed && !_synth_added) {
336                         _synth_added = false;
337                         lookup_synth();
338                 }
339
340                 if (!_synth_added && asynth) {
341                         int rv = add_processor (asynth, PreFader, &ps, true);
342                         if (rv) {
343                                 error << _("Failed to load synth for MIDI-Audition.") << endmsg;
344                         } else {
345                                 _synth_added = true;
346                         }
347                 } else {
348                         _queue_panic = true;
349                 }
350
351                 {
352                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
353
354                         if (configure_processors (&ps)) {
355                                 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
356                                                          region->n_channels()) << endmsg;
357                                 return;
358                         }
359                 }
360
361         } else {
362                 error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
363                 return;
364         }
365
366         /* force a panner reset now that we have all channels */
367         _main_outs->reset_panner();
368
369         _seek_sample = -1;
370         _seeking = false;
371
372         int dir;
373         samplecnt_t offset;
374
375         if (_midi_audition) {
376                 length = midi_region->length();
377                 offset = _import_position + midi_region->sync_offset (dir);
378         } else {
379                 length = the_region->length();
380                 offset = the_region->sync_offset (dir);
381         }
382
383         /* can't audition from a negative sync point */
384
385         if (dir < 0) {
386                 offset = 0;
387         }
388
389         _disk_reader->seek (offset, true);
390         current_sample = offset;
391
392         g_atomic_int_set (&_auditioning, 1);
393 }
394
395 int
396 Auditioner::play_audition (samplecnt_t nframes)
397 {
398         bool need_butler = false;
399         samplecnt_t this_nframes;
400         int ret;
401
402         if (g_atomic_int_get (&_auditioning) == 0) {
403                 silence (nframes);
404                 return 0;
405         }
406
407 #if 0 // TODO
408         if (_seeking && _seek_complete) {
409                 // set FADE-IN
410         } else if (_seek_sample >= 0 && _seek_sample < length && !_seeking) {
411                 // set FADE-OUT -- use/override amp? || use region-gain ?
412         }
413 #endif
414
415         if (_seeking && _seek_complete) {
416                 _seek_complete = false;
417                 _seeking = false;
418                 _seek_sample = -1;
419                 _disk_reader->reset_tracker();
420         }
421
422         if(!_seeking) {
423                 /* process audio */
424                 this_nframes = min (nframes, length - current_sample + _import_position);
425
426                 if ((ret = roll (this_nframes, current_sample, current_sample + nframes, false, need_butler)) != 0) {
427                         silence (nframes);
428                         return ret;
429                 }
430
431                 current_sample += this_nframes;
432
433         } else {
434                 silence (nframes);
435         }
436
437         if (_seek_sample >= 0 && _seek_sample < length && !_seeking) {
438                 _queue_panic = true;
439                 _seek_complete = false;
440                 _seeking = true;
441                 need_butler = true;
442         }
443
444         if (!_seeking) {
445                 AuditionProgress(current_sample - _import_position, length); /* emit */
446         }
447
448         if (current_sample >= length + _import_position) {
449                 _session.cancel_audition ();
450                 return 0;
451         } else {
452                 return need_butler ? 1 : 0;
453         }
454 }
455
456 void
457 Auditioner::output_changed (IOChange change, void* /*src*/)
458 {
459         if (change.type & IOChange::ConnectionsChanged) {
460                 string phys;
461                 vector<string> connections;
462                 vector<string> outputs;
463                 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
464
465                 if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input ())) {
466                         Config->set_auditioner_output_left ("default");
467                         Config->set_auditioner_output_right ("default");
468                         via_monitor = true;
469                         return;
470                 }
471
472                 if (_output->nth (0)->get_connections (connections)) {
473                         if (outputs.size() > 0) {
474                                 phys = outputs[0];
475                         }
476                         if (phys != connections[0]) {
477                                 Config->set_auditioner_output_left (connections[0]);
478                         } else {
479                                 Config->set_auditioner_output_left ("default");
480                         }
481                 } else {
482                         Config->set_auditioner_output_left ("");
483                 }
484
485                 connections.clear ();
486
487                 if (_output->nth (1)->get_connections (connections)) {
488                         if (outputs.size() > 1) {
489                                 phys = outputs[1];
490                         }
491                         if (phys != connections[0]) {
492                                 Config->set_auditioner_output_right (connections[0]);
493                         } else {
494                                 Config->set_auditioner_output_right ("default");
495                         }
496                 } else {
497                         Config->set_auditioner_output_right ("");
498                 }
499         }
500 }
501
502 ChanCount
503 Auditioner::input_streams () const
504 {
505         /* auditioner never has any inputs - its channel configuration
506            depends solely on the region we are auditioning.
507         */
508
509         if (_midi_audition) {
510                 return ChanCount (DataType::MIDI, 1);
511         } else {
512                 if (the_region) {
513                         return ChanCount (DataType::AUDIO, the_region->n_channels ());
514                 }
515         }
516
517         return ChanCount (DataType::AUDIO, 1);
518 }
519
520 MonitorState
521 Auditioner::monitoring_state () const
522 {
523         return MonitoringDisk;
524 }
525