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