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 ((jack_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 cerr << "setup DS using " << srcs.front()->natural_position () << endl;
379 boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, max_frames, _name));
380 _playlist->add_region (region, srcs.front()->natural_position());
384 AudioDiskstream::use_destructive_playlist ()
386 /* this is called from the XML-based constructor. when its done,
387 we already have a playlist and a region, but we need to
388 set up our sources for write. we use the sources associated
389 with the (presumed single, full-extent) region.
392 Playlist::RegionList* rl = _playlist->regions_at (0);
395 reset_write_sources (false, true);
399 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rl->front());
402 throw failed_constructor();
408 ChannelList::iterator chan;
410 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
411 (*chan).write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
412 assert((*chan).write_source);
413 (*chan).write_source->set_allow_remove_if_empty (false);
416 /* the source list will never be reset for a destructive track */
420 AudioDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record)
422 int possibly_recording;
425 const int transport_rolling = 0x4;
426 const int track_rec_enabled = 0x2;
427 const int global_rec_enabled = 0x1;
429 /* merge together the 3 factors that affect record status, and compute
433 rolling = _session.transport_speed() != 0.0f;
434 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
435 change = possibly_recording ^ last_possibly_recording;
437 if (possibly_recording == last_possibly_recording) {
443 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
445 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
446 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
448 /* starting to record: compute first+last frames */
450 first_recordable_frame = transport_frame + _capture_offset;
451 last_recordable_frame = max_frames;
452 capture_start_frame = transport_frame;
454 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
456 /* was stopped, now rolling (and recording) */
458 if (_alignment_style == ExistingMaterial) {
459 first_recordable_frame += _session.worst_output_latency();
461 first_recordable_frame += _roll_delay;
466 /* was rolling, but record state changed */
468 if (_alignment_style == ExistingMaterial) {
471 if (!_session.get_punch_in()) {
473 /* manual punch in happens at the correct transport frame
474 because the user hit a button. but to get alignment correct
475 we have to back up the position of the new region to the
476 appropriate spot given the roll delay.
479 capture_start_frame -= _roll_delay;
481 /* XXX paul notes (august 2005): i don't know why
485 first_recordable_frame += _capture_offset;
489 /* autopunch toggles recording at the precise
490 transport frame, and then the DS waits
491 to start recording for a time that depends
492 on the output latency.
495 first_recordable_frame += _session.worst_output_latency();
500 if (_session.get_punch_in()) {
501 first_recordable_frame += _roll_delay;
503 capture_start_frame -= _roll_delay;
509 if (_flags & Recordable) {
510 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
512 RingBufferNPT<CaptureTransition>::rw_vector transvec;
513 (*chan).capture_transition_buf->get_write_vector(&transvec);
515 if (transvec.len[0] > 0) {
516 transvec.buf[0]->type = CaptureStart;
517 transvec.buf[0]->capture_val = capture_start_frame;
518 (*chan).capture_transition_buf->increment_write_ptr(1);
522 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
528 } else if (!record_enabled() || !can_record) {
532 last_recordable_frame = transport_frame + _capture_offset;
534 if (_alignment_style == ExistingMaterial) {
535 last_recordable_frame += _session.worst_output_latency();
537 last_recordable_frame += _roll_delay;
541 last_possibly_recording = possibly_recording;
545 AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input)
548 ChannelList::iterator c;
550 jack_nframes_t rec_offset = 0;
551 jack_nframes_t rec_nframes = 0;
552 bool nominally_recording;
553 bool re = record_enabled ();
554 bool collect_playback = false;
556 /* if we've already processed the frames corresponding to this call,
557 just return. this allows multiple routes that are taking input
558 from this diskstream to call our ::process() method, but have
559 this stuff only happen once. more commonly, it allows both
560 the AudioTrack that is using this AudioDiskstream *and* the Session
561 to call process() without problems.
568 check_record_status (transport_frame, nframes, can_record);
570 nominally_recording = (can_record && re);
577 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
578 must always be called as a pair. The only exception is if this function
579 returns a non-zero value, in which case, ::commit should not be called.
582 // If we can't take the state lock return.
583 if (!state_lock.trylock()) {
587 adjust_capture_position = 0;
589 for (c = channels.begin(); c != channels.end(); ++c) {
590 (*c).current_capture_buffer = 0;
591 (*c).current_playback_buffer = 0;
594 if (nominally_recording || (_session.get_record_enabled() && _session.get_punch_in())) {
597 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
604 case OverlapInternal:
605 /* ---------- recrange
608 rec_nframes = nframes;
613 /* |--------| recrange
616 rec_nframes = transport_frame + nframes - first_recordable_frame;
618 rec_offset = first_recordable_frame - transport_frame;
623 /* |--------| recrange
626 rec_nframes = last_recordable_frame - transport_frame;
630 case OverlapExternal:
631 /* |--------| recrange
632 -------------- transrange
634 rec_nframes = last_recordable_frame - last_recordable_frame;
635 rec_offset = first_recordable_frame - transport_frame;
639 if (rec_nframes && !was_recording) {
640 capture_captured = 0;
641 was_recording = true;
646 if (can_record && !_last_capture_regions.empty()) {
647 _last_capture_regions.clear ();
650 if (nominally_recording || rec_nframes) {
652 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
654 ChannelInfo& chan (*c);
656 chan.capture_buf->get_write_vector (&chan.capture_vector);
658 if (rec_nframes <= chan.capture_vector.len[0]) {
660 chan.current_capture_buffer = chan.capture_vector.buf[0];
662 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
666 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
670 jack_nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
672 if (rec_nframes > total) {
677 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
678 jack_nframes_t first = chan.capture_vector.len[0];
680 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
681 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
682 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
683 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
685 chan.current_capture_buffer = chan.capture_wrap_buffer;
692 finish_capture (rec_monitors_input);
699 /* data will be written to disk */
701 if (rec_nframes == nframes && rec_offset == 0) {
703 for (c = channels.begin(); c != channels.end(); ++c) {
704 (*c).current_playback_buffer = (*c).current_capture_buffer;
707 playback_distance = nframes;
712 /* we can't use the capture buffer as the playback buffer, because
713 we recorded only a part of the current process' cycle data
717 collect_playback = true;
720 adjust_capture_position = rec_nframes;
722 } else if (nominally_recording) {
724 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
726 for (c = channels.begin(); c != channels.end(); ++c) {
727 (*c).current_playback_buffer = (*c).current_capture_buffer;
730 playback_distance = nframes;
734 collect_playback = true;
737 if (collect_playback) {
739 /* we're doing playback */
741 jack_nframes_t necessary_samples;
743 /* no varispeed playback if we're recording, because the output .... TBD */
745 if (rec_nframes == 0 && _actual_speed != 1.0f) {
746 necessary_samples = (jack_nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
748 necessary_samples = nframes;
751 for (c = channels.begin(); c != channels.end(); ++c) {
752 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
757 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
759 ChannelInfo& chan (*c);
761 if (necessary_samples <= chan.playback_vector.len[0]) {
763 chan.current_playback_buffer = chan.playback_vector.buf[0];
766 jack_nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
768 if (necessary_samples > total) {
774 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
775 chan.playback_vector.len[0] * sizeof (Sample));
776 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
777 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
779 chan.current_playback_buffer = chan.playback_wrap_buffer;
784 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
786 uint64_t phase = last_phase;
787 jack_nframes_t i = 0;
789 // Linearly interpolate into the alt buffer
790 // using 40.24 fixp maths (swh)
792 for (c = channels.begin(); c != channels.end(); ++c) {
795 ChannelInfo& chan (*c);
800 for (jack_nframes_t outsample = 0; outsample < nframes; ++outsample) {
802 fr = (phase & 0xFFFFFF) / 16777216.0f;
803 chan.speed_buffer[outsample] =
804 chan.current_playback_buffer[i] * (1.0f - fr) +
805 chan.current_playback_buffer[i+1] * fr;
809 chan.current_playback_buffer = chan.speed_buffer;
812 playback_distance = i + 1;
813 last_phase = (phase & 0xFFFFFF);
816 playback_distance = nframes;
828 /* we're exiting with failure, so ::commit will not
829 be called. unlock the state lock.
839 AudioDiskstream::commit (jack_nframes_t nframes)
841 bool need_butler = false;
843 if (_actual_speed < 0.0) {
844 playback_sample -= playback_distance;
846 playback_sample += playback_distance;
849 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
851 (*chan).playback_buf->increment_read_ptr (playback_distance);
853 if (adjust_capture_position) {
854 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
858 if (adjust_capture_position != 0) {
859 capture_captured += adjust_capture_position;
860 adjust_capture_position = 0;
864 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
866 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
867 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
878 AudioDiskstream::set_pending_overwrite (bool yn)
880 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
882 pending_overwrite = yn;
884 overwrite_frame = playback_sample;
885 overwrite_offset = channels.front().playback_buf->get_read_ptr();
889 AudioDiskstream::overwrite_existing_buffers ()
891 Sample* mixdown_buffer;
894 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
896 overwrite_queued = false;
898 /* assume all are the same size */
899 jack_nframes_t size = channels[0].playback_buf->bufsize();
901 mixdown_buffer = new Sample[size];
902 gain_buffer = new float[size];
904 /* reduce size so that we can fill the buffer correctly. */
908 jack_nframes_t start;
910 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
912 start = overwrite_frame;
913 jack_nframes_t cnt = size;
915 /* to fill the buffer without resetting the playback sample, we need to
916 do it one or two chunks (normally two).
918 |----------------------------------------------------------------------|
922 |<- second chunk->||<----------------- first chunk ------------------>|
926 jack_nframes_t to_read = size - overwrite_offset;
928 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, *chan, n, reversed)) {
929 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
930 _id, size, playback_sample) << endmsg;
938 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer,
939 start, cnt, *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;
950 pending_overwrite = false;
951 delete [] gain_buffer;
952 delete [] mixdown_buffer;
957 AudioDiskstream::seek (jack_nframes_t frame, bool complete_refill)
959 Glib::Mutex::Lock lm (state_lock);
962 ChannelList::iterator chan;
964 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
965 (*chan).playback_buf->reset ();
966 (*chan).capture_buf->reset ();
969 /* can't rec-enable in destructive mode if transport is before start */
971 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
972 disengage_record_enable ();
975 playback_sample = frame;
978 if (complete_refill) {
979 while ((ret = do_refill_with_alloc ()) > 0) ;
981 ret = do_refill_with_alloc ();
988 AudioDiskstream::can_internal_playback_seek (jack_nframes_t distance)
990 ChannelList::iterator chan;
992 for (chan = channels.begin(); chan != channels.end(); ++chan) {
993 if ((*chan).playback_buf->read_space() < distance) {
1001 AudioDiskstream::internal_playback_seek (jack_nframes_t distance)
1003 ChannelList::iterator chan;
1005 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1006 (*chan).playback_buf->increment_read_ptr (distance);
1009 first_recordable_frame += distance;
1010 playback_sample += distance;
1016 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, jack_nframes_t& start, jack_nframes_t cnt,
1017 ChannelInfo& channel_info, int channel, bool reversed)
1019 jack_nframes_t this_read = 0;
1020 bool reloop = false;
1021 jack_nframes_t loop_end = 0;
1022 jack_nframes_t loop_start = 0;
1023 jack_nframes_t loop_length = 0;
1024 jack_nframes_t offset = 0;
1028 /* Make the use of a Location atomic for this read operation.
1030 Note: Locations don't get deleted, so all we care about
1031 when I say "atomic" is that we are always pointing to
1032 the same one and using a start/length values obtained
1036 if ((loc = loop_location) != 0) {
1037 loop_start = loc->start();
1038 loop_end = loc->end();
1039 loop_length = loop_end - loop_start;
1042 /* if we are looping, ensure that the first frame we read is at the correct
1043 position within the loop.
1046 if (loc && start >= loop_end) {
1047 //cerr << "start adjusted from " << start;
1048 start = loop_start + ((start - loop_start) % loop_length);
1049 //cerr << "to " << start << endl;
1051 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1056 /* take any loop into account. we can't read past the end of the loop. */
1058 if (loc && (loop_end - start < cnt)) {
1059 this_read = loop_end - start;
1060 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1067 if (this_read == 0) {
1071 this_read = min(cnt,this_read);
1073 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1074 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1079 _read_data_count = _playlist->read_data_count();
1083 /* don't adjust start, since caller has already done that
1086 swap_by_ptr (buf, buf + this_read - 1);
1090 /* if we read to the end of the loop, go back to the beginning */
1100 offset += this_read;
1107 AudioDiskstream::do_refill_with_alloc()
1109 Sample* mix_buf = new Sample[disk_io_chunk_frames];
1110 float* gain_buf = new float[disk_io_chunk_frames];
1112 int ret = _do_refill(mix_buf, gain_buf);
1121 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
1124 jack_nframes_t to_read;
1125 RingBufferNPT<Sample>::rw_vector vector;
1126 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1127 jack_nframes_t total_space;
1128 jack_nframes_t zero_fill;
1130 ChannelList::iterator i;
1133 assert(mixdown_buffer);
1134 assert(gain_buffer);
1136 channels.front().playback_buf->get_write_vector (&vector);
1138 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1142 /* if there are 2+ chunks of disk i/o possible for
1143 this track, let the caller know so that it can arrange
1144 for us to be called again, ASAP.
1147 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1151 /* if we're running close to normal speed and there isn't enough
1152 space to do disk_io_chunk_frames of I/O, then don't bother.
1154 at higher speeds, just do it because the sync between butler
1155 and audio thread may not be good enough.
1158 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1162 /* when slaved, don't try to get too close to the read pointer. this
1163 leaves space for the buffer reversal to have something useful to
1167 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1171 total_space = min (disk_io_chunk_frames, total_space);
1175 if (file_frame == 0) {
1177 /* at start: nothing to do but fill with silence */
1179 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1181 ChannelInfo& chan (*i);
1182 chan.playback_buf->get_write_vector (&vector);
1183 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1184 if (vector.len[1]) {
1185 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1187 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1192 if (file_frame < total_space) {
1194 /* too close to the start: read what we can,
1195 and then zero fill the rest
1198 zero_fill = total_space - file_frame;
1199 total_space = file_frame;
1204 /* move read position backwards because we are going
1205 to reverse the data.
1208 file_frame -= total_space;
1214 if (file_frame == max_frames) {
1216 /* at end: nothing to do but fill with silence */
1218 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1220 ChannelInfo& chan (*i);
1221 chan.playback_buf->get_write_vector (&vector);
1222 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1223 if (vector.len[1]) {
1224 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1226 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1231 if (file_frame > max_frames - total_space) {
1233 /* to close to the end: read what we can, and zero fill the rest */
1235 zero_fill = total_space - (max_frames - file_frame);
1236 total_space = max_frames - file_frame;
1243 jack_nframes_t file_frame_tmp = 0;
1245 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1247 ChannelInfo& chan (*i);
1250 jack_nframes_t len1, len2;
1252 chan.playback_buf->get_write_vector (&vector);
1255 file_frame_tmp = file_frame;
1258 buf1 = vector.buf[1];
1259 len1 = vector.len[1];
1260 buf2 = vector.buf[0];
1261 len2 = vector.len[0];
1263 buf1 = vector.buf[0];
1264 len1 = vector.len[0];
1265 buf2 = vector.buf[1];
1266 len2 = vector.len[1];
1270 to_read = min (ts, len1);
1271 to_read = min (to_read, disk_io_chunk_frames);
1275 if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1280 chan.playback_buf->increment_write_ptr (to_read);
1284 to_read = min (ts, len2);
1289 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1290 so read some or all of vector.len[1] as well.
1293 if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1298 chan.playback_buf->increment_write_ptr (to_read);
1307 file_frame = file_frame_tmp;
1314 /** Flush pending data to disk.
1316 * Important note: this function will write *AT MOST* disk_io_chunk_frames
1317 * of data to disk. it will never write more than that. If it writes that
1318 * much and there is more than that waiting to be written, it will return 1,
1319 * otherwise 0 on success or -1 on failure.
1321 * If there is less than disk_io_chunk_frames to be written, no data will be
1322 * written at all unless @a force_flush is true.
1325 AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
1329 RingBufferNPT<Sample>::rw_vector vector;
1330 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1331 jack_nframes_t total;
1333 _write_data_count = 0;
1335 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1337 (*chan).capture_buf->get_read_vector (&vector);
1339 total = vector.len[0] + vector.len[1];
1342 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1346 /* if there are 2+ chunks of disk i/o possible for
1347 this track, let the caller know so that it can arrange
1348 for us to be called again, ASAP.
1350 if we are forcing a flush, then if there is* any* extra
1351 work, let the caller know.
1353 if we are no longer recording and there is any extra work,
1354 let the caller know too.
1357 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1361 to_write = min (disk_io_chunk_frames, (jack_nframes_t) vector.len[0]);
1363 // check the transition buffer when recording destructive
1364 // important that we get this after the capture buf
1366 if (destructive()) {
1367 (*chan).capture_transition_buf->get_read_vector(&transvec);
1368 size_t transcount = transvec.len[0] + transvec.len[1];
1369 bool have_start = false;
1372 for (ti=0; ti < transcount; ++ti) {
1373 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1375 if (captrans.type == CaptureStart) {
1376 // by definition, the first data we got above represents the given capture pos
1378 (*chan).write_source->mark_capture_start (captrans.capture_val);
1379 (*chan).curr_capture_cnt = 0;
1383 else if (captrans.type == CaptureEnd) {
1385 // capture end, the capture_val represents total frames in capture
1387 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1389 // shorten to make the write a perfect fit
1390 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1392 if (nto_write < to_write) {
1393 ret = 1; // should we?
1395 to_write = nto_write;
1397 (*chan).write_source->mark_capture_end ();
1399 // increment past this transition, but go no further
1404 // actually ends just beyond this chunk, so force more work
1412 (*chan).capture_transition_buf->increment_read_ptr(ti);
1416 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write) != to_write) {
1417 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1421 (*chan).capture_buf->increment_read_ptr (to_write);
1422 (*chan).curr_capture_cnt += to_write;
1424 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1426 /* we wrote all of vector.len[0] but it wasn't an entire
1427 disk_io_chunk_frames of data, so arrange for some part
1428 of vector.len[1] to be flushed to disk as well.
1431 to_write = min ((jack_nframes_t)(disk_io_chunk_frames - to_write), (jack_nframes_t) vector.len[1]);
1433 if ((*chan).write_source->write (vector.buf[1], to_write) != to_write) {
1434 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1438 _write_data_count += (*chan).write_source->write_data_count();
1440 (*chan).capture_buf->increment_read_ptr (to_write);
1441 (*chan).curr_capture_cnt += to_write;
1450 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1452 uint32_t buffer_position;
1453 bool more_work = true;
1455 boost::shared_ptr<AudioRegion> region;
1456 jack_nframes_t total_capture;
1458 SourceList::iterator src;
1459 ChannelList::iterator chan;
1460 vector<CaptureInfo*>::iterator ci;
1462 bool mark_write_completed = false;
1464 finish_capture (true);
1466 /* butler is already stopped, but there may be work to do
1467 to flush remaining data to disk.
1470 while (more_work && !err) {
1471 switch (do_flush (Session::TransportContext, true)) {
1478 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1483 /* XXX is there anything we can do if err != 0 ? */
1484 Glib::Mutex::Lock lm (capture_info_lock);
1486 if (capture_info.empty()) {
1490 if (abort_capture) {
1492 ChannelList::iterator chan;
1494 list<boost::shared_ptr<Source> >* deletion_list = new list<boost::shared_ptr<Source> >;
1496 for ( chan = channels.begin(); chan != channels.end(); ++chan) {
1498 if ((*chan).write_source) {
1500 (*chan).write_source->mark_for_remove ();
1502 deletion_list->push_back ((*chan).write_source);
1504 (*chan).write_source.reset ();
1507 /* new source set up in "out" below */
1510 if (!deletion_list->empty()) {
1511 DeleteSources (deletion_list);
1513 delete deletion_list;
1519 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1520 total_capture += (*ci)->frames;
1523 /* figure out the name for this take */
1525 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1527 boost::shared_ptr<AudioFileSource> s = (*chan).write_source;
1531 s->update_header (capture_info.front()->start, when, twhen);
1532 s->set_captured_for (_name);
1537 /* destructive tracks have a single, never changing region */
1539 if (destructive()) {
1541 /* send a signal that any UI can pick up to do the right thing. there is
1542 a small problem here in that a UI may need the peak data to be ready
1543 for the data that was recorded and this isn't interlocked with that
1544 process. this problem is deferred to the UI.
1547 _playlist->Modified();
1551 /* Register a new region with the Session that
1552 describes the entire source. Do this first
1553 so that any sub-regions will obviously be
1554 children of this one (later!)
1558 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1559 region_name_from_path (channels[0].write_source->name()),
1560 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
1562 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1563 region->special_set_position (capture_info.front()->start);
1567 catch (failed_constructor& err) {
1568 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1572 _last_capture_regions.push_back (region);
1574 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1576 XMLNode &before = _playlist->get_state();
1577 _playlist->freeze ();
1579 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1582 _session.region_name (region_name, channels[0].write_source->name(), false);
1584 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
1587 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1588 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1591 catch (failed_constructor& err) {
1592 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1593 continue; /* XXX is this OK? */
1596 _last_capture_regions.push_back (region);
1598 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1600 i_am_the_modifier++;
1601 _playlist->add_region (region, (*ci)->start);
1602 i_am_the_modifier--;
1604 buffer_position += (*ci)->frames;
1608 XMLNode &after = _playlist->get_state();
1609 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1612 mark_write_completed = true;
1614 reset_write_sources (mark_write_completed);
1617 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1621 capture_info.clear ();
1622 capture_start_frame = 0;
1626 AudioDiskstream::finish_capture (bool rec_monitors_input)
1628 was_recording = false;
1630 if (capture_captured == 0) {
1634 if (recordable() && destructive()) {
1635 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1637 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1638 (*chan).capture_transition_buf->get_write_vector(&transvec);
1641 if (transvec.len[0] > 0) {
1642 transvec.buf[0]->type = CaptureEnd;
1643 transvec.buf[0]->capture_val = capture_captured;
1644 (*chan).capture_transition_buf->increment_write_ptr(1);
1648 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1654 CaptureInfo* ci = new CaptureInfo;
1656 ci->start = capture_start_frame;
1657 ci->frames = capture_captured;
1659 /* XXX theoretical race condition here. Need atomic exchange ?
1660 However, the circumstances when this is called right
1661 now (either on record-disable or transport_stopped)
1662 mean that no actual race exists. I think ...
1663 We now have a capture_info_lock, but it is only to be used
1664 to synchronize in the transport_stop and the capture info
1665 accessors, so that invalidation will not occur (both non-realtime).
1668 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1670 capture_info.push_back (ci);
1671 capture_captured = 0;
1675 AudioDiskstream::set_record_enabled (bool yn)
1677 if (!recordable() || !_session.record_enabling_legal()) {
1681 /* can't rec-enable in destructive mode if transport is before start */
1683 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1687 if (yn && channels[0].source == 0) {
1689 /* pick up connections not initiated *from* the IO object
1690 we're associated with.
1693 get_input_sources ();
1696 /* yes, i know that this not proof against race conditions, but its
1697 good enough. i think.
1700 if (record_enabled() != yn) {
1702 engage_record_enable ();
1704 disengage_record_enable ();
1710 AudioDiskstream::engage_record_enable ()
1712 bool rolling = _session.transport_speed() != 0.0f;
1714 g_atomic_int_set (&_record_enabled, 1);
1715 capturing_sources.clear ();
1716 if (Config->get_use_hardware_monitoring()) {
1717 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1718 if ((*chan).source) {
1719 (*chan).source->ensure_monitor_input (!(_session.get_auto_input() && rolling));
1721 capturing_sources.push_back ((*chan).write_source);
1724 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1725 capturing_sources.push_back ((*chan).write_source);
1729 RecordEnableChanged (); /* EMIT SIGNAL */
1733 AudioDiskstream::disengage_record_enable ()
1735 g_atomic_int_set (&_record_enabled, 0);
1736 if (Config->get_use_hardware_monitoring()) {
1737 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1738 if ((*chan).source) {
1739 (*chan).source->ensure_monitor_input (false);
1743 capturing_sources.clear ();
1744 RecordEnableChanged (); /* EMIT SIGNAL */
1749 AudioDiskstream::get_state ()
1751 XMLNode* node = new XMLNode ("AudioDiskstream");
1753 LocaleGuard lg (X_("POSIX"));
1755 snprintf (buf, sizeof(buf), "0x%x", _flags);
1756 node->add_property ("flags", buf);
1758 snprintf (buf, sizeof(buf), "%zd", channels.size());
1759 node->add_property ("channels", buf);
1761 node->add_property ("playlist", _playlist->name());
1763 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1764 node->add_property ("speed", buf);
1766 node->add_property("name", _name);
1768 node->add_property("id", buf);
1770 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1772 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1773 XMLNode* cs_grandchild;
1775 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1776 cs_grandchild = new XMLNode (X_("file"));
1777 cs_grandchild->add_property (X_("path"), (*i)->path());
1778 cs_child->add_child_nocopy (*cs_grandchild);
1781 /* store the location where capture will start */
1785 if (_session.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1786 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1788 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1791 cs_child->add_property (X_("at"), buf);
1792 node->add_child_nocopy (*cs_child);
1796 node->add_child_copy (*_extra_xml);
1803 AudioDiskstream::set_state (const XMLNode& node)
1805 const XMLProperty* prop;
1806 XMLNodeList nlist = node.children();
1807 XMLNodeIterator niter;
1808 uint32_t nchans = 1;
1809 XMLNode* capture_pending_node = 0;
1810 LocaleGuard lg (X_("POSIX"));
1812 in_set_state = true;
1814 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1815 if ((*niter)->name() == IO::state_node_name) {
1816 deprecated_io_node = new XMLNode (**niter);
1819 if ((*niter)->name() == X_("CapturingSources")) {
1820 capture_pending_node = *niter;
1824 /* prevent write sources from being created */
1826 in_set_state = true;
1828 if ((prop = node.property ("name")) != 0) {
1829 _name = prop->value();
1832 if (deprecated_io_node) {
1833 if ((prop = deprecated_io_node->property ("id")) != 0) {
1834 _id = prop->value ();
1837 if ((prop = node.property ("id")) != 0) {
1838 _id = prop->value ();
1842 if ((prop = node.property ("flags")) != 0) {
1843 _flags = strtol (prop->value().c_str(), 0, 0);
1846 if ((prop = node.property ("channels")) != 0) {
1847 nchans = atoi (prop->value().c_str());
1850 // create necessary extra channels
1851 // we are always constructed with one and we always need one
1853 if (nchans > _n_channels) {
1855 // we need to add new channel infos
1856 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1858 int diff = nchans - channels.size();
1860 for (int i=0; i < diff; ++i) {
1864 } else if (nchans < _n_channels) {
1866 // we need to get rid of channels
1867 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1869 int diff = channels.size() - nchans;
1871 for (int i = 0; i < diff; ++i) {
1876 if ((prop = node.property ("playlist")) == 0) {
1881 bool had_playlist = (_playlist != 0);
1883 if (find_and_use_playlist (prop->value())) {
1887 if (!had_playlist) {
1888 _playlist->set_orig_diskstream_id (_id);
1891 if (!destructive() && capture_pending_node) {
1892 /* destructive streams have one and only one source per channel,
1893 and so they never end up in pending capture in any useful
1896 use_pending_capture_data (*capture_pending_node);
1901 if ((prop = node.property ("speed")) != 0) {
1902 double sp = atof (prop->value().c_str());
1904 if (realtime_set_speed (sp, false)) {
1905 non_realtime_set_speed ();
1909 _n_channels = channels.size();
1911 in_set_state = false;
1913 /* make sure this is clear before we do anything else */
1915 capturing_sources.clear ();
1917 /* write sources are handled when we handle the input set
1918 up of the IO that owns this DS (::non_realtime_input_change())
1921 in_set_state = false;
1927 AudioDiskstream::use_new_write_source (uint32_t n)
1929 if (!recordable()) {
1933 if (n >= channels.size()) {
1934 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1938 ChannelInfo &chan = channels[n];
1940 if (chan.write_source) {
1942 if (AudioFileSource::is_empty (_session, chan.write_source->path())) {
1943 chan.write_source->mark_for_remove ();
1944 chan.write_source.reset ();
1946 chan.write_source.reset ();
1951 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
1952 throw failed_constructor();
1956 catch (failed_constructor &err) {
1957 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1958 chan.write_source.reset ();
1962 /* do not remove destructive files even if they are empty */
1964 chan.write_source->set_allow_remove_if_empty (!destructive());
1970 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1972 ChannelList::iterator chan;
1975 if (!recordable()) {
1979 capturing_sources.clear ();
1981 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
1982 if (!destructive()) {
1984 if ((*chan).write_source && mark_write_complete) {
1985 (*chan).write_source->mark_streaming_write_completed ();
1987 use_new_write_source (n);
1989 if (record_enabled()) {
1990 capturing_sources.push_back ((*chan).write_source);
1994 if ((*chan).write_source == 0) {
1995 use_new_write_source (n);
2000 if (destructive()) {
2002 /* we now have all our write sources set up, so create the
2003 playlist's single region.
2006 if (_playlist->empty()) {
2007 setup_destructive_playlist ();
2013 AudioDiskstream::rename_write_sources ()
2015 ChannelList::iterator chan;
2018 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2019 if ((*chan).write_source != 0) {
2020 (*chan).write_source->set_name (_name, destructive());
2021 /* XXX what to do if one of them fails ? */
2029 AudioDiskstream::set_block_size (jack_nframes_t nframes)
2031 if (_session.get_block_size() > speed_buffer_size) {
2032 speed_buffer_size = _session.get_block_size();
2034 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2035 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2036 (*chan).speed_buffer = new Sample[speed_buffer_size];
2039 allocate_temporary_buffers ();
2043 AudioDiskstream::allocate_temporary_buffers ()
2045 /* make sure the wrap buffer is at least large enough to deal
2046 with the speeds up to 1.2, to allow for micro-variation
2047 when slaving to MTC, SMPTE etc.
2050 double sp = max (fabsf (_actual_speed), 1.2f);
2051 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * sp) + 1;
2053 if (required_wrap_size > wrap_buffer_size) {
2055 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2056 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2057 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2058 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2059 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2062 wrap_buffer_size = required_wrap_size;
2067 AudioDiskstream::monitor_input (bool yn)
2069 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2071 if ((*chan).source) {
2072 (*chan).source->ensure_monitor_input (yn);
2078 AudioDiskstream::set_align_style_from_io ()
2080 bool have_physical = false;
2086 get_input_sources ();
2088 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2089 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2090 have_physical = true;
2095 if (have_physical) {
2096 set_align_style (ExistingMaterial);
2098 set_align_style (CaptureTime);
2103 AudioDiskstream::add_channel ()
2105 /* XXX need to take lock??? */
2109 init_channel (chan);
2111 chan.speed_buffer = new Sample[speed_buffer_size];
2112 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2113 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2115 channels.push_back (chan);
2117 _n_channels = channels.size();
2123 AudioDiskstream::remove_channel ()
2125 if (channels.size() > 1) {
2126 /* XXX need to take lock??? */
2127 ChannelInfo & chan = channels.back();
2128 destroy_channel (chan);
2129 channels.pop_back();
2131 _n_channels = channels.size();
2139 AudioDiskstream::playback_buffer_load () const
2141 return (float) ((double) channels.front().playback_buf->read_space()/
2142 (double) channels.front().playback_buf->bufsize());
2146 AudioDiskstream::capture_buffer_load () const
2148 return (float) ((double) channels.front().capture_buf->write_space()/
2149 (double) channels.front().capture_buf->bufsize());
2153 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2155 const XMLProperty* prop;
2156 XMLNodeList nlist = node.children();
2157 XMLNodeIterator niter;
2158 boost::shared_ptr<AudioFileSource> fs;
2159 boost::shared_ptr<AudioFileSource> first_fs;
2160 SourceList pending_sources;
2161 jack_nframes_t position;
2163 if ((prop = node.property (X_("at"))) == 0) {
2167 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2171 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2172 if ((*niter)->name() == X_("file")) {
2174 if ((prop = (*niter)->property (X_("path"))) == 0) {
2179 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (_session, prop->value(), false, _session.frame_rate()));
2182 catch (failed_constructor& err) {
2183 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2184 _name, prop->value())
2189 pending_sources.push_back (fs);
2191 if (first_fs == 0) {
2195 fs->set_captured_for (_name);
2199 if (pending_sources.size() == 0) {
2200 /* nothing can be done */
2204 if (pending_sources.size() != _n_channels) {
2205 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2210 boost::shared_ptr<AudioRegion> region;
2213 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(),
2214 region_name_from_path (first_fs->name()),
2215 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
2216 region->special_set_position (0);
2219 catch (failed_constructor& err) {
2220 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2228 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name())));
2231 catch (failed_constructor& err) {
2232 error << string_compose (_("%1: cannot create region from pending capture sources"),
2239 _playlist->add_region (region, position);