Actually added the code mentioned in my last commit. Whoops.
[ardour.git] / libs / ardour / diskstream.cc
1 /*
2     Copyright (C) 2000-2003 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 <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 <pbd/error.h>
35 #include <pbd/basename.h>
36 #include <glibmm/thread.h>
37 #include <pbd/xml++.h>
38
39 #include <ardour/ardour.h>
40 #include <ardour/audioengine.h>
41 #include <ardour/diskstream.h>
42 #include <ardour/utils.h>
43 #include <ardour/configuration.h>
44 #include <ardour/audiofilesource.h>
45 #include <ardour/destructive_filesource.h>
46 #include <ardour/send.h>
47 #include <ardour/playlist.h>
48 #include <ardour/cycle_timer.h>
49 #include <ardour/region.h>
50
51 #include "i18n.h"
52 #include <locale.h>
53
54 using namespace std;
55 using namespace ARDOUR;
56 using namespace PBD;
57
58 jack_nframes_t Diskstream::disk_io_chunk_frames;
59
60 sigc::signal<void,Diskstream*>    Diskstream::DiskstreamCreated;
61 //sigc::signal<void,list<AudioFileSource*>*> Diskstream::DeleteSources;
62 sigc::signal<void>                Diskstream::DiskOverrun;
63 sigc::signal<void>                Diskstream::DiskUnderrun;
64
65 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
66         : _name (name)
67         , _session (sess)
68 {
69 #if 0
70         /* prevent any write sources from being created */
71
72         in_set_state = true;
73
74         init (flag);
75         //use_new_playlist ();
76
77         in_set_state = false;
78         DiskstreamCreated (this); /* EMIT SIGNAL */
79 #endif
80 }
81         
82 Diskstream::Diskstream (Session& sess, const XMLNode& node)
83         : _session (sess)
84         
85 {
86 #if 0
87         in_set_state = true;
88         init (Recordable);
89
90         /*if (set_state (node)) {
91                 in_set_state = false;
92                 throw failed_constructor();
93         }*/
94
95         in_set_state = false;
96
97         //if (destructive()) {
98         //      use_destructive_playlist ();
99         //}
100         DiskstreamCreated (this); /* EMIT SIGNAL */
101 #endif
102 }
103
104 void
105 Diskstream::init (Flag f)
106 {
107         _id = new_id();
108         _refcnt = 0;
109         _flags = f;
110         _io = 0;
111         _alignment_style = ExistingMaterial;
112         _persistent_alignment_style = ExistingMaterial;
113         first_input_change = true;
114         i_am_the_modifier = 0;
115         g_atomic_int_set (&_record_enabled, 0);
116         was_recording = false;
117         capture_start_frame = 0;
118         capture_captured = 0;
119         _visible_speed = 1.0f;
120         _actual_speed = 1.0f;
121         _buffer_reallocation_required = false;
122         _seek_required = false;
123         first_recordable_frame = max_frames;
124         last_recordable_frame = max_frames;
125         _roll_delay = 0;
126         _capture_offset = 0;
127         _processed = false;
128         _slaved = false;
129         adjust_capture_position = 0;
130         last_possibly_recording = 0;
131         loop_location = 0;
132         wrap_buffer_size = 0;
133         speed_buffer_size = 0;
134         last_phase = 0;
135         phi = (uint64_t) (0x1000000);
136         file_frame = 0;
137         playback_sample = 0;
138         playback_distance = 0;
139         _read_data_count = 0;
140         _write_data_count = 0;
141         deprecated_io_node = 0;
142
143         /* there are no channels at this point, so these
144            two calls just get speed_buffer_size and wrap_buffer
145            size setup without duplicating their code.
146         */
147
148         //set_block_size (_session.get_block_size());
149         //allocate_temporary_buffers ();
150
151         pending_overwrite = false;
152         overwrite_frame = 0;
153         overwrite_queued = false;
154         input_change_pending = NoChange;
155
156         //add_channel ();
157         _n_channels = 0;//1;
158 }
159
160 Diskstream::~Diskstream ()
161 {
162         // Taken by child.. assure lock?
163         //Glib::Mutex::Lock lm (state_lock);
164
165         //if (_playlist) {
166         //      _playlist->unref ();
167         //}
168
169         //for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
170         //      destroy_channel((*chan));
171         //}
172         
173         //channels.clear();
174 }
175
176 void
177 Diskstream::handle_input_change (IOChange change, void *src)
178 {
179         Glib::Mutex::Lock lm (state_lock);
180
181         if (!(input_change_pending & change)) {
182                 input_change_pending = IOChange (input_change_pending|change);
183                 _session.request_input_change_handling ();
184         }
185 }
186
187 bool
188 Diskstream::realtime_set_speed (double sp, bool global)
189 {
190         bool changed = false;
191         double new_speed = sp * _session.transport_speed();
192         
193         if (_visible_speed != sp) {
194                 _visible_speed = sp;
195                 changed = true;
196         }
197         
198         if (new_speed != _actual_speed) {
199                 
200                 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * 
201                                                                             fabs (new_speed)) + 1;
202                 
203                 if (required_wrap_size > wrap_buffer_size) {
204                         _buffer_reallocation_required = true;
205                 }
206                 
207                 _actual_speed = new_speed;
208                 phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
209         }
210
211         if (changed) {
212                 if (!global) {
213                         _seek_required = true;
214                 }
215                  speed_changed (); /* EMIT SIGNAL */
216         }
217
218         return _buffer_reallocation_required || _seek_required;
219 }
220
221 void
222 Diskstream::prepare ()
223 {
224         _processed = false;
225         playback_distance = 0;
226 }
227
228 void
229 Diskstream::recover ()
230 {
231         state_lock.unlock();
232         _processed = false;
233 }
234
235 void
236 Diskstream::set_capture_offset ()
237 {
238         if (_io == 0) {
239                 /* can't capture, so forget it */
240                 return;
241         }
242
243         _capture_offset = _io->input_latency();
244 }
245
246 void
247 Diskstream::set_align_style (AlignStyle a)
248 {
249         if (record_enabled() && _session.actively_recording()) {
250                 return;
251         }
252
253
254         if (a != _alignment_style) {
255                 _alignment_style = a;
256                 AlignmentStyleChanged ();
257         }
258 }
259
260 int
261 Diskstream::set_loop (Location *location)
262 {
263         if (location) {
264                 if (location->start() >= location->end()) {
265                         error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
266                         return -1;
267                 }
268         }
269
270         loop_location = location;
271
272          LoopSet (location); /* EMIT SIGNAL */
273         return 0;
274 }
275
276 jack_nframes_t
277 Diskstream::get_capture_start_frame (uint32_t n)
278 {
279         Glib::Mutex::Lock lm (capture_info_lock);
280
281         if (capture_info.size() > n) {
282                 return capture_info[n]->start;
283         }
284         else {
285                 return capture_start_frame;
286         }
287 }
288
289 jack_nframes_t
290 Diskstream::get_captured_frames (uint32_t n)
291 {
292         Glib::Mutex::Lock lm (capture_info_lock);
293
294         if (capture_info.size() > n) {
295                 return capture_info[n]->frames;
296         }
297         else {
298                 return capture_captured;
299         }
300 }
301
302 void
303 Diskstream::set_roll_delay (jack_nframes_t nframes)
304 {
305         _roll_delay = nframes;
306 }
307
308 void
309 Diskstream::set_speed (double sp)
310 {
311         _session.request_diskstream_speed (*this, sp);
312
313         /* to force a rebuffering at the right place */
314         playlist_modified();
315 }
316
317 void
318 Diskstream::playlist_changed (Change ignored)
319 {
320         playlist_modified ();
321 }
322
323 void
324 Diskstream::playlist_modified ()
325 {
326         if (!i_am_the_modifier && !overwrite_queued) {
327                 _session.request_overwrite_buffer (this);
328                 overwrite_queued = true;
329         } 
330 }
331
332 int
333 Diskstream::set_name (string str, void *src)
334 {
335         if (str != _name) {
336                 assert(playlist());
337                 playlist()->set_name (str);
338                 _name = str;
339                 
340                 if (!in_set_state && recordable()) {
341                         /* rename existing capture files so that they have the correct name */
342                         return rename_write_sources ();
343                 } else {
344                         return -1;
345                 }
346         }
347
348         return 0;
349 }
350
351 void
352 Diskstream::set_destructive (bool yn)
353 {
354         if (yn != destructive()) {
355                 reset_write_sources (true, true);
356                 if (yn) {
357                         _flags |= Destructive;
358                 } else {
359                         _flags &= ~Destructive;
360                 }
361         }
362 }