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.
30 #include "pbd/gstdio_compat.h"
31 #include "pbd/error.h"
32 #include "pbd/xml++.h"
33 #include "pbd/memento_command.h"
34 #include "pbd/enumwriter.h"
35 #include "pbd/stateful_diff_command.h"
37 #include "ardour/analyser.h"
38 #include "ardour/audio_buffer.h"
39 #include "ardour/audio_diskstream.h"
40 #include "ardour/audio_port.h"
41 #include "ardour/audioengine.h"
42 #include "ardour/audiofilesource.h"
43 #include "ardour/audioplaylist.h"
44 #include "ardour/audioregion.h"
45 #include "ardour/butler.h"
46 #include "ardour/debug.h"
47 #include "ardour/io.h"
48 #include "ardour/playlist_factory.h"
49 #include "ardour/profile.h"
50 #include "ardour/region_factory.h"
51 #include "ardour/session.h"
52 #include "ardour/session_playlists.h"
53 #include "ardour/sndfile_helpers.h"
54 #include "ardour/source_factory.h"
55 #include "ardour/track.h"
56 #include "ardour/types.h"
57 #include "ardour/utils.h"
63 using namespace ARDOUR;
66 Sample* AudioDiskstream::_mixdown_buffer = 0;
67 gain_t* AudioDiskstream::_gain_buffer = 0;
69 AudioDiskstream::AudioDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
70 : Diskstream(sess, name, flag)
71 , channels (new ChannelList)
73 /* prevent any write sources from being created */
79 if (flag & Destructive) {
80 use_destructive_playlist ();
84 AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
85 : Diskstream(sess, node)
86 , channels (new ChannelList)
91 if (set_state (node, Stateful::loading_state_version)) {
93 throw failed_constructor();
99 use_destructive_playlist ();
104 AudioDiskstream::init ()
106 /* there are no channels at this point, so these
107 two calls just get speed_buffer_size and wrap_buffer
108 size setup without duplicating their code.
111 set_block_size (_session.get_block_size());
112 allocate_temporary_buffers ();
115 AudioDiskstream::~AudioDiskstream ()
117 DEBUG_TRACE (DEBUG::Destruction, string_compose ("Audio Diskstream %1 destructor\n", _name));
120 RCUWriter<ChannelList> writer (channels);
121 boost::shared_ptr<ChannelList> c = writer.get_copy();
123 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
134 AudioDiskstream::allocate_working_buffers()
136 /* with varifill buffer refilling, we compute the read size in bytes (to optimize
137 for disk i/o bandwidth) and then convert back into samples. These buffers
138 need to reflect the maximum size we could use, which is 4MB reads, or 2M samples
139 using 16 bit samples.
141 _mixdown_buffer = new Sample[2*1048576];
142 _gain_buffer = new gain_t[2*1048576];
146 AudioDiskstream::free_working_buffers()
148 delete [] _mixdown_buffer;
149 delete [] _gain_buffer;
155 AudioDiskstream::non_realtime_input_change ()
157 bool need_write_sources = false;
160 Glib::Threads::Mutex::Lock lm (state_lock);
162 if (input_change_pending.type == IOChange::NoChange) {
166 boost::shared_ptr<ChannelList> cr = channels.reader();
167 if (!cr->empty() && !cr->front()->write_source) {
168 need_write_sources = true;
171 if (input_change_pending.type & IOChange::ConfigurationChanged) {
172 RCUWriter<ChannelList> writer (channels);
173 boost::shared_ptr<ChannelList> c = writer.get_copy();
175 _n_channels.set(DataType::AUDIO, c->size());
177 if (_io->n_ports().n_audio() > _n_channels.n_audio()) {
178 add_channel_to (c, _io->n_ports().n_audio() - _n_channels.n_audio());
179 } else if (_io->n_ports().n_audio() < _n_channels.n_audio()) {
180 remove_channel_from (c, _n_channels.n_audio() - _io->n_ports().n_audio());
183 need_write_sources = true;
186 if (input_change_pending.type & IOChange::ConnectionsChanged) {
187 get_input_sources ();
188 set_capture_offset ();
189 set_align_style_from_io ();
192 input_change_pending = IOChange::NoChange;
194 /* implicit unlock */
197 if (need_write_sources) {
198 reset_write_sources (false);
201 /* now refill channel buffers */
203 if (speed() != 1.0f || speed() != -1.0f) {
204 seek ((framepos_t) (_session.transport_frame() * (double) speed()));
206 seek (_session.transport_frame());
211 AudioDiskstream::non_realtime_locate (framepos_t location)
213 /* now refill channel buffers */
215 if (speed() != 1.0f || speed() != -1.0f) {
216 seek ((framepos_t) (location * (double) speed()), true);
218 seek (location, true);
223 AudioDiskstream::get_input_sources ()
225 boost::shared_ptr<ChannelList> c = channels.reader();
228 ChannelList::iterator chan;
229 uint32_t ni = _io->n_ports().n_audio();
230 vector<string> connections;
232 for (n = 0, chan = c->begin(); chan != c->end() && n < ni; ++chan, ++n) {
234 connections.clear ();
236 if ((_io->nth (n).get()) && (_io->nth (n)->get_connections (connections) == 0)) {
237 if (!(*chan)->source.name.empty()) {
238 // _source->disable_metering ();
240 (*chan)->source.name = string();
242 (*chan)->source.name = connections[0];
248 AudioDiskstream::find_and_use_playlist (const string& name)
250 boost::shared_ptr<AudioPlaylist> playlist;
252 if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (_session.playlists->by_name (name))) == 0) {
253 playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (DataType::AUDIO, _session, name));
257 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
261 return use_playlist (playlist);
265 AudioDiskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
267 assert(boost::dynamic_pointer_cast<AudioPlaylist>(playlist));
269 Diskstream::use_playlist(playlist);
275 AudioDiskstream::use_new_playlist ()
278 boost::shared_ptr<AudioPlaylist> playlist;
280 if (!in_set_state && destructive()) {
285 newname = Playlist::bump_name (_playlist->name(), _session);
287 newname = Playlist::bump_name (_name, _session);
290 if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (DataType::AUDIO, _session, newname, hidden()))) != 0) {
292 return use_playlist (playlist);
300 AudioDiskstream::use_copy_playlist ()
302 assert(audio_playlist());
308 if (_playlist == 0) {
309 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
314 boost::shared_ptr<AudioPlaylist> playlist;
316 newname = Playlist::bump_name (_playlist->name(), _session);
318 if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist>(PlaylistFactory::create (audio_playlist(), newname))) != 0) {
319 playlist->reset_shares();
320 return use_playlist (playlist);
327 AudioDiskstream::setup_destructive_playlist ()
330 boost::shared_ptr<ChannelList> c = channels.reader();
332 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
333 srcs.push_back ((*chan)->write_source);
336 /* a single full-sized region */
338 assert (!srcs.empty ());
341 plist.add (Properties::name, _name.val());
342 plist.add (Properties::start, 0);
343 plist.add (Properties::length, max_framepos - srcs.front()->natural_position());
345 boost::shared_ptr<Region> region (RegionFactory::create (srcs, plist));
346 _playlist->add_region (region, srcs.front()->natural_position());
348 /* apply region properties and update write sources */
349 use_destructive_playlist();
353 AudioDiskstream::use_destructive_playlist ()
355 /* this is called from the XML-based constructor or ::set_destructive. when called,
356 we already have a playlist and a region, but we need to
357 set up our sources for write. we use the sources associated
358 with the (presumed single, full-extent) region.
361 boost::shared_ptr<Region> rp;
363 const RegionList& rl (_playlist->region_list_property().rlist());
365 /* this can happen when dragging a region onto a tape track */
366 assert((rl.size() == 1));
372 reset_write_sources (false, true);
376 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rp);
379 throw failed_constructor();
382 /* be sure to stretch the region out to the maximum length (non-musical)*/
384 region->set_length (max_framepos - region->position(), 0);
387 ChannelList::iterator chan;
388 boost::shared_ptr<ChannelList> c = channels.reader();
390 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
391 (*chan)->write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
392 assert((*chan)->write_source);
393 (*chan)->write_source->set_allow_remove_if_empty (false);
395 /* this might be false if we switched modes, so force it */
397 #ifdef XXX_OLD_DESTRUCTIVE_API_XXX
398 (*chan)->write_source->set_destructive (true);
400 // should be set when creating the source or loading the state
401 assert ((*chan)->write_source->destructive());
405 /* the source list will never be reset for a destructive track */
409 AudioDiskstream::prepare_record_status(framepos_t capture_start_frame)
411 if (recordable() && destructive()) {
412 boost::shared_ptr<ChannelList> c = channels.reader();
413 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
415 RingBufferNPT<CaptureTransition>::rw_vector transitions;
416 (*chan)->capture_transition_buf->get_write_vector (&transitions);
418 if (transitions.len[0] > 0) {
419 transitions.buf[0]->type = CaptureStart;
420 transitions.buf[0]->capture_val = capture_start_frame;
421 (*chan)->capture_transition_buf->increment_write_ptr(1);
424 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
432 /** Do some record stuff [not described in this comment!]
435 * - Setup playback_distance with the nframes, or nframes adjusted
436 * for current varispeed, if appropriate.
437 * - Setup current_playback_buffer in each ChannelInfo to point to data
438 * that someone can read playback_distance worth of data from.
441 AudioDiskstream::process (BufferSet& bufs, framepos_t transport_frame, pframes_t nframes, framecnt_t& playback_distance, bool need_disk_signal)
444 boost::shared_ptr<ChannelList> c = channels.reader();
445 ChannelList::iterator chan;
446 framecnt_t rec_offset = 0;
447 framecnt_t rec_nframes = 0;
448 bool collect_playback = false;
449 bool can_record = _session.actively_recording ();
451 playback_distance = 0;
453 if (!_io || !_io->active()) {
457 check_record_status (transport_frame, can_record);
463 Glib::Threads::Mutex::Lock sm (state_lock, Glib::Threads::TRY_LOCK);
469 adjust_capture_position = 0;
471 for (chan = c->begin(); chan != c->end(); ++chan) {
472 (*chan)->current_capture_buffer = 0;
473 (*chan)->current_playback_buffer = 0;
476 // Safeguard against situations where process() goes haywire when autopunching
477 // and last_recordable_frame < first_recordable_frame
479 if (last_recordable_frame < first_recordable_frame) {
480 last_recordable_frame = max_framepos;
483 if (record_enabled()) {
485 Evoral::OverlapType ot = Evoral::coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
486 // XXX should this be transport_frame + nframes - 1 ? coverage() expects its parameter ranges to include their end points
487 // XXX also, first_recordable_frame & last_recordable_frame may both be == max_framepos: coverage() will return OverlapNone in that case. Is thak OK?
488 calculate_record_range (ot, transport_frame, nframes, rec_nframes, rec_offset);
490 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1: this time record %2 of %3 frames, offset %4\n", _name, rec_nframes, nframes, rec_offset));
492 if (rec_nframes && !was_recording) {
493 capture_captured = 0;
494 was_recording = true;
498 if (can_record && !_last_capture_sources.empty()) {
499 _last_capture_sources.clear ();
504 uint32_t limit = _io->n_ports ().n_audio();
506 /* one or more ports could already have been removed from _io, but our
507 channel setup hasn't yet been updated. prevent us from trying to
508 use channels that correspond to missing ports. note that the
509 process callback (from which this is called) is always atomic
510 with respect to port removal/addition.
513 for (n = 0, chan = c->begin(); chan != c->end() && n < limit; ++chan, ++n) {
515 ChannelInfo* chaninfo (*chan);
517 chaninfo->capture_buf->get_write_vector (&chaninfo->capture_vector);
519 if (rec_nframes <= (framecnt_t) chaninfo->capture_vector.len[0]) {
521 chaninfo->current_capture_buffer = chaninfo->capture_vector.buf[0];
523 /* note: grab the entire port buffer, but only copy what we were supposed to
524 for recording, and use rec_offset
527 boost::shared_ptr<AudioPort> const ap = _io->audio (n);
529 assert(rec_nframes <= (framecnt_t) ap->get_audio_buffer(nframes).capacity());
531 Sample *buf = bufs.get_audio (n).data(rec_offset);
532 memcpy (chaninfo->current_capture_buffer, buf, sizeof (Sample) * rec_nframes);
536 framecnt_t total = chaninfo->capture_vector.len[0] + chaninfo->capture_vector.len[1];
538 if (rec_nframes > total) {
539 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 overrun in %2, rec_nframes = %3 total space = %4\n",
540 DEBUG_THREAD_SELF, name(), rec_nframes, total));
545 boost::shared_ptr<AudioPort> const ap = _io->audio (n);
548 Sample *buf = bufs.get_audio (n).data(rec_offset);
549 framecnt_t first = chaninfo->capture_vector.len[0];
551 memcpy (chaninfo->capture_wrap_buffer, buf, sizeof (Sample) * first);
552 memcpy (chaninfo->capture_vector.buf[0], buf, sizeof (Sample) * first);
553 memcpy (chaninfo->capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
554 memcpy (chaninfo->capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
556 chaninfo->current_capture_buffer = chaninfo->capture_wrap_buffer;
570 /* data will be written to disk */
572 if (rec_nframes == nframes && rec_offset == 0) {
574 for (chan = c->begin(); chan != c->end(); ++chan) {
575 (*chan)->current_playback_buffer = (*chan)->current_capture_buffer;
578 playback_distance = nframes;
583 /* we can't use the capture buffer as the playback buffer, because
584 we recorded only a part of the current process' cycle data
588 collect_playback = true;
591 adjust_capture_position = rec_nframes;
593 } else if (can_record && record_enabled()) {
595 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
597 for (chan = c->begin(); chan != c->end(); ++chan) {
598 (*chan)->current_playback_buffer = (*chan)->current_capture_buffer;
601 playback_distance = nframes;
605 collect_playback = true;
608 if ((_track->monitoring_state () & MonitoringDisk) || collect_playback) {
610 /* we're doing playback */
612 framecnt_t necessary_samples;
614 /* no varispeed playback if we're recording, because the output .... TBD */
616 if (rec_nframes == 0 && _actual_speed != 1.0) {
617 necessary_samples = (framecnt_t) ceil ((nframes * fabs (_actual_speed))) + 2;
619 necessary_samples = nframes;
622 for (chan = c->begin(); chan != c->end(); ++chan) {
623 (*chan)->playback_buf->get_read_vector (&(*chan)->playback_vector);
628 /* Setup current_playback_buffer in each ChannelInfo to point to data that someone
629 can read necessary_samples (== nframes at a transport speed of 1) worth of data
633 for (chan = c->begin(); chan != c->end(); ++chan, ++n) {
635 ChannelInfo* chaninfo (*chan);
637 if (necessary_samples <= (framecnt_t) chaninfo->playback_vector.len[0]) {
638 /* There are enough samples in the first part of the ringbuffer */
639 chaninfo->current_playback_buffer = chaninfo->playback_vector.buf[0];
642 framecnt_t total = chaninfo->playback_vector.len[0] + chaninfo->playback_vector.len[1];
644 if (necessary_samples > total) {
645 cerr << _name << " Need " << necessary_samples << " total = " << total << endl;
646 cerr << "underrun for " << _name << endl;
647 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 underrun in %2, rec_nframes = %3 total space = %4\n",
648 DEBUG_THREAD_SELF, name(), rec_nframes, total));
654 /* We have enough samples, but not in one lump. Coalesce the two parts
655 into one in playback_wrap_buffer in our ChannelInfo, and specify that
656 as our current_playback_buffer.
659 assert(wrap_buffer_size >= necessary_samples);
661 /* Copy buf[0] from playback_buf */
662 memcpy ((char *) chaninfo->playback_wrap_buffer,
663 chaninfo->playback_vector.buf[0],
664 chaninfo->playback_vector.len[0] * sizeof (Sample));
666 /* Copy buf[1] from playback_buf */
667 memcpy (chaninfo->playback_wrap_buffer + chaninfo->playback_vector.len[0],
668 chaninfo->playback_vector.buf[1],
669 (necessary_samples - chaninfo->playback_vector.len[0])
672 chaninfo->current_playback_buffer = chaninfo->playback_wrap_buffer;
677 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
679 interpolation.set_speed (_target_speed);
682 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++channel) {
683 ChannelInfo* chaninfo (*chan);
685 playback_distance = interpolation.interpolate (
686 channel, nframes, chaninfo->current_playback_buffer, chaninfo->speed_buffer);
688 chaninfo->current_playback_buffer = chaninfo->speed_buffer;
692 playback_distance = nframes;
695 _speed = _target_speed;
698 if (need_disk_signal) {
700 /* copy data over to buffer set */
702 size_t n_buffers = bufs.count().n_audio();
703 size_t n_chans = c->size();
704 gain_t scaling = 1.0f;
706 if (n_chans > n_buffers) {
707 scaling = ((float) n_buffers)/n_chans;
710 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
712 AudioBuffer& buf (bufs.get_audio (n%n_buffers));
713 ChannelInfo* chaninfo (*chan);
716 if (scaling != 1.0f) {
717 buf.read_from_with_gain (chaninfo->current_playback_buffer, nframes, scaling);
719 buf.read_from (chaninfo->current_playback_buffer, nframes);
722 if (scaling != 1.0f) {
723 buf.accumulate_with_gain_from (chaninfo->current_playback_buffer, nframes, scaling);
725 buf.accumulate_from (chaninfo->current_playback_buffer, nframes);
730 /* leave the MIDI count alone */
731 ChanCount cnt (DataType::AUDIO, n_chans);
732 cnt.set (DataType::MIDI, bufs.count().n_midi());
733 bufs.set_count (cnt);
735 /* extra buffers will already be silent, so leave them alone */
742 AudioDiskstream::calculate_playback_distance (pframes_t nframes)
744 frameoffset_t playback_distance = nframes;
746 if (record_enabled()) {
747 playback_distance = nframes;
748 } else if (_actual_speed != 1.0f && _actual_speed != -1.0f) {
749 interpolation.set_speed (_target_speed);
750 boost::shared_ptr<ChannelList> c = channels.reader();
752 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++channel) {
753 playback_distance = interpolation.interpolate (channel, nframes, NULL, NULL);
756 playback_distance = nframes;
759 if (_actual_speed < 0.0) {
760 return -playback_distance;
762 return playback_distance;
766 /** Update various things including playback_sample, read pointer on each channel's playback_buf
767 * and write pointer on each channel's capture_buf. Also wout whether the butler is needed.
768 * @return true if the butler is required.
771 AudioDiskstream::commit (framecnt_t playback_distance)
773 bool need_butler = false;
775 if (!_io || !_io->active()) {
779 if (_actual_speed < 0.0) {
780 playback_sample -= playback_distance;
782 playback_sample += playback_distance;
785 boost::shared_ptr<ChannelList> c = channels.reader();
786 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
788 (*chan)->playback_buf->increment_read_ptr (playback_distance);
790 if (adjust_capture_position) {
791 (*chan)->capture_buf->increment_write_ptr (adjust_capture_position);
795 if (adjust_capture_position != 0) {
796 capture_captured += adjust_capture_position;
797 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 now captured %2 (by %3)\n", name(), capture_captured, adjust_capture_position));
798 adjust_capture_position = 0;
806 if (_io && _io->active()) {
807 need_butler = c->front()->playback_buf->write_space() >= c->front()->playback_buf->bufsize() / 2;
812 if (_io && _io->active()) {
813 need_butler = ((framecnt_t) c->front()->playback_buf->write_space() >= disk_read_chunk_frames)
814 || ((framecnt_t) c->front()->capture_buf->read_space() >= disk_write_chunk_frames);
816 need_butler = ((framecnt_t) c->front()->capture_buf->read_space() >= disk_write_chunk_frames);
824 AudioDiskstream::set_pending_overwrite (bool yn)
826 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
828 _pending_overwrite = yn;
830 overwrite_frame = playback_sample;
832 boost::shared_ptr<ChannelList> c = channels.reader ();
834 overwrite_offset = c->front()->playback_buf->get_read_ptr();
839 AudioDiskstream::overwrite_existing_buffers ()
841 boost::shared_ptr<ChannelList> c = channels.reader();
843 _pending_overwrite = false;
847 Sample* mixdown_buffer;
850 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
852 overwrite_queued = false;
854 /* assume all are the same size */
855 framecnt_t size = c->front()->playback_buf->bufsize();
857 mixdown_buffer = new Sample[size];
858 gain_buffer = new float[size];
860 /* reduce size so that we can fill the buffer correctly (ringbuffers
861 can only handle size-1, otherwise they appear to be empty)
868 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan, ++n) {
870 start = overwrite_frame;
871 framecnt_t cnt = size;
873 /* to fill the buffer without resetting the playback sample, we need to
874 do it one or two chunks (normally two).
876 |----------------------------------------------------------------------|
880 |<- second chunk->||<----------------- first chunk ------------------>|
884 framecnt_t to_read = size - overwrite_offset;
886 if (read ((*chan)->playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, n, reversed)) {
887 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
888 id(), size, playback_sample) << endmsg;
896 if (read ((*chan)->playback_buf->buffer(), mixdown_buffer, gain_buffer, start, cnt, n, reversed)) {
897 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
898 id(), size, playback_sample) << endmsg;
907 _pending_overwrite = false;
908 delete [] gain_buffer;
909 delete [] mixdown_buffer;
914 AudioDiskstream::seek (framepos_t frame, bool complete_refill)
918 ChannelList::iterator chan;
919 boost::shared_ptr<ChannelList> c = channels.reader();
921 Glib::Threads::Mutex::Lock lm (state_lock);
923 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
924 (*chan)->playback_buf->reset ();
925 (*chan)->capture_buf->reset ();
928 /* can't rec-enable in destructive mode if transport is before start */
930 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
931 disengage_record_enable ();
934 playback_sample = frame;
937 if (complete_refill) {
938 /* call _do_refill() to refill the entire buffer, using
939 the largest reads possible.
941 while ((ret = do_refill_with_alloc (false)) > 0) ;
943 /* call _do_refill() to refill just one chunk, and then
946 ret = do_refill_with_alloc (true);
953 AudioDiskstream::can_internal_playback_seek (framecnt_t distance)
955 ChannelList::iterator chan;
956 boost::shared_ptr<ChannelList> c = channels.reader();
958 for (chan = c->begin(); chan != c->end(); ++chan) {
959 if ((*chan)->playback_buf->read_space() < (size_t) distance) {
967 AudioDiskstream::internal_playback_seek (framecnt_t distance)
969 ChannelList::iterator chan;
970 boost::shared_ptr<ChannelList> c = channels.reader();
972 for (chan = c->begin(); chan != c->end(); ++chan) {
973 (*chan)->playback_buf->increment_read_ptr (::llabs(distance));
976 if (first_recordable_frame < max_framepos) {
977 first_recordable_frame += distance;
979 playback_sample += distance;
984 /** Read some data for 1 channel from our playlist into a buffer.
985 * @param buf Buffer to write to.
986 * @param start Session frame to start reading from; updated to where we end up
988 * @param cnt Count of samples to read.
989 * @param reversed true if we are running backwards, otherwise false.
992 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
993 framepos_t& start, framecnt_t cnt,
994 int channel, bool reversed)
996 framecnt_t this_read = 0;
998 framepos_t loop_end = 0;
999 framepos_t loop_start = 0;
1000 framecnt_t offset = 0;
1003 /* XXX we don't currently play loops in reverse. not sure why */
1007 framecnt_t loop_length = 0;
1009 /* Make the use of a Location atomic for this read operation.
1011 Note: Locations don't get deleted, so all we care about
1012 when I say "atomic" is that we are always pointing to
1013 the same one and using a start/length values obtained
1017 if ((loc = loop_location) != 0) {
1018 loop_start = loc->start();
1019 loop_end = loc->end();
1020 loop_length = loop_end - loop_start;
1023 /* if we are looping, ensure that the first frame we read is at the correct
1024 position within the loop.
1027 if (loc && start >= loop_end) {
1028 start = loop_start + ((start - loop_start) % loop_length);
1037 /* We need this while loop in case we hit a loop boundary, in which case our read from
1038 the playlist must be split into more than one section.
1043 /* take any loop into account. we can't read past the end of the loop. */
1045 if (loc && (loop_end - start < cnt)) {
1046 this_read = loop_end - start;
1053 if (this_read == 0) {
1057 this_read = min(cnt,this_read);
1059 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1060 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), id(), this_read,
1067 swap_by_ptr (buf, buf + this_read - 1);
1071 /* if we read to the end of the loop, go back to the beginning */
1081 offset += this_read;
1088 AudioDiskstream::_do_refill_with_alloc (bool partial_fill)
1090 /* We limit disk reads to at most 4MB chunks, which with floating point
1091 samples would be 1M samples. But we might use 16 or 14 bit samples,
1092 in which case 4MB is more samples than that. Therefore size this for
1093 the smallest sample value .. 4MB = 2M samples (16 bit).
1096 Sample* mix_buf = new Sample[2*1048576];
1097 float* gain_buf = new float[2*1048576];
1099 int ret = _do_refill (mix_buf, gain_buf, (partial_fill ? disk_read_chunk_frames : 0));
1107 /** Get some more data from disk and put it in our channels' playback_bufs,
1108 * if there is suitable space in them.
1110 * If fill_level is non-zero, then we will refill the buffer so that there is
1111 * still at least fill_level samples of space left to be filled. This is used
1112 * after locates so that we do not need to wait to fill the entire buffer.
1117 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer, framecnt_t fill_level)
1121 RingBufferNPT<Sample>::rw_vector vector;
1122 bool const reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1123 framecnt_t total_space;
1124 framecnt_t zero_fill;
1126 ChannelList::iterator i;
1127 boost::shared_ptr<ChannelList> c = channels.reader();
1130 /* do not read from disk while session is marked as Loading, to avoid
1131 useless redundant I/O.
1134 if (_session.state_of_the_state() & Session::Loading) {
1142 assert(mixdown_buffer);
1143 assert(gain_buffer);
1150 c->front()->playback_buf->get_write_vector (&vector);
1152 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1153 /* nowhere to write to */
1158 if (fill_level < total_space) {
1159 total_space -= fill_level;
1161 /* we can't do anything with it */
1166 /* if we're running close to normal speed and there isn't enough
1167 space to do disk_read_chunk_frames of I/O, then don't bother.
1169 at higher speeds, just do it because the sync between butler
1170 and audio thread may not be good enough.
1172 Note: it is a design assumption that disk_read_chunk_frames is smaller
1173 than the playback buffer size, so this check should never trip when
1174 the playback buffer is empty.
1177 if ((total_space < disk_read_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1181 /* when slaved, don't try to get too close to the read pointer. this
1182 leaves space for the buffer reversal to have something useful to
1186 if (_slaved && total_space < (framecnt_t) (c->front()->playback_buf->bufsize() / 2)) {
1192 if (file_frame == 0) {
1194 /* at start: nothing to do but fill with silence */
1196 for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
1198 ChannelInfo* chan (*i);
1199 chan->playback_buf->get_write_vector (&vector);
1200 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1201 if (vector.len[1]) {
1202 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1204 chan->playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1209 if (file_frame < total_space) {
1211 /* too close to the start: read what we can,
1212 and then zero fill the rest
1215 zero_fill = total_space - file_frame;
1216 total_space = file_frame;
1225 if (file_frame == max_framepos) {
1227 /* at end: nothing to do but fill with silence */
1229 for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
1231 ChannelInfo* chan (*i);
1232 chan->playback_buf->get_write_vector (&vector);
1233 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1234 if (vector.len[1]) {
1235 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1237 chan->playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1242 if (file_frame > max_framepos - total_space) {
1244 /* to close to the end: read what we can, and zero fill the rest */
1246 zero_fill = total_space - (max_framepos - file_frame);
1247 total_space = max_framepos - file_frame;
1254 framepos_t file_frame_tmp = 0;
1256 /* total_space is in samples. We want to optimize read sizes in various sizes using bytes */
1258 const size_t bits_per_sample = format_data_width (_session.config.get_native_file_data_format());
1259 size_t total_bytes = total_space * bits_per_sample / 8;
1261 /* chunk size range is 256kB to 4MB. Bigger is faster in terms of MB/sec, but bigger chunk size always takes longer
1263 size_t byte_size_for_read = max ((size_t) (256 * 1024), min ((size_t) (4 * 1048576), total_bytes));
1265 /* find nearest (lower) multiple of 16384 */
1267 byte_size_for_read = (byte_size_for_read / 16384) * 16384;
1269 /* now back to samples */
1271 framecnt_t samples_to_read = byte_size_for_read / (bits_per_sample / 8);
1273 //cerr << name() << " will read " << byte_size_for_read << " out of total bytes " << total_bytes << " in buffer of "
1274 // << c->front()->playback_buf->bufsize() * bits_per_sample / 8 << " bps = " << bits_per_sample << endl;
1275 // cerr << name () << " read samples = " << samples_to_read << " out of total space " << total_space << " in buffer of " << c->front()->playback_buf->bufsize() << " samples\n";
1277 // uint64_t before = g_get_monotonic_time ();
1278 // uint64_t elapsed;
1280 for (chan_n = 0, i = c->begin(); i != c->end(); ++i, ++chan_n) {
1282 ChannelInfo* chan (*i);
1285 framecnt_t len1, len2;
1287 chan->playback_buf->get_write_vector (&vector);
1289 if ((framecnt_t) vector.len[0] > samples_to_read) {
1291 /* we're not going to fill the first chunk, so certainly do not bother with the
1292 other part. it won't be connected with the part we do fill, as in:
1294 .... => writable space
1295 ++++ => readable space
1296 ^^^^ => 1 x disk_read_chunk_frames that would be filled
1298 |......|+++++++++++++|...............................|
1303 So, just pretend that the buf1 part isn't there.
1313 file_frame_tmp = file_frame;
1315 buf1 = vector.buf[0];
1316 len1 = vector.len[0];
1317 buf2 = vector.buf[1];
1318 len2 = vector.len[1];
1320 to_read = min (ts, len1);
1321 to_read = min (to_read, (framecnt_t) samples_to_read);
1323 assert (to_read >= 0);
1327 if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan_n, reversed)) {
1332 chan->playback_buf->increment_write_ptr (to_read);
1336 to_read = min (ts, len2);
1340 /* we read all of vector.len[0], but it wasn't the
1341 entire samples_to_read of data, so read some or
1342 all of vector.len[1] as well.
1345 if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan_n, reversed)) {
1350 chan->playback_buf->increment_write_ptr (to_read);
1354 /* XXX: do something */
1359 // elapsed = g_get_monotonic_time () - before;
1360 // cerr << "\tbandwidth = " << (byte_size_for_read / 1048576.0) / (elapsed/1000000.0) << "MB/sec\n";
1362 file_frame = file_frame_tmp;
1363 assert (file_frame >= 0);
1365 ret = ((total_space - samples_to_read) > disk_read_chunk_frames);
1367 c->front()->playback_buf->get_write_vector (&vector);
1373 /** Flush pending data to disk.
1375 * Important note: this function will write *AT MOST* disk_write_chunk_frames
1376 * of data to disk. it will never write more than that. If it writes that
1377 * much and there is more than that waiting to be written, it will return 1,
1378 * otherwise 0 on success or -1 on failure.
1380 * If there is less than disk_write_chunk_frames to be written, no data will be
1381 * written at all unless @a force_flush is true.
1384 AudioDiskstream::do_flush (RunContext /*context*/, bool force_flush)
1388 RingBufferNPT<Sample>::rw_vector vector;
1389 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1392 transvec.buf[0] = 0;
1393 transvec.buf[1] = 0;
1397 boost::shared_ptr<ChannelList> c = channels.reader();
1398 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1400 (*chan)->capture_buf->get_read_vector (&vector);
1402 total = vector.len[0] + vector.len[1];
1404 if (total == 0 || (total < disk_write_chunk_frames && !force_flush && was_recording)) {
1408 /* if there are 2+ chunks of disk i/o possible for
1409 this track, let the caller know so that it can arrange
1410 for us to be called again, ASAP.
1412 if we are forcing a flush, then if there is* any* extra
1413 work, let the caller know.
1415 if we are no longer recording and there is any extra work,
1416 let the caller know too.
1419 if (total >= 2 * disk_write_chunk_frames || ((force_flush || !was_recording) && total > disk_write_chunk_frames)) {
1423 to_write = min (disk_write_chunk_frames, (framecnt_t) vector.len[0]);
1425 // check the transition buffer when recording destructive
1426 // important that we get this after the capture buf
1428 if (destructive()) {
1429 (*chan)->capture_transition_buf->get_read_vector(&transvec);
1430 size_t transcount = transvec.len[0] + transvec.len[1];
1433 for (ti=0; ti < transcount; ++ti) {
1434 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1436 if (captrans.type == CaptureStart) {
1437 // by definition, the first data we got above represents the given capture pos
1439 (*chan)->write_source->mark_capture_start (captrans.capture_val);
1440 (*chan)->curr_capture_cnt = 0;
1442 } else if (captrans.type == CaptureEnd) {
1444 // capture end, the capture_val represents total frames in capture
1446 if (captrans.capture_val <= (*chan)->curr_capture_cnt + to_write) {
1448 // shorten to make the write a perfect fit
1449 uint32_t nto_write = (captrans.capture_val - (*chan)->curr_capture_cnt);
1451 if (nto_write < to_write) {
1452 ret = 1; // should we?
1454 to_write = nto_write;
1456 (*chan)->write_source->mark_capture_end ();
1458 // increment past this transition, but go no further
1463 // actually ends just beyond this chunk, so force more work
1471 (*chan)->capture_transition_buf->increment_read_ptr(ti);
1475 if ((!(*chan)->write_source) || (*chan)->write_source->write (vector.buf[0], to_write) != to_write) {
1476 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), id()) << endmsg;
1480 (*chan)->capture_buf->increment_read_ptr (to_write);
1481 (*chan)->curr_capture_cnt += to_write;
1483 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_write_chunk_frames) && !destructive()) {
1485 /* we wrote all of vector.len[0] but it wasn't an entire
1486 disk_write_chunk_frames of data, so arrange for some part
1487 of vector.len[1] to be flushed to disk as well.
1490 to_write = min ((framecnt_t)(disk_write_chunk_frames - to_write), (framecnt_t) vector.len[1]);
1492 DEBUG_TRACE (DEBUG::Butler, string_compose ("%1 additional write of %2\n", name(), to_write));
1494 if ((*chan)->write_source->write (vector.buf[1], to_write) != to_write) {
1495 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), id()) << endmsg;
1499 (*chan)->capture_buf->increment_read_ptr (to_write);
1500 (*chan)->curr_capture_cnt += to_write;
1509 AudioDiskstream::transport_stopped_wallclock (struct tm& when, time_t twhen, bool abort_capture)
1511 uint32_t buffer_position;
1512 bool more_work = true;
1514 boost::shared_ptr<AudioRegion> region;
1515 framecnt_t total_capture;
1517 SourceList::iterator src;
1518 ChannelList::iterator chan;
1519 vector<CaptureInfo*>::iterator ci;
1520 boost::shared_ptr<ChannelList> c = channels.reader();
1522 bool mark_write_completed = false;
1526 /* butler is already stopped, but there may be work to do
1527 to flush remaining data to disk.
1530 while (more_work && !err) {
1531 switch (do_flush (TransportContext, true)) {
1538 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1543 /* XXX is there anything we can do if err != 0 ? */
1544 Glib::Threads::Mutex::Lock lm (capture_info_lock);
1546 if (capture_info.empty()) {
1550 if (abort_capture) {
1552 if (destructive()) {
1556 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1558 if ((*chan)->write_source) {
1560 (*chan)->write_source->mark_for_remove ();
1561 (*chan)->write_source->drop_references ();
1562 (*chan)->write_source.reset ();
1565 /* new source set up in "out" below */
1571 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1572 total_capture += (*ci)->frames;
1575 /* figure out the name for this take */
1577 for (n = 0, chan = c->begin(); chan != c->end(); ++chan, ++n) {
1579 boost::shared_ptr<AudioFileSource> s = (*chan)->write_source;
1583 s->update_header (capture_info.front()->start, when, twhen);
1584 s->set_captured_for (_name.val());
1585 s->mark_immutable ();
1587 if (Config->get_auto_analyse_audio()) {
1588 Analyser::queue_source_for_analysis (s, true);
1591 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("newly captured source %1 length %2\n", s->path(), s->length (0)));
1595 /* destructive tracks have a single, never changing region */
1597 if (destructive()) {
1599 /* send a signal that any UI can pick up to do the right thing. there is
1600 a small problem here in that a UI may need the peak data to be ready
1601 for the data that was recorded and this isn't interlocked with that
1602 process. this problem is deferred to the UI.
1605 _playlist->LayeringChanged(); // XXX this may not get the UI to do the right thing
1609 string whole_file_region_name;
1610 whole_file_region_name = region_name_from_path (c->front()->write_source->name(), true);
1612 /* Register a new region with the Session that
1613 describes the entire source. Do this first
1614 so that any sub-regions will obviously be
1615 children of this one (later!)
1621 plist.add (Properties::start, c->front()->write_source->last_capture_start_frame());
1622 plist.add (Properties::length, total_capture);
1623 plist.add (Properties::name, whole_file_region_name);
1624 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1625 rx->set_automatic (true);
1626 rx->set_whole_file (true);
1628 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1629 region->special_set_position (capture_info.front()->start);
1633 catch (failed_constructor& err) {
1634 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1638 _last_capture_sources.insert (_last_capture_sources.end(), srcs.begin(), srcs.end());
1640 _playlist->clear_changes ();
1641 _playlist->set_capture_insertion_in_progress (true);
1642 _playlist->freeze ();
1644 for (buffer_position = c->front()->write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1648 RegionFactory::region_name (region_name, whole_file_region_name, false);
1650 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("%1 capture bufpos %5 start @ %2 length %3 add new region %4\n",
1651 _name, (*ci)->start, (*ci)->frames, region_name, buffer_position));
1657 plist.add (Properties::start, buffer_position);
1658 plist.add (Properties::length, (*ci)->frames);
1659 plist.add (Properties::name, region_name);
1661 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, plist));
1662 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1665 catch (failed_constructor& err) {
1666 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1667 continue; /* XXX is this OK? */
1670 i_am_the_modifier++;
1672 _playlist->add_region (region, (*ci)->start, 1, non_layered());
1673 _playlist->set_layer (region, DBL_MAX);
1674 i_am_the_modifier--;
1676 buffer_position += (*ci)->frames;
1680 _playlist->set_capture_insertion_in_progress (false);
1681 _session.add_command (new StatefulDiffCommand (_playlist));
1684 mark_write_completed = true;
1687 reset_write_sources (mark_write_completed);
1691 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1695 capture_info.clear ();
1696 capture_start_frame = 0;
1700 AudioDiskstream::transport_looped (framepos_t transport_frame)
1702 if (was_recording) {
1703 // all we need to do is finish this capture, with modified capture length
1704 boost::shared_ptr<ChannelList> c = channels.reader();
1708 // the next region will start recording via the normal mechanism
1709 // we'll set the start position to the current transport pos
1710 // no latency adjustment or capture offset needs to be made, as that already happened the first time
1711 capture_start_frame = transport_frame;
1712 first_recordable_frame = transport_frame; // mild lie
1713 last_recordable_frame = max_framepos;
1714 was_recording = true;
1716 if (recordable() && destructive()) {
1717 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1719 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1720 (*chan)->capture_transition_buf->get_write_vector(&transvec);
1722 if (transvec.len[0] > 0) {
1723 transvec.buf[0]->type = CaptureStart;
1724 transvec.buf[0]->capture_val = capture_start_frame;
1725 (*chan)->capture_transition_buf->increment_write_ptr(1);
1729 fatal << X_("programming error: capture_transition_buf is full on rec loop! inconceivable!")
1739 AudioDiskstream::finish_capture (boost::shared_ptr<ChannelList> c)
1741 was_recording = false;
1742 first_recordable_frame = max_framepos;
1743 last_recordable_frame = max_framepos;
1745 if (capture_captured == 0) {
1749 if (recordable() && destructive()) {
1750 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1752 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1753 (*chan)->capture_transition_buf->get_write_vector(&transvec);
1755 if (transvec.len[0] > 0) {
1756 transvec.buf[0]->type = CaptureEnd;
1757 transvec.buf[0]->capture_val = capture_captured;
1758 (*chan)->capture_transition_buf->increment_write_ptr(1);
1762 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1768 CaptureInfo* ci = new CaptureInfo;
1770 ci->start = capture_start_frame;
1771 ci->frames = capture_captured;
1773 /* XXX theoretical race condition here. Need atomic exchange ?
1774 However, the circumstances when this is called right
1775 now (either on record-disable or transport_stopped)
1776 mean that no actual race exists. I think ...
1777 We now have a capture_info_lock, but it is only to be used
1778 to synchronize in the transport_stop and the capture info
1779 accessors, so that invalidation will not occur (both non-realtime).
1782 DEBUG_TRACE (DEBUG::CaptureAlignment, string_compose ("Finish capture, add new CI, %1 + %2\n", ci->start, ci->frames));
1784 capture_info.push_back (ci);
1785 capture_captured = 0;
1787 /* now we've finished a capture, reset first_recordable_frame for next time */
1788 first_recordable_frame = max_framepos;
1792 AudioDiskstream::set_record_enabled (bool yn)
1794 if (!recordable() || !_session.record_enabling_legal() || _io->n_ports().n_audio() == 0 || record_safe ()) {
1798 /* can't rec-enable in destructive mode if transport is before start */
1800 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1804 /* yes, i know that this not proof against race conditions, but its
1805 good enough. i think.
1808 if (record_enabled() != yn) {
1810 engage_record_enable ();
1812 disengage_record_enable ();
1815 RecordEnableChanged (); /* EMIT SIGNAL */
1820 AudioDiskstream::set_record_safe (bool yn)
1822 if (!recordable() || !_session.record_enabling_legal() || _io->n_ports().n_audio() == 0) {
1826 /* can't rec-safe in destructive mode if transport is before start ????
1829 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1833 /* yes, i know that this not proof against race conditions, but its
1834 good enough. i think.
1837 if (record_safe () != yn) {
1839 engage_record_safe ();
1841 disengage_record_safe ();
1844 RecordSafeChanged (); /* EMIT SIGNAL */
1849 AudioDiskstream::prep_record_enable ()
1851 if (!recordable() || !_session.record_enabling_legal() || _io->n_ports().n_audio() == 0 || record_safe ()) { // REQUIRES REVIEW "|| record_safe ()"
1855 /* can't rec-enable in destructive mode if transport is before start */
1857 if (destructive() && _session.transport_frame() < _session.current_start_frame()) {
1861 bool rolling = _session.transport_speed() != 0.0f;
1862 boost::shared_ptr<ChannelList> c = channels.reader();
1864 capturing_sources.clear ();
1866 if (Config->get_monitoring_model() == HardwareMonitoring) {
1868 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1869 (*chan)->source.request_input_monitoring (!(_session.config.get_auto_input() && rolling));
1870 capturing_sources.push_back ((*chan)->write_source);
1871 Source::Lock lock((*chan)->write_source->mutex());
1872 (*chan)->write_source->mark_streaming_write_started (lock);
1876 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1877 capturing_sources.push_back ((*chan)->write_source);
1878 Source::Lock lock((*chan)->write_source->mutex());
1879 (*chan)->write_source->mark_streaming_write_started (lock);
1887 AudioDiskstream::prep_record_disable ()
1889 boost::shared_ptr<ChannelList> c = channels.reader();
1890 if (Config->get_monitoring_model() == HardwareMonitoring) {
1891 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
1892 (*chan)->source.request_input_monitoring (false);
1895 capturing_sources.clear ();
1901 AudioDiskstream::get_state ()
1903 XMLNode& node (Diskstream::get_state());
1907 boost::shared_ptr<ChannelList> c = channels.reader();
1908 snprintf (buf, sizeof(buf), "%u", (unsigned int) c->size());
1909 node.add_property ("channels", buf);
1911 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1913 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1914 XMLNode* cs_grandchild;
1916 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1917 cs_grandchild = new XMLNode (X_("file"));
1918 cs_grandchild->add_property (X_("path"), (*i)->path());
1919 cs_child->add_child_nocopy (*cs_grandchild);
1922 /* store the location where capture will start */
1926 if (_session.preroll_record_punch_enabled ()) {
1927 snprintf (buf, sizeof (buf), "%" PRId64, _session.preroll_record_punch_pos ());
1928 } else if (_session.config.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1929 snprintf (buf, sizeof (buf), "%" PRId64, pi->start());
1931 snprintf (buf, sizeof (buf), "%" PRId64, _session.transport_frame());
1934 cs_child->add_property (X_("at"), buf);
1935 node.add_child_nocopy (*cs_child);
1942 AudioDiskstream::set_state (const XMLNode& node, int version)
1944 XMLProperty const * prop;
1945 XMLNodeList nlist = node.children();
1946 XMLNodeIterator niter;
1947 uint32_t nchans = 1;
1948 XMLNode* capture_pending_node = 0;
1951 /* prevent write sources from being created */
1953 in_set_state = true;
1955 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1956 if ((*niter)->name() == IO::state_node_name) {
1957 deprecated_io_node = new XMLNode (**niter);
1960 if ((*niter)->name() == X_("CapturingSources")) {
1961 capture_pending_node = *niter;
1965 if (Diskstream::set_state (node, version)) {
1969 if ((prop = node.property ("channels")) != 0) {
1970 nchans = atoi (prop->value().c_str());
1973 // create necessary extra channels
1974 // we are always constructed with one and we always need one
1976 _n_channels.set(DataType::AUDIO, channels.reader()->size());
1978 if (nchans > _n_channels.n_audio()) {
1980 add_channel (nchans - _n_channels.n_audio());
1981 IO::PortCountChanged(_n_channels);
1983 } else if (nchans < _n_channels.n_audio()) {
1985 remove_channel (_n_channels.n_audio() - nchans);
1990 if (!destructive() && capture_pending_node) {
1991 /* destructive streams have one and only one source per channel,
1992 and so they never end up in pending capture in any useful
1995 use_pending_capture_data (*capture_pending_node);
1998 in_set_state = false;
2000 /* make sure this is clear before we do anything else */
2002 capturing_sources.clear ();
2004 /* write sources are handled when we handle the input set
2005 up of the IO that owns this DS (::non_realtime_input_change())
2012 AudioDiskstream::use_new_write_source (uint32_t n)
2014 boost::shared_ptr<ChannelList> c = channels.reader();
2016 if (!recordable()) {
2020 if (n >= c->size()) {
2021 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
2025 ChannelInfo* chan = (*c)[n];
2028 if ((chan->write_source = _session.create_audio_source_for_session (
2029 n_channels().n_audio(), write_source_name(), n, destructive())) == 0) {
2030 throw failed_constructor();
2034 catch (failed_constructor &err) {
2035 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
2036 chan->write_source.reset ();
2040 /* do not remove destructive files even if they are empty */
2042 chan->write_source->set_allow_remove_if_empty (!destructive());
2048 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool /*force*/)
2050 ChannelList::iterator chan;
2051 boost::shared_ptr<ChannelList> c = channels.reader();
2054 if (!_session.writable() || !recordable()) {
2058 capturing_sources.clear ();
2060 for (chan = c->begin(), n = 0; chan != c->end(); ++chan, ++n) {
2062 if (!destructive()) {
2064 if ((*chan)->write_source) {
2066 if (mark_write_complete) {
2067 Source::Lock lock((*chan)->write_source->mutex());
2068 (*chan)->write_source->mark_streaming_write_completed (lock);
2069 (*chan)->write_source->done_with_peakfile_writes ();
2072 if ((*chan)->write_source->removable()) {
2073 (*chan)->write_source->mark_for_remove ();
2074 (*chan)->write_source->drop_references ();
2077 (*chan)->write_source.reset ();
2080 use_new_write_source (n);
2082 if (record_enabled()) {
2083 capturing_sources.push_back ((*chan)->write_source);
2088 if ((*chan)->write_source == 0) {
2089 use_new_write_source (n);
2094 if (destructive() && !c->empty ()) {
2096 /* we now have all our write sources set up, so create the
2097 playlist's single region.
2100 if (_playlist->empty()) {
2101 setup_destructive_playlist ();
2107 AudioDiskstream::set_block_size (pframes_t /*nframes*/)
2109 if (_session.get_block_size() > speed_buffer_size) {
2110 speed_buffer_size = _session.get_block_size();
2111 boost::shared_ptr<ChannelList> c = channels.reader();
2113 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
2114 if ((*chan)->speed_buffer)
2115 delete [] (*chan)->speed_buffer;
2116 (*chan)->speed_buffer = new Sample[speed_buffer_size];
2119 allocate_temporary_buffers ();
2123 AudioDiskstream::allocate_temporary_buffers ()
2125 /* make sure the wrap buffer is at least large enough to deal
2126 with the speeds up to 1.2, to allow for micro-variation
2127 when slaving to MTC, Timecode etc.
2130 double const sp = max (fabs (_actual_speed), 1.2);
2131 framecnt_t required_wrap_size = (framecnt_t) ceil (_session.get_block_size() * sp) + 2;
2133 if (required_wrap_size > wrap_buffer_size) {
2135 boost::shared_ptr<ChannelList> c = channels.reader();
2137 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
2138 if ((*chan)->playback_wrap_buffer) {
2139 delete [] (*chan)->playback_wrap_buffer;
2141 (*chan)->playback_wrap_buffer = new Sample[required_wrap_size];
2142 if ((*chan)->capture_wrap_buffer) {
2143 delete [] (*chan)->capture_wrap_buffer;
2145 (*chan)->capture_wrap_buffer = new Sample[required_wrap_size];
2148 wrap_buffer_size = required_wrap_size;
2153 AudioDiskstream::request_input_monitoring (bool yn)
2155 boost::shared_ptr<ChannelList> c = channels.reader();
2157 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
2158 (*chan)->source.request_input_monitoring (yn);
2163 AudioDiskstream::set_align_style_from_io ()
2165 bool have_physical = false;
2167 if (_alignment_choice != Automatic) {
2175 get_input_sources ();
2177 boost::shared_ptr<ChannelList> c = channels.reader();
2179 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
2180 if ((*chan)->source.is_physical ()) {
2181 have_physical = true;
2187 // compensate for latency when bouncing from master or mixbus.
2188 // we need to use "ExistingMaterial" to pick up the master bus' latency
2189 // see also Route::direct_feeds_according_to_reality
2191 ios.push_back (_io);
2192 if (_session.master_out() && ios.fed_by (_session.master_out()->output())) {
2193 have_physical = true;
2195 for (uint32_t n = 0; n < NUM_MIXBUSES && !have_physical; ++n) {
2196 if (_session.get_mixbus (n) && ios.fed_by (_session.get_mixbus(n)->output())) {
2197 have_physical = true;
2202 if (have_physical) {
2203 set_align_style (ExistingMaterial);
2205 set_align_style (CaptureTime);
2210 AudioDiskstream::add_channel_to (boost::shared_ptr<ChannelList> c, uint32_t how_many)
2212 while (how_many--) {
2213 c->push_back (new ChannelInfo(
2214 _session.butler()->audio_diskstream_playback_buffer_size(),
2215 _session.butler()->audio_diskstream_capture_buffer_size(),
2216 speed_buffer_size, wrap_buffer_size));
2217 interpolation.add_channel_to (
2218 _session.butler()->audio_diskstream_playback_buffer_size(),
2222 _n_channels.set(DataType::AUDIO, c->size());
2228 AudioDiskstream::add_channel (uint32_t how_many)
2230 RCUWriter<ChannelList> writer (channels);
2231 boost::shared_ptr<ChannelList> c = writer.get_copy();
2233 return add_channel_to (c, how_many);
2237 AudioDiskstream::remove_channel_from (boost::shared_ptr<ChannelList> c, uint32_t how_many)
2239 while (how_many-- && !c->empty()) {
2242 interpolation.remove_channel_from ();
2245 _n_channels.set(DataType::AUDIO, c->size());
2251 AudioDiskstream::remove_channel (uint32_t how_many)
2253 RCUWriter<ChannelList> writer (channels);
2254 boost::shared_ptr<ChannelList> c = writer.get_copy();
2256 return remove_channel_from (c, how_many);
2260 AudioDiskstream::playback_buffer_load () const
2262 boost::shared_ptr<ChannelList> c = channels.reader();
2268 return (float) ((double) c->front()->playback_buf->read_space()/
2269 (double) c->front()->playback_buf->bufsize());
2273 AudioDiskstream::capture_buffer_load () const
2275 boost::shared_ptr<ChannelList> c = channels.reader();
2281 return (float) ((double) c->front()->capture_buf->write_space()/
2282 (double) c->front()->capture_buf->bufsize());
2286 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2288 XMLProperty const * prop;
2289 XMLNodeList nlist = node.children();
2290 XMLNodeIterator niter;
2291 boost::shared_ptr<AudioFileSource> fs;
2292 boost::shared_ptr<AudioFileSource> first_fs;
2293 SourceList pending_sources;
2294 framepos_t position;
2296 if ((prop = node.property (X_("at"))) == 0) {
2300 if (sscanf (prop->value().c_str(), "%" PRIu64, &position) != 1) {
2304 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2305 if ((*niter)->name() == X_("file")) {
2307 if ((prop = (*niter)->property (X_("path"))) == 0) {
2311 // This protects sessions from errant CapturingSources in stored sessions
2313 if (g_stat (prop->value().c_str(), &sbuf)) {
2317 /* XXX as of June 2014, we always record to mono
2318 files. Since this Source is being created as part of
2319 crash recovery, we know that we need the first
2320 channel (the final argument to the SourceFactory
2321 call below). If we ever support non-mono files for
2322 capture, this will need rethinking.
2326 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createForRecovery (DataType::AUDIO, _session, prop->value(), 0));
2329 catch (failed_constructor& err) {
2330 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2331 _name, prop->value())
2336 pending_sources.push_back (fs);
2338 if (first_fs == 0) {
2342 fs->set_captured_for (_name.val());
2346 if (pending_sources.size() == 0) {
2347 /* nothing can be done */
2351 if (pending_sources.size() != _n_channels.n_audio()) {
2352 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2359 boost::shared_ptr<AudioRegion> wf_region;
2360 boost::shared_ptr<AudioRegion> region;
2362 /* First create the whole file region */
2366 plist.add (Properties::start, 0);
2367 plist.add (Properties::length, first_fs->length (first_fs->timeline_position()));
2368 plist.add (Properties::name, region_name_from_path (first_fs->name(), true));
2370 wf_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, plist));
2372 wf_region->set_automatic (true);
2373 wf_region->set_whole_file (true);
2374 wf_region->special_set_position (position);
2376 /* Now create a region that isn't the whole file for adding to
2379 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, plist));
2381 _playlist->add_region (region, position);
2384 catch (failed_constructor& err) {
2385 error << string_compose (
2386 _("%1: cannot create whole-file region from pending capture sources"),
2396 #ifdef XXX_OLD_DESTRUCTIVE_API_XXX
2398 AudioDiskstream::set_non_layered (bool yn)
2400 if (yn != non_layered()) {
2403 _flags = Flag (_flags | NonLayered);
2405 _flags = Flag (_flags & ~NonLayered);
2413 AudioDiskstream::set_destructive (bool yn)
2415 if (yn != destructive()) {
2418 bool bounce_ignored;
2419 /* requestor should already have checked this and
2420 bounced if necessary and desired
2422 if (!can_become_destructive (bounce_ignored)) {
2425 _flags = Flag (_flags | Destructive);
2426 use_destructive_playlist ();
2428 _flags = Flag (_flags & ~Destructive);
2429 reset_write_sources (true, true);
2437 AudioDiskstream::can_become_destructive (bool& requires_bounce) const
2439 if (Profile->get_trx()) {
2444 requires_bounce = false;
2448 /* if no regions are present: easy */
2450 if (_playlist->n_regions() == 0) {
2451 requires_bounce = false;
2455 /* is there only one region ? */
2457 if (_playlist->n_regions() != 1) {
2458 requires_bounce = true;
2462 boost::shared_ptr<Region> first;
2464 const RegionList& rl (_playlist->region_list_property().rlist());
2465 assert((rl.size() == 1));
2471 requires_bounce = false;
2475 /* do the source(s) for the region cover the session start position ? */
2477 if (first->position() != _session.current_start_frame()) {
2478 // what is the idea here? why start() ??
2479 if (first->start() > _session.current_start_frame()) {
2480 requires_bounce = true;
2485 /* currently RouteTimeAxisView::set_track_mode does not
2486 * implement bounce. Existing regions cannot be converted.
2488 * so let's make sure this region is already set up
2489 * as tape-track (spanning the complete range)
2491 if (first->length() != max_framepos - first->position()) {
2492 requires_bounce = true;
2496 /* is the source used by only 1 playlist ? */
2498 boost::shared_ptr<AudioRegion> afirst = boost::dynamic_pointer_cast<AudioRegion> (first);
2502 if (_session.playlists->source_use_count (afirst->source()) > 1) {
2503 requires_bounce = true;
2507 requires_bounce = false;
2513 AudioDiskstream::adjust_playback_buffering ()
2515 boost::shared_ptr<ChannelList> c = channels.reader();
2517 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
2518 (*chan)->resize_playback (_session.butler()->audio_diskstream_playback_buffer_size());
2523 AudioDiskstream::adjust_capture_buffering ()
2525 boost::shared_ptr<ChannelList> c = channels.reader();
2527 for (ChannelList::iterator chan = c->begin(); chan != c->end(); ++chan) {
2528 (*chan)->resize_capture (_session.butler()->audio_diskstream_capture_buffer_size());
2533 AudioDiskstream::ChannelSource::is_physical () const
2539 return AudioEngine::instance()->port_is_physical (name);
2543 AudioDiskstream::ChannelSource::request_input_monitoring (bool yn) const
2549 return AudioEngine::instance()->request_input_monitoring (name, yn);
2552 AudioDiskstream::ChannelInfo::ChannelInfo (framecnt_t playback_bufsize, framecnt_t capture_bufsize, framecnt_t speed_size, framecnt_t wrap_size)
2554 current_capture_buffer = 0;
2555 current_playback_buffer = 0;
2556 curr_capture_cnt = 0;
2558 speed_buffer = new Sample[speed_size];
2559 playback_wrap_buffer = new Sample[wrap_size];
2560 capture_wrap_buffer = new Sample[wrap_size];
2562 playback_buf = new RingBufferNPT<Sample> (playback_bufsize);
2563 capture_buf = new RingBufferNPT<Sample> (capture_bufsize);
2564 capture_transition_buf = new RingBufferNPT<CaptureTransition> (256);
2566 /* touch the ringbuffer buffers, which will cause
2567 them to be mapped into locked physical RAM if
2568 we're running with mlockall(). this doesn't do
2572 memset (playback_buf->buffer(), 0, sizeof (Sample) * playback_buf->bufsize());
2573 memset (capture_buf->buffer(), 0, sizeof (Sample) * capture_buf->bufsize());
2574 memset (capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * capture_transition_buf->bufsize());
2578 AudioDiskstream::ChannelInfo::resize_playback (framecnt_t playback_bufsize)
2580 delete playback_buf;
2581 playback_buf = new RingBufferNPT<Sample> (playback_bufsize);
2582 memset (playback_buf->buffer(), 0, sizeof (Sample) * playback_buf->bufsize());
2586 AudioDiskstream::ChannelInfo::resize_capture (framecnt_t capture_bufsize)
2590 capture_buf = new RingBufferNPT<Sample> (capture_bufsize);
2591 memset (capture_buf->buffer(), 0, sizeof (Sample) * capture_buf->bufsize());
2594 AudioDiskstream::ChannelInfo::~ChannelInfo ()
2597 if (write_source->removable()) {
2598 /* this is a "stub" write source which exists in the
2599 Session source list, but is removable. We must emit
2600 a drop references call because it should not
2601 continue to exist. If we do not do this, then the
2602 Session retains a reference to it, it is not
2603 deleted, and later attempts to create a new source
2604 file will use wierd naming because it already
2607 XXX longer term TO-DO: do not add to session source
2608 list until we write to the source.
2610 write_source->drop_references ();
2614 write_source.reset ();
2616 delete [] speed_buffer;
2619 delete [] playback_wrap_buffer;
2620 playback_wrap_buffer = 0;
2622 delete [] capture_wrap_buffer;
2623 capture_wrap_buffer = 0;
2625 delete playback_buf;
2631 delete capture_transition_buf;
2632 capture_transition_buf = 0;
2637 AudioDiskstream::set_name (string const & name)
2639 if (_name == name) {
2642 Diskstream::set_name (name);
2644 /* get a new write source so that its name reflects the new diskstream name */
2646 boost::shared_ptr<ChannelList> c = channels.reader();
2647 ChannelList::iterator i;
2650 for (n = 0, i = c->begin(); i != c->end(); ++i, ++n) {
2651 use_new_write_source (n);
2658 AudioDiskstream::set_write_source_name (const std::string& str) {
2659 if (_write_source_name == str) {
2663 Diskstream::set_write_source_name (str);
2665 if (_write_source_name == name()) {
2668 boost::shared_ptr<ChannelList> c = channels.reader();
2669 ChannelList::iterator i;
2672 for (n = 0, i = c->begin(); i != c->end(); ++i, ++n) {
2673 use_new_write_source (n);