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 <pbd/error.h>
34 #include <pbd/basename.h>
35 #include <glibmm/thread.h>
36 #include <pbd/xml++.h>
37 #include <pbd/memento_command.h>
38 #include <pbd/enumwriter.h>
40 #include <ardour/ardour.h>
41 #include <ardour/audioengine.h>
42 #include <ardour/audio_diskstream.h>
43 #include <ardour/utils.h>
44 #include <ardour/configuration.h>
45 #include <ardour/audiofilesource.h>
46 #include <ardour/destructive_filesource.h>
47 #include <ardour/send.h>
48 #include <ardour/region_factory.h>
49 #include <ardour/audioplaylist.h>
50 #include <ardour/playlist_factory.h>
51 #include <ardour/cycle_timer.h>
52 #include <ardour/audioregion.h>
53 #include <ardour/source_factory.h>
59 using namespace ARDOUR;
62 size_t AudioDiskstream::_working_buffers_size = 0;
63 Sample* AudioDiskstream::_mixdown_buffer = 0;
64 gain_t* AudioDiskstream::_gain_buffer = 0;
66 AudioDiskstream::AudioDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
67 : Diskstream(sess, name, flag)
68 , deprecated_io_node(NULL)
70 /* prevent any write sources from being created */
80 AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
81 : Diskstream(sess, node)
82 , deprecated_io_node(NULL)
87 if (set_state (node)) {
89 throw failed_constructor();
95 use_destructive_playlist ();
100 AudioDiskstream::init_channel (ChannelInfo &chan)
102 chan.playback_wrap_buffer = 0;
103 chan.capture_wrap_buffer = 0;
104 chan.speed_buffer = 0;
105 chan.peak_power = 0.0f;
107 chan.current_capture_buffer = 0;
108 chan.current_playback_buffer = 0;
109 chan.curr_capture_cnt = 0;
111 chan.playback_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
112 chan.capture_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
113 chan.capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
116 /* touch the ringbuffer buffers, which will cause
117 them to be mapped into locked physical RAM if
118 we're running with mlockall(). this doesn't do
121 memset (chan.playback_buf->buffer(), 0, sizeof (Sample) * chan.playback_buf->bufsize());
122 memset (chan.capture_buf->buffer(), 0, sizeof (Sample) * chan.capture_buf->bufsize());
123 memset (chan.capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * chan.capture_transition_buf->bufsize());
128 AudioDiskstream::init (Diskstream::Flag f)
132 /* there are no channels at this point, so these
133 two calls just get speed_buffer_size and wrap_buffer
134 size setup without duplicating their code.
137 set_block_size (_session.get_block_size());
138 allocate_temporary_buffers ();
141 assert(_n_channels == 1);
145 AudioDiskstream::destroy_channel (ChannelInfo &chan)
147 if (chan.write_source) {
148 chan.write_source.reset ();
151 if (chan.speed_buffer) {
152 delete [] chan.speed_buffer;
155 if (chan.playback_wrap_buffer) {
156 delete [] chan.playback_wrap_buffer;
158 if (chan.capture_wrap_buffer) {
159 delete [] chan.capture_wrap_buffer;
162 delete chan.playback_buf;
163 delete chan.capture_buf;
164 delete chan.capture_transition_buf;
166 chan.playback_buf = 0;
167 chan.capture_buf = 0;
170 AudioDiskstream::~AudioDiskstream ()
173 /* don't be holding this lock as we exit the destructor, glib will wince
174 visibly since the mutex gets destroyed before we release it.
177 Glib::Mutex::Lock lm (state_lock);
179 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan)
180 destroy_channel((*chan));
187 AudioDiskstream::allocate_working_buffers()
189 assert(disk_io_frames() > 0);
191 _working_buffers_size = disk_io_frames();
192 _mixdown_buffer = new Sample[_working_buffers_size];
193 _gain_buffer = new gain_t[_working_buffers_size];
197 AudioDiskstream::free_working_buffers()
199 delete [] _mixdown_buffer;
200 delete [] _gain_buffer;
201 _working_buffers_size = 0;
207 AudioDiskstream::non_realtime_input_change ()
210 Glib::Mutex::Lock lm (state_lock);
212 if (input_change_pending == NoChange) {
216 if (input_change_pending & ConfigurationChanged) {
218 if (_io->n_inputs() > _n_channels) {
220 // we need to add new channel infos
222 int diff = _io->n_inputs() - channels.size();
224 for (int i = 0; i < diff; ++i) {
228 } else if (_io->n_inputs() < _n_channels) {
230 // we need to get rid of channels
232 int diff = channels.size() - _io->n_inputs();
234 for (int i = 0; i < diff; ++i) {
240 get_input_sources ();
241 set_capture_offset ();
243 if (first_input_change) {
244 set_align_style (_persistent_alignment_style);
245 first_input_change = false;
247 set_align_style_from_io ();
250 input_change_pending = NoChange;
253 /* reset capture files */
255 reset_write_sources (false);
257 /* now refill channel buffers */
259 if (speed() != 1.0f || speed() != -1.0f) {
260 seek ((nframes_t) (_session.transport_frame() * (double) speed()));
262 seek (_session.transport_frame());
267 AudioDiskstream::get_input_sources ()
269 uint32_t ni = _io->n_inputs();
271 for (uint32_t n = 0; n < ni; ++n) {
273 const char **connections = _io->input(n)->get_connections ();
274 ChannelInfo& chan = channels[n];
276 if (connections == 0 || connections[0] == 0) {
279 // _source->disable_metering ();
285 chan.source = _session.engine().get_port_by_name (connections[0]);
295 AudioDiskstream::find_and_use_playlist (const string& name)
297 boost::shared_ptr<AudioPlaylist> playlist;
299 if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (_session.playlist_by_name (name))) == 0) {
300 playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (_session, name));
304 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
308 return use_playlist (playlist);
312 AudioDiskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
314 assert(boost::dynamic_pointer_cast<AudioPlaylist>(playlist));
316 Diskstream::use_playlist(playlist);
322 AudioDiskstream::use_new_playlist ()
325 boost::shared_ptr<AudioPlaylist> playlist;
327 if (!in_set_state && destructive()) {
332 newname = Playlist::bump_name (_playlist->name(), _session);
334 newname = Playlist::bump_name (_name, _session);
337 if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (_session, newname, hidden()))) != 0) {
339 playlist->set_orig_diskstream_id (id());
340 return use_playlist (playlist);
348 AudioDiskstream::use_copy_playlist ()
350 assert(audio_playlist());
356 if (_playlist == 0) {
357 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
362 boost::shared_ptr<AudioPlaylist> playlist;
364 newname = Playlist::bump_name (_playlist->name(), _session);
366 if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist>(PlaylistFactory::create (audio_playlist(), newname))) != 0) {
367 playlist->set_orig_diskstream_id (id());
368 return use_playlist (playlist);
375 AudioDiskstream::setup_destructive_playlist ()
379 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
380 srcs.push_back ((*chan).write_source);
383 /* a single full-sized region */
385 boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, max_frames - srcs.front()->natural_position(), _name));
386 _playlist->add_region (region, srcs.front()->natural_position());
390 AudioDiskstream::use_destructive_playlist ()
392 /* this is called from the XML-based constructor or ::set_destructive. when called,
393 we already have a playlist and a region, but we need to
394 set up our sources for write. we use the sources associated
395 with the (presumed single, full-extent) region.
398 boost::shared_ptr<Region> rp = _playlist->find_next_region (_session.current_start_frame(), Start, 1);
401 reset_write_sources (false, true);
405 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rp);
408 throw failed_constructor();
411 /* be sure to stretch the region out to the maximum length */
413 region->set_length (max_frames - region->position(), this);
416 ChannelList::iterator chan;
418 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
419 (*chan).write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
420 assert((*chan).write_source);
421 (*chan).write_source->set_allow_remove_if_empty (false);
423 /* this might be false if we switched modes, so force it */
425 (*chan).write_source->set_destructive (true);
428 /* the source list will never be reset for a destructive track */
432 AudioDiskstream::check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record)
434 int possibly_recording;
437 const int transport_rolling = 0x4;
438 const int track_rec_enabled = 0x2;
439 const int global_rec_enabled = 0x1;
441 /* merge together the 3 factors that affect record status, and compute
445 rolling = _session.transport_speed() != 0.0f;
446 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
447 change = possibly_recording ^ last_possibly_recording;
449 if (possibly_recording == last_possibly_recording) {
455 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
457 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
458 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
460 /* starting to record: compute first+last frames */
462 first_recordable_frame = transport_frame + _capture_offset;
463 last_recordable_frame = max_frames;
464 capture_start_frame = transport_frame;
466 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
468 /* was stopped, now rolling (and recording) */
470 if (_alignment_style == ExistingMaterial) {
471 first_recordable_frame += _session.worst_output_latency();
473 first_recordable_frame += _roll_delay;
478 /* was rolling, but record state changed */
480 if (_alignment_style == ExistingMaterial) {
482 if (!Config->get_punch_in()) {
484 /* manual punch in happens at the correct transport frame
485 because the user hit a button. but to get alignment correct
486 we have to back up the position of the new region to the
487 appropriate spot given the roll delay.
490 capture_start_frame -= _roll_delay;
492 /* XXX paul notes (august 2005): i don't know why
496 first_recordable_frame += _capture_offset;
500 /* autopunch toggles recording at the precise
501 transport frame, and then the DS waits
502 to start recording for a time that depends
503 on the output latency.
506 first_recordable_frame += _session.worst_output_latency();
511 if (Config->get_punch_in()) {
512 first_recordable_frame += _roll_delay;
514 capture_start_frame -= _roll_delay;
520 if (_flags & Recordable) {
521 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
523 RingBufferNPT<CaptureTransition>::rw_vector transvec;
524 (*chan).capture_transition_buf->get_write_vector(&transvec);
526 if (transvec.len[0] > 0) {
527 transvec.buf[0]->type = CaptureStart;
528 transvec.buf[0]->capture_val = capture_start_frame;
529 (*chan).capture_transition_buf->increment_write_ptr(1);
533 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
539 } else if (!record_enabled() || !can_record) {
543 last_recordable_frame = transport_frame + _capture_offset;
545 if (_alignment_style == ExistingMaterial) {
546 last_recordable_frame += _session.worst_output_latency();
548 last_recordable_frame += _roll_delay;
552 last_possibly_recording = possibly_recording;
556 AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_t offset, bool can_record, bool rec_monitors_input)
559 ChannelList::iterator c;
561 nframes_t rec_offset = 0;
562 nframes_t rec_nframes = 0;
563 bool nominally_recording;
564 bool re = record_enabled ();
565 bool collect_playback = false;
567 /* if we've already processed the frames corresponding to this call,
568 just return. this allows multiple routes that are taking input
569 from this diskstream to call our ::process() method, but have
570 this stuff only happen once. more commonly, it allows both
571 the AudioTrack that is using this AudioDiskstream *and* the Session
572 to call process() without problems.
579 check_record_status (transport_frame, nframes, can_record);
581 nominally_recording = (can_record && re);
588 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
589 must always be called as a pair. The only exception is if this function
590 returns a non-zero value, in which case, ::commit should not be called.
593 // If we can't take the state lock return.
594 if (!state_lock.trylock()) {
598 adjust_capture_position = 0;
600 for (c = channels.begin(); c != channels.end(); ++c) {
601 (*c).current_capture_buffer = 0;
602 (*c).current_playback_buffer = 0;
605 if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
608 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
615 case OverlapInternal:
616 /* ---------- recrange
619 rec_nframes = nframes;
624 /* |--------| recrange
627 rec_nframes = transport_frame + nframes - first_recordable_frame;
629 rec_offset = first_recordable_frame - transport_frame;
634 /* |--------| recrange
637 rec_nframes = last_recordable_frame - transport_frame;
641 case OverlapExternal:
642 /* |--------| recrange
643 -------------- transrange
645 rec_nframes = last_recordable_frame - last_recordable_frame;
646 rec_offset = first_recordable_frame - transport_frame;
650 if (rec_nframes && !was_recording) {
651 capture_captured = 0;
652 was_recording = true;
657 if (can_record && !_last_capture_regions.empty()) {
658 _last_capture_regions.clear ();
661 if (nominally_recording || rec_nframes) {
663 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
665 ChannelInfo& chan (*c);
667 chan.capture_buf->get_write_vector (&chan.capture_vector);
669 if (rec_nframes <= chan.capture_vector.len[0]) {
671 chan.current_capture_buffer = chan.capture_vector.buf[0];
673 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
677 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
681 nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
683 if (rec_nframes > total) {
688 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
689 nframes_t first = chan.capture_vector.len[0];
691 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
692 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
693 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
694 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
696 chan.current_capture_buffer = chan.capture_wrap_buffer;
703 finish_capture (rec_monitors_input);
710 /* data will be written to disk */
712 if (rec_nframes == nframes && rec_offset == 0) {
714 for (c = channels.begin(); c != channels.end(); ++c) {
715 (*c).current_playback_buffer = (*c).current_capture_buffer;
718 playback_distance = nframes;
723 /* we can't use the capture buffer as the playback buffer, because
724 we recorded only a part of the current process' cycle data
728 collect_playback = true;
731 adjust_capture_position = rec_nframes;
733 } else if (nominally_recording) {
735 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
737 for (c = channels.begin(); c != channels.end(); ++c) {
738 (*c).current_playback_buffer = (*c).current_capture_buffer;
741 playback_distance = nframes;
745 collect_playback = true;
748 if (collect_playback) {
750 /* we're doing playback */
752 nframes_t necessary_samples;
754 /* no varispeed playback if we're recording, because the output .... TBD */
756 if (rec_nframes == 0 && _actual_speed != 1.0f) {
757 necessary_samples = (nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
759 necessary_samples = nframes;
762 for (c = channels.begin(); c != channels.end(); ++c) {
763 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
768 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
770 ChannelInfo& chan (*c);
772 if (necessary_samples <= chan.playback_vector.len[0]) {
774 chan.current_playback_buffer = chan.playback_vector.buf[0];
777 nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
779 if (necessary_samples > total) {
785 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
786 chan.playback_vector.len[0] * sizeof (Sample));
787 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
788 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
790 chan.current_playback_buffer = chan.playback_wrap_buffer;
795 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
797 uint64_t phase = last_phase;
800 // Linearly interpolate into the alt buffer
801 // using 40.24 fixp maths (swh)
803 for (c = channels.begin(); c != channels.end(); ++c) {
806 ChannelInfo& chan (*c);
811 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
813 fr = (phase & 0xFFFFFF) / 16777216.0f;
814 chan.speed_buffer[outsample] =
815 chan.current_playback_buffer[i] * (1.0f - fr) +
816 chan.current_playback_buffer[i+1] * fr;
820 chan.current_playback_buffer = chan.speed_buffer;
823 playback_distance = i + 1;
824 last_phase = (phase & 0xFFFFFF);
827 playback_distance = nframes;
839 /* we're exiting with failure, so ::commit will not
840 be called. unlock the state lock.
850 AudioDiskstream::commit (nframes_t nframes)
852 bool need_butler = false;
854 if (_actual_speed < 0.0) {
855 playback_sample -= playback_distance;
857 playback_sample += playback_distance;
860 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
862 (*chan).playback_buf->increment_read_ptr (playback_distance);
864 if (adjust_capture_position) {
865 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
869 if (adjust_capture_position != 0) {
870 capture_captured += adjust_capture_position;
871 adjust_capture_position = 0;
875 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
877 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
878 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
889 AudioDiskstream::set_pending_overwrite (bool yn)
891 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
893 pending_overwrite = yn;
895 overwrite_frame = playback_sample;
896 overwrite_offset = channels.front().playback_buf->get_read_ptr();
900 AudioDiskstream::overwrite_existing_buffers ()
902 Sample* mixdown_buffer;
905 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
907 overwrite_queued = false;
909 /* assume all are the same size */
910 nframes_t size = channels[0].playback_buf->bufsize();
912 mixdown_buffer = new Sample[size];
913 gain_buffer = new float[size];
915 /* reduce size so that we can fill the buffer correctly. */
921 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
923 start = overwrite_frame;
924 nframes_t cnt = size;
926 /* to fill the buffer without resetting the playback sample, we need to
927 do it one or two chunks (normally two).
929 |----------------------------------------------------------------------|
933 |<- second chunk->||<----------------- first chunk ------------------>|
937 nframes_t to_read = size - overwrite_offset;
939 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, *chan, n, reversed)) {
940 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
941 _id, size, playback_sample) << endmsg;
949 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer,
950 start, cnt, *chan, n, reversed)) {
951 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
952 _id, size, playback_sample) << endmsg;
961 pending_overwrite = false;
962 delete [] gain_buffer;
963 delete [] mixdown_buffer;
968 AudioDiskstream::seek (nframes_t frame, bool complete_refill)
970 Glib::Mutex::Lock lm (state_lock);
973 ChannelList::iterator chan;
975 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
976 (*chan).playback_buf->reset ();
977 (*chan).capture_buf->reset ();
980 /* can't rec-enable in destructive mode if transport is before start */
982 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
983 disengage_record_enable ();
986 playback_sample = frame;
989 if (complete_refill) {
990 while ((ret = do_refill_with_alloc ()) > 0) ;
992 ret = do_refill_with_alloc ();
999 AudioDiskstream::can_internal_playback_seek (nframes_t distance)
1001 ChannelList::iterator chan;
1003 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1004 if ((*chan).playback_buf->read_space() < distance) {
1012 AudioDiskstream::internal_playback_seek (nframes_t distance)
1014 ChannelList::iterator chan;
1016 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1017 (*chan).playback_buf->increment_read_ptr (distance);
1020 first_recordable_frame += distance;
1021 playback_sample += distance;
1027 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, nframes_t& start, nframes_t cnt,
1028 ChannelInfo& channel_info, int channel, bool reversed)
1030 nframes_t this_read = 0;
1031 bool reloop = false;
1032 nframes_t loop_end = 0;
1033 nframes_t loop_start = 0;
1034 nframes_t loop_length = 0;
1035 nframes_t offset = 0;
1039 /* Make the use of a Location atomic for this read operation.
1041 Note: Locations don't get deleted, so all we care about
1042 when I say "atomic" is that we are always pointing to
1043 the same one and using a start/length values obtained
1047 if ((loc = loop_location) != 0) {
1048 loop_start = loc->start();
1049 loop_end = loc->end();
1050 loop_length = loop_end - loop_start;
1053 /* if we are looping, ensure that the first frame we read is at the correct
1054 position within the loop.
1057 if (loc && start >= loop_end) {
1058 //cerr << "start adjusted from " << start;
1059 start = loop_start + ((start - loop_start) % loop_length);
1060 //cerr << "to " << start << endl;
1062 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1067 /* take any loop into account. we can't read past the end of the loop. */
1069 if (loc && (loop_end - start < cnt)) {
1070 this_read = loop_end - start;
1071 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1078 if (this_read == 0) {
1082 this_read = min(cnt,this_read);
1084 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1085 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1090 _read_data_count = _playlist->read_data_count();
1094 /* don't adjust start, since caller has already done that
1097 swap_by_ptr (buf, buf + this_read - 1);
1101 /* if we read to the end of the loop, go back to the beginning */
1111 offset += this_read;
1118 AudioDiskstream::do_refill_with_alloc()
1120 Sample* mix_buf = new Sample[disk_io_chunk_frames];
1121 float* gain_buf = new float[disk_io_chunk_frames];
1123 int ret = _do_refill(mix_buf, gain_buf);
1132 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
1136 RingBufferNPT<Sample>::rw_vector vector;
1137 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1138 nframes_t total_space;
1139 nframes_t zero_fill;
1141 ChannelList::iterator i;
1144 assert(mixdown_buffer);
1145 assert(gain_buffer);
1147 channels.front().playback_buf->get_write_vector (&vector);
1149 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1153 /* if there are 2+ chunks of disk i/o possible for
1154 this track, let the caller know so that it can arrange
1155 for us to be called again, ASAP.
1158 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1162 /* if we're running close to normal speed and there isn't enough
1163 space to do disk_io_chunk_frames of I/O, then don't bother.
1165 at higher speeds, just do it because the sync between butler
1166 and audio thread may not be good enough.
1169 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1173 /* when slaved, don't try to get too close to the read pointer. this
1174 leaves space for the buffer reversal to have something useful to
1178 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1182 total_space = min (disk_io_chunk_frames, total_space);
1186 if (file_frame == 0) {
1188 /* at start: nothing to do but fill with silence */
1190 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1192 ChannelInfo& chan (*i);
1193 chan.playback_buf->get_write_vector (&vector);
1194 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1195 if (vector.len[1]) {
1196 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1198 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1203 if (file_frame < total_space) {
1205 /* too close to the start: read what we can,
1206 and then zero fill the rest
1209 zero_fill = total_space - file_frame;
1210 total_space = file_frame;
1215 /* move read position backwards because we are going
1216 to reverse the data.
1219 file_frame -= total_space;
1225 if (file_frame == max_frames) {
1227 /* at end: nothing to do but fill with silence */
1229 for (chan_n = 0, i = channels.begin(); i != channels.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_frames - total_space) {
1244 /* to close to the end: read what we can, and zero fill the rest */
1246 zero_fill = total_space - (max_frames - file_frame);
1247 total_space = max_frames - file_frame;
1254 nframes_t file_frame_tmp = 0;
1256 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1258 ChannelInfo& chan (*i);
1261 nframes_t len1, len2;
1263 chan.playback_buf->get_write_vector (&vector);
1266 file_frame_tmp = file_frame;
1269 buf1 = vector.buf[1];
1270 len1 = vector.len[1];
1271 buf2 = vector.buf[0];
1272 len2 = vector.len[0];
1274 buf1 = vector.buf[0];
1275 len1 = vector.len[0];
1276 buf2 = vector.buf[1];
1277 len2 = vector.len[1];
1281 to_read = min (ts, len1);
1282 to_read = min (to_read, disk_io_chunk_frames);
1286 if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1291 chan.playback_buf->increment_write_ptr (to_read);
1295 to_read = min (ts, len2);
1300 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1301 so read some or all of vector.len[1] as well.
1304 if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1309 chan.playback_buf->increment_write_ptr (to_read);
1318 file_frame = file_frame_tmp;
1325 /** Flush pending data to disk.
1327 * Important note: this function will write *AT MOST* disk_io_chunk_frames
1328 * of data to disk. it will never write more than that. If it writes that
1329 * much and there is more than that waiting to be written, it will return 1,
1330 * otherwise 0 on success or -1 on failure.
1332 * If there is less than disk_io_chunk_frames to be written, no data will be
1333 * written at all unless @a force_flush is true.
1336 AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
1340 RingBufferNPT<Sample>::rw_vector vector;
1341 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1344 _write_data_count = 0;
1346 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1348 (*chan).capture_buf->get_read_vector (&vector);
1350 total = vector.len[0] + vector.len[1];
1353 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1357 /* if there are 2+ chunks of disk i/o possible for
1358 this track, let the caller know so that it can arrange
1359 for us to be called again, ASAP.
1361 if we are forcing a flush, then if there is* any* extra
1362 work, let the caller know.
1364 if we are no longer recording and there is any extra work,
1365 let the caller know too.
1368 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1372 to_write = min (disk_io_chunk_frames, (nframes_t) vector.len[0]);
1374 // check the transition buffer when recording destructive
1375 // important that we get this after the capture buf
1377 if (destructive()) {
1378 (*chan).capture_transition_buf->get_read_vector(&transvec);
1379 size_t transcount = transvec.len[0] + transvec.len[1];
1380 bool have_start = false;
1383 for (ti=0; ti < transcount; ++ti) {
1384 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1386 if (captrans.type == CaptureStart) {
1387 // by definition, the first data we got above represents the given capture pos
1389 (*chan).write_source->mark_capture_start (captrans.capture_val);
1390 (*chan).curr_capture_cnt = 0;
1394 else if (captrans.type == CaptureEnd) {
1396 // capture end, the capture_val represents total frames in capture
1398 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1400 // shorten to make the write a perfect fit
1401 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1403 if (nto_write < to_write) {
1404 ret = 1; // should we?
1406 to_write = nto_write;
1408 (*chan).write_source->mark_capture_end ();
1410 // increment past this transition, but go no further
1415 // actually ends just beyond this chunk, so force more work
1423 (*chan).capture_transition_buf->increment_read_ptr(ti);
1427 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write) != to_write) {
1428 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1432 (*chan).capture_buf->increment_read_ptr (to_write);
1433 (*chan).curr_capture_cnt += to_write;
1435 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1437 /* we wrote all of vector.len[0] but it wasn't an entire
1438 disk_io_chunk_frames of data, so arrange for some part
1439 of vector.len[1] to be flushed to disk as well.
1442 to_write = min ((nframes_t)(disk_io_chunk_frames - to_write), (nframes_t) vector.len[1]);
1444 if ((*chan).write_source->write (vector.buf[1], to_write) != to_write) {
1445 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1449 _write_data_count += (*chan).write_source->write_data_count();
1451 (*chan).capture_buf->increment_read_ptr (to_write);
1452 (*chan).curr_capture_cnt += to_write;
1461 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1463 uint32_t buffer_position;
1464 bool more_work = true;
1466 boost::shared_ptr<AudioRegion> region;
1467 nframes_t total_capture;
1469 SourceList::iterator src;
1470 ChannelList::iterator chan;
1471 vector<CaptureInfo*>::iterator ci;
1473 bool mark_write_completed = false;
1475 finish_capture (true);
1477 /* butler is already stopped, but there may be work to do
1478 to flush remaining data to disk.
1481 while (more_work && !err) {
1482 switch (do_flush (Session::TransportContext, true)) {
1489 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1494 /* XXX is there anything we can do if err != 0 ? */
1495 Glib::Mutex::Lock lm (capture_info_lock);
1497 if (capture_info.empty()) {
1501 if (abort_capture) {
1503 if (destructive()) {
1507 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1509 if ((*chan).write_source) {
1511 (*chan).write_source->mark_for_remove ();
1512 (*chan).write_source->drop_references ();
1513 (*chan).write_source.reset ();
1516 /* new source set up in "out" below */
1522 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1523 total_capture += (*ci)->frames;
1526 /* figure out the name for this take */
1528 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1530 boost::shared_ptr<AudioFileSource> s = (*chan).write_source;
1534 s->update_header (capture_info.front()->start, when, twhen);
1535 s->set_captured_for (_name);
1540 /* destructive tracks have a single, never changing region */
1542 if (destructive()) {
1544 /* send a signal that any UI can pick up to do the right thing. there is
1545 a small problem here in that a UI may need the peak data to be ready
1546 for the data that was recorded and this isn't interlocked with that
1547 process. this problem is deferred to the UI.
1550 _playlist->Modified();
1554 string whole_file_region_name;
1555 whole_file_region_name = region_name_from_path (channels[0].write_source->name(), true);
1557 /* Register a new region with the Session that
1558 describes the entire source. Do this first
1559 so that any sub-regions will obviously be
1560 children of this one (later!)
1564 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1565 whole_file_region_name,
1566 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
1568 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1569 region->special_set_position (capture_info.front()->start);
1573 catch (failed_constructor& err) {
1574 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1578 _last_capture_regions.push_back (region);
1580 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1582 XMLNode &before = _playlist->get_state();
1583 _playlist->freeze ();
1585 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1589 _session.region_name (region_name, whole_file_region_name, false);
1591 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
1594 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1595 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1598 catch (failed_constructor& err) {
1599 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1600 continue; /* XXX is this OK? */
1603 region->GoingAway.connect (bind (mem_fun (*this, &Diskstream::remove_region_from_last_capture), boost::weak_ptr<Region>(region)));
1605 _last_capture_regions.push_back (region);
1607 i_am_the_modifier++;
1608 _playlist->add_region (region, (*ci)->start);
1609 i_am_the_modifier--;
1611 buffer_position += (*ci)->frames;
1615 XMLNode &after = _playlist->get_state();
1616 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1619 mark_write_completed = true;
1622 reset_write_sources (mark_write_completed);
1626 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1630 capture_info.clear ();
1631 capture_start_frame = 0;
1635 AudioDiskstream::finish_capture (bool rec_monitors_input)
1637 was_recording = false;
1639 if (capture_captured == 0) {
1643 if (recordable() && destructive()) {
1644 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1646 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1647 (*chan).capture_transition_buf->get_write_vector(&transvec);
1650 if (transvec.len[0] > 0) {
1651 transvec.buf[0]->type = CaptureEnd;
1652 transvec.buf[0]->capture_val = capture_captured;
1653 (*chan).capture_transition_buf->increment_write_ptr(1);
1657 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1663 CaptureInfo* ci = new CaptureInfo;
1665 ci->start = capture_start_frame;
1666 ci->frames = capture_captured;
1668 /* XXX theoretical race condition here. Need atomic exchange ?
1669 However, the circumstances when this is called right
1670 now (either on record-disable or transport_stopped)
1671 mean that no actual race exists. I think ...
1672 We now have a capture_info_lock, but it is only to be used
1673 to synchronize in the transport_stop and the capture info
1674 accessors, so that invalidation will not occur (both non-realtime).
1677 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1679 capture_info.push_back (ci);
1680 capture_captured = 0;
1684 AudioDiskstream::set_record_enabled (bool yn)
1686 if (!recordable() || !_session.record_enabling_legal()) {
1690 /* can't rec-enable in destructive mode if transport is before start */
1692 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1696 if (yn && channels[0].source == 0) {
1698 /* pick up connections not initiated *from* the IO object
1699 we're associated with.
1702 get_input_sources ();
1705 /* yes, i know that this not proof against race conditions, but its
1706 good enough. i think.
1709 if (record_enabled() != yn) {
1711 engage_record_enable ();
1713 disengage_record_enable ();
1719 AudioDiskstream::engage_record_enable ()
1721 bool rolling = _session.transport_speed() != 0.0f;
1723 g_atomic_int_set (&_record_enabled, 1);
1724 capturing_sources.clear ();
1725 if (Config->get_monitoring_model() == HardwareMonitoring) {
1726 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1727 if ((*chan).source) {
1728 (*chan).source->ensure_monitor_input (!(Config->get_auto_input() && rolling));
1730 capturing_sources.push_back ((*chan).write_source);
1733 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1734 capturing_sources.push_back ((*chan).write_source);
1738 RecordEnableChanged (); /* EMIT SIGNAL */
1742 AudioDiskstream::disengage_record_enable ()
1744 g_atomic_int_set (&_record_enabled, 0);
1745 if (Config->get_monitoring_model() == HardwareMonitoring) {
1746 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1747 if ((*chan).source) {
1748 (*chan).source->ensure_monitor_input (false);
1752 capturing_sources.clear ();
1753 RecordEnableChanged (); /* EMIT SIGNAL */
1758 AudioDiskstream::get_state ()
1760 XMLNode* node = new XMLNode ("AudioDiskstream");
1762 LocaleGuard lg (X_("POSIX"));
1764 node->add_property ("flags", enum_2_string (_flags));
1766 snprintf (buf, sizeof(buf), "%zd", channels.size());
1767 node->add_property ("channels", buf);
1769 node->add_property ("playlist", _playlist->name());
1771 snprintf (buf, sizeof(buf), "%.12g", _visible_speed);
1772 node->add_property ("speed", buf);
1774 node->add_property("name", _name);
1775 id().print (buf, sizeof (buf));
1776 node->add_property("id", buf);
1778 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1780 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1781 XMLNode* cs_grandchild;
1783 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1784 cs_grandchild = new XMLNode (X_("file"));
1785 cs_grandchild->add_property (X_("path"), (*i)->path());
1786 cs_child->add_child_nocopy (*cs_grandchild);
1789 /* store the location where capture will start */
1793 if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1794 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1796 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1799 cs_child->add_property (X_("at"), buf);
1800 node->add_child_nocopy (*cs_child);
1804 node->add_child_copy (*_extra_xml);
1811 AudioDiskstream::set_state (const XMLNode& node)
1813 const XMLProperty* prop;
1814 XMLNodeList nlist = node.children();
1815 XMLNodeIterator niter;
1816 uint32_t nchans = 1;
1817 XMLNode* capture_pending_node = 0;
1818 LocaleGuard lg (X_("POSIX"));
1820 in_set_state = true;
1822 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1823 if ((*niter)->name() == IO::state_node_name) {
1824 deprecated_io_node = new XMLNode (**niter);
1827 if ((*niter)->name() == X_("CapturingSources")) {
1828 capture_pending_node = *niter;
1832 /* prevent write sources from being created */
1834 in_set_state = true;
1836 if ((prop = node.property ("name")) != 0) {
1837 _name = prop->value();
1840 if (deprecated_io_node) {
1841 if ((prop = deprecated_io_node->property ("id")) != 0) {
1842 _id = prop->value ();
1845 if ((prop = node.property ("id")) != 0) {
1846 _id = prop->value ();
1850 if ((prop = node.property ("flags")) != 0) {
1851 _flags = Flag (string_2_enum (prop->value(), _flags));
1854 if ((prop = node.property ("channels")) != 0) {
1855 nchans = atoi (prop->value().c_str());
1858 // create necessary extra channels
1859 // we are always constructed with one and we always need one
1861 if (nchans > _n_channels) {
1863 // we need to add new channel infos
1864 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1866 int diff = nchans - channels.size();
1868 for (int i=0; i < diff; ++i) {
1872 } else if (nchans < _n_channels) {
1874 // we need to get rid of channels
1875 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1877 int diff = channels.size() - nchans;
1879 for (int i = 0; i < diff; ++i) {
1884 if ((prop = node.property ("playlist")) == 0) {
1889 bool had_playlist = (_playlist != 0);
1891 if (find_and_use_playlist (prop->value())) {
1895 if (!had_playlist) {
1896 _playlist->set_orig_diskstream_id (_id);
1899 if (!destructive() && capture_pending_node) {
1900 /* destructive streams have one and only one source per channel,
1901 and so they never end up in pending capture in any useful
1904 use_pending_capture_data (*capture_pending_node);
1909 if ((prop = node.property ("speed")) != 0) {
1910 double sp = atof (prop->value().c_str());
1912 if (realtime_set_speed (sp, false)) {
1913 non_realtime_set_speed ();
1917 _n_channels = channels.size();
1919 in_set_state = false;
1921 /* make sure this is clear before we do anything else */
1923 capturing_sources.clear ();
1925 /* write sources are handled when we handle the input set
1926 up of the IO that owns this DS (::non_realtime_input_change())
1929 in_set_state = false;
1935 AudioDiskstream::use_new_write_source (uint32_t n)
1937 if (!recordable()) {
1941 if (n >= channels.size()) {
1942 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1946 ChannelInfo &chan = channels[n];
1948 if (chan.write_source) {
1949 chan.write_source->set_allow_remove_if_empty (true);
1950 chan.write_source.reset ();
1954 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
1955 throw failed_constructor();
1959 catch (failed_constructor &err) {
1960 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1961 chan.write_source.reset ();
1965 /* do not remove destructive files even if they are empty */
1967 chan.write_source->set_allow_remove_if_empty (!destructive());
1973 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1975 ChannelList::iterator chan;
1978 if (!recordable()) {
1982 capturing_sources.clear ();
1984 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
1985 if (!destructive()) {
1987 if ((*chan).write_source && mark_write_complete) {
1988 (*chan).write_source->mark_streaming_write_completed ();
1990 use_new_write_source (n);
1992 if (record_enabled()) {
1993 capturing_sources.push_back ((*chan).write_source);
1997 if ((*chan).write_source == 0) {
1998 use_new_write_source (n);
2003 if (destructive()) {
2005 /* we now have all our write sources set up, so create the
2006 playlist's single region.
2009 if (_playlist->empty()) {
2010 setup_destructive_playlist ();
2016 AudioDiskstream::rename_write_sources ()
2018 ChannelList::iterator chan;
2021 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2022 if ((*chan).write_source != 0) {
2023 (*chan).write_source->set_name (_name, destructive());
2024 /* XXX what to do if one of them fails ? */
2032 AudioDiskstream::set_block_size (nframes_t nframes)
2034 if (_session.get_block_size() > speed_buffer_size) {
2035 speed_buffer_size = _session.get_block_size();
2037 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2038 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2039 (*chan).speed_buffer = new Sample[speed_buffer_size];
2042 allocate_temporary_buffers ();
2046 AudioDiskstream::allocate_temporary_buffers ()
2048 /* make sure the wrap buffer is at least large enough to deal
2049 with the speeds up to 1.2, to allow for micro-variation
2050 when slaving to MTC, SMPTE etc.
2053 double sp = max (fabsf (_actual_speed), 1.2f);
2054 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() * sp) + 1;
2056 if (required_wrap_size > wrap_buffer_size) {
2058 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2059 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2060 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2061 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2062 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2065 wrap_buffer_size = required_wrap_size;
2070 AudioDiskstream::monitor_input (bool yn)
2072 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2074 if ((*chan).source) {
2075 (*chan).source->ensure_monitor_input (yn);
2081 AudioDiskstream::set_align_style_from_io ()
2083 bool have_physical = false;
2089 get_input_sources ();
2091 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2092 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2093 have_physical = true;
2098 if (have_physical) {
2099 set_align_style (ExistingMaterial);
2101 set_align_style (CaptureTime);
2106 AudioDiskstream::add_channel ()
2108 /* XXX need to take lock??? */
2112 init_channel (chan);
2114 chan.speed_buffer = new Sample[speed_buffer_size];
2115 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2116 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2118 channels.push_back (chan);
2120 _n_channels = channels.size();
2126 AudioDiskstream::remove_channel ()
2128 if (channels.size() > 1) {
2129 /* XXX need to take lock??? */
2130 ChannelInfo & chan = channels.back();
2131 destroy_channel (chan);
2132 channels.pop_back();
2134 _n_channels = channels.size();
2142 AudioDiskstream::playback_buffer_load () const
2144 return (float) ((double) channels.front().playback_buf->read_space()/
2145 (double) channels.front().playback_buf->bufsize());
2149 AudioDiskstream::capture_buffer_load () const
2151 return (float) ((double) channels.front().capture_buf->write_space()/
2152 (double) channels.front().capture_buf->bufsize());
2156 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2158 const XMLProperty* prop;
2159 XMLNodeList nlist = node.children();
2160 XMLNodeIterator niter;
2161 boost::shared_ptr<AudioFileSource> fs;
2162 boost::shared_ptr<AudioFileSource> first_fs;
2163 SourceList pending_sources;
2166 if ((prop = node.property (X_("at"))) == 0) {
2170 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2174 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2175 if ((*niter)->name() == X_("file")) {
2177 if ((prop = (*niter)->property (X_("path"))) == 0) {
2182 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (_session, prop->value(), false, _session.frame_rate()));
2185 catch (failed_constructor& err) {
2186 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2187 _name, prop->value())
2192 pending_sources.push_back (fs);
2194 if (first_fs == 0) {
2198 fs->set_captured_for (_name);
2202 if (pending_sources.size() == 0) {
2203 /* nothing can be done */
2207 if (pending_sources.size() != _n_channels) {
2208 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2213 boost::shared_ptr<AudioRegion> region;
2216 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(),
2217 region_name_from_path (first_fs->name(), true),
2218 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
2219 region->special_set_position (0);
2222 catch (failed_constructor& err) {
2223 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2231 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name(), true)));
2234 catch (failed_constructor& err) {
2235 error << string_compose (_("%1: cannot create region from pending capture sources"),
2242 _playlist->add_region (region, position);
2248 AudioDiskstream::set_destructive (bool yn)
2250 bool bounce_ignored;
2252 if (yn != destructive()) {
2255 /* requestor should already have checked this and
2256 bounced if necessary and desired
2258 if (!can_become_destructive (bounce_ignored)) {
2261 _flags = Flag (_flags | Destructive);
2262 use_destructive_playlist ();
2264 _flags = Flag (_flags & ~Destructive);
2265 reset_write_sources (true, true);
2273 AudioDiskstream::can_become_destructive (bool& requires_bounce) const
2276 requires_bounce = false;
2280 /* is there only one region ? */
2282 if (_playlist->n_regions() != 1) {
2283 requires_bounce = true;
2287 boost::shared_ptr<Region> first = _playlist->find_next_region (_session.current_start_frame(), Start, 1);
2290 /* do the source(s) for the region cover the session start position ? */
2292 if (first->position() != _session.current_start_frame()) {
2293 if (first->start() > _session.current_start_frame()) {
2294 requires_bounce = true;
2299 /* is the source used by only 1 playlist ? */
2301 boost::shared_ptr<AudioRegion> afirst = boost::dynamic_pointer_cast<AudioRegion> (first);
2305 if (afirst->source()->used() > 1) {
2306 requires_bounce = true;
2310 requires_bounce = false;