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/profile.h"
38 #include "ardour/region_factory.h"
39 #include "ardour/route.h"
40 #include "ardour/session.h"
43 using namespace ARDOUR;
48 Auditioner::Auditioner (Session& s)
49 : Track (s, "auditioner", PresentationInfo::Auditioner)
55 , _seek_complete (false)
57 , _midi_audition (false)
58 , _synth_added (false)
59 , _synth_changed (false)
60 , _queue_panic (false)
61 , _import_position (0)
76 _output->add_port ("", this, DataType::MIDI);
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));
86 Auditioner::~Auditioner ()
89 asynth->drop_references ();
95 Auditioner::lookup_synth ()
97 string plugin_id = Config->get_midi_audition_synth_uri();
99 if (!plugin_id.empty()) {
100 boost::shared_ptr<Plugin> p;
101 p = find_plugin (_session, plugin_id, ARDOUR::LV2);
103 p = find_plugin (_session, "http://gareus.org/oss/lv2/gmsynth", ARDOUR::LV2);
105 p = find_plugin (_session, "https://community.ardour.org/node/7596", ARDOUR::LV2);
108 warning << _("Falling back to Reasonable Synth for Midi Audition") << endmsg;
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)
115 asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
121 Auditioner::config_changed (std::string p)
123 if (p == "midi-audition-synth-uri") {
124 _synth_changed = true;
129 Auditioner::connect ()
131 string left = Config->get_auditioner_output_left();
132 string right = Config->get_auditioner_output_right();
134 vector<string> outputs;
135 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
139 if (left.empty() || left == "default") {
140 if (_session.monitor_out()) {
141 left = _session.monitor_out()->input()->audio (0)->name();
144 if (outputs.size() > 0) {
150 if (right.empty() || right == "default") {
151 if (_session.monitor_out()) {
152 right = _session.monitor_out()->input()->audio (1)->name();
155 if (outputs.size() > 1) {
161 _output->disconnect (this);
163 if (left.empty() && right.empty()) {
164 if (_output->n_ports().n_audio() == 0) {
165 /* ports not set up, so must be during startup */
166 warning << _("no outputs available for auditioner - manual connection required") << endmsg;
170 if (_output->n_ports().n_audio() == 0) {
172 /* create (and connect) new ports */
174 _main_outs->defer_pan_reset ();
177 _output->add_port (left, this, DataType::AUDIO);
180 if (right.length()) {
181 _output->add_port (right, this, DataType::AUDIO);
184 _main_outs->allow_pan_reset ();
185 _main_outs->reset_panner ();
189 /* reconnect existing ports */
191 boost::shared_ptr<Port> oleft (_output->nth (0));
192 boost::shared_ptr<Port> oright (_output->nth (1));
194 oleft->connect (left);
197 oright->connect (right);
208 Auditioner::data_type () const {
209 if (_midi_audition) {
210 return DataType::MIDI;
212 return DataType::AUDIO;
216 boost::shared_ptr<Diskstream>
217 Auditioner::create_diskstream () {
220 AudioDiskstream::Flag dflags = AudioDiskstream::Flag (0);
221 dflags = AudioDiskstream::Flag (dflags | AudioDiskstream::Hidden);
222 _diskstream_audio = boost::shared_ptr<AudioDiskstream> (new AudioDiskstream (_session, name(), dflags));
226 MidiDiskstream::Flag dflags = MidiDiskstream::Flag (0);
227 dflags = MidiDiskstream::Flag (dflags | MidiDiskstream::Hidden);
228 _diskstream_midi = boost::shared_ptr<Diskstream> (new MidiDiskstream (_session, name(), dflags));
229 _diskstream_midi->do_refill_with_alloc ();
230 _diskstream_midi->playlist()->set_orig_track_id (id());
233 return _diskstream_audio;
237 Auditioner::roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler) {
238 if (_midi_audition) {
239 return roll_midi(nframes, start_frame, end_frame, declick, need_butler);
241 return roll_audio(nframes, start_frame, end_frame, declick, need_butler);
246 Auditioner::roll_midi (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
248 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
255 framecnt_t playback_distance = nframes;
256 boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
257 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
258 MidiBuffer& mbuf (bufs.get_midi (0));
261 ChanCount cnt (DataType::MIDI, 1);
262 cnt.set (DataType::AUDIO, bufs.count().n_audio());
263 bufs.set_count (cnt);
266 _queue_panic = false;
267 for (uint8_t chn = 0; chn < 0xf; ++chn) {
268 uint8_t buf[3] = { ((uint8_t) (MIDI_CMD_CONTROL | chn)), ((uint8_t) MIDI_CTL_SUSTAIN), 0 };
269 mbuf.push_back(0, 3, buf);
270 buf[1] = MIDI_CTL_ALL_NOTES_OFF;
271 mbuf.push_back(0, 3, buf);
272 buf[1] = MIDI_CTL_RESET_CONTROLLERS;
273 mbuf.push_back(0, 3, buf);
275 process_output_buffers (bufs, start_frame, start_frame+1, 1, false, false);
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);
285 diskstream->get_playback (mbuf, nframes);
287 process_output_buffers (bufs, start_frame, end_frame, nframes,
288 declick, (!diskstream->record_enabled() && !_session.transport_stopped()));
290 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
291 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
293 d->flush_buffers (nframes);
297 need_butler = diskstream->commit (playback_distance);
303 Auditioner::roll_audio (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler) {
304 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
309 assert(n_outputs().n_total() > 0);
313 framecnt_t playback_distance;
314 framepos_t transport_frame = _session.transport_frame();
315 boost::shared_ptr<AudioDiskstream> diskstream = audio_diskstream();
316 BufferSet& bufs = _session.get_route_buffers (n_process_buffers ());
319 _amp->apply_gain_automation(false);
321 if ((dret = diskstream->process (bufs, transport_frame, nframes, playback_distance, (monitoring_state() == MonitoringDisk))) != 0) {
322 need_butler = diskstream->commit (playback_distance);
327 process_output_buffers (bufs, start_frame, end_frame, nframes, declick, (!diskstream->record_enabled() && _session.transport_rolling()));
328 need_butler = diskstream->commit (playback_distance);
333 Auditioner::set_diskstream (boost::shared_ptr<Diskstream> ds)
335 Track::set_diskstream (ds);
337 _diskstream->set_track (this);
338 #ifdef XXX_OLD_DESTRUCTIVE_API_XXX
339 if (Profile->get_trx()) {
340 _diskstream->set_destructive (false);
342 _diskstream->set_destructive (_mode == Destructive);
344 _diskstream->set_non_layered (_mode == NonLayered);
346 _diskstream->set_record_enabled (false);
347 _diskstream->request_input_monitoring (false);
349 DiskstreamChanged (); /* EMIT SIGNAL */
353 Auditioner::prepare_playlist ()
355 // used by CrossfadeEditor::audition()
357 _midi_audition = false;
358 set_diskstream(_diskstream_audio);
360 remove_processor(asynth);
361 _synth_added = false;
364 // FIXME auditioner is still audio-only
365 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(_diskstream->playlist());
373 Auditioner::audition_region (boost::shared_ptr<Region> region)
375 if (g_atomic_int_get (&_auditioning)) {
376 /* don't go via session for this, because we are going
382 Glib::Threads::Mutex::Lock lm (lock);
384 if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {
386 _midi_audition = false;
387 set_diskstream(_diskstream_audio);
389 remove_processor(asynth);
390 _synth_added = false;
393 _import_position = 0;
396 the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
397 the_region->set_position (0);
399 _diskstream->playlist()->drop_regions ();
400 _diskstream->playlist()->add_region (the_region, 0, 1);
402 if (_diskstream->n_channels().n_audio() < the_region->n_channels()) {
403 audio_diskstream()->add_channel (the_region->n_channels() - _diskstream->n_channels().n_audio());
404 } else if (_diskstream->n_channels().n_audio() > the_region->n_channels()) {
405 audio_diskstream()->remove_channel (_diskstream->n_channels().n_audio() - the_region->n_channels());
410 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
412 if (configure_processors (&ps)) {
413 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
414 _diskstream->n_channels()) << endmsg;
419 } else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
420 _midi_audition = true;
421 set_diskstream(_diskstream_midi);
423 _import_position = region->position();
426 midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
427 midi_region->set_position (_import_position);
429 _diskstream->playlist()->drop_regions ();
430 _diskstream->playlist()->add_region (midi_region, _import_position, 1);
431 midi_diskstream()->reset_tracker();
435 if (_synth_changed && _synth_added) {
436 remove_processor(asynth);
437 _synth_added = false;
439 if (_synth_changed && !_synth_added) {
440 _synth_added = false;
445 if (!_synth_added && asynth) {
446 int rv = add_processor (asynth, PreFader, &ps, true);
448 error << _("Failed to load synth for MIDI-Audition.") << endmsg;
457 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
459 if (configure_processors (&ps)) {
460 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
461 _diskstream->n_channels()) << endmsg;
467 error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
471 /* force a panner reset now that we have all channels */
472 _main_outs->reset_panner();
480 if (_midi_audition) {
481 length = midi_region->length();
482 offset = _import_position + midi_region->sync_offset (dir);
484 length = the_region->length();
485 offset = the_region->sync_offset (dir);
488 /* can't audition from a negative sync point */
494 _diskstream->seek (offset, true);
495 current_frame = offset;
497 g_atomic_int_set (&_auditioning, 1);
501 Auditioner::play_audition (framecnt_t nframes)
503 bool need_butler = false;
504 framecnt_t this_nframes;
507 if (g_atomic_int_get (&_auditioning) == 0) {
513 if (_seeking && _seek_complete) {
515 } else if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
516 // set FADE-OUT -- use/override amp? || use region-gain ?
520 if (_seeking && _seek_complete) {
521 _seek_complete = false;
524 if (_midi_audition && midi_diskstream()) {
525 midi_diskstream()->reset_tracker();
531 this_nframes = min (nframes, length - current_frame + _import_position);
533 if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, need_butler)) != 0) {
538 current_frame += this_nframes;
544 if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
546 _seek_complete = false;
552 AuditionProgress(current_frame - _import_position, length); /* emit */
555 if (current_frame >= length + _import_position) {
556 _session.cancel_audition ();
559 return need_butler ? 1 : 0;
564 Auditioner::output_changed (IOChange change, void* /*src*/)
566 if (change.type & IOChange::ConnectionsChanged) {
568 vector<string> connections;
569 vector<string> outputs;
570 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
571 if (_output->nth (0)->get_connections (connections)) {
572 if (outputs.size() > 0) {
575 if (phys != connections[0]) {
576 Config->set_auditioner_output_left (connections[0]);
578 Config->set_auditioner_output_left ("default");
581 Config->set_auditioner_output_left ("");
584 connections.clear ();
586 if (_output->nth (1)->get_connections (connections)) {
587 if (outputs.size() > 1) {
590 if (phys != connections[0]) {
591 Config->set_auditioner_output_right (connections[0]);
593 Config->set_auditioner_output_right ("default");
596 Config->set_auditioner_output_right ("");
602 Auditioner::input_streams () const
604 /* auditioner never has any inputs - its channel configuration
605 depends solely on the region we are auditioning.
608 if (!_midi_audition && audio_diskstream()) {
609 return audio_diskstream()->n_channels();
611 if (_midi_audition && midi_diskstream()) {
612 ChanCount cnt (DataType::MIDI, 1);
620 Auditioner::monitoring_state () const
622 return MonitoringDisk;
625 boost::shared_ptr<AudioDiskstream>
626 Auditioner::audio_diskstream() const
628 return boost::dynamic_pointer_cast<AudioDiskstream> (_diskstream);
631 boost::shared_ptr<MidiDiskstream>
632 Auditioner::midi_diskstream() const
634 return boost::dynamic_pointer_cast<MidiDiskstream> (_diskstream);