Only show user-presets in favorite sidebar
[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_insert.h"
36 #include "ardour/plugin_manager.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         , _queue_panic (false)
59         , _import_position (0)
60 {
61 }
62
63 int
64 Auditioner::init ()
65 {
66         if (Track::init ()) {
67                 return -1;
68         }
69
70         if (connect ()) {
71                 return -1;
72         }
73
74         _output->add_port ("", this, DataType::MIDI);
75         use_new_playlist (DataType::MIDI);
76
77         lookup_synth (false);
78
79         _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
80
81         return 0;
82 }
83
84 Auditioner::~Auditioner ()
85 {
86         if (asynth) {
87                 asynth->drop_references ();
88         }
89         asynth.reset ();
90 }
91
92 PluginInfoPtr
93 Auditioner::lookup_synth_plugin_info (std::string const& uri) const
94 {
95         PluginManager& mgr (PluginManager::instance());
96         PluginInfoList plugs;
97 #ifdef LV2_SUPPORT
98         plugs = mgr.lv2_plugin_info();
99 #endif
100         for (PluginInfoList::const_iterator i = plugs.begin (); i != plugs.end (); ++i) {
101                 if (uri == (*i)->unique_id){
102                         return (*i);
103                 }
104         }
105         return PluginInfoPtr ();
106 }
107
108 void
109 Auditioner::lookup_synth (bool and_load)
110 {
111         string plugin_id = Config->get_midi_audition_synth_uri();
112         if (plugin_id.empty() && plugin_id != X_("@default@")) {
113                 return;
114         }
115         PluginInfoPtr nfo;
116
117         nfo = lookup_synth_plugin_info (plugin_id);
118         if (!nfo) {
119                 nfo = lookup_synth_plugin_info ("http://gareus.org/oss/lv2/gmsynth");
120         }
121         if (!nfo) {
122                 nfo = lookup_synth_plugin_info ("https://community.ardour.org/node/7596");
123                 if (nfo) {
124                         warning << _("Falling back to Reasonable Synth for Midi Audition") << endmsg;
125                 }
126         }
127         if (!nfo) {
128                 warning << _("No synth for midi-audition found.") << endmsg;
129                 Config->set_midi_audition_synth_uri(""); // Don't check again for Reasonable Synth
130                 return;
131         }
132
133         if (plugin_id == X_("@default@")) {
134                 Config->set_midi_audition_synth_uri (nfo->unique_id);
135         }
136         if (and_load) {
137                 assert (!asynth);
138                 boost::shared_ptr<Plugin> p = nfo->load (_session);
139                 asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
140         }
141 }
142
143 void
144 Auditioner::unload_synth (bool need_lock)
145 {
146         if (!asynth) {
147                 return;
148         }
149         if (0 == remove_processor (asynth, NULL, need_lock)) {
150                 asynth.reset ();
151         }
152 }
153
154 int
155 Auditioner::connect ()
156 {
157         string left = Config->get_auditioner_output_left();
158         string right = Config->get_auditioner_output_right();
159
160         vector<string> outputs;
161         _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
162
163         via_monitor = false;
164
165         if (left.empty() || left == "default") {
166                 if (_session.monitor_out() && _session.monitor_out()->input()->audio (0)) {
167                         left = _session.monitor_out()->input()->audio (0)->name();
168                 } else {
169                         if (outputs.size() > 0) {
170                                 left = outputs[0];
171                         }
172                 }
173         }
174
175         if (right.empty() || right == "default") {
176                 if (_session.monitor_out() && _session.monitor_out()->input()->audio (1)) {
177                         right = _session.monitor_out()->input()->audio (1)->name();
178                 } else {
179                         if (outputs.size() > 1) {
180                                 right = outputs[1];
181                         }
182                 }
183         }
184
185         _output->disconnect (this);
186
187         if (left.empty() && right.empty()) {
188                 if (_output->n_ports().n_audio() == 0) {
189                         /* ports not set up, so must be during startup */
190                         warning << _("no outputs available for auditioner - manual connection required") << endmsg;
191                 }
192         } else {
193
194                 if (_output->n_ports().n_audio() == 0) {
195
196                         /* create (and connect) new ports */
197
198                         _main_outs->defer_pan_reset ();
199
200                         if (left.length()) {
201                                 _output->add_port (left, this, DataType::AUDIO);
202                         }
203
204                         if (right.length()) {
205                                 _output->add_port (right, this, DataType::AUDIO);
206                         }
207
208                         _main_outs->allow_pan_reset ();
209                         _main_outs->reset_panner ();
210
211                 } else {
212
213                         /* reconnect existing ports */
214
215                         boost::shared_ptr<Port> oleft (_output->nth (0));
216                         boost::shared_ptr<Port> oright (_output->nth (1));
217                         if (oleft) {
218                                 oleft->connect (left);
219                         }
220                         if (oright) {
221                                 oright->connect (right);
222                         }
223                 }
224
225         }
226
227         if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input())) {
228                 via_monitor = true;
229         }
230
231         return 0;
232 }
233
234 DataType
235 Auditioner::data_type () const {
236         if (_midi_audition) {
237                 return DataType::MIDI;
238         } else {
239                 return DataType::AUDIO;
240         }
241 }
242
243 int
244 Auditioner::roll (pframes_t nframes, samplepos_t start_sample, samplepos_t end_sample, bool& need_butler)
245 {
246         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
247         if (!lm.locked()) {
248                 return 0;
249         }
250
251         assert(_active);
252
253         BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
254
255         if (_queue_panic) {
256                 MidiBuffer& mbuf (bufs.get_midi (0));
257                 _queue_panic = false;
258                 for (uint8_t chn = 0; chn < 0xf; ++chn) {
259                         uint8_t buf[3] = { ((uint8_t) (MIDI_CMD_CONTROL | chn)), ((uint8_t) MIDI_CTL_SUSTAIN), 0 };
260                         mbuf.push_back(0, 3, buf);
261                         buf[1] = MIDI_CTL_ALL_NOTES_OFF;
262                         mbuf.push_back(0, 3, buf);
263                         buf[1] = MIDI_CTL_RESET_CONTROLLERS;
264                         mbuf.push_back(0, 3, buf);
265                 }
266         }
267
268         process_output_buffers (bufs, start_sample, end_sample, nframes, !_session.transport_stopped(), true);
269
270         /* note: auditioner never writes to disk, so we don't care about the
271          * disk writer status (it's buffers will always have no data in them).
272          */
273
274         if (_disk_reader->need_butler()) {
275                 need_butler = true;
276         }
277
278         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
279                 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
280                 if (d) {
281                         d->flush_buffers (nframes);
282                 }
283         }
284
285         return 0;
286 }
287
288 void
289 Auditioner::audition_region (boost::shared_ptr<Region> region)
290 {
291         if (g_atomic_int_get (&_auditioning)) {
292                 /* don't go via session for this, because we are going
293                    to remain active.
294                 */
295                 cancel_audition ();
296         }
297
298         Glib::Threads::Mutex::Lock lm (lock);
299
300         if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {
301
302                 _midi_audition = false;
303
304                 unload_synth (true);
305
306                 midi_region.reset();
307                 _import_position = 0;
308
309                 /* copy it */
310                 the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
311                 the_region->set_position (0);
312
313                 _disk_reader->midi_playlist()->drop_regions ();
314
315                 _disk_reader->audio_playlist()->drop_regions ();
316                 _disk_reader->audio_playlist()->add_region (the_region, 0, 1);
317
318                 ProcessorStreams ps;
319                 {
320                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
321
322                         if (configure_processors (&ps)) {
323                                 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
324                                                          region->n_channels()) << endmsg;
325                                 return;
326                         }
327                 }
328
329         } else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
330                 _midi_audition = true;
331
332                 the_region.reset();
333                 _import_position = region->position();
334
335                 /* copy it */
336                 midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
337                 midi_region->set_position (_import_position);
338
339                 _disk_reader->audio_playlist()->drop_regions();
340
341                 _disk_reader->midi_playlist()->drop_regions ();
342                 _disk_reader->midi_playlist()->add_region (midi_region, _import_position, 1);
343                 _disk_reader->reset_tracker();
344
345                 ProcessorStreams ps;
346
347                 unload_synth (true);
348                 lookup_synth (true);
349
350                 if (asynth) {
351                         int rv = add_processor (asynth, PreFader, &ps, true);
352                         if (rv) {
353                                 error << _("Failed to load synth for MIDI-Audition.") << endmsg;
354                         }
355                 }
356
357                 {
358                         Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
359
360                         if (configure_processors (&ps)) {
361                                 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
362                                                          region->n_channels()) << endmsg;
363                                 unload_synth (true);
364                                 return;
365                         }
366                 }
367
368         } else {
369                 error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
370                 return;
371         }
372
373         /* force a panner reset now that we have all channels */
374         _main_outs->reset_panner();
375
376         _seek_sample = -1;
377         _seeking = false;
378
379         int dir;
380         samplecnt_t offset;
381
382         if (_midi_audition) {
383                 length = midi_region->length();
384                 offset = _import_position + midi_region->sync_offset (dir);
385         } else {
386                 length = the_region->length();
387                 offset = the_region->sync_offset (dir);
388         }
389
390         if (length == 0) {
391                 error << _("Cannot audition empty file.") << endmsg;
392                 unload_synth (true);
393                 return;
394         }
395
396         /* can't audition from a negative sync point */
397
398         if (dir < 0) {
399                 offset = 0;
400         }
401
402         _disk_reader->seek (offset, true);
403         current_sample = offset;
404
405         g_atomic_int_set (&_auditioning, 1);
406 }
407
408 int
409 Auditioner::play_audition (samplecnt_t nframes)
410 {
411         bool need_butler = false;
412         samplecnt_t this_nframes;
413         int ret;
414
415         if (g_atomic_int_get (&_auditioning) == 0) {
416                 silence (nframes);
417                 unload_synth (false);
418                 return 0;
419         }
420
421 #if 0 // TODO
422         if (_seeking && _seek_complete) {
423                 // set FADE-IN
424         } else if (_seek_sample >= 0 && _seek_sample < length && !_seeking) {
425                 // set FADE-OUT -- use/override amp? || use region-gain ?
426         }
427 #endif
428
429         if (_seeking && _seek_complete) {
430                 _seek_complete = false;
431                 _seeking = false;
432                 _seek_sample = -1;
433                 _disk_reader->reset_tracker();
434         }
435
436         if(!_seeking) {
437                 /* process audio */
438                 this_nframes = min (nframes, length - current_sample + _import_position);
439
440                 if (this_nframes > 0 && 0 != (ret = roll (this_nframes, current_sample, current_sample + this_nframes, need_butler))) {
441                         silence (nframes);
442                         return ret;
443                 }
444
445                 current_sample += this_nframes;
446
447                 if (this_nframes < nframes) {
448                         if (this_nframes > 0) {
449                                 _session.engine().split_cycle (this_nframes);
450                         }
451                         silence (nframes - this_nframes);
452                 }
453
454         } else {
455                 silence (nframes);
456         }
457
458         if (_seek_sample >= 0 && _seek_sample < length && !_seeking) {
459                 _queue_panic = true;
460                 _seek_complete = false;
461                 _seeking = true;
462                 need_butler = true;
463         }
464
465         if (!_seeking) {
466                 AuditionProgress(current_sample - _import_position, length); /* emit */
467         }
468
469         if (current_sample >= length + _import_position) {
470                 _session.cancel_audition ();
471                 unload_synth (false);
472                 return 0;
473         } else {
474                 return need_butler ? 1 : 0;
475         }
476 }
477
478 void
479 Auditioner::cancel_audition () {
480         g_atomic_int_set (&_auditioning, 0);
481 }
482
483 bool
484 Auditioner::auditioning() const {
485         return g_atomic_int_get (&_auditioning);
486 }
487
488 void
489 Auditioner::seek_to_sample (sampleoffset_t pos) {
490         if (_seek_sample < 0 && !_seeking) {
491                 _seek_sample = pos;
492         }
493 }
494
495 void
496 Auditioner::seek_to_percent (float const pos) {
497         if (_seek_sample < 0 && !_seeking) {
498                 _seek_sample = floorf(length * pos / 100.0);
499         }
500 }
501
502 void
503 Auditioner::seek_response (sampleoffset_t pos) {
504         /* called from the butler thread */
505         _seek_complete = true;
506         if (_seeking) {
507                 current_sample = pos;
508                 _seek_complete = true;
509         }
510 }
511
512
513 void
514 Auditioner::output_changed (IOChange change, void* /*src*/)
515 {
516         if (change.type & IOChange::ConnectionsChanged) {
517                 string phys;
518                 vector<string> connections;
519                 vector<string> outputs;
520                 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
521
522                 if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input ())) {
523                         Config->set_auditioner_output_left ("default");
524                         Config->set_auditioner_output_right ("default");
525                         via_monitor = true;
526                         return;
527                 }
528
529                 if (_output->nth (0)->get_connections (connections)) {
530                         if (outputs.size() > 0) {
531                                 phys = outputs[0];
532                         }
533                         if (phys != connections[0]) {
534                                 Config->set_auditioner_output_left (connections[0]);
535                         } else {
536                                 Config->set_auditioner_output_left ("default");
537                         }
538                 } else {
539                         Config->set_auditioner_output_left ("");
540                 }
541
542                 connections.clear ();
543
544                 if (_output->nth (1)->get_connections (connections)) {
545                         if (outputs.size() > 1) {
546                                 phys = outputs[1];
547                         }
548                         if (phys != connections[0]) {
549                                 Config->set_auditioner_output_right (connections[0]);
550                         } else {
551                                 Config->set_auditioner_output_right ("default");
552                         }
553                 } else {
554                         Config->set_auditioner_output_right ("");
555                 }
556         }
557 }
558
559 ChanCount
560 Auditioner::input_streams () const
561 {
562         /* auditioner never has any inputs - its channel configuration
563            depends solely on the region we are auditioning.
564         */
565
566         if (_midi_audition) {
567                 return ChanCount (DataType::MIDI, 1);
568         } else {
569                 if (the_region) {
570                         return ChanCount (DataType::AUDIO, the_region->n_channels ());
571                 }
572         }
573
574         return ChanCount (DataType::AUDIO, 1);
575 }
576
577 MonitorState
578 Auditioner::monitoring_state () const
579 {
580         return MonitoringDisk;
581 }
582