2 Copyright (C) 2000-2003 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 $
34 #include <pbd/error.h>
35 #include <pbd/basename.h>
36 #include <glibmm/thread.h>
37 #include <pbd/xml++.h>
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>
55 using namespace ARDOUR;
58 jack_nframes_t Diskstream::disk_io_chunk_frames;
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;
65 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
70 /* prevent any write sources from being created */
75 //use_new_playlist ();
78 DiskstreamCreated (this); /* EMIT SIGNAL */
82 Diskstream::Diskstream (Session& sess, const XMLNode& node)
90 /*if (set_state (node)) {
92 throw failed_constructor();
97 //if (destructive()) {
98 // use_destructive_playlist ();
100 DiskstreamCreated (this); /* EMIT SIGNAL */
105 Diskstream::init (Flag f)
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;
129 adjust_capture_position = 0;
130 last_possibly_recording = 0;
132 wrap_buffer_size = 0;
133 speed_buffer_size = 0;
135 phi = (uint64_t) (0x1000000);
138 playback_distance = 0;
139 _read_data_count = 0;
140 _write_data_count = 0;
141 deprecated_io_node = 0;
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.
148 //set_block_size (_session.get_block_size());
149 //allocate_temporary_buffers ();
151 pending_overwrite = false;
153 overwrite_queued = false;
154 input_change_pending = NoChange;
160 Diskstream::~Diskstream ()
162 // Taken by child.. assure lock?
163 //Glib::Mutex::Lock lm (state_lock);
166 // _playlist->unref ();
169 //for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
170 // destroy_channel((*chan));
177 Diskstream::handle_input_change (IOChange change, void *src)
179 Glib::Mutex::Lock lm (state_lock);
181 if (!(input_change_pending & change)) {
182 input_change_pending = IOChange (input_change_pending|change);
183 _session.request_input_change_handling ();
188 Diskstream::realtime_set_speed (double sp, bool global)
190 bool changed = false;
191 double new_speed = sp * _session.transport_speed();
193 if (_visible_speed != sp) {
198 if (new_speed != _actual_speed) {
200 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() *
201 fabs (new_speed)) + 1;
203 if (required_wrap_size > wrap_buffer_size) {
204 _buffer_reallocation_required = true;
207 _actual_speed = new_speed;
208 phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
213 _seek_required = true;
215 speed_changed (); /* EMIT SIGNAL */
218 return _buffer_reallocation_required || _seek_required;
222 Diskstream::prepare ()
225 playback_distance = 0;
229 Diskstream::recover ()
236 Diskstream::set_capture_offset ()
239 /* can't capture, so forget it */
243 _capture_offset = _io->input_latency();
247 Diskstream::set_align_style (AlignStyle a)
249 if (record_enabled() && _session.actively_recording()) {
254 if (a != _alignment_style) {
255 _alignment_style = a;
256 AlignmentStyleChanged ();
261 Diskstream::set_loop (Location *location)
264 if (location->start() >= location->end()) {
265 error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
270 loop_location = location;
272 LoopSet (location); /* EMIT SIGNAL */
277 Diskstream::get_capture_start_frame (uint32_t n)
279 Glib::Mutex::Lock lm (capture_info_lock);
281 if (capture_info.size() > n) {
282 return capture_info[n]->start;
285 return capture_start_frame;
290 Diskstream::get_captured_frames (uint32_t n)
292 Glib::Mutex::Lock lm (capture_info_lock);
294 if (capture_info.size() > n) {
295 return capture_info[n]->frames;
298 return capture_captured;
303 Diskstream::set_roll_delay (jack_nframes_t nframes)
305 _roll_delay = nframes;
309 Diskstream::set_speed (double sp)
311 _session.request_diskstream_speed (*this, sp);
313 /* to force a rebuffering at the right place */
318 Diskstream::playlist_changed (Change ignored)
320 playlist_modified ();
324 Diskstream::playlist_modified ()
326 if (!i_am_the_modifier && !overwrite_queued) {
327 _session.request_overwrite_buffer (this);
328 overwrite_queued = true;
333 Diskstream::set_name (string str, void *src)
337 playlist()->set_name (str);
340 if (!in_set_state && recordable()) {
341 /* rename existing capture files so that they have the correct name */
342 return rename_write_sources ();
352 Diskstream::set_destructive (bool yn)
354 if (yn != destructive()) {
355 reset_write_sources (true, true);
357 _flags |= Destructive;
359 _flags &= ~Destructive;