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