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