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_insert.h"
36 #include "ardour/plugin_manager.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 , _queue_panic (false)
59 , _import_position (0)
74 _output->add_port ("", this, DataType::MIDI);
75 use_new_playlist (DataType::MIDI);
79 _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
84 Auditioner::~Auditioner ()
87 asynth->drop_references ();
93 Auditioner::lookup_synth_plugin_info (std::string const& uri) const
95 PluginManager& mgr (PluginManager::instance());
98 plugs = mgr.lv2_plugin_info();
100 for (PluginInfoList::const_iterator i = plugs.begin (); i != plugs.end (); ++i) {
101 if (uri == (*i)->unique_id){
105 return PluginInfoPtr ();
109 Auditioner::lookup_synth (bool and_load)
111 string plugin_id = Config->get_midi_audition_synth_uri();
112 if (plugin_id.empty() && plugin_id != X_("@default@")) {
117 nfo = lookup_synth_plugin_info (plugin_id);
119 nfo = lookup_synth_plugin_info ("http://gareus.org/oss/lv2/gmsynth");
122 nfo = lookup_synth_plugin_info ("https://community.ardour.org/node/7596");
124 warning << _("Falling back to Reasonable Synth for Midi Audition") << endmsg;
128 warning << _("No synth for midi-audition found.") << endmsg;
129 Config->set_midi_audition_synth_uri(""); // Don't check again for Reasonable Synth
133 if (plugin_id == X_("@default@")) {
134 Config->set_midi_audition_synth_uri (nfo->unique_id);
138 boost::shared_ptr<Plugin> p = nfo->load (_session);
139 asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
144 Auditioner::unload_synth (bool need_lock)
149 remove_processor (asynth, NULL, need_lock);
150 asynth->drop_references ();
155 Auditioner::connect ()
157 string left = Config->get_auditioner_output_left();
158 string right = Config->get_auditioner_output_right();
160 vector<string> outputs;
161 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
165 if (left.empty() || left == "default") {
166 if (_session.monitor_out() && _session.monitor_out()->input()->audio (0)) {
167 left = _session.monitor_out()->input()->audio (0)->name();
169 if (outputs.size() > 0) {
175 if (right.empty() || right == "default") {
176 if (_session.monitor_out() && _session.monitor_out()->input()->audio (1)) {
177 right = _session.monitor_out()->input()->audio (1)->name();
179 if (outputs.size() > 1) {
185 _output->disconnect (this);
187 if (left.empty() && right.empty()) {
188 if (_output->n_ports().n_audio() == 0) {
189 /* ports not set up, so must be during startup */
190 warning << _("no outputs available for auditioner - manual connection required") << endmsg;
194 if (_output->n_ports().n_audio() == 0) {
196 /* create (and connect) new ports */
198 _main_outs->defer_pan_reset ();
201 _output->add_port (left, this, DataType::AUDIO);
204 if (right.length()) {
205 _output->add_port (right, this, DataType::AUDIO);
208 _main_outs->allow_pan_reset ();
209 _main_outs->reset_panner ();
213 /* reconnect existing ports */
215 boost::shared_ptr<Port> oleft (_output->nth (0));
216 boost::shared_ptr<Port> oright (_output->nth (1));
218 oleft->connect (left);
221 oright->connect (right);
227 if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input())) {
236 Auditioner::data_type () const {
237 if (_midi_audition) {
238 return DataType::MIDI;
240 return DataType::AUDIO;
245 Auditioner::roll (pframes_t nframes, samplepos_t start_sample, samplepos_t end_sample, bool& need_butler)
247 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
254 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
257 MidiBuffer& mbuf (bufs.get_midi (0));
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);
269 process_output_buffers (bufs, start_sample, end_sample, nframes, !_session.transport_stopped(), true);
271 /* note: auditioner never writes to disk, so we don't care about the
272 * disk writer status (it's buffers will always have no data in them).
275 if (_disk_reader->need_butler()) {
279 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
280 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
282 d->flush_buffers (nframes);
290 Auditioner::audition_region (boost::shared_ptr<Region> region)
292 if (g_atomic_int_get (&_auditioning)) {
293 /* don't go via session for this, because we are going
299 Glib::Threads::Mutex::Lock lm (lock);
301 if (boost::dynamic_pointer_cast<AudioRegion>(region) != 0) {
303 _midi_audition = false;
308 _import_position = 0;
311 the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
312 the_region->set_position (0);
314 _disk_reader->midi_playlist()->drop_regions ();
316 _disk_reader->audio_playlist()->drop_regions ();
317 _disk_reader->audio_playlist()->add_region (the_region, 0, 1);
321 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
323 if (configure_processors (&ps)) {
324 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
325 region->n_channels()) << endmsg;
330 } else if (boost::dynamic_pointer_cast<MidiRegion>(region)) {
331 _midi_audition = true;
334 _import_position = region->position();
337 midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
338 midi_region->set_position (_import_position);
340 _disk_reader->audio_playlist()->drop_regions();
342 _disk_reader->midi_playlist()->drop_regions ();
343 _disk_reader->midi_playlist()->add_region (midi_region, _import_position, 1);
344 _disk_reader->reset_tracker();
352 int rv = add_processor (asynth, PreFader, &ps, true);
354 error << _("Failed to load synth for MIDI-Audition.") << endmsg;
359 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
361 if (configure_processors (&ps)) {
362 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
363 region->n_channels()) << endmsg;
370 error << _("Auditioning of regions other than Audio or Midi is not supported.") << endmsg;
374 /* force a panner reset now that we have all channels */
375 _main_outs->reset_panner();
383 if (_midi_audition) {
384 length = midi_region->length();
385 offset = _import_position + midi_region->sync_offset (dir);
387 length = the_region->length();
388 offset = the_region->sync_offset (dir);
392 error << _("Cannot audition empty file.") << endmsg;
397 /* can't audition from a negative sync point */
403 _disk_reader->seek (offset, true);
404 current_sample = offset;
406 g_atomic_int_set (&_auditioning, 1);
410 Auditioner::play_audition (samplecnt_t nframes)
412 bool need_butler = false;
413 samplecnt_t this_nframes;
416 if (g_atomic_int_get (&_auditioning) == 0) {
418 unload_synth (false);
423 if (_seeking && _seek_complete) {
425 } else if (_seek_sample >= 0 && _seek_sample < length && !_seeking) {
426 // set FADE-OUT -- use/override amp? || use region-gain ?
430 if (_seeking && _seek_complete) {
431 _seek_complete = false;
434 _disk_reader->reset_tracker();
439 this_nframes = min (nframes, length - current_sample + _import_position);
441 if (this_nframes > 0 && 0 != (ret = roll (this_nframes, current_sample, current_sample + this_nframes, need_butler))) {
446 current_sample += this_nframes;
448 if (this_nframes < nframes) {
449 if (this_nframes > 0) {
450 _session.engine().split_cycle (this_nframes);
452 silence (nframes - this_nframes);
459 if (_seek_sample >= 0 && _seek_sample < length && !_seeking) {
461 _seek_complete = false;
467 AuditionProgress(current_sample - _import_position, length); /* emit */
470 if (current_sample >= length + _import_position) {
471 _session.cancel_audition ();
472 unload_synth (false);
475 return need_butler ? 1 : 0;
480 Auditioner::output_changed (IOChange change, void* /*src*/)
482 if (change.type & IOChange::ConnectionsChanged) {
484 vector<string> connections;
485 vector<string> outputs;
486 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
488 if (_session.monitor_out () && _output->connected_to (_session.monitor_out ()->input ())) {
489 Config->set_auditioner_output_left ("default");
490 Config->set_auditioner_output_right ("default");
495 if (_output->nth (0)->get_connections (connections)) {
496 if (outputs.size() > 0) {
499 if (phys != connections[0]) {
500 Config->set_auditioner_output_left (connections[0]);
502 Config->set_auditioner_output_left ("default");
505 Config->set_auditioner_output_left ("");
508 connections.clear ();
510 if (_output->nth (1)->get_connections (connections)) {
511 if (outputs.size() > 1) {
514 if (phys != connections[0]) {
515 Config->set_auditioner_output_right (connections[0]);
517 Config->set_auditioner_output_right ("default");
520 Config->set_auditioner_output_right ("");
526 Auditioner::input_streams () const
528 /* auditioner never has any inputs - its channel configuration
529 depends solely on the region we are auditioning.
532 if (_midi_audition) {
533 return ChanCount (DataType::MIDI, 1);
536 return ChanCount (DataType::AUDIO, the_region->n_channels ());
540 return ChanCount (DataType::AUDIO, 1);
544 Auditioner::monitoring_state () const
546 return MonitoringDisk;