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