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