Install everything to *ardour3* instead of *ardour2*.
[ardour.git] / libs / ardour / diskstream.cc
1 /*
2     Copyright (C) 2000-2006 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 <fstream>
21 #include <cassert>
22 #include <cstdio>
23 #include <unistd.h>
24 #include <cmath>
25 #include <cerrno>
26 #include <string>
27 #include <climits>
28 #include <fcntl.h>
29 #include <cstdlib>
30 #include <ctime>
31 #include <sys/stat.h>
32 #include <sys/mman.h>
33
34 #include <sigc++/bind.h>
35
36 #include <pbd/error.h>
37 #include <pbd/basename.h>
38 #include <glibmm/thread.h>
39 #include <pbd/xml++.h>
40
41 #include <ardour/ardour.h>
42 #include <ardour/audioengine.h>
43 #include <ardour/diskstream.h>
44 #include <ardour/utils.h>
45 #include <ardour/configuration.h>
46 #include <ardour/audiofilesource.h>
47 #include <ardour/send.h>
48 #include <ardour/playlist.h>
49 #include <ardour/cycle_timer.h>
50 #include <ardour/region.h>
51
52 #include "i18n.h"
53 #include <locale.h>
54
55 using namespace std;
56 using namespace ARDOUR;
57 using namespace PBD;
58
59 /* XXX This goes uninitialized when there is no ~/.ardour3 directory.
60  * I can't figure out why, so this will do for now (just stole the
61  * default from configuration_vars.h).  0 is not a good value for
62  * allocating buffer sizes..
63  */
64 ARDOUR::nframes_t Diskstream::disk_io_chunk_frames = 1024 * 256;
65
66 sigc::signal<void>                Diskstream::DiskOverrun;
67 sigc::signal<void>                Diskstream::DiskUnderrun;
68
69 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
70         : SessionObject(sess, name)
71 {
72         init (flag);
73 }
74         
75 Diskstream::Diskstream (Session& sess, const XMLNode& node)
76         : SessionObject(sess, "unnamed diskstream")
77 {
78         init (Recordable);
79 }
80
81 void
82 Diskstream::init (Flag f)
83 {
84         _flags = f;
85         _io = 0;
86         _alignment_style = ExistingMaterial;
87         _persistent_alignment_style = ExistingMaterial;
88         first_input_change = true;
89         i_am_the_modifier = 0;
90         g_atomic_int_set (&_record_enabled, 0);
91         was_recording = false;
92         capture_start_frame = 0;
93         capture_captured = 0;
94         _visible_speed = 1.0f;
95         _actual_speed = 1.0f;
96         _buffer_reallocation_required = false;
97         _seek_required = false;
98         first_recordable_frame = max_frames;
99         last_recordable_frame = max_frames;
100         _roll_delay = 0;
101         _capture_offset = 0;
102         _processed = false;
103         _slaved = false;
104         adjust_capture_position = 0;
105         last_possibly_recording = 0;
106         loop_location = 0;
107         wrap_buffer_size = 0;
108         speed_buffer_size = 0;
109         last_phase = 0;
110         phi = (uint64_t) (0x1000000);
111         target_phi = phi;
112         file_frame = 0;
113         playback_sample = 0;
114         playback_distance = 0;
115         _read_data_count = 0;
116         _write_data_count = 0;
117         commit_should_unlock = false;
118
119         pending_overwrite = false;
120         overwrite_frame = 0;
121         overwrite_queued = false;
122         input_change_pending = NoChange;
123 }
124
125 Diskstream::~Diskstream ()
126 {
127         if (_playlist)
128                 _playlist->release ();
129 }
130
131 void
132 Diskstream::set_io (IO& io)
133 {
134         _io = &io;
135         set_align_style_from_io ();
136 }
137
138 void
139 Diskstream::handle_input_change (IOChange change, void *src)
140 {
141         Glib::Mutex::Lock lm (state_lock);
142
143         if (!(input_change_pending & change)) {
144                 input_change_pending = IOChange (input_change_pending|change);
145                 _session.request_input_change_handling ();
146         }
147 }
148
149 void
150 Diskstream::non_realtime_set_speed ()
151 {
152         if (_buffer_reallocation_required)
153         {
154                 Glib::Mutex::Lock lm (state_lock);
155                 allocate_temporary_buffers ();
156
157                 _buffer_reallocation_required = false;
158         }
159
160         if (_seek_required) {
161                 if (speed() != 1.0f || speed() != -1.0f) {
162                         seek ((nframes_t) (_session.transport_frame() * (double) speed()), true);
163                 }
164                 else {
165                         seek (_session.transport_frame(), true);
166                 }
167
168                 _seek_required = false;
169         }
170 }
171
172 bool
173 Diskstream::realtime_set_speed (double sp, bool global)
174 {
175         bool changed = false;
176         double new_speed = sp * _session.transport_speed();
177         
178         if (_visible_speed != sp) {
179                 _visible_speed = sp;
180                 changed = true;
181         }
182         
183         if (new_speed != _actual_speed) {
184                 
185                 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() * 
186                                                                             fabs (new_speed)) + 1;
187                 
188                 if (required_wrap_size > wrap_buffer_size) {
189                         _buffer_reallocation_required = true;
190                 }
191                 
192                 _actual_speed = new_speed;
193                 target_phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
194         }
195
196         if (changed) {
197                 if (!global) {
198                         _seek_required = true;
199                 }
200                 SpeedChanged (); /* EMIT SIGNAL */
201         }
202
203         return _buffer_reallocation_required || _seek_required;
204 }
205
206 void
207 Diskstream::prepare ()
208 {
209         _processed = false;
210         playback_distance = 0;
211 }
212
213 void
214 Diskstream::recover ()
215 {
216         if (commit_should_unlock) {
217                 state_lock.unlock();
218         }
219         _processed = false;
220 }
221
222 void
223 Diskstream::set_capture_offset ()
224 {
225         if (_io == 0) {
226                 /* can't capture, so forget it */
227                 return;
228         }
229
230         _capture_offset = _io->input_latency();
231 }
232
233 void
234 Diskstream::set_align_style (AlignStyle a)
235 {
236         if (record_enabled() && _session.actively_recording()) {
237                 return;
238         }
239
240         if (a != _alignment_style) {
241                 _alignment_style = a;
242                 AlignmentStyleChanged ();
243         }
244 }
245
246 int
247 Diskstream::set_loop (Location *location)
248 {
249         if (location) {
250                 if (location->start() >= location->end()) {
251                         error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
252                         return -1;
253                 }
254         }
255
256         loop_location = location;
257
258          LoopSet (location); /* EMIT SIGNAL */
259         return 0;
260 }
261
262 ARDOUR::nframes_t
263 Diskstream::get_capture_start_frame (uint32_t n)
264 {
265         Glib::Mutex::Lock lm (capture_info_lock);
266
267         if (capture_info.size() > n) {
268                 return capture_info[n]->start;
269         }
270         else {
271                 return capture_start_frame;
272         }
273 }
274
275 ARDOUR::nframes_t
276 Diskstream::get_captured_frames (uint32_t n)
277 {
278         Glib::Mutex::Lock lm (capture_info_lock);
279
280         if (capture_info.size() > n) {
281                 return capture_info[n]->frames;
282         }
283         else {
284                 return capture_captured;
285         }
286 }
287
288 void
289 Diskstream::set_roll_delay (ARDOUR::nframes_t nframes)
290 {
291         _roll_delay = nframes;
292 }
293
294 void
295 Diskstream::set_speed (double sp)
296 {
297         _session.request_diskstream_speed (*this, sp);
298
299         /* to force a rebuffering at the right place */
300         playlist_modified();
301 }
302
303 int
304 Diskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
305 {
306         {
307                 Glib::Mutex::Lock lm (state_lock);
308
309                 if (playlist == _playlist) {
310                         return 0;
311                 }
312
313                 plmod_connection.disconnect ();
314                 plgone_connection.disconnect ();
315
316                 if (_playlist) {
317                         _playlist->release();
318                 }
319                         
320                 _playlist = playlist;
321                 _playlist->use();
322
323                 if (!in_set_state && recordable()) {
324                         reset_write_sources (false);
325                 }
326                 
327                 plmod_connection = _playlist->Modified.connect (mem_fun (*this, &Diskstream::playlist_modified));
328                 plgone_connection = _playlist->GoingAway.connect (bind (mem_fun (*this, &Diskstream::playlist_deleted), boost::weak_ptr<Playlist>(_playlist)));
329         }
330
331         /* don't do this if we've already asked for it *or* if we are setting up
332            the diskstream for the very first time - the input changed handling will
333            take care of the buffer refill.
334         */
335
336         if (!overwrite_queued && !(_session.state_of_the_state() & Session::CannotSave)) {
337                 _session.request_overwrite_buffer (this);
338                 overwrite_queued = true;
339         }
340         
341         PlaylistChanged (); /* EMIT SIGNAL */
342         _session.set_dirty ();
343
344         return 0;
345 }
346
347 void
348 Diskstream::playlist_changed (Change ignored)
349 {
350         playlist_modified ();
351 }
352
353 void
354 Diskstream::playlist_modified ()
355 {
356         if (!i_am_the_modifier && !overwrite_queued) {
357                 _session.request_overwrite_buffer (this);
358                 overwrite_queued = true;
359         } 
360 }
361
362 void
363 Diskstream::playlist_deleted (boost::weak_ptr<Playlist> wpl)
364 {
365         boost::shared_ptr<Playlist> pl (wpl.lock());
366
367         if (pl == _playlist) {
368
369                 /* this catches an ordering issue with session destruction. playlists 
370                    are destroyed before diskstreams. we have to invalidate any handles
371                    we have to the playlist.
372                 */
373                 
374                 if (_playlist) {
375                         _playlist.reset ();
376                 } 
377         }
378 }
379
380 bool
381 Diskstream::set_name (const string& str)
382 {
383         if (str != _name) {
384                 assert(playlist());
385                 playlist()->set_name (str);
386                 
387                 SessionObject::set_name(str);
388                 
389                 if (!in_set_state && recordable()) {
390                         /* rename existing capture files so that they have the correct name */
391                         return rename_write_sources ();
392                 } else {
393                         return false;
394                 }
395         }
396
397         return true;
398 }
399
400 void
401 Diskstream::remove_region_from_last_capture (boost::weak_ptr<Region> wregion)
402 {
403         boost::shared_ptr<Region> region (wregion.lock());
404
405         if (!region) {
406                 return;
407         }
408         
409         _last_capture_regions.remove (region);
410 }
411