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