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