8de2faeb15635af7f00b7d0edf4f52c8a2c49808
[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 ARDOUR::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         : SessionObject(sess, name)
71 {
72         init (flag);
73 }
74         
75 Diskstream::Diskstream (Session& sess, const XMLNode& node)
76         : SessionObject(sess, "unnamed diskstream")
77 {
78         init (Recordable);
79 }
80
81 void
82 Diskstream::init (Flag f)
83 {
84         _flags = f;
85         _io = 0;
86         _alignment_style = ExistingMaterial;
87         _persistent_alignment_style = ExistingMaterial;
88         first_input_change = true;
89         i_am_the_modifier = 0;
90         g_atomic_int_set (&_record_enabled, 0);
91         was_recording = false;
92         capture_start_frame = 0;
93         capture_captured = 0;
94         _visible_speed = 1.0f;
95         _actual_speed = 1.0f;
96         _buffer_reallocation_required = false;
97         _seek_required = false;
98         first_recordable_frame = max_frames;
99         last_recordable_frame = max_frames;
100         _roll_delay = 0;
101         _capture_offset = 0;
102         _processed = false;
103         _slaved = false;
104         adjust_capture_position = 0;
105         last_possibly_recording = 0;
106         loop_location = 0;
107         wrap_buffer_size = 0;
108         speed_buffer_size = 0;
109         last_phase = 0;
110         phi = (uint64_t) (0x1000000);
111         file_frame = 0;
112         playback_sample = 0;
113         playback_distance = 0;
114         _read_data_count = 0;
115         _write_data_count = 0;
116         commit_should_unlock = false;
117
118         pending_overwrite = false;
119         overwrite_frame = 0;
120         overwrite_queued = false;
121         input_change_pending = NoChange;
122 }
123
124 Diskstream::~Diskstream ()
125 {
126         if (_playlist)
127                 _playlist->release ();
128 }
129
130 void
131 Diskstream::set_io (IO& io)
132 {
133         _io = &io;
134         set_align_style_from_io ();
135 }
136
137 void
138 Diskstream::handle_input_change (IOChange change, void *src)
139 {
140         Glib::Mutex::Lock lm (state_lock);
141
142         if (!(input_change_pending & change)) {
143                 input_change_pending = IOChange (input_change_pending|change);
144                 _session.request_input_change_handling ();
145         }
146 }
147
148 void
149 Diskstream::non_realtime_set_speed ()
150 {
151         if (_buffer_reallocation_required)
152         {
153                 Glib::Mutex::Lock lm (state_lock);
154                 allocate_temporary_buffers ();
155
156                 _buffer_reallocation_required = false;
157         }
158
159         if (_seek_required) {
160                 if (speed() != 1.0f || speed() != -1.0f) {
161                         seek ((nframes_t) (_session.transport_frame() * (double) speed()), true);
162                 }
163                 else {
164                         seek (_session.transport_frame(), true);
165                 }
166
167                 _seek_required = false;
168         }
169 }
170
171 bool
172 Diskstream::realtime_set_speed (double sp, bool global)
173 {
174         bool changed = false;
175         double new_speed = sp * _session.transport_speed();
176         
177         if (_visible_speed != sp) {
178                 _visible_speed = sp;
179                 changed = true;
180         }
181         
182         if (new_speed != _actual_speed) {
183                 
184                 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() * 
185                                                                             fabs (new_speed)) + 1;
186                 
187                 if (required_wrap_size > wrap_buffer_size) {
188                         _buffer_reallocation_required = true;
189                 }
190                 
191                 _actual_speed = new_speed;
192                 phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
193         }
194
195         if (changed) {
196                 if (!global) {
197                         _seek_required = true;
198                 }
199                 SpeedChanged (); /* EMIT SIGNAL */
200         }
201
202         return _buffer_reallocation_required || _seek_required;
203 }
204
205 void
206 Diskstream::prepare ()
207 {
208         _processed = false;
209         playback_distance = 0;
210 }
211
212 void
213 Diskstream::recover ()
214 {
215         if (commit_should_unlock) {
216                 state_lock.unlock();
217         }
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 ARDOUR::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 ARDOUR::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 (ARDOUR::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 bool
380 Diskstream::set_name (const string& str)
381 {
382         if (str != _name) {
383                 assert(playlist());
384                 playlist()->set_name (str);
385                 
386                 SessionObject::set_name(str);
387                 
388                 if (!in_set_state && recordable()) {
389                         /* rename existing capture files so that they have the correct name */
390                         return rename_write_sources ();
391                 } else {
392                         return false;
393                 }
394         }
395
396         return true;
397 }
398
399 void
400 Diskstream::remove_region_from_last_capture (boost::weak_ptr<Region> wregion)
401 {
402         boost::shared_ptr<Region> region (wregion.lock());
403
404         if (!region) {
405                 return;
406         }
407         
408         _last_capture_regions.remove (region);
409 }
410