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 _capture_offset = _io->latency();
256 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: using IO latency, capture offset set to %2\n", name(), _capture_offset));
261 Diskstream::set_align_style (AlignStyle a, bool force)
263 if (record_enabled() && _session.actively_recording()) {
267 if ((a != _alignment_style) || force) {
268 _alignment_style = a;
269 AlignmentStyleChanged ();
274 Diskstream::set_align_choice (AlignChoice a, bool force)
276 if (record_enabled() && _session.actively_recording()) {
280 if ((a != _alignment_choice) || force) {
281 _alignment_choice = a;
283 switch (_alignment_choice) {
285 set_align_style_from_io ();
287 case UseExistingMaterial:
288 set_align_style (ExistingMaterial);
291 set_align_style (CaptureTime);
298 Diskstream::set_loop (Location *location)
301 if (location->start() >= location->end()) {
302 error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
307 loop_location = location;
309 LoopSet (location); /* EMIT SIGNAL */
313 /** Get the start position (in session frames) of the nth capture in the current pass */
315 Diskstream::get_capture_start_frame (uint32_t n) const
317 Glib::Threads::Mutex::Lock lm (capture_info_lock);
319 if (capture_info.size() > n) {
320 /* this is a completed capture */
321 return capture_info[n]->start;
323 /* this is the currently in-progress capture */
324 return capture_start_frame;
329 Diskstream::get_captured_frames (uint32_t n) const
331 Glib::Threads::Mutex::Lock lm (capture_info_lock);
333 if (capture_info.size() > n) {
334 /* this is a completed capture */
335 return capture_info[n]->frames;
337 /* this is the currently in-progress capture */
338 return capture_captured;
343 Diskstream::set_roll_delay (ARDOUR::framecnt_t nframes)
345 _roll_delay = nframes;
349 Diskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
355 bool prior_playlist = false;
358 Glib::Threads::Mutex::Lock lm (state_lock);
360 if (playlist == _playlist) {
364 playlist_connections.drop_connections ();
367 _playlist->release();
368 prior_playlist = true;
371 _playlist = playlist;
374 if (!in_set_state && recordable()) {
375 reset_write_sources (false);
378 _playlist->ContentsChanged.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_modified, this));
379 _playlist->DropReferences.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_deleted, this, boost::weak_ptr<Playlist>(_playlist)));
380 _playlist->RangesMoved.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_ranges_moved, this, _1, _2));
383 /* don't do this if we've already asked for it *or* if we are setting up
384 the diskstream for the very first time - the input changed handling will
385 take care of the buffer refill.
388 if (!overwrite_queued && prior_playlist) {
389 _session.request_overwrite_buffer (_track);
390 overwrite_queued = true;
393 PlaylistChanged (); /* EMIT SIGNAL */
394 _session.set_dirty ();
400 Diskstream::playlist_changed (const PropertyChange&)
402 playlist_modified ();
406 Diskstream::playlist_modified ()
408 if (!i_am_the_modifier && !overwrite_queued) {
409 _session.request_overwrite_buffer (_track);
410 overwrite_queued = true;
415 Diskstream::playlist_deleted (boost::weak_ptr<Playlist> wpl)
417 boost::shared_ptr<Playlist> pl (wpl.lock());
419 if (pl == _playlist) {
421 /* this catches an ordering issue with session destruction. playlists
422 are destroyed before diskstreams. we have to invalidate any handles
423 we have to the playlist.
433 Diskstream::set_name (const string& str)
437 playlist()->set_name (str);
438 SessionObject::set_name(str);
444 Diskstream::get_state ()
446 XMLNode* node = new XMLNode ("Diskstream");
448 LocaleGuard lg (X_("POSIX"));
450 node->add_property ("flags", enum_2_string (_flags));
451 node->add_property ("playlist", _playlist->name());
452 node->add_property("name", _name);
453 id().print (buf, sizeof (buf));
454 node->add_property("id", buf);
455 snprintf (buf, sizeof(buf), "%f", _visible_speed);
456 node->add_property ("speed", buf);
457 node->add_property ("capture-alignment", enum_2_string (_alignment_choice));
460 node->add_child_copy (*_extra_xml);
467 Diskstream::set_state (const XMLNode& node, int /*version*/)
469 const XMLProperty* prop;
471 if ((prop = node.property ("name")) != 0) {
472 _name = prop->value();
475 if (deprecated_io_node) {
476 set_id (*deprecated_io_node);
481 if ((prop = node.property ("flags")) != 0) {
482 _flags = Flag (string_2_enum (prop->value(), _flags));
485 if ((prop = node.property (X_("capture-alignment"))) != 0) {
486 set_align_choice (AlignChoice (string_2_enum (prop->value(), _alignment_choice)), true);
488 set_align_choice (Automatic, true);
491 if ((prop = node.property ("playlist")) == 0) {
495 if (find_and_use_playlist (prop->value())) {
499 if ((prop = node.property ("speed")) != 0) {
500 double sp = atof (prop->value().c_str());
502 if (realtime_set_speed (sp, false)) {
503 non_realtime_set_speed ();
511 Diskstream::playlist_ranges_moved (list< Evoral::RangeMove<framepos_t> > const & movements_frames, bool from_undo)
513 /* If we're coming from an undo, it will have handled
514 automation undo (it must, since automation-follows-regions
515 can lose automation data). Hence we can do nothing here.
522 if (!_track || Config->get_automation_follows_regions () == false) {
526 list< Evoral::RangeMove<double> > movements;
528 for (list< Evoral::RangeMove<framepos_t> >::const_iterator i = movements_frames.begin();
529 i != movements_frames.end();
532 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
535 /* move panner automation */
536 boost::shared_ptr<Pannable> pannable = _track->pannable();
537 Evoral::ControlSet::Controls& c (pannable->controls());
539 for (Evoral::ControlSet::Controls::iterator ci = c.begin(); ci != c.end(); ++ci) {
540 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
544 boost::shared_ptr<AutomationList> alist = ac->alist();
546 XMLNode & before = alist->get_state ();
547 bool const things_moved = alist->move_ranges (movements);
549 _session.add_command (new MementoCommand<AutomationList> (
550 *alist.get(), &before, &alist->get_state ()));
554 /* move processor automation */
555 _track->foreach_processor (boost::bind (&Diskstream::move_processor_automation, this, _1, movements_frames));
559 Diskstream::move_processor_automation (boost::weak_ptr<Processor> p, list< Evoral::RangeMove<framepos_t> > const & movements_frames)
561 boost::shared_ptr<Processor> processor (p.lock ());
566 list< Evoral::RangeMove<double> > movements;
567 for (list< Evoral::RangeMove<framepos_t> >::const_iterator i = movements_frames.begin(); i != movements_frames.end(); ++i) {
568 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
571 set<Evoral::Parameter> const a = processor->what_can_be_automated ();
573 for (set<Evoral::Parameter>::const_iterator i = a.begin (); i != a.end (); ++i) {
574 boost::shared_ptr<AutomationList> al = processor->automation_control(*i)->alist();
575 XMLNode & before = al->get_state ();
576 bool const things_moved = al->move_ranges (movements);
578 _session.add_command (
579 new MementoCommand<AutomationList> (
580 *al.get(), &before, &al->get_state ()
588 Diskstream::check_record_status (framepos_t transport_frame, bool can_record)
590 int possibly_recording;
593 const int transport_rolling = 0x4;
594 const int track_rec_enabled = 0x2;
595 const int global_rec_enabled = 0x1;
596 const int fully_rec_enabled = (transport_rolling|track_rec_enabled|global_rec_enabled);
598 /* merge together the 3 factors that affect record status, and compute
602 rolling = _session.transport_speed() != 0.0f;
603 possibly_recording = (rolling << 2) | ((int)record_enabled() << 1) | (int)can_record;
604 change = possibly_recording ^ last_possibly_recording;
606 if (possibly_recording == last_possibly_recording) {
610 framecnt_t existing_material_offset = _session.worst_playback_latency();
612 if (possibly_recording == fully_rec_enabled) {
614 if (last_possibly_recording == fully_rec_enabled) {
618 capture_start_frame = _session.transport_frame();
619 first_recordable_frame = capture_start_frame + _capture_offset;
620 last_recordable_frame = max_framepos;
622 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: @ %7 (%9) FRF = %2 CSF = %4 CO = %5, EMO = %6 RD = %8 WOL %10 WTL %11\n",
623 name(), first_recordable_frame, last_recordable_frame, capture_start_frame,
625 existing_material_offset,
628 _session.transport_frame(),
629 _session.worst_output_latency(),
630 _session.worst_track_latency()));
633 if (_alignment_style == ExistingMaterial) {
634 first_recordable_frame += existing_material_offset;
635 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("\tshift FRF by EMO %1\n",
636 first_recordable_frame));
639 prepare_record_status (capture_start_frame);
643 if (last_possibly_recording == fully_rec_enabled) {
645 /* we were recording last time */
647 if (change & transport_rolling) {
649 /* transport-change (stopped rolling): last_recordable_frame was set in ::prepare_to_stop(). We
650 had to set it there because we likely rolled past the stopping point to declick out,
657 last_recordable_frame = _session.transport_frame() + _capture_offset;
659 if (_alignment_style == ExistingMaterial) {
660 last_recordable_frame += existing_material_offset;
666 last_possibly_recording = possibly_recording;
670 Diskstream::route_going_away ()
676 Diskstream::calculate_record_range (Evoral::OverlapType ot, framepos_t transport_frame, framecnt_t nframes,
677 framecnt_t & rec_nframes, framecnt_t & rec_offset)
680 case Evoral::OverlapNone:
684 case Evoral::OverlapInternal:
685 /* ---------- recrange
688 rec_nframes = nframes;
692 case Evoral::OverlapStart:
693 /* |--------| recrange
696 rec_nframes = transport_frame + nframes - first_recordable_frame;
698 rec_offset = first_recordable_frame - transport_frame;
702 case Evoral::OverlapEnd:
703 /* |--------| recrange
706 rec_nframes = last_recordable_frame - transport_frame;
710 case Evoral::OverlapExternal:
711 /* |--------| recrange
712 -------------- transrange
714 rec_nframes = last_recordable_frame - first_recordable_frame;
715 rec_offset = first_recordable_frame - transport_frame;
719 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 rec? %2 @ %3 (for %4) FRF %5 LRF %6 : rf %7 @ %8\n",
720 _name, enum_2_string (ot), transport_frame, nframes,
721 first_recordable_frame, last_recordable_frame, rec_nframes, rec_offset));
725 Diskstream::prepare_to_stop (framepos_t pos)
727 last_recordable_frame = pos + _capture_offset;
731 Diskstream::engage_record_enable ()
733 g_atomic_int_set (&_record_enabled, 1);
737 Diskstream::disengage_record_enable ()
739 g_atomic_int_set (&_record_enabled, 0);