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.
33 #include <glibmm/threads.h>
35 #include "pbd/error.h"
36 #include "pbd/basename.h"
37 #include "pbd/memento_command.h"
38 #include "pbd/xml++.h"
39 #include "pbd/stacktrace.h"
41 #include "ardour/debug.h"
42 #include "ardour/diskstream.h"
43 #include "ardour/io.h"
44 #include "ardour/pannable.h"
45 #include "ardour/playlist.h"
46 #include "ardour/session.h"
47 #include "ardour/track.h"
53 using namespace ARDOUR;
56 /* XXX This goes uninitialized when there is no ~/.config/ardour3 directory.
57 * I can't figure out why, so this will do for now (just stole the
58 * default from configuration_vars.h). 0 is not a good value for
59 * allocating buffer sizes..
61 ARDOUR::framecnt_t Diskstream::disk_io_chunk_frames = 1024 * 256 / sizeof (Sample);
63 PBD::Signal0<void> Diskstream::DiskOverrun;
64 PBD::Signal0<void> Diskstream::DiskUnderrun;
66 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
67 : SessionObject(sess, name)
68 , i_am_the_modifier (0)
71 , _visible_speed (1.0f)
72 , _actual_speed (1.0f)
73 , _buffer_reallocation_required (false)
74 , _seek_required (false)
75 , capture_start_frame (0)
76 , capture_captured (0)
77 , was_recording (false)
78 , adjust_capture_position (0)
81 , first_recordable_frame (max_framepos)
82 , last_recordable_frame (max_framepos)
83 , last_possibly_recording (0)
84 , _alignment_style (ExistingMaterial)
85 , _alignment_choice (Automatic)
89 , overwrite_offset (0)
90 , _pending_overwrite (false)
91 , overwrite_queued (false)
92 , wrap_buffer_size (0)
93 , speed_buffer_size (0)
95 , _target_speed (_speed)
98 , in_set_state (false)
100 , deprecated_io_node (0)
104 Diskstream::Diskstream (Session& sess, const XMLNode& /*node*/)
105 : SessionObject(sess, "unnamed diskstream")
106 , i_am_the_modifier (0)
108 , _record_enabled (0)
109 , _visible_speed (1.0f)
110 , _actual_speed (1.0f)
111 , _buffer_reallocation_required (false)
112 , _seek_required (false)
113 , capture_start_frame (0)
114 , capture_captured (0)
115 , was_recording (false)
116 , adjust_capture_position (0)
117 , _capture_offset (0)
119 , first_recordable_frame (max_framepos)
120 , last_recordable_frame (max_framepos)
121 , last_possibly_recording (0)
122 , _alignment_style (ExistingMaterial)
123 , _alignment_choice (Automatic)
126 , overwrite_frame (0)
127 , overwrite_offset (0)
128 , _pending_overwrite (false)
129 , overwrite_queued (false)
130 , wrap_buffer_size (0)
131 , speed_buffer_size (0)
133 , _target_speed (_speed)
135 , playback_sample (0)
136 , in_set_state (false)
137 , _flags (Recordable)
138 , deprecated_io_node (0)
142 Diskstream::~Diskstream ()
144 DEBUG_TRACE (DEBUG::Destruction, string_compose ("Diskstream %1 deleted\n", _name));
147 _playlist->release ();
150 delete deprecated_io_node;
154 Diskstream::set_track (Track* t)
157 _io = _track->input();
159 ic_connection.disconnect();
160 _io->changed.connect_same_thread (ic_connection, boost::bind (&Diskstream::handle_input_change, this, _1, _2));
162 if (_io->n_ports() != ChanCount::ZERO) {
163 input_change_pending.type = IOChange::Type (IOChange::ConfigurationChanged|IOChange::ConnectionsChanged);
164 non_realtime_input_change ();
167 _track->Destroyed.connect_same_thread (*this, boost::bind (&Diskstream::route_going_away, this));
171 Diskstream::handle_input_change (IOChange change, void * /*src*/)
173 Glib::Threads::Mutex::Lock lm (state_lock);
175 if (change.type & (IOChange::ConfigurationChanged|IOChange::ConnectionsChanged)) {
177 /* rather than handle this here on a DS-by-DS basis we defer to the
178 session transport/butler thread, and let it tackle
179 as many diskstreams as need it in one shot. this avoids many repeated
180 takings of the audioengine process lock.
183 if (!(input_change_pending.type & change.type)) {
184 input_change_pending.type = IOChange::Type (input_change_pending.type | change.type);
185 _session.request_input_change_handling ();
191 Diskstream::non_realtime_set_speed ()
193 if (_buffer_reallocation_required)
195 Glib::Threads::Mutex::Lock lm (state_lock);
196 allocate_temporary_buffers ();
198 _buffer_reallocation_required = false;
201 if (_seek_required) {
202 if (speed() != 1.0f || speed() != -1.0f) {
203 seek ((framepos_t) (_session.transport_frame() * (double) speed()), true);
206 seek (_session.transport_frame(), true);
209 _seek_required = false;
214 Diskstream::realtime_set_speed (double sp, bool global)
216 bool changed = false;
217 double new_speed = sp * _session.transport_speed();
219 if (_visible_speed != sp) {
224 if (new_speed != _actual_speed) {
226 framecnt_t required_wrap_size = (framecnt_t) ceil (_session.get_block_size() *
227 fabs (new_speed)) + 2;
229 if (required_wrap_size > wrap_buffer_size) {
230 _buffer_reallocation_required = true;
233 _actual_speed = new_speed;
234 _target_speed = fabs(_actual_speed);
239 _seek_required = true;
241 SpeedChanged (); /* EMIT SIGNAL */
244 return _buffer_reallocation_required || _seek_required;
248 Diskstream::set_capture_offset ()
251 /* can't capture, so forget it */
255 switch (_alignment_style) {
256 case ExistingMaterial:
257 _capture_offset = _io->latency();
266 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: using IO latency, capture offset set to %2 with style = %3\n", name(), _capture_offset, enum_2_string (_alignment_style)));
271 Diskstream::set_align_style (AlignStyle a, bool force)
273 if (record_enabled() && _session.actively_recording()) {
277 if ((a != _alignment_style) || force) {
278 _alignment_style = a;
279 set_capture_offset ();
280 AlignmentStyleChanged ();
285 Diskstream::set_align_choice (AlignChoice a, bool force)
287 if (record_enabled() && _session.actively_recording()) {
291 if ((a != _alignment_choice) || force) {
292 _alignment_choice = a;
294 switch (_alignment_choice) {
296 set_align_style_from_io ();
298 case UseExistingMaterial:
299 set_align_style (ExistingMaterial);
302 set_align_style (CaptureTime);
309 Diskstream::set_loop (Location *location)
312 if (location->start() >= location->end()) {
313 error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
318 loop_location = location;
320 LoopSet (location); /* EMIT SIGNAL */
324 /** Get the start position (in session frames) of the nth capture in the current pass */
326 Diskstream::get_capture_start_frame (uint32_t n) const
328 Glib::Threads::Mutex::Lock lm (capture_info_lock);
330 if (capture_info.size() > n) {
331 /* this is a completed capture */
332 return capture_info[n]->start;
334 /* this is the currently in-progress capture */
335 return capture_start_frame;
340 Diskstream::get_captured_frames (uint32_t n) const
342 Glib::Threads::Mutex::Lock lm (capture_info_lock);
344 if (capture_info.size() > n) {
345 /* this is a completed capture */
346 return capture_info[n]->frames;
348 /* this is the currently in-progress capture */
349 return capture_captured;
354 Diskstream::set_roll_delay (ARDOUR::framecnt_t nframes)
356 _roll_delay = nframes;
360 Diskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
366 bool prior_playlist = false;
369 Glib::Threads::Mutex::Lock lm (state_lock);
371 if (playlist == _playlist) {
375 playlist_connections.drop_connections ();
378 _playlist->release();
379 prior_playlist = true;
382 _playlist = playlist;
385 if (!in_set_state && recordable()) {
386 reset_write_sources (false);
389 _playlist->ContentsChanged.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_modified, this));
390 _playlist->DropReferences.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_deleted, this, boost::weak_ptr<Playlist>(_playlist)));
391 _playlist->RangesMoved.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_ranges_moved, this, _1, _2));
394 /* don't do this if we've already asked for it *or* if we are setting up
395 the diskstream for the very first time - the input changed handling will
396 take care of the buffer refill.
399 if (!overwrite_queued && prior_playlist) {
400 _session.request_overwrite_buffer (_track);
401 overwrite_queued = true;
404 PlaylistChanged (); /* EMIT SIGNAL */
405 _session.set_dirty ();
411 Diskstream::playlist_changed (const PropertyChange&)
413 playlist_modified ();
417 Diskstream::playlist_modified ()
419 if (!i_am_the_modifier && !overwrite_queued) {
420 _session.request_overwrite_buffer (_track);
421 overwrite_queued = true;
426 Diskstream::playlist_deleted (boost::weak_ptr<Playlist> wpl)
428 boost::shared_ptr<Playlist> pl (wpl.lock());
430 if (pl == _playlist) {
432 /* this catches an ordering issue with session destruction. playlists
433 are destroyed before diskstreams. we have to invalidate any handles
434 we have to the playlist.
444 Diskstream::set_name (const string& str)
448 playlist()->set_name (str);
449 SessionObject::set_name(str);
455 Diskstream::set_write_source_name (const std::string& str) {
456 _write_source_name = str;
461 Diskstream::get_state ()
463 XMLNode* node = new XMLNode ("Diskstream");
465 LocaleGuard lg (X_("POSIX"));
467 node->add_property ("flags", enum_2_string (_flags));
468 node->add_property ("playlist", _playlist->name());
469 node->add_property("name", _name);
470 id().print (buf, sizeof (buf));
471 node->add_property("id", buf);
472 snprintf (buf, sizeof(buf), "%f", _visible_speed);
473 node->add_property ("speed", buf);
474 node->add_property ("capture-alignment", enum_2_string (_alignment_choice));
477 node->add_child_copy (*_extra_xml);
484 Diskstream::set_state (const XMLNode& node, int /*version*/)
486 const XMLProperty* prop;
488 if ((prop = node.property ("name")) != 0) {
489 _name = prop->value();
492 if (deprecated_io_node) {
493 set_id (*deprecated_io_node);
498 if ((prop = node.property ("flags")) != 0) {
499 _flags = Flag (string_2_enum (prop->value(), _flags));
502 if ((prop = node.property (X_("capture-alignment"))) != 0) {
503 set_align_choice (AlignChoice (string_2_enum (prop->value(), _alignment_choice)), true);
505 set_align_choice (Automatic, true);
508 if ((prop = node.property ("playlist")) == 0) {
512 if (find_and_use_playlist (prop->value())) {
516 if ((prop = node.property ("speed")) != 0) {
517 double sp = atof (prop->value().c_str());
519 if (realtime_set_speed (sp, false)) {
520 non_realtime_set_speed ();
528 Diskstream::playlist_ranges_moved (list< Evoral::RangeMove<framepos_t> > const & movements_frames, bool from_undo)
530 /* If we're coming from an undo, it will have handled
531 automation undo (it must, since automation-follows-regions
532 can lose automation data). Hence we can do nothing here.
539 if (!_track || Config->get_automation_follows_regions () == false) {
543 list< Evoral::RangeMove<double> > movements;
545 for (list< Evoral::RangeMove<framepos_t> >::const_iterator i = movements_frames.begin();
546 i != movements_frames.end();
549 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
552 /* move panner automation */
553 boost::shared_ptr<Pannable> pannable = _track->pannable();
554 Evoral::ControlSet::Controls& c (pannable->controls());
556 for (Evoral::ControlSet::Controls::iterator ci = c.begin(); ci != c.end(); ++ci) {
557 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
561 boost::shared_ptr<AutomationList> alist = ac->alist();
563 XMLNode & before = alist->get_state ();
564 bool const things_moved = alist->move_ranges (movements);
566 _session.add_command (new MementoCommand<AutomationList> (
567 *alist.get(), &before, &alist->get_state ()));
571 /* move processor automation */
572 _track->foreach_processor (boost::bind (&Diskstream::move_processor_automation, this, _1, movements_frames));
576 Diskstream::move_processor_automation (boost::weak_ptr<Processor> p, list< Evoral::RangeMove<framepos_t> > const & movements_frames)
578 boost::shared_ptr<Processor> processor (p.lock ());
583 list< Evoral::RangeMove<double> > movements;
584 for (list< Evoral::RangeMove<framepos_t> >::const_iterator i = movements_frames.begin(); i != movements_frames.end(); ++i) {
585 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
588 set<Evoral::Parameter> const a = processor->what_can_be_automated ();
590 for (set<Evoral::Parameter>::const_iterator i = a.begin (); i != a.end (); ++i) {
591 boost::shared_ptr<AutomationList> al = processor->automation_control(*i)->alist();
592 XMLNode & before = al->get_state ();
593 bool const things_moved = al->move_ranges (movements);
595 _session.add_command (
596 new MementoCommand<AutomationList> (
597 *al.get(), &before, &al->get_state ()
605 Diskstream::check_record_status (framepos_t transport_frame, bool can_record)
607 int possibly_recording;
610 const int transport_rolling = 0x4;
611 const int track_rec_enabled = 0x2;
612 const int global_rec_enabled = 0x1;
613 const int fully_rec_enabled = (transport_rolling|track_rec_enabled|global_rec_enabled);
615 /* merge together the 3 factors that affect record status, and compute
619 rolling = _session.transport_speed() != 0.0f;
620 possibly_recording = (rolling << 2) | ((int)record_enabled() << 1) | (int)can_record;
621 change = possibly_recording ^ last_possibly_recording;
623 if (possibly_recording == last_possibly_recording) {
627 const framecnt_t existing_material_offset = _session.worst_playback_latency();
629 if (possibly_recording == fully_rec_enabled) {
631 if (last_possibly_recording == fully_rec_enabled) {
635 capture_start_frame = _session.transport_frame();
636 first_recordable_frame = capture_start_frame + _capture_offset;
637 last_recordable_frame = max_framepos;
639 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: @ %7 (%9) FRF = %2 CSF = %4 CO = %5, EMO = %6 RD = %8 WOL %10 WTL %11\n",
640 name(), first_recordable_frame, last_recordable_frame, capture_start_frame,
642 existing_material_offset,
645 _session.transport_frame(),
646 _session.worst_output_latency(),
647 _session.worst_track_latency()));
650 if (_alignment_style == ExistingMaterial) {
651 first_recordable_frame += existing_material_offset;
652 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("\tshift FRF by EMO %1\n",
653 first_recordable_frame));
656 prepare_record_status (capture_start_frame);
660 if (last_possibly_recording == fully_rec_enabled) {
662 /* we were recording last time */
664 if (change & transport_rolling) {
666 /* transport-change (stopped rolling): last_recordable_frame was set in ::prepare_to_stop(). We
667 * had to set it there because we likely rolled past the stopping point to declick out,
668 * and then backed up.
674 last_recordable_frame = _session.transport_frame() + _capture_offset;
676 if (_alignment_style == ExistingMaterial) {
677 last_recordable_frame += existing_material_offset;
683 last_possibly_recording = possibly_recording;
687 Diskstream::route_going_away ()
693 Diskstream::calculate_record_range (Evoral::OverlapType ot, framepos_t transport_frame, framecnt_t nframes,
694 framecnt_t & rec_nframes, framecnt_t & rec_offset)
697 case Evoral::OverlapNone:
701 case Evoral::OverlapInternal:
702 /* ---------- recrange
705 rec_nframes = nframes;
709 case Evoral::OverlapStart:
710 /* |--------| recrange
713 rec_nframes = transport_frame + nframes - first_recordable_frame;
715 rec_offset = first_recordable_frame - transport_frame;
719 case Evoral::OverlapEnd:
720 /* |--------| recrange
721 * |-------- transrange
723 rec_nframes = last_recordable_frame - transport_frame;
727 case Evoral::OverlapExternal:
728 /* |--------| recrange
729 * -------------- transrange
731 rec_nframes = last_recordable_frame - first_recordable_frame;
732 rec_offset = first_recordable_frame - transport_frame;
736 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 rec? %2 @ %3 (for %4) FRF %5 LRF %6 : rf %7 @ %8\n",
737 _name, enum_2_string (ot), transport_frame, nframes,
738 first_recordable_frame, last_recordable_frame, rec_nframes, rec_offset));
742 Diskstream::prepare_to_stop (framepos_t transport_frame, framepos_t audible_frame)
744 switch (_alignment_style) {
745 case ExistingMaterial:
746 last_recordable_frame = transport_frame + _capture_offset;
747 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose("%1: prepare to stop sets last recordable frame to %2 + %3 = %4\n", _name, transport_frame, _capture_offset, last_recordable_frame));
751 last_recordable_frame = audible_frame; // note that capture_offset is zero
752 /* we may already have captured audio before the last_recordable_frame (audible frame),
755 if (last_recordable_frame > capture_start_frame) {
756 capture_captured = min (capture_captured, last_recordable_frame - capture_start_frame);
758 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose("%1: prepare to stop sets last recordable frame to audible frame @ %2\n", _name, audible_frame));
765 Diskstream::engage_record_enable ()
767 g_atomic_int_set (&_record_enabled, 1);
771 Diskstream::disengage_record_enable ()
773 g_atomic_int_set (&_record_enabled, 0);