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.
35 #include <glibmm/thread.h>
37 #include "pbd/error.h"
38 #include "pbd/basename.h"
39 #include "pbd/memento_command.h"
40 #include "pbd/xml++.h"
41 #include "pbd/stacktrace.h"
43 #include "ardour/ardour.h"
44 #include "ardour/audioengine.h"
45 #include "ardour/debug.h"
46 #include "ardour/diskstream.h"
47 #include "ardour/utils.h"
48 #include "ardour/configuration.h"
49 #include "ardour/audiofilesource.h"
50 #include "ardour/send.h"
51 #include "ardour/pannable.h"
52 #include "ardour/panner_shell.h"
53 #include "ardour/playlist.h"
54 #include "ardour/cycle_timer.h"
55 #include "ardour/region.h"
56 #include "ardour/panner.h"
57 #include "ardour/session.h"
58 #include "ardour/io.h"
59 #include "ardour/track.h"
65 using namespace ARDOUR;
68 /* XXX This goes uninitialized when there is no ~/.config/ardour3 directory.
69 * I can't figure out why, so this will do for now (just stole the
70 * default from configuration_vars.h). 0 is not a good value for
71 * allocating buffer sizes..
73 ARDOUR::framecnt_t Diskstream::disk_io_chunk_frames = 1024 * 256 / sizeof (Sample);
75 PBD::Signal0<void> Diskstream::DiskOverrun;
76 PBD::Signal0<void> Diskstream::DiskUnderrun;
78 Diskstream::Diskstream (Session &sess, const string &name, Flag flag)
79 : SessionObject(sess, name)
80 , i_am_the_modifier (0)
83 , _visible_speed (1.0f)
84 , _actual_speed (1.0f)
85 , _buffer_reallocation_required (false)
86 , _seek_required (false)
87 , capture_start_frame (0)
88 , capture_captured (0)
89 , was_recording (false)
90 , adjust_capture_position (0)
93 , first_recordable_frame (max_framepos)
94 , last_recordable_frame (max_framepos)
95 , last_possibly_recording (0)
96 , _alignment_style (ExistingMaterial)
97 , _alignment_choice (Automatic)
100 , overwrite_frame (0)
101 , overwrite_offset (0)
102 , _pending_overwrite (false)
103 , overwrite_queued (false)
104 , wrap_buffer_size (0)
105 , speed_buffer_size (0)
107 , _target_speed (_speed)
109 , playback_sample (0)
110 , in_set_state (false)
112 , deprecated_io_node (0)
116 Diskstream::Diskstream (Session& sess, const XMLNode& /*node*/)
117 : SessionObject(sess, "unnamed diskstream")
118 , i_am_the_modifier (0)
120 , _record_enabled (0)
121 , _visible_speed (1.0f)
122 , _actual_speed (1.0f)
123 , _buffer_reallocation_required (false)
124 , _seek_required (false)
125 , capture_start_frame (0)
126 , capture_captured (0)
127 , was_recording (false)
128 , adjust_capture_position (0)
129 , _capture_offset (0)
131 , first_recordable_frame (max_framepos)
132 , last_recordable_frame (max_framepos)
133 , last_possibly_recording (0)
134 , _alignment_style (ExistingMaterial)
135 , _alignment_choice (Automatic)
138 , overwrite_frame (0)
139 , overwrite_offset (0)
140 , _pending_overwrite (false)
141 , overwrite_queued (false)
142 , wrap_buffer_size (0)
143 , speed_buffer_size (0)
145 , _target_speed (_speed)
147 , playback_sample (0)
148 , in_set_state (false)
149 , _flags (Recordable)
150 , deprecated_io_node (0)
154 Diskstream::~Diskstream ()
156 DEBUG_TRACE (DEBUG::Destruction, string_compose ("Diskstream %1 deleted\n", _name));
159 _playlist->release ();
162 delete deprecated_io_node;
166 Diskstream::set_track (Track* t)
169 _io = _track->input();
171 ic_connection.disconnect();
172 _io->changed.connect_same_thread (ic_connection, boost::bind (&Diskstream::handle_input_change, this, _1, _2));
174 if (_io->n_ports() != ChanCount::ZERO) {
175 input_change_pending.type = IOChange::Type (IOChange::ConfigurationChanged|IOChange::ConnectionsChanged);
176 non_realtime_input_change ();
179 _track->Destroyed.connect_same_thread (*this, boost::bind (&Diskstream::route_going_away, this));
183 Diskstream::handle_input_change (IOChange change, void * /*src*/)
185 Glib::Mutex::Lock lm (state_lock);
187 if (change.type & (IOChange::ConfigurationChanged|IOChange::ConnectionsChanged)) {
189 /* rather than handle this here on a DS-by-DS basis we defer to the
190 session transport/butler thread, and let it tackle
191 as many diskstreams as need it in one shot. this avoids many repeated
192 takings of the audioengine process lock.
195 if (!(input_change_pending.type & change.type)) {
196 input_change_pending.type = IOChange::Type (input_change_pending.type | change.type);
197 _session.request_input_change_handling ();
203 Diskstream::non_realtime_set_speed ()
205 if (_buffer_reallocation_required)
207 Glib::Mutex::Lock lm (state_lock);
208 allocate_temporary_buffers ();
210 _buffer_reallocation_required = false;
213 if (_seek_required) {
214 if (speed() != 1.0f || speed() != -1.0f) {
215 seek ((framepos_t) (_session.transport_frame() * (double) speed()), true);
218 seek (_session.transport_frame(), true);
221 _seek_required = false;
226 Diskstream::realtime_set_speed (double sp, bool global)
228 bool changed = false;
229 double new_speed = sp * _session.transport_speed();
231 if (_visible_speed != sp) {
236 if (new_speed != _actual_speed) {
238 framecnt_t required_wrap_size = (framecnt_t) floor (_session.get_block_size() *
239 fabs (new_speed)) + 1;
241 if (required_wrap_size > wrap_buffer_size) {
242 _buffer_reallocation_required = true;
245 _actual_speed = new_speed;
246 _target_speed = fabs(_actual_speed);
251 _seek_required = true;
253 SpeedChanged (); /* EMIT SIGNAL */
256 return _buffer_reallocation_required || _seek_required;
260 Diskstream::set_capture_offset ()
263 /* can't capture, so forget it */
267 _capture_offset = _io->latency();
268 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: using IO latency, capture offset set to %2\n", name(), _capture_offset));
273 Diskstream::set_align_style (AlignStyle a, bool force)
275 if (record_enabled() && _session.actively_recording()) {
279 if ((a != _alignment_style) || force) {
280 _alignment_style = a;
281 AlignmentStyleChanged ();
286 Diskstream::set_align_choice (AlignChoice a, bool force)
288 if (record_enabled() && _session.actively_recording()) {
292 if ((a != _alignment_choice) || force) {
293 _alignment_choice = a;
295 switch (_alignment_choice) {
297 set_align_style_from_io ();
299 case UseExistingMaterial:
300 set_align_style (ExistingMaterial);
303 set_align_style (CaptureTime);
310 Diskstream::set_loop (Location *location)
313 if (location->start() >= location->end()) {
314 error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
319 loop_location = location;
321 LoopSet (location); /* EMIT SIGNAL */
325 /** Get the start position (in session frames) of the nth capture in the current pass */
327 Diskstream::get_capture_start_frame (uint32_t n) const
329 Glib::Mutex::Lock lm (capture_info_lock);
331 if (capture_info.size() > n) {
332 /* this is a completed capture */
333 return capture_info[n]->start;
335 /* this is the currently in-progress capture */
336 return capture_start_frame;
341 Diskstream::get_captured_frames (uint32_t n) const
343 Glib::Mutex::Lock lm (capture_info_lock);
345 if (capture_info.size() > n) {
346 /* this is a completed capture */
347 return capture_info[n]->frames;
349 /* this is the currently in-progress capture */
350 return capture_captured;
355 Diskstream::set_roll_delay (ARDOUR::framecnt_t nframes)
357 _roll_delay = nframes;
361 Diskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
367 bool prior_playlist = false;
370 Glib::Mutex::Lock lm (state_lock);
372 if (playlist == _playlist) {
376 playlist_connections.drop_connections ();
379 _playlist->release();
380 prior_playlist = true;
383 _playlist = playlist;
386 if (!in_set_state && recordable()) {
387 reset_write_sources (false);
390 _playlist->ContentsChanged.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_modified, this));
391 _playlist->DropReferences.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_deleted, this, boost::weak_ptr<Playlist>(_playlist)));
392 _playlist->RangesMoved.connect_same_thread (playlist_connections, boost::bind (&Diskstream::playlist_ranges_moved, this, _1, _2));
395 /* don't do this if we've already asked for it *or* if we are setting up
396 the diskstream for the very first time - the input changed handling will
397 take care of the buffer refill.
400 if (!overwrite_queued && prior_playlist) {
401 _session.request_overwrite_buffer (_track);
402 overwrite_queued = true;
405 PlaylistChanged (); /* EMIT SIGNAL */
406 _session.set_dirty ();
412 Diskstream::playlist_changed (const PropertyChange&)
414 playlist_modified ();
418 Diskstream::playlist_modified ()
420 if (!i_am_the_modifier && !overwrite_queued) {
421 _session.request_overwrite_buffer (_track);
422 overwrite_queued = true;
427 Diskstream::playlist_deleted (boost::weak_ptr<Playlist> wpl)
429 boost::shared_ptr<Playlist> pl (wpl.lock());
431 if (pl == _playlist) {
433 /* this catches an ordering issue with session destruction. playlists
434 are destroyed before diskstreams. we have to invalidate any handles
435 we have to the playlist.
445 Diskstream::set_name (const string& str)
449 playlist()->set_name (str);
450 SessionObject::set_name(str);
456 Diskstream::get_state ()
458 XMLNode* node = new XMLNode ("Diskstream");
460 LocaleGuard lg (X_("POSIX"));
462 node->add_property ("flags", enum_2_string (_flags));
463 node->add_property ("playlist", _playlist->name());
464 node->add_property("name", _name);
465 id().print (buf, sizeof (buf));
466 node->add_property("id", buf);
467 snprintf (buf, sizeof(buf), "%f", _visible_speed);
468 node->add_property ("speed", buf);
469 node->add_property ("capture-alignment", enum_2_string (_alignment_choice));
472 node->add_child_copy (*_extra_xml);
479 Diskstream::set_state (const XMLNode& node, int /*version*/)
481 const XMLProperty* prop;
483 if ((prop = node.property ("name")) != 0) {
484 _name = prop->value();
487 if (deprecated_io_node) {
488 set_id (*deprecated_io_node);
493 if ((prop = node.property ("flags")) != 0) {
494 _flags = Flag (string_2_enum (prop->value(), _flags));
497 if ((prop = node.property (X_("capture-alignment"))) != 0) {
498 set_align_choice (AlignChoice (string_2_enum (prop->value(), _alignment_choice)), true);
500 set_align_choice (Automatic, true);
503 if ((prop = node.property ("playlist")) == 0) {
507 if (find_and_use_playlist (prop->value())) {
511 if ((prop = node.property ("speed")) != 0) {
512 double sp = atof (prop->value().c_str());
514 if (realtime_set_speed (sp, false)) {
515 non_realtime_set_speed ();
523 Diskstream::playlist_ranges_moved (list< Evoral::RangeMove<framepos_t> > const & movements_frames, bool from_undo)
525 /* If we're coming from an undo, it will have handled
526 automation undo (it must, since automation-follows-regions
527 can lose automation data). Hence we can do nothing here.
534 if (!_track || Config->get_automation_follows_regions () == false) {
538 list< Evoral::RangeMove<double> > movements;
540 for (list< Evoral::RangeMove<framepos_t> >::const_iterator i = movements_frames.begin();
541 i != movements_frames.end();
544 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
547 /* move panner automation */
548 boost::shared_ptr<Pannable> pannable = _track->pannable();
549 Evoral::ControlSet::Controls& c (pannable->controls());
551 for (Evoral::ControlSet::Controls::iterator ci = c.begin(); ci != c.end(); ++ci) {
552 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
556 boost::shared_ptr<AutomationList> alist = ac->alist();
558 XMLNode & before = alist->get_state ();
559 bool const things_moved = alist->move_ranges (movements);
561 _session.add_command (new MementoCommand<AutomationList> (
562 *alist.get(), &before, &alist->get_state ()));
566 /* move processor automation */
567 _track->foreach_processor (boost::bind (&Diskstream::move_processor_automation, this, _1, movements_frames));
571 Diskstream::move_processor_automation (boost::weak_ptr<Processor> p, list< Evoral::RangeMove<framepos_t> > const & movements_frames)
573 boost::shared_ptr<Processor> processor (p.lock ());
578 list< Evoral::RangeMove<double> > movements;
579 for (list< Evoral::RangeMove<framepos_t> >::const_iterator i = movements_frames.begin(); i != movements_frames.end(); ++i) {
580 movements.push_back(Evoral::RangeMove<double>(i->from, i->length, i->to));
583 set<Evoral::Parameter> const a = processor->what_can_be_automated ();
585 for (set<Evoral::Parameter>::iterator i = a.begin (); i != a.end (); ++i) {
586 boost::shared_ptr<AutomationList> al = processor->automation_control(*i)->alist();
587 XMLNode & before = al->get_state ();
588 bool const things_moved = al->move_ranges (movements);
590 _session.add_command (
591 new MementoCommand<AutomationList> (
592 *al.get(), &before, &al->get_state ()
600 Diskstream::check_record_status (framepos_t transport_frame, bool can_record)
602 int possibly_recording;
605 const int transport_rolling = 0x4;
606 const int track_rec_enabled = 0x2;
607 const int global_rec_enabled = 0x1;
608 const int fully_rec_enabled = (transport_rolling|track_rec_enabled|global_rec_enabled);
610 /* merge together the 3 factors that affect record status, and compute
614 rolling = _session.transport_speed() != 0.0f;
615 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
616 change = possibly_recording ^ last_possibly_recording;
618 if (possibly_recording == last_possibly_recording) {
622 framecnt_t existing_material_offset = _session.worst_playback_latency();
624 if (possibly_recording == fully_rec_enabled) {
626 if (last_possibly_recording == fully_rec_enabled) {
630 capture_start_frame = _session.transport_frame();
631 first_recordable_frame = capture_start_frame + _capture_offset;
632 last_recordable_frame = max_framepos;
634 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: @ %7 (%9) FRF = %2 CSF = %4 CO = %5, EMO = %6 RD = %8 WOL %10 WTL %11\n",
635 name(), first_recordable_frame, last_recordable_frame, capture_start_frame,
637 existing_material_offset,
640 _session.transport_frame(),
641 _session.worst_output_latency(),
642 _session.worst_track_latency()));
645 if (_alignment_style == ExistingMaterial) {
646 first_recordable_frame += existing_material_offset;
647 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("\tshift FRF by EMO %1\n",
648 first_recordable_frame));
651 prepare_record_status (capture_start_frame);
655 if (last_possibly_recording == fully_rec_enabled) {
657 /* we were recording last time */
659 if (change & transport_rolling) {
661 /* transport-change (stopped rolling): last_recordable_frame was set in ::prepare_to_stop(). We
662 had to set it there because we likely rolled past the stopping point to declick out,
669 last_recordable_frame = _session.transport_frame() + _capture_offset;
671 if (_alignment_style == ExistingMaterial) {
672 last_recordable_frame += existing_material_offset;
678 last_possibly_recording = possibly_recording;
682 Diskstream::route_going_away ()
688 Diskstream::calculate_record_range(OverlapType ot, framepos_t transport_frame, framecnt_t nframes,
689 framecnt_t & rec_nframes, framecnt_t & rec_offset)
696 case OverlapInternal:
697 /* ---------- recrange
700 rec_nframes = nframes;
705 /* |--------| recrange
708 rec_nframes = transport_frame + nframes - first_recordable_frame;
710 rec_offset = first_recordable_frame - transport_frame;
715 /* |--------| recrange
718 rec_nframes = last_recordable_frame - transport_frame;
722 case OverlapExternal:
723 /* |--------| recrange
724 -------------- transrange
726 rec_nframes = last_recordable_frame - first_recordable_frame;
727 rec_offset = first_recordable_frame - transport_frame;
731 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 rec? %2 @ %3 (for %4) FRF %5 LRF %6 : rf %7 @ %8\n",
732 _name, enum_2_string (ot), transport_frame, nframes,
733 first_recordable_frame, last_recordable_frame, rec_nframes, rec_offset));
737 Diskstream::prepare_to_stop (framepos_t pos)
739 last_recordable_frame = pos + _capture_offset;