2 Copyright (C) 2001 Paul Davis
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.
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.
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.
20 #include <glibmm/threads.h>
22 #include "pbd/error.h"
24 #include "ardour/amp.h"
25 #include "ardour/audio_diskstream.h"
26 #include "ardour/audio_port.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/audioplaylist.h"
29 #include "ardour/audioregion.h"
30 #include "ardour/auditioner.h"
31 #include "ardour/data_type.h"
32 #include "ardour/delivery.h"
33 #include "ardour/midi_diskstream.h"
34 #include "ardour/midi_region.h"
35 #include "ardour/plugin.h"
36 #include "ardour/plugin_insert.h"
37 #include "ardour/region_factory.h"
38 #include "ardour/route.h"
39 #include "ardour/session.h"
42 using namespace ARDOUR;
47 Auditioner::Auditioner (Session& s)
48 : Track (s, "auditioner", Route::Auditioner)
54 , _seek_complete (false)
56 , _midi_audition (false)
57 , _synth_added (false)
58 , _synth_changed (false)
59 , _queue_panic (false)
60 , _import_position (0)
75 _output->add_port ("", this, DataType::MIDI);
79 _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
80 Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Auditioner::config_changed, this, _1));
85 Auditioner::~Auditioner ()
90 Auditioner::lookup_synth ()
92 string plugin_id = Config->get_midi_audition_synth_uri();
93 asynth = boost::shared_ptr<Processor>();
94 if (!plugin_id.empty()) {
95 boost::shared_ptr<Plugin> p;
96 p = find_plugin (_session, plugin_id, ARDOUR::LV2);
98 p = find_plugin (_session, "https://community.ardour.org/node/7596", ARDOUR::LV2);
100 warning << _("Falling back to Reasonable Synth for Midi Audition") << endmsg;
102 warning << _("No synth for midi-audition found.") << endmsg;
103 Config->set_midi_audition_synth_uri(""); // Don't check again for Reasonable Synth (ie --no-lv2)
107 asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
113 Auditioner::config_changed (std::string p)
115 if (p == "midi-audition-synth-uri") {
116 _synth_changed = true;
121 Auditioner::connect ()
123 string left = Config->get_auditioner_output_left();
124 string right = Config->get_auditioner_output_right();
126 vector<string> outputs;
127 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
131 if (left.empty() || left == "default") {
132 if (_session.monitor_out()) {
133 left = _session.monitor_out()->input()->audio (0)->name();
136 if (outputs.size() > 0) {
142 if (right.empty() || right == "default") {
143 if (_session.monitor_out()) {
144 right = _session.monitor_out()->input()->audio (1)->name();
147 if (outputs.size() > 1) {
153 _output->disconnect (this);
155 if (left.empty() && right.empty()) {
156 if (_output->n_ports().n_audio() == 0) {
157 /* ports not set up, so must be during startup */
158 warning << _("no outputs available for auditioner - manual connection required") << endmsg;
162 if (_output->n_ports().n_audio() == 0) {
164 /* create (and connect) new ports */
166 _main_outs->defer_pan_reset ();
169 _output->add_port (left, this, DataType::AUDIO);
172 if (right.length()) {
173 _output->add_port (right, this, DataType::AUDIO);
176 _main_outs->allow_pan_reset ();
177 _main_outs->reset_panner ();
181 /* reconnect existing ports */
183 boost::shared_ptr<Port> oleft (_output->nth (0));
184 boost::shared_ptr<Port> oright (_output->nth (1));
186 oleft->connect (left);
189 oright->connect (right);
200 Auditioner::data_type () const {
201 if (_midi_audition) {
202 return DataType::MIDI;
204 return DataType::AUDIO;
208 boost::shared_ptr<Diskstream>
209 Auditioner::create_diskstream () {
212 AudioDiskstream::Flag dflags = AudioDiskstream::Flag (0);
213 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Hidden);
214 _diskstream_audio = boost::shared_ptr<AudioDiskstream> (new AudioDiskstream (_session, name(), dflags));
218 MidiDiskstream::Flag dflags = MidiDiskstream::Flag (0);
219 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Hidden);
220 _diskstream_midi = boost::shared_ptr<Diskstream> (new MidiDiskstream (_session, name(), dflags));
221 _diskstream_midi->do_refill_with_alloc ();
222 _diskstream_midi->playlist()->set_orig_track_id (id());
225 return _diskstream_audio;
229 Auditioner::roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler) {
230 if (_midi_audition) {
231 return roll_midi(nframes, start_frame, end_frame, declick, need_butler);
233 return roll_audio(nframes, start_frame, end_frame, declick, need_butler);
238 Auditioner::roll_midi (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
240 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
247 framecnt_t playback_distance = nframes;
248 boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
249 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
250 MidiBuffer& mbuf (bufs.get_midi (0));
253 ChanCount cnt (DataType::MIDI, 1);
254 cnt.set (DataType::AUDIO, bufs.count().n_audio());
255 bufs.set_count (cnt);
258 _queue_panic = false;
259 for (uint8_t chn = 0; chn < 0xf; ++chn) {
260 uint8_t buf[3] = { ((uint8_t) (MIDI_CMD_CONTROL | chn)), ((uint8_t) MIDI_CTL_SUSTAIN), 0 };
261 mbuf.push_back(0, 3, buf);
262 buf[1] = MIDI_CTL_ALL_NOTES_OFF;
263 mbuf.push_back(0, 3, buf);
264 buf[1] = MIDI_CTL_RESET_CONTROLLERS;
265 mbuf.push_back(0, 3, buf);
267 process_output_buffers (bufs, start_frame, start_frame+1, 1, false, false);
269 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
270 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
272 d->flush_buffers (nframes);
277 diskstream->get_playback (mbuf, nframes);
279 process_output_buffers (bufs, start_frame, end_frame, nframes,
280 declick, (!diskstream->record_enabled() && !_session.transport_stopped()));
282 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
283 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
285 d->flush_buffers (nframes);
289 need_butler = diskstream->commit (playback_distance);
295 Auditioner::roll_audio (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler) {
296 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
301 assert(n_outputs().n_total() > 0);
305 framecnt_t playback_distance;
306 framepos_t transport_frame = _session.transport_frame();
307 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
308 BufferSet& bufs = _session.get_route_buffers (n_process_buffers ());
311 _amp->apply_gain_automation(false);
313 if ((dret = diskstream->process (bufs, transport_frame, nframes, playback_distance, (monitoring_state() == MonitoringDisk))) != 0) {
314 need_butler = diskstream->commit (playback_distance);
319 process_output_buffers (bufs, start_frame, end_frame, nframes, declick, (!diskstream->record_enabled() && _session.transport_rolling()));
320 need_butler = diskstream->commit (playback_distance);
325 Auditioner::set_diskstream (boost::shared_ptr<Diskstream> ds)
327 Track::set_diskstream (ds);
329 _diskstream->set_track (this);
330 _diskstream->set_destructive (_mode == Destructive);
331 _diskstream->set_non_layered (_mode == NonLayered);
332 _diskstream->set_record_enabled (false);
333 _diskstream->request_input_monitoring (false);
335 DiskstreamChanged (); /* EMIT SIGNAL */
339 Auditioner::prepare_playlist ()
341 // used by CrossfadeEditor::audition()
343 _midi_audition = false;
344 set_diskstream(_diskstream_audio);
346 remove_processor(asynth);
347 _synth_added = false;
350 // FIXME auditioner is still audio-only
351 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(_diskstream->playlist());
359 Auditioner::audition_region (boost::shared_ptr<Region> region)
361 if (g_atomic_int_get (&_auditioning)) {
362 /* don't go via session for this, because we are going
368 Glib::Threads::Mutex::Lock lm (lock);
370 if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {
372 _midi_audition = false;
373 set_diskstream(_diskstream_audio);
375 remove_processor(asynth);
376 _synth_added = false;
379 _import_position = 0;
382 the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
383 the_region->set_position (0);
385 _diskstream->playlist()->drop_regions ();
386 _diskstream->playlist()->add_region (the_region, 0, 1);
388 if (_diskstream->n_channels().n_audio() < the_region->n_channels()) {
389 audio_diskstream()->add_channel (the_region->n_channels() - _diskstream->n_channels().n_audio());
390 } else if (_diskstream->n_channels().n_audio() > the_region->n_channels()) {
391 audio_diskstream()->remove_channel (_diskstream->n_channels().n_audio() - the_region->n_channels());
396 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
398 if (configure_processors (&ps)) {
399 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
400 _diskstream->n_channels()) << endmsg;
405 } else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
406 _midi_audition = true;
407 set_diskstream(_diskstream_midi);
409 _import_position = region->position();
412 midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
413 midi_region->set_position (_import_position);
415 _diskstream->playlist()->drop_regions ();
416 _diskstream->playlist()->add_region (midi_region, _import_position, 1);
417 midi_diskstream()->reset_tracker();
421 if (_synth_changed && _synth_added) {
422 remove_processor(asynth);
423 _synth_added = false;
425 if (_synth_changed && !_synth_added) {
426 _synth_added = false;
431 if (!_synth_added && asynth) {
432 int rv = add_processor (asynth, PreFader, &ps, true);
434 error << _("Failed to load synth for MIDI-Audition.") << endmsg;
443 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
445 if (configure_processors (&ps)) {
446 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
447 _diskstream->n_channels()) << endmsg;
453 error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
457 /* force a panner reset now that we have all channels */
458 _main_outs->reset_panner();
466 if (_midi_audition) {
467 length = midi_region->length();
468 offset = _import_position + midi_region->sync_offset (dir);
470 length = the_region->length();
471 offset = the_region->sync_offset (dir);
474 /* can't audition from a negative sync point */
480 _diskstream->seek (offset);
481 current_frame = offset;
483 g_atomic_int_set (&_auditioning, 1);
487 Auditioner::play_audition (framecnt_t nframes)
489 bool need_butler = false;
490 framecnt_t this_nframes;
493 if (g_atomic_int_get (&_auditioning) == 0) {
499 if (_seeking && _seek_complete) {
501 } else if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
502 // set FADE-OUT -- use/override amp? || use region-gain ?
506 if (_seeking && _seek_complete) {
507 _seek_complete = false;
510 if (_midi_audition && midi_diskstream()) {
511 midi_diskstream()->reset_tracker();
517 this_nframes = min (nframes, length - current_frame + _import_position);
519 if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, need_butler)) != 0) {
524 current_frame += this_nframes;
530 if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
532 _seek_complete = false;
538 AuditionProgress(current_frame - _import_position, length); /* emit */
541 if (current_frame >= length + _import_position) {
542 _session.cancel_audition ();
545 return need_butler ? 1 : 0;
550 Auditioner::output_changed (IOChange change, void* /*src*/)
552 if (change.type & IOChange::ConnectionsChanged) {
554 vector<string> connections;
555 vector<string> outputs;
556 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
557 if (_output->nth (0)->get_connections (connections)) {
558 if (outputs.size() > 0) {
561 if (phys != connections[0]) {
562 Config->set_auditioner_output_left (connections[0]);
564 Config->set_auditioner_output_left ("default");
567 Config->set_auditioner_output_left ("");
570 connections.clear ();
572 if (_output->nth (1)->get_connections (connections)) {
573 if (outputs.size() > 1) {
576 if (phys != connections[0]) {
577 Config->set_auditioner_output_right (connections[0]);
579 Config->set_auditioner_output_right ("default");
582 Config->set_auditioner_output_right ("");
588 Auditioner::input_streams () const
590 /* auditioner never has any inputs - its channel configuration
591 depends solely on the region we are auditioning.
594 if (!_midi_audition && audio_diskstream()) {
595 return audio_diskstream()->n_channels();
597 if (_midi_audition && midi_diskstream()) {
598 ChanCount cnt (DataType::MIDI, 1);
606 Auditioner::monitoring_state () const
608 return MonitoringDisk;
611 boost::shared_ptr<AudioDiskstream>
612 Auditioner::audio_diskstream() const
614 return boost::dynamic_pointer_cast<AudioDiskstream> (_diskstream);
617 boost::shared_ptr<MidiDiskstream>
618 Auditioner::midi_diskstream() const
620 return boost::dynamic_pointer_cast<MidiDiskstream> (_diskstream);