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