2 Copyright (C) 2000-2006 Paul Davis
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.
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.
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.
18 $Id: diskstream.cc 567 2006-06-07 14:54:12Z trutkin $
35 #include <pbd/error.h>
36 #include <pbd/basename.h>
37 #include <glibmm/thread.h>
38 #include <pbd/xml++.h>
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>
56 using namespace ARDOUR;
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..
64 jack_nframes_t Diskstream::disk_io_chunk_frames = 1024 * 256;
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;
71 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
79 Diskstream::Diskstream (Session& sess, const XMLNode& node)
87 Diskstream::init (Flag f)
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;
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;
110 adjust_capture_position = 0;
111 last_possibly_recording = 0;
113 wrap_buffer_size = 0;
114 speed_buffer_size = 0;
116 phi = (uint64_t) (0x1000000);
119 playback_distance = 0;
120 _read_data_count = 0;
121 _write_data_count = 0;
123 pending_overwrite = false;
125 overwrite_queued = false;
126 input_change_pending = NoChange;
129 Diskstream::~Diskstream ()
131 // Taken by derived class destrctors.. should assure locked here somehow?
132 //Glib::Mutex::Lock lm (state_lock);
139 Diskstream::set_io (IO& io)
142 set_align_style_from_io ();
146 Diskstream::handle_input_change (IOChange change, void *src)
148 Glib::Mutex::Lock lm (state_lock);
150 if (!(input_change_pending & change)) {
151 input_change_pending = IOChange (input_change_pending|change);
152 _session.request_input_change_handling ();
157 Diskstream::non_realtime_set_speed ()
159 if (_buffer_reallocation_required)
161 Glib::Mutex::Lock lm (state_lock);
162 allocate_temporary_buffers ();
164 _buffer_reallocation_required = false;
167 if (_seek_required) {
168 if (speed() != 1.0f || speed() != -1.0f) {
169 seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()), true);
172 seek (_session.transport_frame(), true);
175 _seek_required = false;
180 Diskstream::realtime_set_speed (double sp, bool global)
182 bool changed = false;
183 double new_speed = sp * _session.transport_speed();
185 if (_visible_speed != sp) {
190 if (new_speed != _actual_speed) {
192 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() *
193 fabs (new_speed)) + 1;
195 if (required_wrap_size > wrap_buffer_size) {
196 _buffer_reallocation_required = true;
199 _actual_speed = new_speed;
200 phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
205 _seek_required = true;
207 SpeedChanged (); /* EMIT SIGNAL */
210 return _buffer_reallocation_required || _seek_required;
214 Diskstream::prepare ()
217 playback_distance = 0;
221 Diskstream::recover ()
228 Diskstream::set_capture_offset ()
231 /* can't capture, so forget it */
235 _capture_offset = _io->input_latency();
239 Diskstream::set_align_style (AlignStyle a)
241 if (record_enabled() && _session.actively_recording()) {
245 if (a != _alignment_style) {
246 _alignment_style = a;
247 AlignmentStyleChanged ();
252 Diskstream::set_loop (Location *location)
255 if (location->start() >= location->end()) {
256 error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
261 loop_location = location;
263 LoopSet (location); /* EMIT SIGNAL */
268 Diskstream::get_capture_start_frame (uint32_t n)
270 Glib::Mutex::Lock lm (capture_info_lock);
272 if (capture_info.size() > n) {
273 return capture_info[n]->start;
276 return capture_start_frame;
281 Diskstream::get_captured_frames (uint32_t n)
283 Glib::Mutex::Lock lm (capture_info_lock);
285 if (capture_info.size() > n) {
286 return capture_info[n]->frames;
289 return capture_captured;
294 Diskstream::set_roll_delay (jack_nframes_t nframes)
296 _roll_delay = nframes;
300 Diskstream::set_speed (double sp)
302 _session.request_diskstream_speed (*this, sp);
304 /* to force a rebuffering at the right place */
309 Diskstream::use_playlist (Playlist* playlist)
312 Glib::Mutex::Lock lm (state_lock);
314 if (playlist == _playlist) {
318 plstate_connection.disconnect();
319 plmod_connection.disconnect ();
320 plgone_connection.disconnect ();
326 _playlist = playlist;
329 if (!in_set_state && recordable()) {
330 reset_write_sources (false);
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));
338 if (!overwrite_queued) {
339 _session.request_overwrite_buffer (this);
340 overwrite_queued = true;
343 PlaylistChanged (); /* EMIT SIGNAL */
344 _session.set_dirty ();
350 Diskstream::playlist_changed (Change ignored)
352 playlist_modified ();
356 Diskstream::playlist_modified ()
358 if (!i_am_the_modifier && !overwrite_queued) {
359 _session.request_overwrite_buffer (this);
360 overwrite_queued = true;
365 Diskstream::playlist_deleted (Playlist* pl)
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.
376 Diskstream::set_name (string str)
380 playlist()->set_name (str);
383 if (!in_set_state && recordable()) {
384 /* rename existing capture files so that they have the correct name */
385 return rename_write_sources ();
395 Diskstream::set_destructive (bool yn)
397 if (yn != destructive()) {
398 reset_write_sources (true, true);
400 _flags |= Destructive;
402 _flags &= ~Destructive;