first pass on track mode switch; fixes to dangling region refs after capture; destroy...
[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         , _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         _flags = f;
90         _io = 0;
91         _alignment_style = ExistingMaterial;
92         _persistent_alignment_style = ExistingMaterial;
93         first_input_change = true;
94         i_am_the_modifier = 0;
95         g_atomic_int_set (&_record_enabled, 0);
96         was_recording = false;
97         capture_start_frame = 0;
98         capture_captured = 0;
99         _visible_speed = 1.0f;
100         _actual_speed = 1.0f;
101         _buffer_reallocation_required = false;
102         _seek_required = false;
103         first_recordable_frame = max_frames;
104         last_recordable_frame = max_frames;
105         _roll_delay = 0;
106         _capture_offset = 0;
107         _processed = false;
108         _slaved = false;
109         adjust_capture_position = 0;
110         last_possibly_recording = 0;
111         loop_location = 0;
112         wrap_buffer_size = 0;
113         speed_buffer_size = 0;
114         last_phase = 0;
115         phi = (uint64_t) (0x1000000);
116         file_frame = 0;
117         playback_sample = 0;
118         playback_distance = 0;
119         _read_data_count = 0;
120         _write_data_count = 0;
121
122         pending_overwrite = false;
123         overwrite_frame = 0;
124         overwrite_queued = false;
125         input_change_pending = NoChange;
126
127         _n_channels = 0;
128 }
129
130 Diskstream::~Diskstream ()
131 {
132         // Taken by derived class destrctors.. should assure locked here somehow?
133         //Glib::Mutex::Lock lm (state_lock);
134
135         if (_playlist)
136                 _playlist->unref ();
137 }
138
139 void
140 Diskstream::set_io (IO& io)
141 {
142         _io = &io;
143         set_align_style_from_io ();
144 }
145
146 void
147 Diskstream::handle_input_change (IOChange change, void *src)
148 {
149         Glib::Mutex::Lock lm (state_lock);
150
151         if (!(input_change_pending & change)) {
152                 input_change_pending = IOChange (input_change_pending|change);
153                 _session.request_input_change_handling ();
154         }
155 }
156
157 void
158 Diskstream::non_realtime_set_speed ()
159 {
160         if (_buffer_reallocation_required)
161         {
162                 Glib::Mutex::Lock lm (state_lock);
163                 allocate_temporary_buffers ();
164
165                 _buffer_reallocation_required = false;
166         }
167
168         if (_seek_required) {
169                 if (speed() != 1.0f || speed() != -1.0f) {
170                         seek ((nframes_t) (_session.transport_frame() * (double) speed()), true);
171                 }
172                 else {
173                         seek (_session.transport_frame(), true);
174                 }
175
176                 _seek_required = false;
177         }
178 }
179
180 bool
181 Diskstream::realtime_set_speed (double sp, bool global)
182 {
183         bool changed = false;
184         double new_speed = sp * _session.transport_speed();
185         
186         if (_visible_speed != sp) {
187                 _visible_speed = sp;
188                 changed = true;
189         }
190         
191         if (new_speed != _actual_speed) {
192                 
193                 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() * 
194                                                                             fabs (new_speed)) + 1;
195                 
196                 if (required_wrap_size > wrap_buffer_size) {
197                         _buffer_reallocation_required = true;
198                 }
199                 
200                 _actual_speed = new_speed;
201                 phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
202         }
203
204         if (changed) {
205                 if (!global) {
206                         _seek_required = true;
207                 }
208                 SpeedChanged (); /* EMIT SIGNAL */
209         }
210
211         return _buffer_reallocation_required || _seek_required;
212 }
213
214 void
215 Diskstream::prepare ()
216 {
217         _processed = false;
218         playback_distance = 0;
219 }
220
221 void
222 Diskstream::recover ()
223 {
224         state_lock.unlock();
225         _processed = false;
226 }
227
228 void
229 Diskstream::set_capture_offset ()
230 {
231         if (_io == 0) {
232                 /* can't capture, so forget it */
233                 return;
234         }
235
236         _capture_offset = _io->input_latency();
237 }
238
239 void
240 Diskstream::set_align_style (AlignStyle a)
241 {
242         if (record_enabled() && _session.actively_recording()) {
243                 return;
244         }
245
246         if (a != _alignment_style) {
247                 _alignment_style = a;
248                 AlignmentStyleChanged ();
249         }
250 }
251
252 int
253 Diskstream::set_loop (Location *location)
254 {
255         if (location) {
256                 if (location->start() >= location->end()) {
257                         error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
258                         return -1;
259                 }
260         }
261
262         loop_location = location;
263
264          LoopSet (location); /* EMIT SIGNAL */
265         return 0;
266 }
267
268 nframes_t
269 Diskstream::get_capture_start_frame (uint32_t n)
270 {
271         Glib::Mutex::Lock lm (capture_info_lock);
272
273         if (capture_info.size() > n) {
274                 return capture_info[n]->start;
275         }
276         else {
277                 return capture_start_frame;
278         }
279 }
280
281 nframes_t
282 Diskstream::get_captured_frames (uint32_t n)
283 {
284         Glib::Mutex::Lock lm (capture_info_lock);
285
286         if (capture_info.size() > n) {
287                 return capture_info[n]->frames;
288         }
289         else {
290                 return capture_captured;
291         }
292 }
293
294 void
295 Diskstream::set_roll_delay (nframes_t nframes)
296 {
297         _roll_delay = nframes;
298 }
299
300 void
301 Diskstream::set_speed (double sp)
302 {
303         _session.request_diskstream_speed (*this, sp);
304
305         /* to force a rebuffering at the right place */
306         playlist_modified();
307 }
308
309 int
310 Diskstream::use_playlist (Playlist* playlist)
311 {
312         {
313                 Glib::Mutex::Lock lm (state_lock);
314
315                 if (playlist == _playlist) {
316                         return 0;
317                 }
318
319                 plstate_connection.disconnect();
320                 plmod_connection.disconnect ();
321                 plgone_connection.disconnect ();
322
323                 if (_playlist) {
324                         _playlist->unref();
325                 }
326                         
327                 _playlist = playlist;
328                 _playlist->ref();
329
330                 if (!in_set_state && recordable()) {
331                         reset_write_sources (false);
332                 }
333                 
334                 plmod_connection = _playlist->Modified.connect (mem_fun (*this, &Diskstream::playlist_modified));
335                 plgone_connection = _playlist->GoingAway.connect (bind (mem_fun (*this, &Diskstream::playlist_deleted), _playlist));
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::remove_region_from_last_capture (boost::weak_ptr<Region> wregion)
396 {
397         boost::shared_ptr<Region> region (wregion.lock());
398
399         if (!region) {
400                 return;
401         }
402         
403         _last_capture_regions.remove (region);
404 }
405