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>
39 #include <ardour/ardour.h>
40 #include <ardour/audioengine.h>
41 #include <ardour/audio_diskstream.h>
42 #include <ardour/utils.h>
43 #include <ardour/configuration.h>
44 #include <ardour/audiofilesource.h>
45 #include <ardour/destructive_filesource.h>
46 #include <ardour/send.h>
47 #include <ardour/region_factory.h>
48 #include <ardour/audioplaylist.h>
49 #include <ardour/cycle_timer.h>
50 #include <ardour/audioregion.h>
51 #include <ardour/source_factory.h>
57 using namespace ARDOUR;
60 size_t AudioDiskstream::_working_buffers_size = 0;
61 Sample* AudioDiskstream::_mixdown_buffer = 0;
62 gain_t* AudioDiskstream::_gain_buffer = 0;
64 AudioDiskstream::AudioDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
65 : Diskstream(sess, name, flag)
66 , deprecated_io_node(NULL)
68 /* prevent any write sources from being created */
78 AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
79 : Diskstream(sess, node)
80 , deprecated_io_node(NULL)
85 if (set_state (node)) {
87 throw failed_constructor();
93 use_destructive_playlist ();
98 AudioDiskstream::init_channel (ChannelInfo &chan)
100 chan.playback_wrap_buffer = 0;
101 chan.capture_wrap_buffer = 0;
102 chan.speed_buffer = 0;
103 chan.peak_power = 0.0f;
105 chan.current_capture_buffer = 0;
106 chan.current_playback_buffer = 0;
107 chan.curr_capture_cnt = 0;
109 chan.playback_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
110 chan.capture_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
111 chan.capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
114 /* touch the ringbuffer buffers, which will cause
115 them to be mapped into locked physical RAM if
116 we're running with mlockall(). this doesn't do
119 memset (chan.playback_buf->buffer(), 0, sizeof (Sample) * chan.playback_buf->bufsize());
120 memset (chan.capture_buf->buffer(), 0, sizeof (Sample) * chan.capture_buf->bufsize());
121 memset (chan.capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * chan.capture_transition_buf->bufsize());
126 AudioDiskstream::init (Diskstream::Flag f)
130 /* there are no channels at this point, so these
131 two calls just get speed_buffer_size and wrap_buffer
132 size setup without duplicating their code.
135 set_block_size (_session.get_block_size());
136 allocate_temporary_buffers ();
139 assert(_n_channels == 1);
143 AudioDiskstream::destroy_channel (ChannelInfo &chan)
145 if (chan.write_source) {
146 chan.write_source.reset ();
149 if (chan.speed_buffer) {
150 delete [] chan.speed_buffer;
153 if (chan.playback_wrap_buffer) {
154 delete [] chan.playback_wrap_buffer;
156 if (chan.capture_wrap_buffer) {
157 delete [] chan.capture_wrap_buffer;
160 delete chan.playback_buf;
161 delete chan.capture_buf;
162 delete chan.capture_transition_buf;
164 chan.playback_buf = 0;
165 chan.capture_buf = 0;
168 AudioDiskstream::~AudioDiskstream ()
170 Glib::Mutex::Lock lm (state_lock);
172 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan)
173 destroy_channel((*chan));
179 AudioDiskstream::allocate_working_buffers()
181 assert(disk_io_frames() > 0);
183 _working_buffers_size = disk_io_frames();
184 _mixdown_buffer = new Sample[_working_buffers_size];
185 _gain_buffer = new gain_t[_working_buffers_size];
189 AudioDiskstream::free_working_buffers()
191 delete [] _mixdown_buffer;
192 delete [] _gain_buffer;
193 _working_buffers_size = 0;
199 AudioDiskstream::non_realtime_input_change ()
202 Glib::Mutex::Lock lm (state_lock);
204 if (input_change_pending == NoChange) {
208 if (input_change_pending & ConfigurationChanged) {
210 if (_io->n_inputs() > _n_channels) {
212 // we need to add new channel infos
214 int diff = _io->n_inputs() - channels.size();
216 for (int i = 0; i < diff; ++i) {
220 } else if (_io->n_inputs() < _n_channels) {
222 // we need to get rid of channels
224 int diff = channels.size() - _io->n_inputs();
226 for (int i = 0; i < diff; ++i) {
232 get_input_sources ();
233 set_capture_offset ();
235 if (first_input_change) {
236 set_align_style (_persistent_alignment_style);
237 first_input_change = false;
239 set_align_style_from_io ();
242 input_change_pending = NoChange;
245 /* reset capture files */
247 reset_write_sources (false);
249 /* now refill channel buffers */
251 if (speed() != 1.0f || speed() != -1.0f) {
252 seek ((nframes_t) (_session.transport_frame() * (double) speed()));
254 seek (_session.transport_frame());
259 AudioDiskstream::get_input_sources ()
261 uint32_t ni = _io->n_inputs();
263 for (uint32_t n = 0; n < ni; ++n) {
265 const char **connections = _io->input(n)->get_connections ();
266 ChannelInfo& chan = channels[n];
268 if (connections == 0 || connections[0] == 0) {
271 // _source->disable_metering ();
277 chan.source = _session.engine().get_port_by_name (connections[0]);
287 AudioDiskstream::find_and_use_playlist (const string& name)
290 AudioPlaylist* playlist;
292 if ((pl = _session.playlist_by_name (name)) == 0) {
293 playlist = new AudioPlaylist(_session, name);
297 if ((playlist = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
298 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
302 return use_playlist (playlist);
306 AudioDiskstream::use_playlist (Playlist* playlist)
308 assert(dynamic_cast<AudioPlaylist*>(playlist));
310 Diskstream::use_playlist(playlist);
316 AudioDiskstream::use_new_playlist ()
319 AudioPlaylist* playlist;
321 if (!in_set_state && destructive()) {
326 newname = Playlist::bump_name (_playlist->name(), _session);
328 newname = Playlist::bump_name (_name, _session);
331 if ((playlist = new AudioPlaylist (_session, newname, hidden())) != 0) {
332 playlist->set_orig_diskstream_id (id());
333 return use_playlist (playlist);
340 AudioDiskstream::use_copy_playlist ()
342 assert(audio_playlist());
348 if (_playlist == 0) {
349 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
354 AudioPlaylist* playlist;
356 newname = Playlist::bump_name (_playlist->name(), _session);
358 if ((playlist = new AudioPlaylist (*audio_playlist(), newname)) != 0) {
359 playlist->set_orig_diskstream_id (id());
360 return use_playlist (playlist);
367 AudioDiskstream::setup_destructive_playlist ()
371 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
372 srcs.push_back ((*chan).write_source);
375 /* a single full-sized region */
377 boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, max_frames - srcs.front()->natural_position(), _name));
378 _playlist->add_region (region, srcs.front()->natural_position());
382 AudioDiskstream::use_destructive_playlist ()
384 /* this is called from the XML-based constructor. when its done,
385 we already have a playlist and a region, but we need to
386 set up our sources for write. we use the sources associated
387 with the (presumed single, full-extent) region.
390 boost::shared_ptr<Region> rp = _playlist->find_next_region (_session.current_start_frame(), Start, 1);
393 reset_write_sources (false, true);
397 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rp);
400 throw failed_constructor();
404 ChannelList::iterator chan;
406 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
407 (*chan).write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
408 assert((*chan).write_source);
409 (*chan).write_source->set_allow_remove_if_empty (false);
412 /* the source list will never be reset for a destructive track */
416 AudioDiskstream::check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record)
418 int possibly_recording;
421 const int transport_rolling = 0x4;
422 const int track_rec_enabled = 0x2;
423 const int global_rec_enabled = 0x1;
425 /* merge together the 3 factors that affect record status, and compute
429 rolling = _session.transport_speed() != 0.0f;
430 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
431 change = possibly_recording ^ last_possibly_recording;
433 if (possibly_recording == last_possibly_recording) {
439 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
441 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
442 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
444 /* starting to record: compute first+last frames */
446 first_recordable_frame = transport_frame + _capture_offset;
447 last_recordable_frame = max_frames;
448 capture_start_frame = transport_frame;
450 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
452 /* was stopped, now rolling (and recording) */
454 if (_alignment_style == ExistingMaterial) {
455 first_recordable_frame += _session.worst_output_latency();
457 first_recordable_frame += _roll_delay;
462 /* was rolling, but record state changed */
464 if (_alignment_style == ExistingMaterial) {
466 if (!Config->get_punch_in()) {
468 /* manual punch in happens at the correct transport frame
469 because the user hit a button. but to get alignment correct
470 we have to back up the position of the new region to the
471 appropriate spot given the roll delay.
474 capture_start_frame -= _roll_delay;
476 /* XXX paul notes (august 2005): i don't know why
480 first_recordable_frame += _capture_offset;
484 /* autopunch toggles recording at the precise
485 transport frame, and then the DS waits
486 to start recording for a time that depends
487 on the output latency.
490 first_recordable_frame += _session.worst_output_latency();
495 if (Config->get_punch_in()) {
496 first_recordable_frame += _roll_delay;
498 capture_start_frame -= _roll_delay;
504 if (_flags & Recordable) {
505 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
507 RingBufferNPT<CaptureTransition>::rw_vector transvec;
508 (*chan).capture_transition_buf->get_write_vector(&transvec);
510 if (transvec.len[0] > 0) {
511 transvec.buf[0]->type = CaptureStart;
512 transvec.buf[0]->capture_val = capture_start_frame;
513 (*chan).capture_transition_buf->increment_write_ptr(1);
517 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
523 } else if (!record_enabled() || !can_record) {
527 last_recordable_frame = transport_frame + _capture_offset;
529 if (_alignment_style == ExistingMaterial) {
530 last_recordable_frame += _session.worst_output_latency();
532 last_recordable_frame += _roll_delay;
536 last_possibly_recording = possibly_recording;
540 AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_t offset, bool can_record, bool rec_monitors_input)
543 ChannelList::iterator c;
545 nframes_t rec_offset = 0;
546 nframes_t rec_nframes = 0;
547 bool nominally_recording;
548 bool re = record_enabled ();
549 bool collect_playback = false;
551 /* if we've already processed the frames corresponding to this call,
552 just return. this allows multiple routes that are taking input
553 from this diskstream to call our ::process() method, but have
554 this stuff only happen once. more commonly, it allows both
555 the AudioTrack that is using this AudioDiskstream *and* the Session
556 to call process() without problems.
563 check_record_status (transport_frame, nframes, can_record);
565 nominally_recording = (can_record && re);
572 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
573 must always be called as a pair. The only exception is if this function
574 returns a non-zero value, in which case, ::commit should not be called.
577 // If we can't take the state lock return.
578 if (!state_lock.trylock()) {
582 adjust_capture_position = 0;
584 for (c = channels.begin(); c != channels.end(); ++c) {
585 (*c).current_capture_buffer = 0;
586 (*c).current_playback_buffer = 0;
589 if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
592 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
599 case OverlapInternal:
600 /* ---------- recrange
603 rec_nframes = nframes;
608 /* |--------| recrange
611 rec_nframes = transport_frame + nframes - first_recordable_frame;
613 rec_offset = first_recordable_frame - transport_frame;
618 /* |--------| recrange
621 rec_nframes = last_recordable_frame - transport_frame;
625 case OverlapExternal:
626 /* |--------| recrange
627 -------------- transrange
629 rec_nframes = last_recordable_frame - last_recordable_frame;
630 rec_offset = first_recordable_frame - transport_frame;
634 if (rec_nframes && !was_recording) {
635 capture_captured = 0;
636 was_recording = true;
641 if (can_record && !_last_capture_regions.empty()) {
642 _last_capture_regions.clear ();
645 if (nominally_recording || rec_nframes) {
647 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
649 ChannelInfo& chan (*c);
651 chan.capture_buf->get_write_vector (&chan.capture_vector);
653 if (rec_nframes <= chan.capture_vector.len[0]) {
655 chan.current_capture_buffer = chan.capture_vector.buf[0];
657 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
661 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
665 nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
667 if (rec_nframes > total) {
672 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
673 nframes_t first = chan.capture_vector.len[0];
675 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
676 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
677 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
678 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
680 chan.current_capture_buffer = chan.capture_wrap_buffer;
687 finish_capture (rec_monitors_input);
694 /* data will be written to disk */
696 if (rec_nframes == nframes && rec_offset == 0) {
698 for (c = channels.begin(); c != channels.end(); ++c) {
699 (*c).current_playback_buffer = (*c).current_capture_buffer;
702 playback_distance = nframes;
707 /* we can't use the capture buffer as the playback buffer, because
708 we recorded only a part of the current process' cycle data
712 collect_playback = true;
715 adjust_capture_position = rec_nframes;
717 } else if (nominally_recording) {
719 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
721 for (c = channels.begin(); c != channels.end(); ++c) {
722 (*c).current_playback_buffer = (*c).current_capture_buffer;
725 playback_distance = nframes;
729 collect_playback = true;
732 if (collect_playback) {
734 /* we're doing playback */
736 nframes_t necessary_samples;
738 /* no varispeed playback if we're recording, because the output .... TBD */
740 if (rec_nframes == 0 && _actual_speed != 1.0f) {
741 necessary_samples = (nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
743 necessary_samples = nframes;
746 for (c = channels.begin(); c != channels.end(); ++c) {
747 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
752 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
754 ChannelInfo& chan (*c);
756 if (necessary_samples <= chan.playback_vector.len[0]) {
758 chan.current_playback_buffer = chan.playback_vector.buf[0];
761 nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
763 if (necessary_samples > total) {
769 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
770 chan.playback_vector.len[0] * sizeof (Sample));
771 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
772 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
774 chan.current_playback_buffer = chan.playback_wrap_buffer;
779 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
781 uint64_t phase = last_phase;
784 // Linearly interpolate into the alt buffer
785 // using 40.24 fixp maths (swh)
787 for (c = channels.begin(); c != channels.end(); ++c) {
790 ChannelInfo& chan (*c);
795 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
797 fr = (phase & 0xFFFFFF) / 16777216.0f;
798 chan.speed_buffer[outsample] =
799 chan.current_playback_buffer[i] * (1.0f - fr) +
800 chan.current_playback_buffer[i+1] * fr;
804 chan.current_playback_buffer = chan.speed_buffer;
807 playback_distance = i + 1;
808 last_phase = (phase & 0xFFFFFF);
811 playback_distance = nframes;
823 /* we're exiting with failure, so ::commit will not
824 be called. unlock the state lock.
834 AudioDiskstream::commit (nframes_t nframes)
836 bool need_butler = false;
838 if (_actual_speed < 0.0) {
839 playback_sample -= playback_distance;
841 playback_sample += playback_distance;
844 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
846 (*chan).playback_buf->increment_read_ptr (playback_distance);
848 if (adjust_capture_position) {
849 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
853 if (adjust_capture_position != 0) {
854 capture_captured += adjust_capture_position;
855 adjust_capture_position = 0;
859 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
861 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
862 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
873 AudioDiskstream::set_pending_overwrite (bool yn)
875 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
877 pending_overwrite = yn;
879 overwrite_frame = playback_sample;
880 overwrite_offset = channels.front().playback_buf->get_read_ptr();
884 AudioDiskstream::overwrite_existing_buffers ()
886 Sample* mixdown_buffer;
889 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
891 overwrite_queued = false;
893 /* assume all are the same size */
894 nframes_t size = channels[0].playback_buf->bufsize();
896 mixdown_buffer = new Sample[size];
897 gain_buffer = new float[size];
899 /* reduce size so that we can fill the buffer correctly. */
905 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
907 start = overwrite_frame;
908 nframes_t cnt = size;
910 /* to fill the buffer without resetting the playback sample, we need to
911 do it one or two chunks (normally two).
913 |----------------------------------------------------------------------|
917 |<- second chunk->||<----------------- first chunk ------------------>|
921 nframes_t to_read = size - overwrite_offset;
923 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, *chan, n, reversed)) {
924 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
925 _id, size, playback_sample) << endmsg;
933 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer,
934 start, cnt, *chan, n, reversed)) {
935 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
936 _id, size, playback_sample) << endmsg;
945 pending_overwrite = false;
946 delete [] gain_buffer;
947 delete [] mixdown_buffer;
952 AudioDiskstream::seek (nframes_t frame, bool complete_refill)
954 Glib::Mutex::Lock lm (state_lock);
957 ChannelList::iterator chan;
959 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
960 (*chan).playback_buf->reset ();
961 (*chan).capture_buf->reset ();
964 /* can't rec-enable in destructive mode if transport is before start */
966 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
967 disengage_record_enable ();
970 playback_sample = frame;
973 if (complete_refill) {
974 while ((ret = do_refill_with_alloc ()) > 0) ;
976 ret = do_refill_with_alloc ();
983 AudioDiskstream::can_internal_playback_seek (nframes_t distance)
985 ChannelList::iterator chan;
987 for (chan = channels.begin(); chan != channels.end(); ++chan) {
988 if ((*chan).playback_buf->read_space() < distance) {
996 AudioDiskstream::internal_playback_seek (nframes_t distance)
998 ChannelList::iterator chan;
1000 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1001 (*chan).playback_buf->increment_read_ptr (distance);
1004 first_recordable_frame += distance;
1005 playback_sample += distance;
1011 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, nframes_t& start, nframes_t cnt,
1012 ChannelInfo& channel_info, int channel, bool reversed)
1014 nframes_t this_read = 0;
1015 bool reloop = false;
1016 nframes_t loop_end = 0;
1017 nframes_t loop_start = 0;
1018 nframes_t loop_length = 0;
1019 nframes_t offset = 0;
1023 /* Make the use of a Location atomic for this read operation.
1025 Note: Locations don't get deleted, so all we care about
1026 when I say "atomic" is that we are always pointing to
1027 the same one and using a start/length values obtained
1031 if ((loc = loop_location) != 0) {
1032 loop_start = loc->start();
1033 loop_end = loc->end();
1034 loop_length = loop_end - loop_start;
1037 /* if we are looping, ensure that the first frame we read is at the correct
1038 position within the loop.
1041 if (loc && start >= loop_end) {
1042 //cerr << "start adjusted from " << start;
1043 start = loop_start + ((start - loop_start) % loop_length);
1044 //cerr << "to " << start << endl;
1046 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1051 /* take any loop into account. we can't read past the end of the loop. */
1053 if (loc && (loop_end - start < cnt)) {
1054 this_read = loop_end - start;
1055 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1062 if (this_read == 0) {
1066 this_read = min(cnt,this_read);
1068 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1069 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1074 _read_data_count = _playlist->read_data_count();
1078 /* don't adjust start, since caller has already done that
1081 swap_by_ptr (buf, buf + this_read - 1);
1085 /* if we read to the end of the loop, go back to the beginning */
1095 offset += this_read;
1102 AudioDiskstream::do_refill_with_alloc()
1104 Sample* mix_buf = new Sample[disk_io_chunk_frames];
1105 float* gain_buf = new float[disk_io_chunk_frames];
1107 int ret = _do_refill(mix_buf, gain_buf);
1116 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
1120 RingBufferNPT<Sample>::rw_vector vector;
1121 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1122 nframes_t total_space;
1123 nframes_t zero_fill;
1125 ChannelList::iterator i;
1128 assert(mixdown_buffer);
1129 assert(gain_buffer);
1131 channels.front().playback_buf->get_write_vector (&vector);
1133 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1137 /* if there are 2+ chunks of disk i/o possible for
1138 this track, let the caller know so that it can arrange
1139 for us to be called again, ASAP.
1142 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1146 /* if we're running close to normal speed and there isn't enough
1147 space to do disk_io_chunk_frames of I/O, then don't bother.
1149 at higher speeds, just do it because the sync between butler
1150 and audio thread may not be good enough.
1153 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1157 /* when slaved, don't try to get too close to the read pointer. this
1158 leaves space for the buffer reversal to have something useful to
1162 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1166 total_space = min (disk_io_chunk_frames, total_space);
1170 if (file_frame == 0) {
1172 /* at start: nothing to do but fill with silence */
1174 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1176 ChannelInfo& chan (*i);
1177 chan.playback_buf->get_write_vector (&vector);
1178 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1179 if (vector.len[1]) {
1180 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1182 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1187 if (file_frame < total_space) {
1189 /* too close to the start: read what we can,
1190 and then zero fill the rest
1193 zero_fill = total_space - file_frame;
1194 total_space = file_frame;
1199 /* move read position backwards because we are going
1200 to reverse the data.
1203 file_frame -= total_space;
1209 if (file_frame == max_frames) {
1211 /* at end: nothing to do but fill with silence */
1213 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1215 ChannelInfo& chan (*i);
1216 chan.playback_buf->get_write_vector (&vector);
1217 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1218 if (vector.len[1]) {
1219 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1221 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1226 if (file_frame > max_frames - total_space) {
1228 /* to close to the end: read what we can, and zero fill the rest */
1230 zero_fill = total_space - (max_frames - file_frame);
1231 total_space = max_frames - file_frame;
1238 nframes_t file_frame_tmp = 0;
1240 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1242 ChannelInfo& chan (*i);
1245 nframes_t len1, len2;
1247 chan.playback_buf->get_write_vector (&vector);
1250 file_frame_tmp = file_frame;
1253 buf1 = vector.buf[1];
1254 len1 = vector.len[1];
1255 buf2 = vector.buf[0];
1256 len2 = vector.len[0];
1258 buf1 = vector.buf[0];
1259 len1 = vector.len[0];
1260 buf2 = vector.buf[1];
1261 len2 = vector.len[1];
1265 to_read = min (ts, len1);
1266 to_read = min (to_read, disk_io_chunk_frames);
1270 if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1275 chan.playback_buf->increment_write_ptr (to_read);
1279 to_read = min (ts, len2);
1284 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1285 so read some or all of vector.len[1] as well.
1288 if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1293 chan.playback_buf->increment_write_ptr (to_read);
1302 file_frame = file_frame_tmp;
1309 /** Flush pending data to disk.
1311 * Important note: this function will write *AT MOST* disk_io_chunk_frames
1312 * of data to disk. it will never write more than that. If it writes that
1313 * much and there is more than that waiting to be written, it will return 1,
1314 * otherwise 0 on success or -1 on failure.
1316 * If there is less than disk_io_chunk_frames to be written, no data will be
1317 * written at all unless @a force_flush is true.
1320 AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
1324 RingBufferNPT<Sample>::rw_vector vector;
1325 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1328 _write_data_count = 0;
1330 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1332 (*chan).capture_buf->get_read_vector (&vector);
1334 total = vector.len[0] + vector.len[1];
1337 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1341 /* if there are 2+ chunks of disk i/o possible for
1342 this track, let the caller know so that it can arrange
1343 for us to be called again, ASAP.
1345 if we are forcing a flush, then if there is* any* extra
1346 work, let the caller know.
1348 if we are no longer recording and there is any extra work,
1349 let the caller know too.
1352 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1356 to_write = min (disk_io_chunk_frames, (nframes_t) vector.len[0]);
1358 // check the transition buffer when recording destructive
1359 // important that we get this after the capture buf
1361 if (destructive()) {
1362 (*chan).capture_transition_buf->get_read_vector(&transvec);
1363 size_t transcount = transvec.len[0] + transvec.len[1];
1364 bool have_start = false;
1367 for (ti=0; ti < transcount; ++ti) {
1368 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1370 if (captrans.type == CaptureStart) {
1371 // by definition, the first data we got above represents the given capture pos
1373 (*chan).write_source->mark_capture_start (captrans.capture_val);
1374 (*chan).curr_capture_cnt = 0;
1378 else if (captrans.type == CaptureEnd) {
1380 // capture end, the capture_val represents total frames in capture
1382 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1384 // shorten to make the write a perfect fit
1385 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1387 if (nto_write < to_write) {
1388 ret = 1; // should we?
1390 to_write = nto_write;
1392 (*chan).write_source->mark_capture_end ();
1394 // increment past this transition, but go no further
1399 // actually ends just beyond this chunk, so force more work
1407 (*chan).capture_transition_buf->increment_read_ptr(ti);
1411 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write) != to_write) {
1412 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1416 (*chan).capture_buf->increment_read_ptr (to_write);
1417 (*chan).curr_capture_cnt += to_write;
1419 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1421 /* we wrote all of vector.len[0] but it wasn't an entire
1422 disk_io_chunk_frames of data, so arrange for some part
1423 of vector.len[1] to be flushed to disk as well.
1426 to_write = min ((nframes_t)(disk_io_chunk_frames - to_write), (nframes_t) vector.len[1]);
1428 if ((*chan).write_source->write (vector.buf[1], to_write) != to_write) {
1429 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1433 _write_data_count += (*chan).write_source->write_data_count();
1435 (*chan).capture_buf->increment_read_ptr (to_write);
1436 (*chan).curr_capture_cnt += to_write;
1445 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1447 uint32_t buffer_position;
1448 bool more_work = true;
1450 boost::shared_ptr<AudioRegion> region;
1451 nframes_t total_capture;
1453 SourceList::iterator src;
1454 ChannelList::iterator chan;
1455 vector<CaptureInfo*>::iterator ci;
1457 bool mark_write_completed = false;
1459 finish_capture (true);
1461 /* butler is already stopped, but there may be work to do
1462 to flush remaining data to disk.
1465 while (more_work && !err) {
1466 switch (do_flush (Session::TransportContext, true)) {
1473 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1478 /* XXX is there anything we can do if err != 0 ? */
1479 Glib::Mutex::Lock lm (capture_info_lock);
1481 if (capture_info.empty()) {
1485 if (abort_capture) {
1487 ChannelList::iterator chan;
1489 list<boost::shared_ptr<Source> >* deletion_list = new list<boost::shared_ptr<Source> >;
1491 for ( chan = channels.begin(); chan != channels.end(); ++chan) {
1493 if ((*chan).write_source) {
1495 (*chan).write_source->mark_for_remove ();
1497 deletion_list->push_back ((*chan).write_source);
1499 (*chan).write_source.reset ();
1502 /* new source set up in "out" below */
1505 if (!deletion_list->empty()) {
1506 DeleteSources (deletion_list);
1508 delete deletion_list;
1514 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1515 total_capture += (*ci)->frames;
1518 /* figure out the name for this take */
1520 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1522 boost::shared_ptr<AudioFileSource> s = (*chan).write_source;
1526 s->update_header (capture_info.front()->start, when, twhen);
1527 s->set_captured_for (_name);
1532 /* destructive tracks have a single, never changing region */
1534 if (destructive()) {
1536 /* send a signal that any UI can pick up to do the right thing. there is
1537 a small problem here in that a UI may need the peak data to be ready
1538 for the data that was recorded and this isn't interlocked with that
1539 process. this problem is deferred to the UI.
1542 _playlist->Modified();
1546 /* Register a new region with the Session that
1547 describes the entire source. Do this first
1548 so that any sub-regions will obviously be
1549 children of this one (later!)
1553 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1554 region_name_from_path (channels[0].write_source->name()),
1555 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
1557 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1558 region->special_set_position (capture_info.front()->start);
1562 catch (failed_constructor& err) {
1563 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1567 _last_capture_regions.push_back (region);
1569 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1571 XMLNode &before = _playlist->get_state();
1572 _playlist->freeze ();
1574 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1577 _session.region_name (region_name, channels[0].write_source->name(), false);
1579 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
1582 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1583 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1586 catch (failed_constructor& err) {
1587 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1588 continue; /* XXX is this OK? */
1591 _last_capture_regions.push_back (region);
1593 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1595 i_am_the_modifier++;
1596 _playlist->add_region (region, (*ci)->start);
1597 i_am_the_modifier--;
1599 buffer_position += (*ci)->frames;
1603 XMLNode &after = _playlist->get_state();
1604 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1607 mark_write_completed = true;
1609 reset_write_sources (mark_write_completed);
1612 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1616 capture_info.clear ();
1617 capture_start_frame = 0;
1621 AudioDiskstream::finish_capture (bool rec_monitors_input)
1623 was_recording = false;
1625 if (capture_captured == 0) {
1629 if (recordable() && destructive()) {
1630 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1632 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1633 (*chan).capture_transition_buf->get_write_vector(&transvec);
1636 if (transvec.len[0] > 0) {
1637 transvec.buf[0]->type = CaptureEnd;
1638 transvec.buf[0]->capture_val = capture_captured;
1639 (*chan).capture_transition_buf->increment_write_ptr(1);
1643 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1649 CaptureInfo* ci = new CaptureInfo;
1651 ci->start = capture_start_frame;
1652 ci->frames = capture_captured;
1654 /* XXX theoretical race condition here. Need atomic exchange ?
1655 However, the circumstances when this is called right
1656 now (either on record-disable or transport_stopped)
1657 mean that no actual race exists. I think ...
1658 We now have a capture_info_lock, but it is only to be used
1659 to synchronize in the transport_stop and the capture info
1660 accessors, so that invalidation will not occur (both non-realtime).
1663 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1665 capture_info.push_back (ci);
1666 capture_captured = 0;
1670 AudioDiskstream::set_record_enabled (bool yn)
1672 if (!recordable() || !_session.record_enabling_legal()) {
1676 /* can't rec-enable in destructive mode if transport is before start */
1678 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1682 if (yn && channels[0].source == 0) {
1684 /* pick up connections not initiated *from* the IO object
1685 we're associated with.
1688 get_input_sources ();
1691 /* yes, i know that this not proof against race conditions, but its
1692 good enough. i think.
1695 if (record_enabled() != yn) {
1697 engage_record_enable ();
1699 disengage_record_enable ();
1705 AudioDiskstream::engage_record_enable ()
1707 bool rolling = _session.transport_speed() != 0.0f;
1709 g_atomic_int_set (&_record_enabled, 1);
1710 capturing_sources.clear ();
1711 if (Config->get_monitoring_model() == HardwareMonitoring) {
1712 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1713 if ((*chan).source) {
1714 (*chan).source->ensure_monitor_input (!(Config->get_auto_input() && rolling));
1716 capturing_sources.push_back ((*chan).write_source);
1719 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1720 capturing_sources.push_back ((*chan).write_source);
1724 RecordEnableChanged (); /* EMIT SIGNAL */
1728 AudioDiskstream::disengage_record_enable ()
1730 g_atomic_int_set (&_record_enabled, 0);
1731 if (Config->get_monitoring_model() == HardwareMonitoring) {
1732 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1733 if ((*chan).source) {
1734 (*chan).source->ensure_monitor_input (false);
1738 capturing_sources.clear ();
1739 RecordEnableChanged (); /* EMIT SIGNAL */
1744 AudioDiskstream::get_state ()
1746 XMLNode* node = new XMLNode ("AudioDiskstream");
1748 LocaleGuard lg (X_("POSIX"));
1750 snprintf (buf, sizeof(buf), "0x%x", _flags);
1751 node->add_property ("flags", buf);
1753 snprintf (buf, sizeof(buf), "%zd", channels.size());
1754 node->add_property ("channels", buf);
1756 node->add_property ("playlist", _playlist->name());
1758 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1759 node->add_property ("speed", buf);
1761 node->add_property("name", _name);
1763 node->add_property("id", buf);
1765 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1767 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1768 XMLNode* cs_grandchild;
1770 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1771 cs_grandchild = new XMLNode (X_("file"));
1772 cs_grandchild->add_property (X_("path"), (*i)->path());
1773 cs_child->add_child_nocopy (*cs_grandchild);
1776 /* store the location where capture will start */
1780 if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1781 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1783 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1786 cs_child->add_property (X_("at"), buf);
1787 node->add_child_nocopy (*cs_child);
1791 node->add_child_copy (*_extra_xml);
1798 AudioDiskstream::set_state (const XMLNode& node)
1800 const XMLProperty* prop;
1801 XMLNodeList nlist = node.children();
1802 XMLNodeIterator niter;
1803 uint32_t nchans = 1;
1804 XMLNode* capture_pending_node = 0;
1805 LocaleGuard lg (X_("POSIX"));
1807 in_set_state = true;
1809 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1810 if ((*niter)->name() == IO::state_node_name) {
1811 deprecated_io_node = new XMLNode (**niter);
1814 if ((*niter)->name() == X_("CapturingSources")) {
1815 capture_pending_node = *niter;
1819 /* prevent write sources from being created */
1821 in_set_state = true;
1823 if ((prop = node.property ("name")) != 0) {
1824 _name = prop->value();
1827 if (deprecated_io_node) {
1828 if ((prop = deprecated_io_node->property ("id")) != 0) {
1829 _id = prop->value ();
1832 if ((prop = node.property ("id")) != 0) {
1833 _id = prop->value ();
1837 if ((prop = node.property ("flags")) != 0) {
1838 _flags = strtol (prop->value().c_str(), 0, 0);
1841 if ((prop = node.property ("channels")) != 0) {
1842 nchans = atoi (prop->value().c_str());
1845 // create necessary extra channels
1846 // we are always constructed with one and we always need one
1848 if (nchans > _n_channels) {
1850 // we need to add new channel infos
1851 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1853 int diff = nchans - channels.size();
1855 for (int i=0; i < diff; ++i) {
1859 } else if (nchans < _n_channels) {
1861 // we need to get rid of channels
1862 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1864 int diff = channels.size() - nchans;
1866 for (int i = 0; i < diff; ++i) {
1871 if ((prop = node.property ("playlist")) == 0) {
1876 bool had_playlist = (_playlist != 0);
1878 if (find_and_use_playlist (prop->value())) {
1882 if (!had_playlist) {
1883 _playlist->set_orig_diskstream_id (_id);
1886 if (!destructive() && capture_pending_node) {
1887 /* destructive streams have one and only one source per channel,
1888 and so they never end up in pending capture in any useful
1891 use_pending_capture_data (*capture_pending_node);
1896 if ((prop = node.property ("speed")) != 0) {
1897 double sp = atof (prop->value().c_str());
1899 if (realtime_set_speed (sp, false)) {
1900 non_realtime_set_speed ();
1904 _n_channels = channels.size();
1906 in_set_state = false;
1908 /* make sure this is clear before we do anything else */
1910 capturing_sources.clear ();
1912 /* write sources are handled when we handle the input set
1913 up of the IO that owns this DS (::non_realtime_input_change())
1916 in_set_state = false;
1922 AudioDiskstream::use_new_write_source (uint32_t n)
1924 if (!recordable()) {
1928 if (n >= channels.size()) {
1929 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1933 ChannelInfo &chan = channels[n];
1935 if (chan.write_source) {
1937 if (AudioFileSource::is_empty (_session, chan.write_source->path())) {
1938 chan.write_source->mark_for_remove ();
1939 chan.write_source.reset ();
1941 chan.write_source.reset ();
1946 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
1947 throw failed_constructor();
1951 catch (failed_constructor &err) {
1952 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1953 chan.write_source.reset ();
1957 /* do not remove destructive files even if they are empty */
1959 chan.write_source->set_allow_remove_if_empty (!destructive());
1965 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1967 ChannelList::iterator chan;
1970 if (!recordable()) {
1974 capturing_sources.clear ();
1976 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
1977 if (!destructive()) {
1979 if ((*chan).write_source && mark_write_complete) {
1980 (*chan).write_source->mark_streaming_write_completed ();
1982 use_new_write_source (n);
1984 if (record_enabled()) {
1985 capturing_sources.push_back ((*chan).write_source);
1989 if ((*chan).write_source == 0) {
1990 use_new_write_source (n);
1995 if (destructive()) {
1997 /* we now have all our write sources set up, so create the
1998 playlist's single region.
2001 if (_playlist->empty()) {
2002 setup_destructive_playlist ();
2008 AudioDiskstream::rename_write_sources ()
2010 ChannelList::iterator chan;
2013 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2014 if ((*chan).write_source != 0) {
2015 (*chan).write_source->set_name (_name, destructive());
2016 /* XXX what to do if one of them fails ? */
2024 AudioDiskstream::set_block_size (nframes_t nframes)
2026 if (_session.get_block_size() > speed_buffer_size) {
2027 speed_buffer_size = _session.get_block_size();
2029 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2030 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2031 (*chan).speed_buffer = new Sample[speed_buffer_size];
2034 allocate_temporary_buffers ();
2038 AudioDiskstream::allocate_temporary_buffers ()
2040 /* make sure the wrap buffer is at least large enough to deal
2041 with the speeds up to 1.2, to allow for micro-variation
2042 when slaving to MTC, SMPTE etc.
2045 double sp = max (fabsf (_actual_speed), 1.2f);
2046 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() * sp) + 1;
2048 if (required_wrap_size > wrap_buffer_size) {
2050 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2051 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2052 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2053 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2054 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2057 wrap_buffer_size = required_wrap_size;
2062 AudioDiskstream::monitor_input (bool yn)
2064 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2066 if ((*chan).source) {
2067 (*chan).source->ensure_monitor_input (yn);
2073 AudioDiskstream::set_align_style_from_io ()
2075 bool have_physical = false;
2081 get_input_sources ();
2083 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2084 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2085 have_physical = true;
2090 if (have_physical) {
2091 set_align_style (ExistingMaterial);
2093 set_align_style (CaptureTime);
2098 AudioDiskstream::add_channel ()
2100 /* XXX need to take lock??? */
2104 init_channel (chan);
2106 chan.speed_buffer = new Sample[speed_buffer_size];
2107 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2108 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2110 channels.push_back (chan);
2112 _n_channels = channels.size();
2118 AudioDiskstream::remove_channel ()
2120 if (channels.size() > 1) {
2121 /* XXX need to take lock??? */
2122 ChannelInfo & chan = channels.back();
2123 destroy_channel (chan);
2124 channels.pop_back();
2126 _n_channels = channels.size();
2134 AudioDiskstream::playback_buffer_load () const
2136 return (float) ((double) channels.front().playback_buf->read_space()/
2137 (double) channels.front().playback_buf->bufsize());
2141 AudioDiskstream::capture_buffer_load () const
2143 return (float) ((double) channels.front().capture_buf->write_space()/
2144 (double) channels.front().capture_buf->bufsize());
2148 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2150 const XMLProperty* prop;
2151 XMLNodeList nlist = node.children();
2152 XMLNodeIterator niter;
2153 boost::shared_ptr<AudioFileSource> fs;
2154 boost::shared_ptr<AudioFileSource> first_fs;
2155 SourceList pending_sources;
2158 if ((prop = node.property (X_("at"))) == 0) {
2162 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2166 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2167 if ((*niter)->name() == X_("file")) {
2169 if ((prop = (*niter)->property (X_("path"))) == 0) {
2174 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (_session, prop->value(), false, _session.frame_rate()));
2177 catch (failed_constructor& err) {
2178 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2179 _name, prop->value())
2184 pending_sources.push_back (fs);
2186 if (first_fs == 0) {
2190 fs->set_captured_for (_name);
2194 if (pending_sources.size() == 0) {
2195 /* nothing can be done */
2199 if (pending_sources.size() != _n_channels) {
2200 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2205 boost::shared_ptr<AudioRegion> region;
2208 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(),
2209 region_name_from_path (first_fs->name()),
2210 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
2211 region->special_set_position (0);
2214 catch (failed_constructor& err) {
2215 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2223 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name())));
2226 catch (failed_constructor& err) {
2227 error << string_compose (_("%1: cannot create region from pending capture sources"),
2234 _playlist->add_region (region, position);