f955c75ea4877c3afcef51708d58a232561ac513
[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/thread.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/panner.h"
34 #include "ardour/data_type.h"
35 #include "ardour/region_factory.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 #include "i18n.h"
42
43 Auditioner::Auditioner (Session& s)
44         : AudioTrack (s, "auditioner", Route::Hidden)
45 {
46 }
47
48 int
49 Auditioner::init ()
50 {
51         if (Track::init ()) {
52                 return -1;
53         }
54
55         string left = _session.config.get_auditioner_output_left();
56         string right = _session.config.get_auditioner_output_right();
57
58         if (left == "default") {
59                 if (_session.monitor_out()) {
60                         left = _session.monitor_out()->input()->audio (0)->name();
61                 } else {
62                         left = _session.engine().get_nth_physical_output (DataType::AUDIO, 0);
63                 }
64         }
65
66         if (right == "default") {
67                 if (_session.monitor_out()) {
68                         right = _session.monitor_out()->input()->audio (1)->name();
69                 } else {
70                         right = _session.engine().get_nth_physical_output (DataType::AUDIO, 1);
71                 }
72         }
73
74         if ((left.length() == 0) && (right.length() == 0)) {
75                 warning << _("no outputs available for auditioner - manual connection required") << endmsg;
76                 return -1;
77         }
78
79         _main_outs->defer_pan_reset ();
80
81         if (left.length()) {
82                 _output->add_port (left, this, DataType::AUDIO);
83         }
84
85         if (right.length()) {
86                 _output->add_port (right, this, DataType::AUDIO);
87         }
88
89         _main_outs->allow_pan_reset ();
90         _main_outs->reset_panner ();
91
92         _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
93
94         the_region.reset ((AudioRegion*) 0);
95         g_atomic_int_set (&_auditioning, 0);
96
97         return 0;
98 }
99
100 Auditioner::~Auditioner ()
101 {
102 }
103
104 AudioPlaylist&
105 Auditioner::prepare_playlist ()
106 {
107         // FIXME auditioner is still audio-only
108         boost::shared_ptr<AudioPlaylist> apl = boost::dynamic_pointer_cast<AudioPlaylist>(_diskstream->playlist());
109         assert(apl);
110
111         apl->clear ();
112         return *apl;
113 }
114
115 void
116 Auditioner::audition_current_playlist ()
117 {
118         if (g_atomic_int_get (&_auditioning)) {
119                 /* don't go via session for this, because we are going
120                    to remain active.
121                 */
122                 cancel_audition ();
123         }
124
125         Glib::Mutex::Lock lm (lock);
126         _diskstream->seek (0);
127         length = _diskstream->playlist()->get_maximum_extent();
128         current_frame = 0;
129
130         /* force a panner reset now that we have all channels */
131
132         _main_outs->panner()->reset (n_outputs().n_audio(), _diskstream->n_channels().n_audio());
133
134         g_atomic_int_set (&_auditioning, 1);
135 }
136
137 void
138 Auditioner::audition_region (boost::shared_ptr<Region> region)
139 {
140         if (g_atomic_int_get (&_auditioning)) {
141                 /* don't go via session for this, because we are going
142                    to remain active.
143                 */
144                 cancel_audition ();
145         }
146
147         if (boost::dynamic_pointer_cast<AudioRegion>(region) == 0) {
148                 error << _("Auditioning of non-audio regions not yet supported") << endmsg;
149                 return;
150         }
151
152         Glib::Mutex::Lock lm (lock);
153
154         /* copy it */
155
156         boost::shared_ptr<AudioRegion> the_region (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region)));
157         the_region->set_position (0, this);
158
159         _diskstream->playlist()->drop_regions ();
160         _diskstream->playlist()->add_region (the_region, 0, 1);
161
162         if (_diskstream->n_channels().n_audio() < the_region->n_channels()) {
163                 audio_diskstream()->add_channel (the_region->n_channels() - _diskstream->n_channels().n_audio());
164         } else if (_diskstream->n_channels().n_audio() > the_region->n_channels()) {
165                 audio_diskstream()->remove_channel (_diskstream->n_channels().n_audio() - the_region->n_channels());
166         }
167
168         ProcessorStreams ps;
169         if (configure_processors (&ps)) {
170                 error << string_compose (_("Cannot setup auditioner processing flow for %1 channels"), 
171                                          _diskstream->n_channels()) << endmsg;
172                 return;
173         }
174
175         /* force a panner reset now that we have all channels */
176
177         _main_outs->reset_panner();
178
179         length = the_region->length();
180
181         int dir;
182         nframes_t offset = the_region->sync_offset (dir);
183
184         /* can't audition from a negative sync point */
185
186         if (dir < 0) {
187                 offset = 0;
188         }
189
190         _diskstream->seek (offset);
191         current_frame = offset;
192
193         g_atomic_int_set (&_auditioning, 1);
194 }
195
196 int
197 Auditioner::play_audition (nframes_t nframes)
198 {
199         bool need_butler;
200         nframes_t this_nframes;
201         int ret;
202
203         if (g_atomic_int_get (&_auditioning) == 0) {
204                 silence (nframes);
205                 return 0;
206         }
207
208         this_nframes = min (nframes, length - current_frame);
209
210         _diskstream->prepare ();
211
212         if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, false, false)) != 0) {
213                 silence (nframes);
214                 return ret;
215         }
216
217         need_butler = _diskstream->commit (this_nframes);
218         current_frame += this_nframes;
219
220         if (current_frame >= length) {
221                 _session.cancel_audition ();
222                 return 0;
223         } else {
224                 return need_butler ? 1 : 0;
225         }
226 }
227
228 void
229 Auditioner::output_changed (IOChange change, void* /*src*/)
230 {
231         if (change & ConnectionsChanged) {
232                 string phys;
233                 vector<string> connections;
234                 if (_output->nth (0)->get_connections (connections)) {
235                         phys = _session.engine().get_nth_physical_output (DataType::AUDIO, 0);
236                         if (phys != connections[0]) {
237                                 _session.config.set_auditioner_output_left (connections[0]);
238                         } else {
239                                 _session.config.set_auditioner_output_left ("default");
240                         }
241                 } else {
242                         _session.config.set_auditioner_output_left ("");
243                 }
244
245                 connections.clear ();
246
247                 if (_output->nth (1)->get_connections (connections)) {
248                         phys = _session.engine().get_nth_physical_output (DataType::AUDIO, 1);
249                         if (phys != connections[0]) {
250                                 _session.config.set_auditioner_output_right (connections[0]);
251                         } else {
252                                 _session.config.set_auditioner_output_right ("default");
253                         }
254                 } else {
255                         _session.config.set_auditioner_output_right ("");
256                 }
257         }
258 }