Merge branch 'master' into cairocanvas
[ardour.git] / libs / ardour / auditioner.cc
1 /*
2     Copyright (C) 2001 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <glibmm/threads.h>
21
22 #include "pbd/error.h"
23
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"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39
40 #include "i18n.h"
41
42 Auditioner::Auditioner (Session& s)
43         : AudioTrack (s, "auditioner", Route::Auditioner)
44         , current_frame (0)
45         , _auditioning (0)
46         , length (0)
47         , _seek_frame (-1)
48         , _seeking (false)
49         , _seek_complete (false)
50         , via_monitor (false)
51 {
52 }
53
54 int
55 Auditioner::init ()
56 {
57         if (Track::init ()) {
58                 return -1;
59         }
60         
61         if (connect ()) {
62                 return -1;
63         }
64
65         _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
66
67         return 0;
68 }
69
70 Auditioner::~Auditioner ()
71 {
72 }
73
74 int
75 Auditioner::connect ()
76 {
77         string left = Config->get_auditioner_output_left();
78         string right = Config->get_auditioner_output_right();
79
80         vector<string> outputs;
81         _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
82
83         via_monitor = false;
84
85         if (left.empty() || left == "default") {
86                 if (_session.monitor_out()) {
87                         left = _session.monitor_out()->input()->audio (0)->name();
88                         via_monitor = true;
89                 } else {
90                         if (outputs.size() > 0) {
91                                 left = outputs[0];
92                         }
93                 }
94         }
95
96         if (right.empty() || right == "default") {
97                 if (_session.monitor_out()) {
98                         right = _session.monitor_out()->input()->audio (1)->name();
99                         via_monitor = true;
100                 } else {
101                         if (outputs.size() > 1) {
102                                 right = outputs[1];
103                         }
104                 }
105         }
106
107         _output->disconnect (this);
108
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;
113                 }
114         } else {
115
116                 if (_output->n_ports().n_audio() == 0) {
117
118                         /* create (and connect) new ports */
119
120                         _main_outs->defer_pan_reset ();
121                         
122                         if (left.length()) {
123                                 _output->add_port (left, this, DataType::AUDIO);
124                         }
125                         
126                         if (right.length()) {
127                                 _output->add_port (right, this, DataType::AUDIO);
128                         }
129                         
130                         _main_outs->allow_pan_reset ();
131                         _main_outs->reset_panner ();
132
133                 } else {
134                         
135                         /* reconnect existing ports */
136
137                         boost::shared_ptr<Port> oleft (_output->nth (0));
138                         boost::shared_ptr<Port> oright (_output->nth (1));
139                         if (oleft) {
140                                 oleft->connect (left);
141                         }
142                         if (oright) {
143                                 oright->connect (right);
144                         }
145                 }
146                         
147         }
148
149         return 0;
150 }
151
152 AudioPlaylist&
153 Auditioner::prepare_playlist ()
154 {
155         // FIXME auditioner is still audio-only
156         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(_diskstream->playlist());
157         assert(apl);
158
159         apl->clear ();
160         return *apl;
161 }
162
163 void
164 Auditioner::audition_region (boost::shared_ptr<Region> region)
165 {
166         if (g_atomic_int_get (&_auditioning)) {
167                 /* don't go via session for this, because we are going
168                    to remain active.
169                 */
170                 cancel_audition ();
171         }
172
173         if (boost::dynamic_pointer_cast<AudioRegion>(region) == 0) {
174                 error << _("Auditioning of non-audio regions not yet supported") << endmsg;
175                 return;
176         }
177
178         Glib::Threads::Mutex::Lock lm (lock);
179
180         /* copy it */
181
182         boost::shared_ptr<AudioRegion> the_region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
183         the_region->set_position (0);
184
185         _diskstream->playlist()->drop_regions ();
186         _diskstream->playlist()->add_region (the_region, 0, 1);
187
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());
192         }
193
194         ProcessorStreams ps;
195         {
196                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
197
198                 if (configure_processors (&ps)) {
199                         error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
200                                                  _diskstream->n_channels()) << endmsg;
201                         return;
202                 }
203         }
204
205         /* force a panner reset now that we have all channels */
206
207         _main_outs->reset_panner();
208
209         _seek_frame = -1;
210         _seeking = false;
211         length = the_region->length();
212
213         int dir;
214         framecnt_t offset = the_region->sync_offset (dir);
215
216         /* can't audition from a negative sync point */
217
218         if (dir < 0) {
219                 offset = 0;
220         }
221
222         _diskstream->seek (offset);
223         current_frame = offset;
224
225         g_atomic_int_set (&_auditioning, 1);
226 }
227
228 int
229 Auditioner::play_audition (framecnt_t nframes)
230 {
231         bool need_butler = false;
232         framecnt_t this_nframes;
233         int ret;
234
235         if (g_atomic_int_get (&_auditioning) == 0) {
236                 silence (nframes);
237                 return 0;
238         }
239
240 #if 0 // TODO
241         if (_seeking && _seek_complete) {
242                 // set FADE-IN
243         } else if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
244                 // set FADE-OUT -- use/override amp? || use region-gain ?
245         }
246 #endif
247
248         if (_seeking && _seek_complete) {
249                 _seek_complete = false;
250                 _seeking = false;
251                 _seek_frame = -1;
252         }
253
254         if(!_seeking) {
255                 /* process audio */
256                 this_nframes = min (nframes, length - current_frame);
257
258                 if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, need_butler)) != 0) {
259                         silence (nframes);
260                         return ret;
261                 }
262
263                 current_frame += this_nframes;
264
265         } else {
266                 silence (nframes);
267         }
268
269         if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
270                 _seek_complete = false;
271                 _seeking = true;
272                 need_butler = true;
273         }
274
275         if (!_seeking) {
276                 AuditionProgress(current_frame, length); /* emit */
277         }
278
279         if (current_frame >= length) {
280                 _session.cancel_audition ();
281                 return 0;
282         } else {
283                 return need_butler ? 1 : 0;
284         }
285 }
286
287 void
288 Auditioner::output_changed (IOChange change, void* /*src*/)
289 {
290         if (change.type & IOChange::ConnectionsChanged) {
291                 string phys;
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) {
297                                 phys = outputs[0];
298                         }
299                         if (phys != connections[0]) {
300                                 Config->set_auditioner_output_left (connections[0]);
301                         } else {
302                                 Config->set_auditioner_output_left ("default");
303                         }
304                 } else {
305                         Config->set_auditioner_output_left ("");
306                 }
307
308                 connections.clear ();
309
310                 if (_output->nth (1)->get_connections (connections)) {
311                         if (outputs.size() > 1) {
312                                 phys = outputs[1];
313                         }
314                         if (phys != connections[0]) {
315                                 Config->set_auditioner_output_right (connections[0]);
316                         } else {
317                                 Config->set_auditioner_output_right ("default");
318                         }
319                 } else {
320                         Config->set_auditioner_output_right ("");
321                 }
322         }
323 }
324
325 ChanCount
326 Auditioner::input_streams () const
327 {
328         /* auditioner never has any inputs - its channel configuration
329            depends solely on the region we are auditioning.
330         */
331
332         if (audio_diskstream()) {
333                 return audio_diskstream()->n_channels();
334         }
335
336         return ChanCount ();
337 }
338
339 MonitorState 
340 Auditioner::monitoring_state () const
341 {
342         return MonitoringDisk;
343 }