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/audioregion.h"
26 #include "ardour/audioengine.h"
27 #include "ardour/audioplaylist.h"
28 #include "ardour/auditioner.h"
29 #include "ardour/audio_port.h"
30 #include "ardour/data_type.h"
31 #include "ardour/delivery.h"
32 #include "ardour/plugin.h"
33 #include "ardour/region_factory.h"
34 #include "ardour/route.h"
35 #include "ardour/session.h"
38 using namespace ARDOUR;
43 Auditioner::Auditioner (Session& s)
44 : Track (s, "auditioner", Route::Auditioner)
50 , _seek_complete (false)
52 , _midi_audition (false)
53 , _synth_added (false)
54 , _synth_changed (false)
55 , _queue_panic (false)
56 , _import_position (0)
71 _output->add_port ("Midiaudition", this, DataType::MIDI);
75 _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
76 Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Auditioner::config_changed, this, _1));
81 Auditioner::~Auditioner ()
86 Auditioner::lookup_synth ()
88 string plugin_id = Config->get_midi_audition_synth_uri();
89 asynth = boost::shared_ptr<Processor>();
90 if (!plugin_id.empty()) {
91 boost::shared_ptr<Plugin> p;
92 p = find_plugin (_session, plugin_id, ARDOUR::LV2);
94 p = find_plugin (_session, "https://community.ardour.org/node/7596", ARDOUR::LV2);
96 warning << _("Falling back to Reasonable Synth for Midi Audition") << endmsg;
98 warning << _("No synth for midi-audition found.") << endmsg;
102 asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
108 Auditioner::config_changed (std::string p)
110 if (p == "midi-audition-synth-uri") {
111 _synth_changed = true;
116 Auditioner::connect ()
118 string left = Config->get_auditioner_output_left();
119 string right = Config->get_auditioner_output_right();
121 vector<string> outputs;
122 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
126 if (left.empty() || left == "default") {
127 if (_session.monitor_out()) {
128 left = _session.monitor_out()->input()->audio (0)->name();
131 if (outputs.size() > 0) {
137 if (right.empty() || right == "default") {
138 if (_session.monitor_out()) {
139 right = _session.monitor_out()->input()->audio (1)->name();
142 if (outputs.size() > 1) {
148 _output->disconnect (this);
150 if (left.empty() && right.empty()) {
151 if (_output->n_ports().n_audio() == 0) {
152 /* ports not set up, so must be during startup */
153 warning << _("no outputs available for auditioner - manual connection required") << endmsg;
157 if (_output->n_ports().n_audio() == 0) {
159 /* create (and connect) new ports */
161 _main_outs->defer_pan_reset ();
164 _output->add_port (left, this, DataType::AUDIO);
167 if (right.length()) {
168 _output->add_port (right, this, DataType::AUDIO);
171 _main_outs->allow_pan_reset ();
172 _main_outs->reset_panner ();
176 /* reconnect existing ports */
178 boost::shared_ptr<Port> oleft (_output->nth (0));
179 boost::shared_ptr<Port> oright (_output->nth (1));
181 oleft->connect (left);
184 oright->connect (right);
195 Auditioner::data_type () const {
196 if (_midi_audition) {
197 return DataType::MIDI;
199 return DataType::AUDIO;
203 boost::shared_ptr<Diskstream>
204 Auditioner::create_diskstream () {
207 AudioDiskstream::Flag dflags = AudioDiskstream::Flag (0);
208 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Hidden);
209 _diskstream_audio = boost::shared_ptr<AudioDiskstream> (new AudioDiskstream (_session, name(), dflags));
213 MidiDiskstream::Flag dflags = MidiDiskstream::Flag (0);
214 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Hidden);
215 _diskstream_midi = boost::shared_ptr<Diskstream> (new MidiDiskstream (_session, name(), dflags));
216 _diskstream_midi->do_refill_with_alloc ();
217 _diskstream_midi->playlist()->set_orig_track_id (id());
220 return _diskstream_audio;
224 Auditioner::roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler) {
225 if (_midi_audition) {
226 return roll_midi(nframes, start_frame, end_frame, declick, need_butler);
228 return roll_audio(nframes, start_frame, end_frame, declick, need_butler);
233 Auditioner::roll_midi (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
235 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
242 framecnt_t playback_distance = nframes;
243 boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
244 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
245 MidiBuffer& mbuf (bufs.get_midi (0));
248 ChanCount cnt (DataType::MIDI, 1);
249 cnt.set (DataType::AUDIO, bufs.count().n_audio());
250 bufs.set_count (cnt);
253 _queue_panic = false;
254 for (uint8_t chn = 0; chn < 0xf; ++chn) {
255 uint8_t buf[3] = { ((uint8_t) (MIDI_CMD_CONTROL | chn)), ((uint8_t) MIDI_CTL_SUSTAIN), 0 };
256 mbuf.push_back(0, 3, buf);
257 buf[1] = MIDI_CTL_ALL_NOTES_OFF;
258 mbuf.push_back(0, 3, buf);
259 buf[1] = MIDI_CTL_RESET_CONTROLLERS;
260 mbuf.push_back(0, 3, buf);
262 process_output_buffers (bufs, start_frame, start_frame+1, 1, false, false);
264 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
265 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
267 d->flush_buffers (nframes);
272 diskstream->get_playback (mbuf, nframes);
274 process_output_buffers (bufs, start_frame, end_frame, nframes,
275 declick, (!diskstream->record_enabled() && !_session.transport_stopped()));
277 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
278 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
280 d->flush_buffers (nframes);
284 need_butler = diskstream->commit (playback_distance);
290 Auditioner::roll_audio (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler) {
291 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
296 assert(n_outputs().n_total() > 0);
300 framecnt_t playback_distance;
301 framepos_t transport_frame = _session.transport_frame();
302 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
303 BufferSet& bufs = _session.get_route_buffers (n_process_buffers ());
306 _amp->apply_gain_automation(false);
308 if ((dret = diskstream->process (bufs, transport_frame, nframes, playback_distance, (monitoring_state() == MonitoringDisk))) != 0) {
309 need_butler = diskstream->commit (playback_distance);
314 process_output_buffers (bufs, start_frame, end_frame, nframes, declick, (!diskstream->record_enabled() && _session.transport_rolling()));
315 need_butler = diskstream->commit (playback_distance);
320 Auditioner::set_diskstream (boost::shared_ptr<Diskstream> ds)
322 Track::set_diskstream (ds);
324 _diskstream->set_track (this);
325 _diskstream->set_destructive (_mode == Destructive);
326 _diskstream->set_non_layered (_mode == NonLayered);
327 _diskstream->set_record_enabled (false);
328 _diskstream->request_input_monitoring (false);
330 DiskstreamChanged (); /* EMIT SIGNAL */
334 Auditioner::prepare_playlist ()
336 // used by CrossfadeEditor::audition()
338 _midi_audition = false;
339 set_diskstream(_diskstream_audio);
341 remove_processor(asynth);
342 _synth_added = false;
345 // FIXME auditioner is still audio-only
346 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(_diskstream->playlist());
354 Auditioner::audition_region (boost::shared_ptr<Region> region)
356 if (g_atomic_int_get (&_auditioning)) {
357 /* don't go via session for this, because we are going
363 Glib::Threads::Mutex::Lock lm (lock);
365 if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {
367 _midi_audition = false;
368 set_diskstream(_diskstream_audio);
370 remove_processor(asynth);
371 _synth_added = false;
374 _import_position = 0;
377 the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
378 the_region->set_position (0);
380 _diskstream->playlist()->drop_regions ();
381 _diskstream->playlist()->add_region (the_region, 0, 1);
383 if (_diskstream->n_channels().n_audio() < the_region->n_channels()) {
384 audio_diskstream()->add_channel (the_region->n_channels() - _diskstream->n_channels().n_audio());
385 } else if (_diskstream->n_channels().n_audio() > the_region->n_channels()) {
386 audio_diskstream()->remove_channel (_diskstream->n_channels().n_audio() - the_region->n_channels());
391 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
393 if (configure_processors (&ps)) {
394 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
395 _diskstream->n_channels()) << endmsg;
400 } else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
401 _midi_audition = true;
402 set_diskstream(_diskstream_midi);
404 _import_position = region->position();
407 midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
408 midi_region->set_position (_import_position);
410 _diskstream->playlist()->drop_regions ();
411 _diskstream->playlist()->add_region (midi_region, _import_position, 1);
412 midi_diskstream()->reset_tracker();
416 if (_synth_changed && _synth_added) {
417 remove_processor(asynth);
418 _synth_added = false;
420 if (_synth_changed && !_synth_added) {
421 _synth_added = false;
426 if (!_synth_added && asynth) {
427 int rv = add_processor_by_index(asynth, PreFader, &ps, true);
429 error << _("Failed to load synth for MIDI-Audition.") << endmsg;
438 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
440 if (configure_processors (&ps)) {
441 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
442 _diskstream->n_channels()) << endmsg;
448 error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
452 /* force a panner reset now that we have all channels */
453 _main_outs->reset_panner();
461 if (_midi_audition) {
462 length = midi_region->length();
463 offset = _import_position + midi_region->sync_offset (dir);
465 length = the_region->length();
466 offset = the_region->sync_offset (dir);
469 /* can't audition from a negative sync point */
475 _diskstream->seek (offset);
476 current_frame = offset;
478 g_atomic_int_set (&_auditioning, 1);
482 Auditioner::play_audition (framecnt_t nframes)
484 bool need_butler = false;
485 framecnt_t this_nframes;
488 if (g_atomic_int_get (&_auditioning) == 0) {
494 if (_seeking && _seek_complete) {
496 } else if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
497 // set FADE-OUT -- use/override amp? || use region-gain ?
501 if (_seeking && _seek_complete) {
502 _seek_complete = false;
505 if (_midi_audition && midi_diskstream()) {
506 midi_diskstream()->reset_tracker();
512 this_nframes = min (nframes, length - current_frame);
514 if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, need_butler)) != 0) {
519 current_frame += this_nframes;
525 if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
527 _seek_complete = false;
533 AuditionProgress(current_frame - _import_position, length); /* emit */
536 if (current_frame >= length) {
537 _session.cancel_audition ();
540 return need_butler ? 1 : 0;
545 Auditioner::output_changed (IOChange change, void* /*src*/)
547 if (change.type & IOChange::ConnectionsChanged) {
549 vector<string> connections;
550 vector<string> outputs;
551 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
552 if (_output->nth (0)->get_connections (connections)) {
553 if (outputs.size() > 0) {
556 if (phys != connections[0]) {
557 Config->set_auditioner_output_left (connections[0]);
559 Config->set_auditioner_output_left ("default");
562 Config->set_auditioner_output_left ("");
565 connections.clear ();
567 if (_output->nth (1)->get_connections (connections)) {
568 if (outputs.size() > 1) {
571 if (phys != connections[0]) {
572 Config->set_auditioner_output_right (connections[0]);
574 Config->set_auditioner_output_right ("default");
577 Config->set_auditioner_output_right ("");
583 Auditioner::input_streams () const
585 /* auditioner never has any inputs - its channel configuration
586 depends solely on the region we are auditioning.
589 if (!_midi_audition && audio_diskstream()) {
590 return audio_diskstream()->n_channels();
592 if (_midi_audition && midi_diskstream()) {
593 ChanCount cnt (DataType::MIDI, 1);
601 Auditioner::monitoring_state () const
603 return MonitoringDisk;