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