allow auditioning via the monitor section to work.
[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         , via_monitor (false)
48 {
49 }
50
51 int
52 Auditioner::init ()
53 {
54         if (Track::init ()) {
55                 return -1;
56         }
57         
58         if (connect ()) {
59                 return -1;
60         }
61
62         _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
63
64         return 0;
65 }
66
67 Auditioner::~Auditioner ()
68 {
69 }
70
71 int
72 Auditioner::connect ()
73 {
74         string left = Config->get_auditioner_output_left();
75         string right = Config->get_auditioner_output_right();
76
77         vector<string> outputs;
78         _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
79
80         via_monitor = false;
81
82         if (left.empty() || left == "default") {
83                 if (_session.monitor_out()) {
84                         left = _session.monitor_out()->input()->audio (0)->name();
85                         via_monitor = true;
86                 } else {
87                         if (outputs.size() > 0) {
88                                 left = outputs[0];
89                         }
90                 }
91         }
92
93         if (right.empty() || right == "default") {
94                 if (_session.monitor_out()) {
95                         right = _session.monitor_out()->input()->audio (1)->name();
96                         via_monitor = true;
97                 } else {
98                         if (outputs.size() > 1) {
99                                 right = outputs[1];
100                         }
101                 }
102         }
103
104         _output->disconnect (this);
105
106         if (left.empty() && right.empty()) {
107                 if (_output->n_ports().n_audio() == 0) {
108                         /* ports not set up, so must be during startup */
109                         warning << _("no outputs available for auditioner - manual connection required") << endmsg;
110                 }
111         } else {
112
113                 if (_output->n_ports().n_audio() == 0) {
114
115                         /* create (and connect) new ports */
116
117                         _main_outs->defer_pan_reset ();
118                         
119                         if (left.length()) {
120                                 _output->add_port (left, this, DataType::AUDIO);
121                         }
122                         
123                         if (right.length()) {
124                                 _output->add_port (right, this, DataType::AUDIO);
125                         }
126                         
127                         _main_outs->allow_pan_reset ();
128                         _main_outs->reset_panner ();
129
130                 } else {
131                         
132                         /* reconnect existing ports */
133
134                         boost::shared_ptr<Port> oleft (_output->nth (0));
135                         boost::shared_ptr<Port> oright (_output->nth (1));
136                         if (oleft) {
137                                 oleft->connect (left);
138                         }
139                         if (oright) {
140                                 oright->connect (right);
141                         }
142                 }
143                         
144         }
145
146         return 0;
147 }
148
149 AudioPlaylist&
150 Auditioner::prepare_playlist ()
151 {
152         // FIXME auditioner is still audio-only
153         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(_diskstream->playlist());
154         assert(apl);
155
156         apl->clear ();
157         return *apl;
158 }
159
160 void
161 Auditioner::audition_region (boost::shared_ptr<Region> region)
162 {
163         if (g_atomic_int_get (&_auditioning)) {
164                 /* don't go via session for this, because we are going
165                    to remain active.
166                 */
167                 cancel_audition ();
168         }
169
170         if (boost::dynamic_pointer_cast<AudioRegion>(region) == 0) {
171                 error << _("Auditioning of non-audio regions not yet supported") << endmsg;
172                 return;
173         }
174
175         Glib::Threads::Mutex::Lock lm (lock);
176
177         /* copy it */
178
179         boost::shared_ptr<AudioRegion> the_region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
180         the_region->set_position (0);
181
182         _diskstream->playlist()->drop_regions ();
183         _diskstream->playlist()->add_region (the_region, 0, 1);
184
185         if (_diskstream->n_channels().n_audio() < the_region->n_channels()) {
186                 audio_diskstream()->add_channel (the_region->n_channels() - _diskstream->n_channels().n_audio());
187         } else if (_diskstream->n_channels().n_audio() > the_region->n_channels()) {
188                 audio_diskstream()->remove_channel (_diskstream->n_channels().n_audio() - the_region->n_channels());
189         }
190
191         ProcessorStreams ps;
192         {
193                 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
194
195                 if (configure_processors (&ps)) {
196                         error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"),
197                                                  _diskstream->n_channels()) << endmsg;
198                         return;
199                 }
200         }
201
202         /* force a panner reset now that we have all channels */
203
204         _main_outs->reset_panner();
205
206         length = the_region->length();
207
208         int dir;
209         framecnt_t offset = the_region->sync_offset (dir);
210
211         /* can't audition from a negative sync point */
212
213         if (dir < 0) {
214                 offset = 0;
215         }
216
217         _diskstream->seek (offset);
218         current_frame = offset;
219
220         g_atomic_int_set (&_auditioning, 1);
221 }
222
223 int
224 Auditioner::play_audition (framecnt_t nframes)
225 {
226         bool need_butler = false;
227         framecnt_t this_nframes;
228         int ret;
229
230         if (g_atomic_int_get (&_auditioning) == 0) {
231                 silence (nframes);
232                 return 0;
233         }
234
235         this_nframes = min (nframes, length - current_frame);
236
237         if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, need_butler)) != 0) {
238                 silence (nframes);
239                 return ret;
240         }
241
242         current_frame += this_nframes;
243
244         if (current_frame >= length) {
245                 _session.cancel_audition ();
246                 return 0;
247         } else {
248                 return need_butler ? 1 : 0;
249         }
250 }
251
252 void
253 Auditioner::output_changed (IOChange change, void* /*src*/)
254 {
255         if (change.type & IOChange::ConnectionsChanged) {
256                 string phys;
257                 vector<string> connections;
258                 vector<string> outputs;
259                 _session.engine().get_physical_outputs (DataType::AUDIO, outputs);
260                 if (_output->nth (0)->get_connections (connections)) {
261                         if (outputs.size() > 0) {
262                                 phys = outputs[0];
263                         }
264                         if (phys != connections[0]) {
265                                 Config->set_auditioner_output_left (connections[0]);
266                         } else {
267                                 Config->set_auditioner_output_left ("default");
268                         }
269                 } else {
270                         Config->set_auditioner_output_left ("");
271                 }
272
273                 connections.clear ();
274
275                 if (_output->nth (1)->get_connections (connections)) {
276                         if (outputs.size() > 1) {
277                                 phys = outputs[1];
278                         }
279                         if (phys != connections[0]) {
280                                 Config->set_auditioner_output_right (connections[0]);
281                         } else {
282                                 Config->set_auditioner_output_right ("default");
283                         }
284                 } else {
285                         Config->set_auditioner_output_right ("");
286                 }
287         }
288 }
289
290 ChanCount
291 Auditioner::input_streams () const
292 {
293         /* auditioner never has any inputs - its channel configuration
294            depends solely on the region we are auditioning.
295         */
296
297         if (audio_diskstream()) {
298                 return audio_diskstream()->n_channels();
299         }
300
301         return ChanCount ();
302 }
303
304 MonitorState 
305 Auditioner::monitoring_state () const
306 {
307         return MonitoringDisk;
308 }