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_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"
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() && _session.monitor_out()->input()->audio (0)) {
141 left = _session.monitor_out()->input()->audio (0)->name();
143 if (outputs.size() > 0) {
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();
153 if (outputs.size() > 1) {
159 _output->disconnect (this);
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;
168 if (_output->n_ports().n_audio() == 0) {
170 /* create (and connect) new ports */
172 _main_outs->defer_pan_reset ();
175 _output->add_port (left, this, DataType::AUDIO);
178 if (right.length()) {
179 _output->add_port (right, this, DataType::AUDIO);
182 _main_outs->allow_pan_reset ();
183 _main_outs->reset_panner ();
187 /* reconnect existing ports */
189 boost::shared_ptr<Port> oleft (_output->nth (0));
190 boost::shared_ptr<Port> oright (_output->nth (1));
192 oleft->connect (left);
195 oright->connect (right);
201 if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input())) {
210 Auditioner::data_type () const {
211 if (_midi_audition) {
212 return DataType::MIDI;
214 return DataType::AUDIO;
219 Auditioner::roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& need_butler)
221 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
228 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
231 _amp->apply_gain_automation(false);
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);
246 process_output_buffers (bufs, start_frame, end_frame, nframes, declick, !_session.transport_stopped());
248 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
249 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
251 d->flush_buffers (nframes);
259 Auditioner::prepare_playlist ()
261 // used by CrossfadeEditor::audition()
263 _midi_audition = false;
266 remove_processor(asynth);
267 _synth_added = false;
270 // FIXME auditioner is still audio-only
271 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(playlist());
279 Auditioner::audition_region (boost::shared_ptr<Region> region)
281 if (g_atomic_int_get (&_auditioning)) {
282 /* don't go via session for this, because we are going
288 Glib::Threads::Mutex::Lock lm (lock);
290 if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {
292 _midi_audition = false;
295 remove_processor(asynth);
296 _synth_added = false;
299 _import_position = 0;
302 the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
303 the_region->set_position (0);
305 _disk_reader->audio_playlist()->drop_regions ();
306 _disk_reader->audio_playlist()->add_region (the_region, 0, 1);
310 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
312 if (configure_processors (&ps)) {
313 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
314 region->n_channels()) << endmsg;
319 } else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
320 _midi_audition = true;
323 _import_position = region->position();
326 midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
327 midi_region->set_position (_import_position);
329 _disk_reader->midi_playlist()->drop_regions ();
330 _disk_reader->midi_playlist()->add_region (midi_region, _import_position, 1);
331 _disk_reader->reset_tracker();
335 if (_synth_changed && _synth_added) {
336 remove_processor(asynth);
337 _synth_added = false;
339 if (_synth_changed && !_synth_added) {
340 _synth_added = false;
344 if (!_synth_added && asynth) {
345 int rv = add_processor (asynth, PreFader, &ps, true);
347 error << _("Failed to load synth for MIDI-Audition.") << endmsg;
356 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
358 if (configure_processors (&ps)) {
359 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
360 region->n_channels()) << endmsg;
366 error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
370 /* force a panner reset now that we have all channels */
371 _main_outs->reset_panner();
379 if (_midi_audition) {
380 length = midi_region->length();
381 offset = _import_position + midi_region->sync_offset (dir);
383 length = the_region->length();
384 offset = the_region->sync_offset (dir);
387 /* can't audition from a negative sync point */
393 _disk_reader->seek (offset, true);
394 current_frame = offset;
396 g_atomic_int_set (&_auditioning, 1);
400 Auditioner::play_audition (framecnt_t nframes)
402 bool need_butler = false;
403 framecnt_t this_nframes;
406 if (g_atomic_int_get (&_auditioning) == 0) {
412 if (_seeking && _seek_complete) {
414 } else if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
415 // set FADE-OUT -- use/override amp? || use region-gain ?
419 if (_seeking && _seek_complete) {
420 _seek_complete = false;
423 _disk_reader->reset_tracker();
428 this_nframes = min (nframes, length - current_frame + _import_position);
430 if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, need_butler)) != 0) {
435 current_frame += this_nframes;
441 if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
443 _seek_complete = false;
449 AuditionProgress(current_frame - _import_position, length); /* emit */
452 if (current_frame >= length + _import_position) {
453 _session.cancel_audition ();
456 return need_butler ? 1 : 0;
461 Auditioner::output_changed (IOChange change, void* /*src*/)
463 if (change.type & IOChange::ConnectionsChanged) {
465 vector<string> connections;
466 vector<string> outputs;
467 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
469 if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input ())) {
470 Config->set_auditioner_output_left ("default");
471 Config->set_auditioner_output_right ("default");
476 if (_output->nth (0)->get_connections (connections)) {
477 if (outputs.size() > 0) {
480 if (phys != connections[0]) {
481 Config->set_auditioner_output_left (connections[0]);
483 Config->set_auditioner_output_left ("default");
486 Config->set_auditioner_output_left ("");
489 connections.clear ();
491 if (_output->nth (1)->get_connections (connections)) {
492 if (outputs.size() > 1) {
495 if (phys != connections[0]) {
496 Config->set_auditioner_output_right (connections[0]);
498 Config->set_auditioner_output_right ("default");
501 Config->set_auditioner_output_right ("");
507 Auditioner::input_streams () const
509 /* auditioner never has any inputs - its channel configuration
510 depends solely on the region we are auditioning.
514 return _disk_reader->input_streams ();
517 return ChanCount (DataType::AUDIO, 1);
521 Auditioner::monitoring_state () const
523 return MonitoringDisk;