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