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 <sigc++/bind.h>
37 #include <pbd/error.h>
38 #include <pbd/basename.h>
39 #include <glibmm/thread.h>
40 #include <pbd/xml++.h>
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>
57 using namespace ARDOUR;
60 /* XXX This goes uninitialized when there is no ~/.ardour2 directory.
61 * I can't figure out why, so this will do for now (just stole the
62 * default from configuration_vars.h). 0 is not a good value for
63 * allocating buffer sizes..
65 nframes_t Diskstream::disk_io_chunk_frames = 1024 * 256;
67 sigc::signal<void> Diskstream::DiskOverrun;
68 sigc::signal<void> Diskstream::DiskUnderrun;
70 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
77 Diskstream::Diskstream (Session& sess, const XMLNode& node)
84 Diskstream::init (Flag f)
88 _alignment_style = ExistingMaterial;
89 _persistent_alignment_style = ExistingMaterial;
90 first_input_change = true;
91 i_am_the_modifier = 0;
92 g_atomic_int_set (&_record_enabled, 0);
93 was_recording = false;
94 capture_start_frame = 0;
96 _visible_speed = 1.0f;
98 _buffer_reallocation_required = false;
99 _seek_required = false;
100 first_recordable_frame = max_frames;
101 last_recordable_frame = max_frames;
106 adjust_capture_position = 0;
107 last_possibly_recording = 0;
109 wrap_buffer_size = 0;
110 speed_buffer_size = 0;
112 phi = (uint64_t) (0x1000000);
115 playback_distance = 0;
116 _read_data_count = 0;
117 _write_data_count = 0;
119 pending_overwrite = false;
121 overwrite_queued = false;
122 input_change_pending = NoChange;
127 Diskstream::~Diskstream ()
129 // Taken by derived class destrctors.. should assure locked here somehow?
130 //Glib::Mutex::Lock lm (state_lock);
133 _playlist->release ();
137 Diskstream::set_io (IO& io)
140 set_align_style_from_io ();
144 Diskstream::handle_input_change (IOChange change, void *src)
146 Glib::Mutex::Lock lm (state_lock);
148 if (!(input_change_pending & change)) {
149 input_change_pending = IOChange (input_change_pending|change);
150 _session.request_input_change_handling ();
155 Diskstream::non_realtime_set_speed ()
157 if (_buffer_reallocation_required)
159 Glib::Mutex::Lock lm (state_lock);
160 allocate_temporary_buffers ();
162 _buffer_reallocation_required = false;
165 if (_seek_required) {
166 if (speed() != 1.0f || speed() != -1.0f) {
167 seek ((nframes_t) (_session.transport_frame() * (double) speed()), true);
170 seek (_session.transport_frame(), true);
173 _seek_required = false;
178 Diskstream::realtime_set_speed (double sp, bool global)
180 bool changed = false;
181 double new_speed = sp * _session.transport_speed();
183 if (_visible_speed != sp) {
188 if (new_speed != _actual_speed) {
190 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() *
191 fabs (new_speed)) + 1;
193 if (required_wrap_size > wrap_buffer_size) {
194 _buffer_reallocation_required = true;
197 _actual_speed = new_speed;
198 phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
203 _seek_required = true;
205 SpeedChanged (); /* EMIT SIGNAL */
208 return _buffer_reallocation_required || _seek_required;
212 Diskstream::prepare ()
215 playback_distance = 0;
219 Diskstream::recover ()
226 Diskstream::set_capture_offset ()
229 /* can't capture, so forget it */
233 _capture_offset = _io->input_latency();
237 Diskstream::set_align_style (AlignStyle a)
239 if (record_enabled() && _session.actively_recording()) {
243 if (a != _alignment_style) {
244 _alignment_style = a;
245 AlignmentStyleChanged ();
250 Diskstream::set_loop (Location *location)
253 if (location->start() >= location->end()) {
254 error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
259 loop_location = location;
261 LoopSet (location); /* EMIT SIGNAL */
266 Diskstream::get_capture_start_frame (uint32_t n)
268 Glib::Mutex::Lock lm (capture_info_lock);
270 if (capture_info.size() > n) {
271 return capture_info[n]->start;
274 return capture_start_frame;
279 Diskstream::get_captured_frames (uint32_t n)
281 Glib::Mutex::Lock lm (capture_info_lock);
283 if (capture_info.size() > n) {
284 return capture_info[n]->frames;
287 return capture_captured;
292 Diskstream::set_roll_delay (nframes_t nframes)
294 _roll_delay = nframes;
298 Diskstream::set_speed (double sp)
300 _session.request_diskstream_speed (*this, sp);
302 /* to force a rebuffering at the right place */
307 Diskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
310 Glib::Mutex::Lock lm (state_lock);
312 if (playlist == _playlist) {
316 plmod_connection.disconnect ();
317 plgone_connection.disconnect ();
320 _playlist->release();
323 _playlist = playlist;
326 if (!in_set_state && recordable()) {
327 reset_write_sources (false);
330 plmod_connection = _playlist->Modified.connect (mem_fun (*this, &Diskstream::playlist_modified));
331 plgone_connection = _playlist->GoingAway.connect (bind (mem_fun (*this, &Diskstream::playlist_deleted), boost::weak_ptr<Playlist>(_playlist)));
334 /* don't do this if we've already asked for it *or* if we are setting up
335 the diskstream for the very first time - the input changed handling will
336 take care of the buffer refill.
339 if (!overwrite_queued && !(_session.state_of_the_state() & Session::CannotSave)) {
340 _session.request_overwrite_buffer (this);
341 overwrite_queued = true;
344 PlaylistChanged (); /* EMIT SIGNAL */
345 _session.set_dirty ();
351 Diskstream::playlist_changed (Change ignored)
353 playlist_modified ();
357 Diskstream::playlist_modified ()
359 if (!i_am_the_modifier && !overwrite_queued) {
360 _session.request_overwrite_buffer (this);
361 overwrite_queued = true;
366 Diskstream::playlist_deleted (boost::weak_ptr<Playlist> wpl)
368 boost::shared_ptr<Playlist> pl (wpl.lock());
370 if (pl == _playlist) {
372 /* this catches an ordering issue with session destruction. playlists
373 are destroyed before diskstreams. we have to invalidate any handles
374 we have to the playlist.
384 Diskstream::set_name (string str)
388 playlist()->set_name (str);
391 if (!in_set_state && recordable()) {
392 /* rename existing capture files so that they have the correct name */
393 return rename_write_sources ();
403 Diskstream::remove_region_from_last_capture (boost::weak_ptr<Region> wregion)
405 boost::shared_ptr<Region> region (wregion.lock());
411 _last_capture_regions.remove (region);