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/audio_diskstream.h"
25 #include "ardour/audioregion.h"
26 #include "ardour/audioengine.h"
27 #include "ardour/delivery.h"
28 #include "ardour/route.h"
29 #include "ardour/session.h"
30 #include "ardour/auditioner.h"
31 #include "ardour/audioplaylist.h"
32 #include "ardour/audio_port.h"
33 #include "ardour/data_type.h"
34 #include "ardour/region_factory.h"
37 using namespace ARDOUR;
42 Auditioner::Auditioner (Session& s)
43 : AudioTrack (s, "auditioner", Route::Auditioner)
49 , _seek_complete (false)
65 _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
70 Auditioner::~Auditioner ()
75 Auditioner::connect ()
77 string left = Config->get_auditioner_output_left();
78 string right = Config->get_auditioner_output_right();
80 vector<string> outputs;
81 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
85 if (left.empty() || left == "default") {
86 if (_session.monitor_out()) {
87 left = _session.monitor_out()->input()->audio (0)->name();
90 if (outputs.size() > 0) {
96 if (right.empty() || right == "default") {
97 if (_session.monitor_out()) {
98 right = _session.monitor_out()->input()->audio (1)->name();
101 if (outputs.size() > 1) {
107 _output->disconnect (this);
109 if (left.empty() && right.empty()) {
110 if (_output->n_ports().n_audio() == 0) {
111 /* ports not set up, so must be during startup */
112 warning << _("no outputs available for auditioner - manual connection required") << endmsg;
116 if (_output->n_ports().n_audio() == 0) {
118 /* create (and connect) new ports */
120 _main_outs->defer_pan_reset ();
123 _output->add_port (left, this, DataType::AUDIO);
126 if (right.length()) {
127 _output->add_port (right, this, DataType::AUDIO);
130 _main_outs->allow_pan_reset ();
131 _main_outs->reset_panner ();
135 /* reconnect existing ports */
137 boost::shared_ptr<Port> oleft (_output->nth (0));
138 boost::shared_ptr<Port> oright (_output->nth (1));
140 oleft->connect (left);
143 oright->connect (right);
153 Auditioner::prepare_playlist ()
155 // FIXME auditioner is still audio-only
156 boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(_diskstream->playlist());
164 Auditioner::audition_region (boost::shared_ptr<Region> region)
166 if (g_atomic_int_get (&_auditioning)) {
167 /* don't go via session for this, because we are going
173 if (boost::dynamic_pointer_cast<AudioRegion>(region) == 0) {
174 error << _("Auditioning of non-audio regions not yet supported") << endmsg;
178 Glib::Threads::Mutex::Lock lm (lock);
182 boost::shared_ptr<AudioRegion> the_region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
183 the_region->set_position (0);
185 _diskstream->playlist()->drop_regions ();
186 _diskstream->playlist()->add_region (the_region, 0, 1);
188 if (_diskstream->n_channels().n_audio() < the_region->n_channels()) {
189 audio_diskstream()->add_channel (the_region->n_channels() - _diskstream->n_channels().n_audio());
190 } else if (_diskstream->n_channels().n_audio() > the_region->n_channels()) {
191 audio_diskstream()->remove_channel (_diskstream->n_channels().n_audio() - the_region->n_channels());
196 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
198 if (configure_processors (&ps)) {
199 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
200 _diskstream->n_channels()) << endmsg;
205 /* force a panner reset now that we have all channels */
207 _main_outs->reset_panner();
211 length = the_region->length();
214 framecnt_t offset = the_region->sync_offset (dir);
216 /* can't audition from a negative sync point */
222 _diskstream->seek (offset);
223 current_frame = offset;
225 g_atomic_int_set (&_auditioning, 1);
229 Auditioner::play_audition (framecnt_t nframes)
231 bool need_butler = false;
232 framecnt_t this_nframes;
235 if (g_atomic_int_get (&_auditioning) == 0) {
241 if (_seeking && _seek_complete) {
243 } else if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
244 // set FADE-OUT -- use/override amp? || use region-gain ?
248 if (_seeking && _seek_complete) {
249 _seek_complete = false;
256 this_nframes = min (nframes, length - current_frame);
258 if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, need_butler)) != 0) {
263 current_frame += this_nframes;
269 if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
270 _seek_complete = false;
276 AuditionProgress(current_frame, length); /* emit */
279 if (current_frame >= length) {
280 _session.cancel_audition ();
283 return need_butler ? 1 : 0;
288 Auditioner::output_changed (IOChange change, void* /*src*/)
290 if (change.type & IOChange::ConnectionsChanged) {
292 vector<string> connections;
293 vector<string> outputs;
294 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
295 if (_output->nth (0)->get_connections (connections)) {
296 if (outputs.size() > 0) {
299 if (phys != connections[0]) {
300 Config->set_auditioner_output_left (connections[0]);
302 Config->set_auditioner_output_left ("default");
305 Config->set_auditioner_output_left ("");
308 connections.clear ();
310 if (_output->nth (1)->get_connections (connections)) {
311 if (outputs.size() > 1) {
314 if (phys != connections[0]) {
315 Config->set_auditioner_output_right (connections[0]);
317 Config->set_auditioner_output_right ("default");
320 Config->set_auditioner_output_right ("");
326 Auditioner::input_streams () const
328 /* auditioner never has any inputs - its channel configuration
329 depends solely on the region we are auditioning.
332 if (audio_diskstream()) {
333 return audio_diskstream()->n_channels();
340 Auditioner::monitoring_state () const
342 return MonitoringDisk;