420dd20fcbe918c5add84fc92b1d74ae600df7b5
[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 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 {
75         init (flag);
76 }
77         
78 Diskstream::Diskstream (Session& sess, const XMLNode& node)
79         : _session (sess)
80 {
81         init (Recordable);
82 }
83
84 void
85 Diskstream::init (Flag f)
86 {
87         _flags = f;
88         _io = 0;
89         _alignment_style = ExistingMaterial;
90         _persistent_alignment_style = ExistingMaterial;
91         first_input_change = true;
92         i_am_the_modifier = 0;
93         g_atomic_int_set (&_record_enabled, 0);
94         was_recording = false;
95         capture_start_frame = 0;
96         capture_captured = 0;
97         _visible_speed = 1.0f;
98         _actual_speed = 1.0f;
99         _buffer_reallocation_required = false;
100         _seek_required = false;
101         first_recordable_frame = max_frames;
102         last_recordable_frame = max_frames;
103         _roll_delay = 0;
104         _capture_offset = 0;
105         _processed = false;
106         _slaved = false;
107         adjust_capture_position = 0;
108         last_possibly_recording = 0;
109         loop_location = 0;
110         wrap_buffer_size = 0;
111         speed_buffer_size = 0;
112         last_phase = 0;
113         phi = (uint64_t) (0x1000000);
114         file_frame = 0;
115         playback_sample = 0;
116         playback_distance = 0;
117         _read_data_count = 0;
118         _write_data_count = 0;
119
120         pending_overwrite = false;
121         overwrite_frame = 0;
122         overwrite_queued = false;
123         input_change_pending = NoChange;
124
125         _n_channels = 0;
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->release ();
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 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 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 (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 (boost::shared_ptr<Playlist> playlist)
309 {
310         {
311                 Glib::Mutex::Lock lm (state_lock);
312
313                 if (playlist == _playlist) {
314                         return 0;
315                 }
316
317                 plmod_connection.disconnect ();
318                 plgone_connection.disconnect ();
319
320                 if (_playlist) {
321                         _playlist->release();
322                 }
323                         
324                 _playlist = playlist;
325                 _playlist->use();
326
327                 if (!in_set_state && recordable()) {
328                         reset_write_sources (false);
329                 }
330                 
331                 plmod_connection = _playlist->Modified.connect (mem_fun (*this, &Diskstream::playlist_modified));
332                 plgone_connection = _playlist->GoingAway.connect (bind (mem_fun (*this, &Diskstream::playlist_deleted), boost::weak_ptr<Playlist>(_playlist)));
333         }
334
335         /* don't do this if we've already asked for it *or* if we are setting up
336            the diskstream for the very first time - the input changed handling will
337            take care of the buffer refill.
338         */
339
340         if (!overwrite_queued && !(_session.state_of_the_state() & Session::CannotSave)) {
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 (boost::weak_ptr<Playlist> wpl)
368 {
369         boost::shared_ptr<Playlist> pl (wpl.lock());
370
371         if (pl == _playlist) {
372
373                 /* this catches an ordering issue with session destruction. playlists 
374                    are destroyed before diskstreams. we have to invalidate any handles
375                    we have to the playlist.
376                 */
377                 
378                 if (_playlist) {
379                         _playlist.reset ();
380                 } 
381         }
382 }
383
384 int
385 Diskstream::set_name (string str)
386 {
387         if (str != _name) {
388                 assert(playlist());
389                 playlist()->set_name (str);
390                 _name = str;
391                 
392                 if (!in_set_state && recordable()) {
393                         /* rename existing capture files so that they have the correct name */
394                         return rename_write_sources ();
395                 } else {
396                         return -1;
397                 }
398         }
399
400         return 0;
401 }
402
403 void
404 Diskstream::remove_region_from_last_capture (boost::weak_ptr<Region> wregion)
405 {
406         boost::shared_ptr<Region> region (wregion.lock());
407
408         if (!region) {
409                 return;
410         }
411         
412         _last_capture_regions.remove (region);
413 }
414