Added missing #include <cassert> (whoops)
[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 <pbd/error.h>
36 #include <pbd/basename.h>
37 #include <glibmm/thread.h>
38 #include <pbd/xml++.h>
39
40 #include <ardour/ardour.h>
41 #include <ardour/audioengine.h>
42 #include <ardour/diskstream.h>
43 #include <ardour/utils.h>
44 #include <ardour/configuration.h>
45 #include <ardour/audiofilesource.h>
46 #include <ardour/destructive_filesource.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 jack_nframes_t Diskstream::disk_io_chunk_frames = 0;
60
61 sigc::signal<void,Diskstream*>    Diskstream::DiskstreamCreated;
62 sigc::signal<void,list<Source*>*> Diskstream::DeleteSources;
63 sigc::signal<void>                Diskstream::DiskOverrun;
64 sigc::signal<void>                Diskstream::DiskUnderrun;
65
66 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
67         : _name (name)
68         , _session (sess)
69         , _playlist(NULL)
70 {
71         init (flag);
72 }
73         
74 Diskstream::Diskstream (Session& sess, const XMLNode& node)
75         : _session (sess)
76         , _playlist(NULL)
77 {
78         init (Recordable);
79 }
80
81 void
82 Diskstream::init (Flag f)
83 {
84         _refcnt = 0;
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         // Taken by derived class destrctors.. should assure locked here somehow?
129         //Glib::Mutex::Lock lm (state_lock);
130
131         if (_playlist)
132                 _playlist->unref ();
133 }
134
135 void
136 Diskstream::set_io (IO& io)
137 {
138         _io = &io;
139         set_align_style_from_io ();
140 }
141
142 void
143 Diskstream::handle_input_change (IOChange change, void *src)
144 {
145         Glib::Mutex::Lock lm (state_lock);
146
147         if (!(input_change_pending & change)) {
148                 input_change_pending = IOChange (input_change_pending|change);
149                 _session.request_input_change_handling ();
150         }
151 }
152
153 void
154 Diskstream::non_realtime_set_speed ()
155 {
156         if (_buffer_reallocation_required)
157         {
158                 Glib::Mutex::Lock lm (state_lock);
159                 allocate_temporary_buffers ();
160
161                 _buffer_reallocation_required = false;
162         }
163
164         if (_seek_required) {
165                 if (speed() != 1.0f || speed() != -1.0f) {
166                         seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()), true);
167                 }
168                 else {
169                         seek (_session.transport_frame(), true);
170                 }
171
172                 _seek_required = false;
173         }
174 }
175
176 bool
177 Diskstream::realtime_set_speed (double sp, bool global)
178 {
179         bool changed = false;
180         double new_speed = sp * _session.transport_speed();
181         
182         if (_visible_speed != sp) {
183                 _visible_speed = sp;
184                 changed = true;
185         }
186         
187         if (new_speed != _actual_speed) {
188                 
189                 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * 
190                                                                             fabs (new_speed)) + 1;
191                 
192                 if (required_wrap_size > wrap_buffer_size) {
193                         _buffer_reallocation_required = true;
194                 }
195                 
196                 _actual_speed = new_speed;
197                 phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
198         }
199
200         if (changed) {
201                 if (!global) {
202                         _seek_required = true;
203                 }
204                 SpeedChanged (); /* EMIT SIGNAL */
205         }
206
207         return _buffer_reallocation_required || _seek_required;
208 }
209
210 void
211 Diskstream::prepare ()
212 {
213         _processed = false;
214         playback_distance = 0;
215 }
216
217 void
218 Diskstream::recover ()
219 {
220         state_lock.unlock();
221         _processed = false;
222 }
223
224 void
225 Diskstream::set_capture_offset ()
226 {
227         if (_io == 0) {
228                 /* can't capture, so forget it */
229                 return;
230         }
231
232         _capture_offset = _io->input_latency();
233 }
234
235 void
236 Diskstream::set_align_style (AlignStyle a)
237 {
238         if (record_enabled() && _session.actively_recording()) {
239                 return;
240         }
241
242         if (a != _alignment_style) {
243                 _alignment_style = a;
244                 AlignmentStyleChanged ();
245         }
246 }
247
248 int
249 Diskstream::set_loop (Location *location)
250 {
251         if (location) {
252                 if (location->start() >= location->end()) {
253                         error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
254                         return -1;
255                 }
256         }
257
258         loop_location = location;
259
260          LoopSet (location); /* EMIT SIGNAL */
261         return 0;
262 }
263
264 jack_nframes_t
265 Diskstream::get_capture_start_frame (uint32_t n)
266 {
267         Glib::Mutex::Lock lm (capture_info_lock);
268
269         if (capture_info.size() > n) {
270                 return capture_info[n]->start;
271         }
272         else {
273                 return capture_start_frame;
274         }
275 }
276
277 jack_nframes_t
278 Diskstream::get_captured_frames (uint32_t n)
279 {
280         Glib::Mutex::Lock lm (capture_info_lock);
281
282         if (capture_info.size() > n) {
283                 return capture_info[n]->frames;
284         }
285         else {
286                 return capture_captured;
287         }
288 }
289
290 void
291 Diskstream::set_roll_delay (jack_nframes_t nframes)
292 {
293         _roll_delay = nframes;
294 }
295
296 void
297 Diskstream::set_speed (double sp)
298 {
299         _session.request_diskstream_speed (*this, sp);
300
301         /* to force a rebuffering at the right place */
302         playlist_modified();
303 }
304
305 int
306 Diskstream::use_playlist (Playlist* playlist)
307 {
308         {
309                 Glib::Mutex::Lock lm (state_lock);
310
311                 if (playlist == _playlist) {
312                         return 0;
313                 }
314
315                 plstate_connection.disconnect();
316                 plmod_connection.disconnect ();
317                 plgone_connection.disconnect ();
318
319                 if (_playlist) {
320                         _playlist->unref();
321                 }
322                         
323                 _playlist = playlist;
324                 _playlist->ref();
325
326                 if (!in_set_state && recordable()) {
327                         reset_write_sources (false);
328                 }
329                 
330                 plstate_connection = _playlist->StateChanged.connect (mem_fun (*this, &Diskstream::playlist_changed));
331                 plmod_connection = _playlist->Modified.connect (mem_fun (*this, &Diskstream::playlist_modified));
332                 plgone_connection = _playlist->GoingAway.connect (mem_fun (*this, &Diskstream::playlist_deleted));
333         }
334
335         if (!overwrite_queued) {
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 (Playlist* pl)
363 {
364         /* this catches an ordering issue with session destruction. playlists 
365            are destroyed before diskstreams. we have to invalidate any handles
366            we have to the playlist.
367         */
368
369         _playlist = 0;
370 }
371
372 int
373 Diskstream::set_name (string str)
374 {
375         if (str != _name) {
376                 assert(playlist());
377                 playlist()->set_name (str);
378                 _name = str;
379                 
380                 if (!in_set_state && recordable()) {
381                         /* rename existing capture files so that they have the correct name */
382                         return rename_write_sources ();
383                 } else {
384                         return -1;
385                 }
386         }
387
388         return 0;
389 }
390
391 void
392 Diskstream::set_destructive (bool yn)
393 {
394         if (yn != destructive()) {
395                 reset_write_sources (true, true);
396                 if (yn) {
397                         _flags |= Destructive;
398                 } else {
399                         _flags &= ~Destructive;
400                 }
401         }
402 }