acb763a3dd7cd4d61b60ba299bd5f67f4b26e19a
[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         /* note: auditioner never writes to disk, so we don't care about the
250          * disk writer status (it's buffers will always have no data in them).
251          */
252
253         if (_disk_reader->need_butler()) {
254                 need_butler = true;
255         }
256
257         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
258                 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
259                 if (d) {
260                         d->flush_buffers (nframes);
261                 }
262         }
263
264         return 0;
265 }
266
267 void
268 Auditioner::audition_region (boost::shared_ptr<Region> region)
269 {
270         if (g_atomic_int_get (&_auditioning)) {
271                 /* don't go via session for this, because we are going
272                    to remain active.
273                 */
274                 cancel_audition ();
275         }
276
277         Glib::Threads::Mutex::Lock lm (lock);
278
279         if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {
280
281                 _midi_audition = false;
282
283                 if (_synth_added) {
284                         remove_processor(asynth);
285                         _synth_added = false;
286                 }
287                 midi_region.reset();
288                 _import_position = 0;
289
290                 /* copy it */
291                 the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
292                 the_region->set_position (0);
293
294                 _disk_reader->audio_playlist()->drop_regions ();
295                 _disk_reader->audio_playlist()->add_region (the_region, 0, 1);
296
297                 ProcessorStreams ps;
298                 {
299                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
300
301                         if (configure_processors (&ps)) {
302                                 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
303                                                          region->n_channels()) << endmsg;
304                                 return;
305                         }
306                 }
307
308         } else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
309                 _midi_audition = true;
310
311                 the_region.reset();
312                 _import_position = region->position();
313
314                 /* copy it */
315                 midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
316                 midi_region->set_position (_import_position);
317
318                 _disk_reader->midi_playlist()->drop_regions ();
319                 _disk_reader->midi_playlist()->add_region (midi_region, _import_position, 1);
320                 _disk_reader->reset_tracker();
321
322                 ProcessorStreams ps;
323
324                 if (_synth_changed && _synth_added) {
325                         remove_processor(asynth);
326                         _synth_added = false;
327                 }
328                 if (_synth_changed && !_synth_added) {
329                         _synth_added = false;
330                         lookup_synth();
331                 }
332
333                 if (!_synth_added && asynth) {
334                         int rv = add_processor (asynth, PreFader, &ps, true);
335                         if (rv) {
336                                 error << _("Failed to load synth for MIDI-Audition.") << endmsg;
337                         } else {
338                                 _synth_added = true;
339                         }
340                 } else {
341                         _queue_panic = true;
342                 }
343
344                 {
345                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
346
347                         if (configure_processors (&ps)) {
348                                 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
349                                                          region->n_channels()) << endmsg;
350                                 return;
351                         }
352                 }
353
354         } else {
355                 error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
356                 return;
357         }
358
359         /* force a panner reset now that we have all channels */
360         _main_outs->reset_panner();
361
362         _seek_frame = -1;
363         _seeking = false;
364
365         int dir;
366         framecnt_t offset;
367
368         if (_midi_audition) {
369                 length = midi_region->length();
370                 offset = _import_position + midi_region->sync_offset (dir);
371         } else {
372                 length = the_region->length();
373                 offset = the_region->sync_offset (dir);
374         }
375
376         /* can't audition from a negative sync point */
377
378         if (dir < 0) {
379                 offset = 0;
380         }
381
382         _disk_reader->seek (offset, true);
383         current_frame = offset;
384
385         g_atomic_int_set (&_auditioning, 1);
386 }
387
388 int
389 Auditioner::play_audition (framecnt_t nframes)
390 {
391         bool need_butler = false;
392         framecnt_t this_nframes;
393         int ret;
394
395         if (g_atomic_int_get (&_auditioning) == 0) {
396                 silence (nframes);
397                 return 0;
398         }
399
400 #if 0 // TODO
401         if (_seeking && _seek_complete) {
402                 // set FADE-IN
403         } else if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
404                 // set FADE-OUT -- use/override amp? || use region-gain ?
405         }
406 #endif
407
408         if (_seeking && _seek_complete) {
409                 _seek_complete = false;
410                 _seeking = false;
411                 _seek_frame = -1;
412                 _disk_reader->reset_tracker();
413         }
414
415         if(!_seeking) {
416                 /* process audio */
417                 this_nframes = min (nframes, length - current_frame + _import_position);
418
419                 if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, need_butler)) != 0) {
420                         silence (nframes);
421                         return ret;
422                 }
423
424                 current_frame += this_nframes;
425
426         } else {
427                 silence (nframes);
428         }
429
430         if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
431                 _queue_panic = true;
432                 _seek_complete = false;
433                 _seeking = true;
434                 need_butler = true;
435         }
436
437         if (!_seeking) {
438                 AuditionProgress(current_frame - _import_position, length); /* emit */
439         }
440
441         if (current_frame >= length + _import_position) {
442                 _session.cancel_audition ();
443                 return 0;
444         } else {
445                 return need_butler ? 1 : 0;
446         }
447 }
448
449 void
450 Auditioner::output_changed (IOChange change, void* /*src*/)
451 {
452         if (change.type & IOChange::ConnectionsChanged) {
453                 string phys;
454                 vector<string> connections;
455                 vector<string> outputs;
456                 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
457
458                 if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input ())) {
459                         Config->set_auditioner_output_left ("default");
460                         Config->set_auditioner_output_right ("default");
461                         via_monitor = true;
462                         return;
463                 }
464
465                 if (_output->nth (0)->get_connections (connections)) {
466                         if (outputs.size() > 0) {
467                                 phys = outputs[0];
468                         }
469                         if (phys != connections[0]) {
470                                 Config->set_auditioner_output_left (connections[0]);
471                         } else {
472                                 Config->set_auditioner_output_left ("default");
473                         }
474                 } else {
475                         Config->set_auditioner_output_left ("");
476                 }
477
478                 connections.clear ();
479
480                 if (_output->nth (1)->get_connections (connections)) {
481                         if (outputs.size() > 1) {
482                                 phys = outputs[1];
483                         }
484                         if (phys != connections[0]) {
485                                 Config->set_auditioner_output_right (connections[0]);
486                         } else {
487                                 Config->set_auditioner_output_right ("default");
488                         }
489                 } else {
490                         Config->set_auditioner_output_right ("");
491                 }
492         }
493 }
494
495 ChanCount
496 Auditioner::input_streams () const
497 {
498         /* auditioner never has any inputs - its channel configuration
499            depends solely on the region we are auditioning.
500         */
501
502         if (_disk_reader) {
503                 return _disk_reader->input_streams ();
504         }
505
506         return ChanCount (DataType::AUDIO, 1);
507 }
508
509 MonitorState
510 Auditioner::monitoring_state () const
511 {
512         return MonitoringDisk;
513 }
514