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"
53 #include "ardour/session.h"
54 #include "ardour/io.h"
55 #include "ardour/route.h"
61 using namespace ARDOUR;
64 /* XXX This goes uninitialized when there is no ~/.ardour3 directory.
65 * I can't figure out why, so this will do for now (just stole the
66 * default from configuration_vars.h). 0 is not a good value for
67 * allocating buffer sizes..
69 ARDOUR::nframes_t Diskstream::disk_io_chunk_frames = 1024 * 256;
71 sigc::signal<void> Diskstream::DiskOverrun;
72 sigc::signal<void> Diskstream::DiskUnderrun;
74 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
75 : SessionObject(sess, name)
80 Diskstream::Diskstream (Session& sess, const XMLNode& node)
81 : SessionObject(sess, "unnamed diskstream")
87 Diskstream::init (Flag f)
91 _alignment_style = ExistingMaterial;
92 _persistent_alignment_style = ExistingMaterial;
93 first_input_change = true;
94 i_am_the_modifier = 0;
95 g_atomic_int_set (&_record_enabled, 0);
96 was_recording = false;
97 capture_start_frame = 0;
99 _visible_speed = 1.0f;
100 _actual_speed = 1.0f;
101 _buffer_reallocation_required = false;
102 _seek_required = false;
103 first_recordable_frame = max_frames;
104 last_recordable_frame = max_frames;
109 adjust_capture_position = 0;
110 last_possibly_recording = 0;
112 wrap_buffer_size = 0;
113 speed_buffer_size = 0;
115 _target_speed = _speed;
118 playback_distance = 0;
119 _read_data_count = 0;
120 _write_data_count = 0;
121 commit_should_unlock = false;
123 pending_overwrite = false;
125 overwrite_queued = false;
126 input_change_pending = NoChange;
129 Diskstream::~Diskstream ()
132 _playlist->release ();
136 Diskstream::set_io (IO& io)
139 input_change_pending = ConfigurationChanged;
140 non_realtime_input_change ();
141 set_align_style_from_io ();
145 Diskstream::handle_input_change (IOChange change, void *src)
147 Glib::Mutex::Lock lm (state_lock);
149 if (!(input_change_pending & change)) {
150 input_change_pending = IOChange (input_change_pending|change);
151 _session.request_input_change_handling ();
156 Diskstream::non_realtime_set_speed ()
158 if (_buffer_reallocation_required)
160 Glib::Mutex::Lock lm (state_lock);
161 allocate_temporary_buffers ();
163 _buffer_reallocation_required = false;
166 if (_seek_required) {
167 if (speed() != 1.0f || speed() != -1.0f) {
168 seek ((nframes_t) (_session.transport_frame() * (double) speed()), true);
171 seek (_session.transport_frame(), true);
174 _seek_required = false;
179 Diskstream::realtime_set_speed (double sp, bool global)
181 bool changed = false;
182 double new_speed = sp * _session.transport_speed();
184 if (_visible_speed != sp) {
189 if (new_speed != _actual_speed) {
191 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() *
192 fabs (new_speed)) + 1;
194 if (required_wrap_size > wrap_buffer_size) {
195 _buffer_reallocation_required = true;
198 _actual_speed = new_speed;
199 _target_speed = fabs(_actual_speed);
204 _seek_required = true;
206 SpeedChanged (); /* EMIT SIGNAL */
209 return _buffer_reallocation_required || _seek_required;
213 Diskstream::prepare ()
216 playback_distance = 0;
220 Diskstream::recover ()
222 if (commit_should_unlock) {
229 Diskstream::set_capture_offset ()
232 /* can't capture, so forget it */
236 _capture_offset = _io->latency();
240 Diskstream::set_align_style (AlignStyle a)
242 if (record_enabled() && _session.actively_recording()) {
246 if (a != _alignment_style) {
247 _alignment_style = a;
248 AlignmentStyleChanged ();
253 Diskstream::set_loop (Location *location)
256 if (location->start() >= location->end()) {
257 error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
262 loop_location = location;
264 LoopSet (location); /* EMIT SIGNAL */
269 Diskstream::get_capture_start_frame (uint32_t n)
271 Glib::Mutex::Lock lm (capture_info_lock);
273 if (capture_info.size() > n) {
274 return capture_info[n]->start;
277 return capture_start_frame;
282 Diskstream::get_captured_frames (uint32_t n)
284 Glib::Mutex::Lock lm (capture_info_lock);
286 if (capture_info.size() > n) {
287 return capture_info[n]->frames;
290 return capture_captured;
295 Diskstream::set_roll_delay (ARDOUR::nframes_t nframes)
297 _roll_delay = nframes;
301 Diskstream::set_speed (double sp)
303 _session.request_diskstream_speed (*this, sp);
305 /* to force a rebuffering at the right place */
310 Diskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
313 Glib::Mutex::Lock lm (state_lock);
315 if (playlist == _playlist) {
319 plmod_connection.disconnect ();
320 plgone_connection.disconnect ();
321 plregion_connection.disconnect ();
324 _playlist->release();
327 _playlist = playlist;
330 if (!in_set_state && recordable()) {
331 reset_write_sources (false);
334 plmod_connection = _playlist->Modified.connect (mem_fun (*this, &Diskstream::playlist_modified));
335 plgone_connection = _playlist->GoingAway.connect (bind (mem_fun (*this, &Diskstream::playlist_deleted), boost::weak_ptr<Playlist>(_playlist)));
336 plregion_connection = _playlist->RangesMoved.connect (mem_fun (*this, &Diskstream::playlist_ranges_moved));
339 /* don't do this if we've already asked for it *or* if we are setting up
340 the diskstream for the very first time - the input changed handling will
341 take care of the buffer refill.
344 if (!overwrite_queued && !(_session.state_of_the_state() & Session::CannotSave)) {
345 _session.request_overwrite_buffer (this);
346 overwrite_queued = true;
349 PlaylistChanged (); /* EMIT SIGNAL */
350 _session.set_dirty ();
356 Diskstream::playlist_changed (Change ignored)
358 playlist_modified ();
362 Diskstream::playlist_modified ()
364 if (!i_am_the_modifier && !overwrite_queued) {
365 _session.request_overwrite_buffer (this);
366 overwrite_queued = true;
371 Diskstream::playlist_deleted (boost::weak_ptr<Playlist> wpl)
373 boost::shared_ptr<Playlist> pl (wpl.lock());
375 if (pl == _playlist) {
377 /* this catches an ordering issue with session destruction. playlists
378 are destroyed before diskstreams. we have to invalidate any handles
379 we have to the playlist.
389 Diskstream::set_name (const string& str)
393 playlist()->set_name (str);
395 SessionObject::set_name(str);
397 if (!in_set_state && recordable()) {
398 /* rename existing capture files so that they have the correct name */
399 return rename_write_sources ();
409 Diskstream::remove_region_from_last_capture (boost::weak_ptr<Region> wregion)
411 boost::shared_ptr<Region> region (wregion.lock());
417 _last_capture_regions.remove (region);
421 Diskstream::playlist_ranges_moved (list< Evoral::RangeMove<nframes_t> > const & movements_frames)
425 XXX THIS HAS TO BE FIXED FOR 3.0
428 if (Config->get_automation_follows_regions () == false) {
432 list< Evoral::RangeMove<double> > movements;
433 for (list< Evoral::RangeMove<nframes_t> >::const_iterator i = movements_frames.begin();
434 i != movements_frames.end(); ++i) {
435 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
438 /* move gain automation */
439 boost::shared_ptr<AutomationList> gain_alist = _io->gain_control()->list();
440 XMLNode & before = gain_alist->get_state ();
441 gain_alist->move_ranges (movements);
442 _session.add_command (
443 new MementoCommand<AutomationList> (
444 *gain_alist.get(), &before, &gain_alist->get_state ()
448 /* move panner automation */
449 boost::shared_ptr<Panner> p = _io->panner ();
451 for (uint32_t i = 0; i < p->npanners (); ++i) {
452 boost::shared_ptr<AutomationList> pan_alist = p->streampanner(i).pan_control()->alist();
453 XMLNode & before = pan_alist->get_state ();
454 pan_alist->move_ranges (movements);
455 _session.add_command (new MementoCommand<AutomationList> (
456 *pan_alist.get(), &before, &pan_alist->get_state ()));
460 /* move processor automation */
462 Route * route = dynamic_cast<Route*> (_io);
464 route->foreach_processor (sigc::bind (sigc::mem_fun (*this, &Diskstream::move_processor_automation), movements_frames));
470 Diskstream::move_processor_automation (boost::weak_ptr<Processor> p,
471 list< Evoral::RangeMove<nframes_t> > const & movements_frames)
473 boost::shared_ptr<Processor> processor (p.lock ());
478 list< Evoral::RangeMove<double> > movements;
479 for (list< Evoral::RangeMove<nframes_t> >::const_iterator i = movements_frames.begin();
480 i != movements_frames.end(); ++i) {
481 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
484 set<Evoral::Parameter> const a = processor->what_can_be_automated ();
486 for (set<Evoral::Parameter>::iterator i = a.begin (); i != a.end (); ++i) {
487 boost::shared_ptr<AutomationList> al = processor->automation_control(*i)->alist();
488 XMLNode & before = al->get_state ();
489 al->move_ranges (movements);
490 _session.add_command (
491 new MementoCommand<AutomationList> (
492 *al.get(), &before, &al->get_state ()