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