Prefer const references for Pin/Channel maps
[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, 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         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_sample, end_sample, nframes, !_session.transport_stopped(), true);
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_sample = -1;
367         _seeking = false;
368
369         int dir;
370         samplecnt_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         if (length == 0) {
381                 error << _("Cannot audition empty file.") << endmsg;
382                 return;
383         }
384
385         /* can't audition from a negative sync point */
386
387         if (dir < 0) {
388                 offset = 0;
389         }
390
391         _disk_reader->seek (offset, true);
392         current_sample = offset;
393
394         g_atomic_int_set (&_auditioning, 1);
395 }
396
397 int
398 Auditioner::play_audition (samplecnt_t nframes)
399 {
400         bool need_butler = false;
401         samplecnt_t this_nframes;
402         int ret;
403
404         if (g_atomic_int_get (&_auditioning) == 0) {
405                 silence (nframes);
406                 return 0;
407         }
408
409 #if 0 // TODO
410         if (_seeking && _seek_complete) {
411                 // set FADE-IN
412         } else if (_seek_sample >= 0 && _seek_sample < length && !_seeking) {
413                 // set FADE-OUT -- use/override amp? || use region-gain ?
414         }
415 #endif
416
417         if (_seeking && _seek_complete) {
418                 _seek_complete = false;
419                 _seeking = false;
420                 _seek_sample = -1;
421                 _disk_reader->reset_tracker();
422         }
423
424         if(!_seeking) {
425                 /* process audio */
426                 this_nframes = min (nframes, length - current_sample + _import_position);
427
428                 if (this_nframes > 0 && 0 != (ret = roll (this_nframes, current_sample, current_sample + this_nframes, need_butler))) {
429                         silence (nframes);
430                         return ret;
431                 }
432
433                 current_sample += this_nframes;
434
435                 if (this_nframes < nframes) {
436                         if (this_nframes > 0) {
437                                 _session.engine().split_cycle (this_nframes);
438                         }
439                         silence (nframes - this_nframes);
440                 }
441
442         } else {
443                 silence (nframes);
444         }
445
446         if (_seek_sample >= 0 && _seek_sample < length && !_seeking) {
447                 _queue_panic = true;
448                 _seek_complete = false;
449                 _seeking = true;
450                 need_butler = true;
451         }
452
453         if (!_seeking) {
454                 AuditionProgress(current_sample - _import_position, length); /* emit */
455         }
456
457         if (current_sample >= length + _import_position) {
458                 _session.cancel_audition ();
459                 return 0;
460         } else {
461                 return need_butler ? 1 : 0;
462         }
463 }
464
465 void
466 Auditioner::output_changed (IOChange change, void* /*src*/)
467 {
468         if (change.type & IOChange::ConnectionsChanged) {
469                 string phys;
470                 vector<string> connections;
471                 vector<string> outputs;
472                 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
473
474                 if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input ())) {
475                         Config->set_auditioner_output_left ("default");
476                         Config->set_auditioner_output_right ("default");
477                         via_monitor = true;
478                         return;
479                 }
480
481                 if (_output->nth (0)->get_connections (connections)) {
482                         if (outputs.size() > 0) {
483                                 phys = outputs[0];
484                         }
485                         if (phys != connections[0]) {
486                                 Config->set_auditioner_output_left (connections[0]);
487                         } else {
488                                 Config->set_auditioner_output_left ("default");
489                         }
490                 } else {
491                         Config->set_auditioner_output_left ("");
492                 }
493
494                 connections.clear ();
495
496                 if (_output->nth (1)->get_connections (connections)) {
497                         if (outputs.size() > 1) {
498                                 phys = outputs[1];
499                         }
500                         if (phys != connections[0]) {
501                                 Config->set_auditioner_output_right (connections[0]);
502                         } else {
503                                 Config->set_auditioner_output_right ("default");
504                         }
505                 } else {
506                         Config->set_auditioner_output_right ("");
507                 }
508         }
509 }
510
511 ChanCount
512 Auditioner::input_streams () const
513 {
514         /* auditioner never has any inputs - its channel configuration
515            depends solely on the region we are auditioning.
516         */
517
518         if (_midi_audition) {
519                 return ChanCount (DataType::MIDI, 1);
520         } else {
521                 if (the_region) {
522                         return ChanCount (DataType::AUDIO, the_region->n_channels ());
523                 }
524         }
525
526         return ChanCount (DataType::AUDIO, 1);
527 }
528
529 MonitorState
530 Auditioner::monitoring_state () const
531 {
532         return MonitoringDisk;
533 }
534