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