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)
72 Diskstream::Diskstream (Session& sess, const XMLNode& node)
80 Diskstream::init (Flag f)
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;
93 _visible_speed = 1.0f;
95 _buffer_reallocation_required = false;
96 _seek_required = false;
97 first_recordable_frame = max_frames;
98 last_recordable_frame = max_frames;
103 adjust_capture_position = 0;
104 last_possibly_recording = 0;
106 wrap_buffer_size = 0;
107 speed_buffer_size = 0;
109 phi = (uint64_t) (0x1000000);
112 playback_distance = 0;
113 _read_data_count = 0;
114 _write_data_count = 0;
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.
121 //set_block_size (_session.get_block_size());
122 //allocate_temporary_buffers ();
124 pending_overwrite = false;
126 overwrite_queued = false;
127 input_change_pending = NoChange;
133 Diskstream::~Diskstream ()
135 // Taken by child.. assure lock?
136 //Glib::Mutex::Lock lm (state_lock);
139 // _playlist->unref ();
142 //for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
143 // destroy_channel((*chan));
150 Diskstream::handle_input_change (IOChange change, void *src)
152 Glib::Mutex::Lock lm (state_lock);
154 if (!(input_change_pending & change)) {
155 input_change_pending = IOChange (input_change_pending|change);
156 _session.request_input_change_handling ();
161 Diskstream::realtime_set_speed (double sp, bool global)
163 bool changed = false;
164 double new_speed = sp * _session.transport_speed();
166 if (_visible_speed != sp) {
171 if (new_speed != _actual_speed) {
173 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() *
174 fabs (new_speed)) + 1;
176 if (required_wrap_size > wrap_buffer_size) {
177 _buffer_reallocation_required = true;
180 _actual_speed = new_speed;
181 phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
186 _seek_required = true;
188 SpeedChanged (); /* EMIT SIGNAL */
191 return _buffer_reallocation_required || _seek_required;
195 Diskstream::prepare ()
198 playback_distance = 0;
202 Diskstream::recover ()
209 Diskstream::set_capture_offset ()
212 /* can't capture, so forget it */
216 _capture_offset = _io->input_latency();
220 Diskstream::set_align_style (AlignStyle a)
222 if (record_enabled() && _session.actively_recording()) {
227 if (a != _alignment_style) {
228 _alignment_style = a;
229 AlignmentStyleChanged ();
234 Diskstream::set_loop (Location *location)
237 if (location->start() >= location->end()) {
238 error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
243 loop_location = location;
245 LoopSet (location); /* EMIT SIGNAL */
250 Diskstream::get_capture_start_frame (uint32_t n)
252 Glib::Mutex::Lock lm (capture_info_lock);
254 if (capture_info.size() > n) {
255 return capture_info[n]->start;
258 return capture_start_frame;
263 Diskstream::get_captured_frames (uint32_t n)
265 Glib::Mutex::Lock lm (capture_info_lock);
267 if (capture_info.size() > n) {
268 return capture_info[n]->frames;
271 return capture_captured;
276 Diskstream::set_roll_delay (jack_nframes_t nframes)
278 _roll_delay = nframes;
282 Diskstream::set_speed (double sp)
284 _session.request_diskstream_speed (*this, sp);
286 /* to force a rebuffering at the right place */
291 Diskstream::playlist_changed (Change ignored)
293 playlist_modified ();
297 Diskstream::playlist_modified ()
299 if (!i_am_the_modifier && !overwrite_queued) {
300 _session.request_overwrite_buffer (this);
301 overwrite_queued = true;
306 Diskstream::set_name (string str, void *src)
310 playlist()->set_name (str);
313 if (!in_set_state && recordable()) {
314 /* rename existing capture files so that they have the correct name */
315 return rename_write_sources ();
325 Diskstream::set_destructive (bool yn)
327 if (yn != destructive()) {
328 reset_write_sources (true, true);
330 _flags |= Destructive;
332 _flags &= ~Destructive;