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.
34 #include <sigc++/bind.h>
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>
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>
58 using namespace ARDOUR;
61 /* XXX This goes uninitialized when there is no ~/.ardour3 directory.
62 * I can't figure out why, so this will do for now (just stole the
63 * default from configuration_vars.h). 0 is not a good value for
64 * allocating buffer sizes..
66 ARDOUR::nframes_t Diskstream::disk_io_chunk_frames = 1024 * 256;
68 sigc::signal<void> Diskstream::DiskOverrun;
69 sigc::signal<void> Diskstream::DiskUnderrun;
71 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
72 : SessionObject(sess, name)
77 Diskstream::Diskstream (Session& sess, const XMLNode& node)
78 : SessionObject(sess, "unnamed diskstream")
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);
116 playback_distance = 0;
117 _read_data_count = 0;
118 _write_data_count = 0;
119 commit_should_unlock = false;
121 pending_overwrite = false;
123 overwrite_queued = false;
124 input_change_pending = NoChange;
127 Diskstream::~Diskstream ()
130 _playlist->release ();
134 Diskstream::set_io (IO& io)
137 set_align_style_from_io ();
141 Diskstream::handle_input_change (IOChange change, void *src)
143 Glib::Mutex::Lock lm (state_lock);
145 if (!(input_change_pending & change)) {
146 input_change_pending = IOChange (input_change_pending|change);
147 _session.request_input_change_handling ();
152 Diskstream::non_realtime_set_speed ()
154 if (_buffer_reallocation_required)
156 Glib::Mutex::Lock lm (state_lock);
157 allocate_temporary_buffers ();
159 _buffer_reallocation_required = false;
162 if (_seek_required) {
163 if (speed() != 1.0f || speed() != -1.0f) {
164 seek ((nframes_t) (_session.transport_frame() * (double) speed()), true);
167 seek (_session.transport_frame(), true);
170 _seek_required = false;
175 Diskstream::realtime_set_speed (double sp, bool global)
177 bool changed = false;
178 double new_speed = sp * _session.transport_speed();
180 if (_visible_speed != sp) {
185 if (new_speed != _actual_speed) {
187 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() *
188 fabs (new_speed)) + 1;
190 if (required_wrap_size > wrap_buffer_size) {
191 _buffer_reallocation_required = true;
194 _actual_speed = new_speed;
195 target_phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
200 _seek_required = true;
202 SpeedChanged (); /* EMIT SIGNAL */
205 return _buffer_reallocation_required || _seek_required;
209 Diskstream::prepare ()
212 playback_distance = 0;
216 Diskstream::recover ()
218 if (commit_should_unlock) {
225 Diskstream::set_capture_offset ()
228 /* can't capture, so forget it */
232 _capture_offset = _io->input_latency();
236 Diskstream::set_align_style (AlignStyle a)
238 if (record_enabled() && _session.actively_recording()) {
242 if (a != _alignment_style) {
243 _alignment_style = a;
244 AlignmentStyleChanged ();
249 Diskstream::set_loop (Location *location)
252 if (location->start() >= location->end()) {
253 error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
258 loop_location = location;
260 LoopSet (location); /* EMIT SIGNAL */
265 Diskstream::get_capture_start_frame (uint32_t n)
267 Glib::Mutex::Lock lm (capture_info_lock);
269 if (capture_info.size() > n) {
270 return capture_info[n]->start;
273 return capture_start_frame;
278 Diskstream::get_captured_frames (uint32_t n)
280 Glib::Mutex::Lock lm (capture_info_lock);
282 if (capture_info.size() > n) {
283 return capture_info[n]->frames;
286 return capture_captured;
291 Diskstream::set_roll_delay (ARDOUR::nframes_t nframes)
293 _roll_delay = nframes;
297 Diskstream::set_speed (double sp)
299 _session.request_diskstream_speed (*this, sp);
301 /* to force a rebuffering at the right place */
306 Diskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
309 Glib::Mutex::Lock lm (state_lock);
311 if (playlist == _playlist) {
315 plmod_connection.disconnect ();
316 plgone_connection.disconnect ();
317 plregion_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)));
332 plregion_connection = _playlist->RangesMoved.connect (mem_fun (*this, &Diskstream::playlist_ranges_moved));
335 /* don't do this if we've already asked for it *or* if we are setting up
336 the diskstream for the very first time - the input changed handling will
337 take care of the buffer refill.
340 if (!overwrite_queued && !(_session.state_of_the_state() & Session::CannotSave)) {
341 _session.request_overwrite_buffer (this);
342 overwrite_queued = true;
345 PlaylistChanged (); /* EMIT SIGNAL */
346 _session.set_dirty ();
352 Diskstream::playlist_changed (Change ignored)
354 playlist_modified ();
358 Diskstream::playlist_modified ()
360 if (!i_am_the_modifier && !overwrite_queued) {
361 _session.request_overwrite_buffer (this);
362 overwrite_queued = true;
367 Diskstream::playlist_deleted (boost::weak_ptr<Playlist> wpl)
369 boost::shared_ptr<Playlist> pl (wpl.lock());
371 if (pl == _playlist) {
373 /* this catches an ordering issue with session destruction. playlists
374 are destroyed before diskstreams. we have to invalidate any handles
375 we have to the playlist.
385 Diskstream::set_name (const string& str)
389 playlist()->set_name (str);
391 SessionObject::set_name(str);
393 if (!in_set_state && recordable()) {
394 /* rename existing capture files so that they have the correct name */
395 return rename_write_sources ();
405 Diskstream::remove_region_from_last_capture (boost::weak_ptr<Region> wregion)
407 boost::shared_ptr<Region> region (wregion.lock());
413 _last_capture_regions.remove (region);
417 Diskstream::playlist_ranges_moved (Evoral::RangeMoveList const & movements)
419 if (Config->get_automation_follows_regions () == false) {
423 /* move gain automation */
424 boost::shared_ptr<AutomationList> gain_alist = _io->gain_control()->alist();
425 XMLNode & before = gain_alist->get_state ();
426 gain_alist->move_ranges (movements);
427 _session.add_command (
428 new MementoCommand<AutomationList> (
429 *gain_alist.get(), &before, &gain_alist->get_state ()
433 /* move panner automation */
434 Panner & p = _io->panner ();
435 for (uint32_t i = 0; i < p.npanners (); ++i) {
437 boost::shared_ptr<AutomationList> pan_alist = p.streampanner(i).pan_control()->alist();
438 XMLNode & before = pan_alist->get_state ();
439 pan_alist->move_ranges (movements);
440 _session.add_command (
441 new MementoCommand<AutomationList> (
442 *pan_alist.get(), &before, &pan_alist->get_state ()
447 /* move processor automation */
449 Route * route = dynamic_cast<Route*> (_io);
451 route->foreach_processor (sigc::bind (sigc::mem_fun (*this, &Diskstream::move_processor_automation), movements));
456 Diskstream::move_processor_automation (boost::weak_ptr<Processor> p, Evoral::RangeMoveList const & movements)
458 boost::shared_ptr<Processor> processor (p.lock ());
463 set<Evoral::Parameter> const a = processor->what_can_be_automated ();
465 for (set<Evoral::Parameter>::iterator i = a.begin (); i != a.end (); ++i) {
466 boost::shared_ptr<AutomationList> al = processor->automation_control(*i)->alist();
467 XMLNode & before = al->get_state ();
468 al->move_ranges (movements);
469 _session.add_command (
470 new MementoCommand<AutomationList> (
471 *al.get(), &before, &al->get_state ()