Trim include dependency graph, especially for io.h and session.h.
[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 */
19
20 #include <fstream>
21 #include <cassert>
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 <sigc++/bind.h>
35
36 #include <pbd/error.h>
37 #include <pbd/basename.h>
38 #include <glibmm/thread.h>
39 #include <pbd/xml++.h>
40 #include <pbd/memento_command.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/send.h>
49 #include <ardour/playlist.h>
50 #include <ardour/cycle_timer.h>
51 #include <ardour/region.h>
52 #include <ardour/panner.h>
53 #include <ardour/session.h>
54 #include <ardour/io.h>
55 #include <ardour/route.h>
56
57 #include "i18n.h"
58 #include <locale.h>
59
60 using namespace std;
61 using namespace ARDOUR;
62 using namespace PBD;
63
64 /* XXX This goes uninitialized when there is no ~/.ardour3 directory.
65  * I can't figure out why, so this will do for now (just stole the
66  * default from configuration_vars.h).  0 is not a good value for
67  * allocating buffer sizes..
68  */
69 ARDOUR::nframes_t Diskstream::disk_io_chunk_frames = 1024 * 256;
70
71 sigc::signal<void>                Diskstream::DiskOverrun;
72 sigc::signal<void>                Diskstream::DiskUnderrun;
73
74 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
75         : SessionObject(sess, name)
76 {
77         init (flag);
78 }
79         
80 Diskstream::Diskstream (Session& sess, const XMLNode& node)
81         : SessionObject(sess, "unnamed diskstream")
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         // speed = 1 in 40.24 fixed point math
116         phi = (uint64_t) (0x1000000);
117         target_phi = phi;
118         file_frame = 0;
119         playback_sample = 0;
120         playback_distance = 0;
121         _read_data_count = 0;
122         _write_data_count = 0;
123         commit_should_unlock = false;
124
125         pending_overwrite = false;
126         overwrite_frame = 0;
127         overwrite_queued = false;
128         input_change_pending = NoChange;
129 }
130
131 Diskstream::~Diskstream ()
132 {
133         if (_playlist)
134                 _playlist->release ();
135 }
136
137 void
138 Diskstream::set_io (IO& io)
139 {
140         _io = &io;
141         set_align_style_from_io ();
142 }
143
144 void
145 Diskstream::handle_input_change (IOChange change, void *src)
146 {
147         Glib::Mutex::Lock lm (state_lock);
148
149         if (!(input_change_pending & change)) {
150                 input_change_pending = IOChange (input_change_pending|change);
151                 _session.request_input_change_handling ();
152         }
153 }
154
155 void
156 Diskstream::non_realtime_set_speed ()
157 {
158         if (_buffer_reallocation_required)
159         {
160                 Glib::Mutex::Lock lm (state_lock);
161                 allocate_temporary_buffers ();
162
163                 _buffer_reallocation_required = false;
164         }
165
166         if (_seek_required) {
167                 if (speed() != 1.0f || speed() != -1.0f) {
168                         seek ((nframes_t) (_session.transport_frame() * (double) speed()), true);
169                 }
170                 else {
171                         seek (_session.transport_frame(), true);
172                 }
173
174                 _seek_required = false;
175         }
176 }
177
178 bool
179 Diskstream::realtime_set_speed (double sp, bool global)
180 {
181         bool changed = false;
182         double new_speed = sp * _session.transport_speed();
183         
184         if (_visible_speed != sp) {
185                 _visible_speed = sp;
186                 changed = true;
187         }
188         
189         if (new_speed != _actual_speed) {
190                 
191                 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() * 
192                                                                             fabs (new_speed)) + 1;
193                 
194                 if (required_wrap_size > wrap_buffer_size) {
195                         _buffer_reallocation_required = true;
196                 }
197                 
198                 _actual_speed = new_speed;
199                 target_phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
200         }
201
202         if (changed) {
203                 if (!global) {
204                         _seek_required = true;
205                 }
206                 SpeedChanged (); /* EMIT SIGNAL */
207         }
208
209         return _buffer_reallocation_required || _seek_required;
210 }
211
212 void
213 Diskstream::prepare ()
214 {
215         _processed = false;
216         playback_distance = 0;
217 }
218
219 void
220 Diskstream::recover ()
221 {
222         if (commit_should_unlock) {
223                 state_lock.unlock();
224         }
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 ARDOUR::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 ARDOUR::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 (ARDOUR::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 (boost::shared_ptr<Playlist> playlist)
311 {
312         {
313                 Glib::Mutex::Lock lm (state_lock);
314
315                 if (playlist == _playlist) {
316                         return 0;
317                 }
318
319                 plmod_connection.disconnect ();
320                 plgone_connection.disconnect ();
321                 plregion_connection.disconnect ();
322
323                 if (_playlist) {
324                         _playlist->release();
325                 }
326                         
327                 _playlist = playlist;
328                 _playlist->use();
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), boost::weak_ptr<Playlist>(_playlist)));
336                 plregion_connection = _playlist->RangesMoved.connect (mem_fun (*this, &Diskstream::playlist_ranges_moved));
337         }
338
339         /* don't do this if we've already asked for it *or* if we are setting up
340            the diskstream for the very first time - the input changed handling will
341            take care of the buffer refill.
342         */
343
344         if (!overwrite_queued && !(_session.state_of_the_state() & Session::CannotSave)) {
345                 _session.request_overwrite_buffer (this);
346                 overwrite_queued = true;
347         }
348         
349         PlaylistChanged (); /* EMIT SIGNAL */
350         _session.set_dirty ();
351
352         return 0;
353 }
354
355 void
356 Diskstream::playlist_changed (Change ignored)
357 {
358         playlist_modified ();
359 }
360
361 void
362 Diskstream::playlist_modified ()
363 {
364         if (!i_am_the_modifier && !overwrite_queued) {
365                 _session.request_overwrite_buffer (this);
366                 overwrite_queued = true;
367         } 
368 }
369
370 void
371 Diskstream::playlist_deleted (boost::weak_ptr<Playlist> wpl)
372 {
373         boost::shared_ptr<Playlist> pl (wpl.lock());
374
375         if (pl == _playlist) {
376
377                 /* this catches an ordering issue with session destruction. playlists 
378                    are destroyed before diskstreams. we have to invalidate any handles
379                    we have to the playlist.
380                 */
381                 
382                 if (_playlist) {
383                         _playlist.reset ();
384                 } 
385         }
386 }
387
388 bool
389 Diskstream::set_name (const string& str)
390 {
391         if (str != _name) {
392                 assert(playlist());
393                 playlist()->set_name (str);
394                 
395                 SessionObject::set_name(str);
396                 
397                 if (!in_set_state && recordable()) {
398                         /* rename existing capture files so that they have the correct name */
399                         return rename_write_sources ();
400                 } else {
401                         return false;
402                 }
403         }
404
405         return true;
406 }
407
408 void
409 Diskstream::remove_region_from_last_capture (boost::weak_ptr<Region> wregion)
410 {
411         boost::shared_ptr<Region> region (wregion.lock());
412
413         if (!region) {
414                 return;
415         }
416         
417         _last_capture_regions.remove (region);
418 }
419
420 void
421 Diskstream::playlist_ranges_moved (Evoral::RangeMoveList const & movements)
422 {
423         if (Config->get_automation_follows_regions () == false) {
424                 return;
425         }
426         
427         /* move gain automation */
428         boost::shared_ptr<AutomationList> gain_alist = _io->gain_control()->alist();
429         XMLNode & before = gain_alist->get_state ();
430         gain_alist->move_ranges (movements);
431         _session.add_command (
432                 new MementoCommand<AutomationList> (
433                         *gain_alist.get(), &before, &gain_alist->get_state ()
434                         )
435                 );
436         
437         /* move panner automation */
438         Panner & p = _io->panner ();
439         for (uint32_t i = 0; i < p.npanners (); ++i) {
440
441                 boost::shared_ptr<AutomationList> pan_alist = p.streampanner(i).pan_control()->alist();
442                 XMLNode & before = pan_alist->get_state ();
443                 pan_alist->move_ranges (movements);
444                 _session.add_command (
445                         new MementoCommand<AutomationList> (
446                                 *pan_alist.get(), &before, &pan_alist->get_state ()
447                                 )
448                         );
449         }
450
451         /* move processor automation */
452         /* XXX: ewww */
453         Route * route = dynamic_cast<Route*> (_io);
454         if (route) {
455                 route->foreach_processor (sigc::bind (sigc::mem_fun (*this, &Diskstream::move_processor_automation), movements));
456         }
457 }
458
459 void
460 Diskstream::move_processor_automation (boost::weak_ptr<Processor> p, Evoral::RangeMoveList const & movements)
461 {
462         boost::shared_ptr<Processor> processor (p.lock ());
463         if (!processor) {
464                 return;
465         }
466         
467         set<Evoral::Parameter> const a = processor->what_can_be_automated ();
468
469         for (set<Evoral::Parameter>::iterator i = a.begin (); i != a.end (); ++i) {
470                 boost::shared_ptr<AutomationList> al = processor->automation_control(*i)->alist();
471                 XMLNode & before = al->get_state ();
472                 al->move_ranges (movements);
473                 _session.add_command (
474                         new MementoCommand<AutomationList> (
475                                 *al.get(), &before, &al->get_state ()
476                                 )
477                         );
478         }
479 }
480