Work towards removal of Session's Diskstream list.
[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         init (flag);
70 }
71         
72 Diskstream::Diskstream (Session& sess, const XMLNode& node)
73         : _session (sess)
74         
75 {
76         init (Recordable);
77 }
78
79 void
80 Diskstream::init (Flag f)
81 {
82         _refcnt = 0;
83         _flags = f;
84         _io = 0;
85         _alignment_style = ExistingMaterial;
86         _persistent_alignment_style = ExistingMaterial;
87         first_input_change = true;
88         i_am_the_modifier = 0;
89         g_atomic_int_set (&_record_enabled, 0);
90         was_recording = false;
91         capture_start_frame = 0;
92         capture_captured = 0;
93         _visible_speed = 1.0f;
94         _actual_speed = 1.0f;
95         _buffer_reallocation_required = false;
96         _seek_required = false;
97         first_recordable_frame = max_frames;
98         last_recordable_frame = max_frames;
99         _roll_delay = 0;
100         _capture_offset = 0;
101         _processed = false;
102         _slaved = false;
103         adjust_capture_position = 0;
104         last_possibly_recording = 0;
105         loop_location = 0;
106         wrap_buffer_size = 0;
107         speed_buffer_size = 0;
108         last_phase = 0;
109         phi = (uint64_t) (0x1000000);
110         file_frame = 0;
111         playback_sample = 0;
112         playback_distance = 0;
113         _read_data_count = 0;
114         _write_data_count = 0;
115
116         /* there are no channels at this point, so these
117            two calls just get speed_buffer_size and wrap_buffer
118            size setup without duplicating their code.
119         */
120
121         //set_block_size (_session.get_block_size());
122         //allocate_temporary_buffers ();
123
124         pending_overwrite = false;
125         overwrite_frame = 0;
126         overwrite_queued = false;
127         input_change_pending = NoChange;
128
129         //add_channel ();
130         _n_channels = 0;//1;
131 }
132
133 Diskstream::~Diskstream ()
134 {
135         // Taken by child.. assure lock?
136         //Glib::Mutex::Lock lm (state_lock);
137
138         //if (_playlist) {
139         //      _playlist->unref ();
140         //}
141
142         //for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
143         //      destroy_channel((*chan));
144         //}
145         
146         //channels.clear();
147 }
148
149 void
150 Diskstream::handle_input_change (IOChange change, void *src)
151 {
152         Glib::Mutex::Lock lm (state_lock);
153
154         if (!(input_change_pending & change)) {
155                 input_change_pending = IOChange (input_change_pending|change);
156                 _session.request_input_change_handling ();
157         }
158 }
159
160 bool
161 Diskstream::realtime_set_speed (double sp, bool global)
162 {
163         bool changed = false;
164         double new_speed = sp * _session.transport_speed();
165         
166         if (_visible_speed != sp) {
167                 _visible_speed = sp;
168                 changed = true;
169         }
170         
171         if (new_speed != _actual_speed) {
172                 
173                 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * 
174                                                                             fabs (new_speed)) + 1;
175                 
176                 if (required_wrap_size > wrap_buffer_size) {
177                         _buffer_reallocation_required = true;
178                 }
179                 
180                 _actual_speed = new_speed;
181                 phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
182         }
183
184         if (changed) {
185                 if (!global) {
186                         _seek_required = true;
187                 }
188                 SpeedChanged (); /* EMIT SIGNAL */
189         }
190
191         return _buffer_reallocation_required || _seek_required;
192 }
193
194 void
195 Diskstream::prepare ()
196 {
197         _processed = false;
198         playback_distance = 0;
199 }
200
201 void
202 Diskstream::recover ()
203 {
204         state_lock.unlock();
205         _processed = false;
206 }
207
208 void
209 Diskstream::set_capture_offset ()
210 {
211         if (_io == 0) {
212                 /* can't capture, so forget it */
213                 return;
214         }
215
216         _capture_offset = _io->input_latency();
217 }
218
219 void
220 Diskstream::set_align_style (AlignStyle a)
221 {
222         if (record_enabled() && _session.actively_recording()) {
223                 return;
224         }
225
226
227         if (a != _alignment_style) {
228                 _alignment_style = a;
229                 AlignmentStyleChanged ();
230         }
231 }
232
233 int
234 Diskstream::set_loop (Location *location)
235 {
236         if (location) {
237                 if (location->start() >= location->end()) {
238                         error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
239                         return -1;
240                 }
241         }
242
243         loop_location = location;
244
245          LoopSet (location); /* EMIT SIGNAL */
246         return 0;
247 }
248
249 jack_nframes_t
250 Diskstream::get_capture_start_frame (uint32_t n)
251 {
252         Glib::Mutex::Lock lm (capture_info_lock);
253
254         if (capture_info.size() > n) {
255                 return capture_info[n]->start;
256         }
257         else {
258                 return capture_start_frame;
259         }
260 }
261
262 jack_nframes_t
263 Diskstream::get_captured_frames (uint32_t n)
264 {
265         Glib::Mutex::Lock lm (capture_info_lock);
266
267         if (capture_info.size() > n) {
268                 return capture_info[n]->frames;
269         }
270         else {
271                 return capture_captured;
272         }
273 }
274
275 void
276 Diskstream::set_roll_delay (jack_nframes_t nframes)
277 {
278         _roll_delay = nframes;
279 }
280
281 void
282 Diskstream::set_speed (double sp)
283 {
284         _session.request_diskstream_speed (*this, sp);
285
286         /* to force a rebuffering at the right place */
287         playlist_modified();
288 }
289
290 void
291 Diskstream::playlist_changed (Change ignored)
292 {
293         playlist_modified ();
294 }
295
296 void
297 Diskstream::playlist_modified ()
298 {
299         if (!i_am_the_modifier && !overwrite_queued) {
300                 _session.request_overwrite_buffer (this);
301                 overwrite_queued = true;
302         } 
303 }
304
305 int
306 Diskstream::set_name (string str, void *src)
307 {
308         if (str != _name) {
309                 assert(playlist());
310                 playlist()->set_name (str);
311                 _name = str;
312                 
313                 if (!in_set_state && recordable()) {
314                         /* rename existing capture files so that they have the correct name */
315                         return rename_write_sources ();
316                 } else {
317                         return -1;
318                 }
319         }
320
321         return 0;
322 }
323
324 void
325 Diskstream::set_destructive (bool yn)
326 {
327         if (yn != destructive()) {
328                 reset_write_sources (true, true);
329                 if (yn) {
330                         _flags |= Destructive;
331                 } else {
332                         _flags &= ~Destructive;
333                 }
334         }
335 }