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