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