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::audition_region (boost::shared_ptr<Region> region)
261 if (g_atomic_int_get (&_auditioning)) {
262 /* don't go via session for this, because we are going
268 Glib::Threads::Mutex::Lock lm (lock);
270 if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {
272 _midi_audition = false;
275 remove_processor(asynth);
276 _synth_added = false;
279 _import_position = 0;
282 the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
283 the_region->set_position (0);
285 _disk_reader->audio_playlist()->drop_regions ();
286 _disk_reader->audio_playlist()->add_region (the_region, 0, 1);
290 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
292 if (configure_processors (&ps)) {
293 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
294 region->n_channels()) << endmsg;
299 } else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
300 _midi_audition = true;
303 _import_position = region->position();
306 midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
307 midi_region->set_position (_import_position);
309 _disk_reader->midi_playlist()->drop_regions ();
310 _disk_reader->midi_playlist()->add_region (midi_region, _import_position, 1);
311 _disk_reader->reset_tracker();
315 if (_synth_changed && _synth_added) {
316 remove_processor(asynth);
317 _synth_added = false;
319 if (_synth_changed && !_synth_added) {
320 _synth_added = false;
324 if (!_synth_added && asynth) {
325 int rv = add_processor (asynth, PreFader, &ps, true);
327 error << _("Failed to load synth for MIDI-Audition.") << endmsg;
336 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
338 if (configure_processors (&ps)) {
339 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
340 region->n_channels()) << endmsg;
346 error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
350 /* force a panner reset now that we have all channels */
351 _main_outs->reset_panner();
359 if (_midi_audition) {
360 length = midi_region->length();
361 offset = _import_position + midi_region->sync_offset (dir);
363 length = the_region->length();
364 offset = the_region->sync_offset (dir);
367 /* can't audition from a negative sync point */
373 _disk_reader->seek (offset, true);
374 current_frame = offset;
376 g_atomic_int_set (&_auditioning, 1);
380 Auditioner::play_audition (framecnt_t nframes)
382 bool need_butler = false;
383 framecnt_t this_nframes;
386 if (g_atomic_int_get (&_auditioning) == 0) {
392 if (_seeking && _seek_complete) {
394 } else if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
395 // set FADE-OUT -- use/override amp? || use region-gain ?
399 if (_seeking && _seek_complete) {
400 _seek_complete = false;
403 _disk_reader->reset_tracker();
408 this_nframes = min (nframes, length - current_frame + _import_position);
410 if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, need_butler)) != 0) {
415 current_frame += this_nframes;
421 if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
423 _seek_complete = false;
429 AuditionProgress(current_frame - _import_position, length); /* emit */
432 if (current_frame >= length + _import_position) {
433 _session.cancel_audition ();
436 return need_butler ? 1 : 0;
441 Auditioner::output_changed (IOChange change, void* /*src*/)
443 if (change.type & IOChange::ConnectionsChanged) {
445 vector<string> connections;
446 vector<string> outputs;
447 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
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");
456 if (_output->nth (0)->get_connections (connections)) {
457 if (outputs.size() > 0) {
460 if (phys != connections[0]) {
461 Config->set_auditioner_output_left (connections[0]);
463 Config->set_auditioner_output_left ("default");
466 Config->set_auditioner_output_left ("");
469 connections.clear ();
471 if (_output->nth (1)->get_connections (connections)) {
472 if (outputs.size() > 1) {
475 if (phys != connections[0]) {
476 Config->set_auditioner_output_right (connections[0]);
478 Config->set_auditioner_output_right ("default");
481 Config->set_auditioner_output_right ("");
487 Auditioner::input_streams () const
489 /* auditioner never has any inputs - its channel configuration
490 depends solely on the region we are auditioning.
494 return _disk_reader->input_streams ();
497 return ChanCount (DataType::AUDIO, 1);
501 Auditioner::monitoring_state () const
503 return MonitoringDisk;