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()));
255 seek (_session.transport_frame());
260 AudioDiskstream::get_input_sources ()
262 uint32_t ni = _io->n_inputs();
264 for (uint32_t n = 0; n < ni; ++n) {
266 const char **connections = _io->input(n)->get_connections ();
267 ChannelInfo& chan = channels[n];
269 if (connections == 0 || connections[0] == 0) {
272 // _source->disable_metering ();
278 chan.source = _session.engine().get_port_by_name (connections[0]);
288 AudioDiskstream::find_and_use_playlist (const string& name)
291 AudioPlaylist* playlist;
293 if ((pl = _session.playlist_by_name (name)) == 0) {
294 playlist = new AudioPlaylist(_session, name);
298 if ((playlist = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
299 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
303 return use_playlist (playlist);
307 AudioDiskstream::use_playlist (Playlist* playlist)
309 assert(dynamic_cast<AudioPlaylist*>(playlist));
311 Diskstream::use_playlist(playlist);
317 AudioDiskstream::use_new_playlist ()
320 AudioPlaylist* playlist;
322 if (!in_set_state && destructive()) {
327 newname = Playlist::bump_name (_playlist->name(), _session);
329 newname = Playlist::bump_name (_name, _session);
332 if ((playlist = new AudioPlaylist (_session, newname, hidden())) != 0) {
333 playlist->set_orig_diskstream_id (id());
334 return use_playlist (playlist);
341 AudioDiskstream::use_copy_playlist ()
343 assert(audio_playlist());
349 if (_playlist == 0) {
350 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
355 AudioPlaylist* playlist;
357 newname = Playlist::bump_name (_playlist->name(), _session);
359 if ((playlist = new AudioPlaylist (*audio_playlist(), newname)) != 0) {
360 playlist->set_orig_diskstream_id (id());
361 return use_playlist (playlist);
368 AudioDiskstream::setup_destructive_playlist ()
372 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
373 srcs.push_back ((*chan).write_source);
376 /* a single full-sized region */
378 cerr << "setup DS using " << srcs.front()->natural_position () << endl;
380 boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, max_frames, _name));
381 _playlist->add_region (region, srcs.front()->natural_position());
385 AudioDiskstream::use_destructive_playlist ()
387 /* use the sources associated with the single full-extent region */
389 Playlist::RegionList* rl = _playlist->regions_at (0);
392 reset_write_sources (false, true);
396 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rl->front());
399 throw failed_constructor();
405 ChannelList::iterator chan;
407 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
408 (*chan).write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
409 assert((*chan).write_source);
410 (*chan).write_source->set_allow_remove_if_empty (false);
413 /* the source list will never be reset for a destructive track */
417 AudioDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record)
419 int possibly_recording;
422 const int transport_rolling = 0x4;
423 const int track_rec_enabled = 0x2;
424 const int global_rec_enabled = 0x1;
426 /* merge together the 3 factors that affect record status, and compute
430 rolling = _session.transport_speed() != 0.0f;
431 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
432 change = possibly_recording ^ last_possibly_recording;
434 if (possibly_recording == last_possibly_recording) {
440 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
442 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
443 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
445 /* starting to record: compute first+last frames */
447 first_recordable_frame = transport_frame + _capture_offset;
448 last_recordable_frame = max_frames;
449 capture_start_frame = transport_frame;
451 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
453 /* was stopped, now rolling (and recording) */
455 if (_alignment_style == ExistingMaterial) {
456 first_recordable_frame += _session.worst_output_latency();
458 first_recordable_frame += _roll_delay;
463 /* was rolling, but record state changed */
465 if (_alignment_style == ExistingMaterial) {
468 if (!_session.get_punch_in()) {
470 /* manual punch in happens at the correct transport frame
471 because the user hit a button. but to get alignment correct
472 we have to back up the position of the new region to the
473 appropriate spot given the roll delay.
476 capture_start_frame -= _roll_delay;
478 /* XXX paul notes (august 2005): i don't know why
482 first_recordable_frame += _capture_offset;
486 /* autopunch toggles recording at the precise
487 transport frame, and then the DS waits
488 to start recording for a time that depends
489 on the output latency.
492 first_recordable_frame += _session.worst_output_latency();
497 if (_session.get_punch_in()) {
498 first_recordable_frame += _roll_delay;
500 capture_start_frame -= _roll_delay;
506 if (_flags & Recordable) {
507 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
509 RingBufferNPT<CaptureTransition>::rw_vector transvec;
510 (*chan).capture_transition_buf->get_write_vector(&transvec);
512 if (transvec.len[0] > 0) {
513 transvec.buf[0]->type = CaptureStart;
514 transvec.buf[0]->capture_val = capture_start_frame;
515 (*chan).capture_transition_buf->increment_write_ptr(1);
519 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
525 } else if (!record_enabled() || !can_record) {
529 last_recordable_frame = transport_frame + _capture_offset;
531 if (_alignment_style == ExistingMaterial) {
532 last_recordable_frame += _session.worst_output_latency();
534 last_recordable_frame += _roll_delay;
538 last_possibly_recording = possibly_recording;
542 AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input)
545 ChannelList::iterator c;
547 jack_nframes_t rec_offset = 0;
548 jack_nframes_t rec_nframes = 0;
549 bool nominally_recording;
550 bool re = record_enabled ();
551 bool collect_playback = false;
553 /* if we've already processed the frames corresponding to this call,
554 just return. this allows multiple routes that are taking input
555 from this diskstream to call our ::process() method, but have
556 this stuff only happen once. more commonly, it allows both
557 the AudioTrack that is using this AudioDiskstream *and* the Session
558 to call process() without problems.
565 check_record_status (transport_frame, nframes, can_record);
567 nominally_recording = (can_record && re);
574 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
575 must always be called as a pair. The only exception is if this function
576 returns a non-zero value, in which case, ::commit should not be called.
579 // If we can't take the state lock return.
580 if (!state_lock.trylock()) {
584 adjust_capture_position = 0;
586 for (c = channels.begin(); c != channels.end(); ++c) {
587 (*c).current_capture_buffer = 0;
588 (*c).current_playback_buffer = 0;
591 if (nominally_recording || (_session.get_record_enabled() && _session.get_punch_in())) {
594 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
601 case OverlapInternal:
602 /* ---------- recrange
605 rec_nframes = nframes;
610 /* |--------| recrange
613 rec_nframes = transport_frame + nframes - first_recordable_frame;
615 rec_offset = first_recordable_frame - transport_frame;
620 /* |--------| recrange
623 rec_nframes = last_recordable_frame - transport_frame;
627 case OverlapExternal:
628 /* |--------| recrange
629 -------------- transrange
631 rec_nframes = last_recordable_frame - last_recordable_frame;
632 rec_offset = first_recordable_frame - transport_frame;
636 if (rec_nframes && !was_recording) {
637 capture_captured = 0;
638 was_recording = true;
643 if (can_record && !_last_capture_regions.empty()) {
644 _last_capture_regions.clear ();
647 if (nominally_recording || rec_nframes) {
649 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
651 ChannelInfo& chan (*c);
653 chan.capture_buf->get_write_vector (&chan.capture_vector);
655 if (rec_nframes <= chan.capture_vector.len[0]) {
657 chan.current_capture_buffer = chan.capture_vector.buf[0];
659 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
663 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
667 jack_nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
669 if (rec_nframes > total) {
674 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
675 jack_nframes_t first = chan.capture_vector.len[0];
677 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
678 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
679 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
680 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
682 chan.current_capture_buffer = chan.capture_wrap_buffer;
689 finish_capture (rec_monitors_input);
696 /* data will be written to disk */
698 if (rec_nframes == nframes && rec_offset == 0) {
700 for (c = channels.begin(); c != channels.end(); ++c) {
701 (*c).current_playback_buffer = (*c).current_capture_buffer;
704 playback_distance = nframes;
709 /* we can't use the capture buffer as the playback buffer, because
710 we recorded only a part of the current process' cycle data
714 collect_playback = true;
717 adjust_capture_position = rec_nframes;
719 } else if (nominally_recording) {
721 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
723 for (c = channels.begin(); c != channels.end(); ++c) {
724 (*c).current_playback_buffer = (*c).current_capture_buffer;
727 playback_distance = nframes;
731 collect_playback = true;
734 if (collect_playback) {
736 /* we're doing playback */
738 jack_nframes_t necessary_samples;
740 /* no varispeed playback if we're recording, because the output .... TBD */
742 if (rec_nframes == 0 && _actual_speed != 1.0f) {
743 necessary_samples = (jack_nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
745 necessary_samples = nframes;
748 for (c = channels.begin(); c != channels.end(); ++c) {
749 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
754 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
756 ChannelInfo& chan (*c);
758 if (necessary_samples <= chan.playback_vector.len[0]) {
760 chan.current_playback_buffer = chan.playback_vector.buf[0];
763 jack_nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
765 if (necessary_samples > total) {
771 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
772 chan.playback_vector.len[0] * sizeof (Sample));
773 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
774 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
776 chan.current_playback_buffer = chan.playback_wrap_buffer;
781 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
783 uint64_t phase = last_phase;
784 jack_nframes_t i = 0;
786 // Linearly interpolate into the alt buffer
787 // using 40.24 fixp maths (swh)
789 for (c = channels.begin(); c != channels.end(); ++c) {
792 ChannelInfo& chan (*c);
797 for (jack_nframes_t outsample = 0; outsample < nframes; ++outsample) {
799 fr = (phase & 0xFFFFFF) / 16777216.0f;
800 chan.speed_buffer[outsample] =
801 chan.current_playback_buffer[i] * (1.0f - fr) +
802 chan.current_playback_buffer[i+1] * fr;
806 chan.current_playback_buffer = chan.speed_buffer;
809 playback_distance = i + 1;
810 last_phase = (phase & 0xFFFFFF);
813 playback_distance = nframes;
825 /* we're exiting with failure, so ::commit will not
826 be called. unlock the state lock.
836 AudioDiskstream::commit (jack_nframes_t nframes)
838 bool need_butler = false;
840 if (_actual_speed < 0.0) {
841 playback_sample -= playback_distance;
843 playback_sample += playback_distance;
846 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
848 (*chan).playback_buf->increment_read_ptr (playback_distance);
850 if (adjust_capture_position) {
851 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
855 if (adjust_capture_position != 0) {
856 capture_captured += adjust_capture_position;
857 adjust_capture_position = 0;
861 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
863 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
864 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
875 AudioDiskstream::set_pending_overwrite (bool yn)
877 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
879 pending_overwrite = yn;
881 overwrite_frame = playback_sample;
882 overwrite_offset = channels.front().playback_buf->get_read_ptr();
886 AudioDiskstream::overwrite_existing_buffers ()
888 Sample* mixdown_buffer;
891 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
893 overwrite_queued = false;
895 /* assume all are the same size */
896 jack_nframes_t size = channels[0].playback_buf->bufsize();
898 mixdown_buffer = new Sample[size];
899 gain_buffer = new float[size];
901 /* reduce size so that we can fill the buffer correctly. */
905 jack_nframes_t start;
907 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
909 start = overwrite_frame;
910 jack_nframes_t cnt = size;
912 /* to fill the buffer without resetting the playback sample, we need to
913 do it one or two chunks (normally two).
915 |----------------------------------------------------------------------|
919 |<- second chunk->||<----------------- first chunk ------------------>|
923 jack_nframes_t to_read = size - overwrite_offset;
925 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, *chan, n, reversed)) {
926 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
927 _id, size, playback_sample) << endmsg;
935 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer,
936 start, cnt, *chan, n, reversed)) {
937 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
938 _id, size, playback_sample) << endmsg;
947 pending_overwrite = false;
948 delete [] gain_buffer;
949 delete [] mixdown_buffer;
954 AudioDiskstream::seek (jack_nframes_t frame, bool complete_refill)
956 Glib::Mutex::Lock lm (state_lock);
959 ChannelList::iterator chan;
961 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
962 (*chan).playback_buf->reset ();
963 (*chan).capture_buf->reset ();
966 /* can't rec-enable in destructive mode if transport is before start */
968 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
969 disengage_record_enable ();
972 playback_sample = frame;
975 if (complete_refill) {
976 while ((ret = do_refill_with_alloc ()) > 0) ;
978 ret = do_refill_with_alloc ();
985 AudioDiskstream::can_internal_playback_seek (jack_nframes_t distance)
987 ChannelList::iterator chan;
989 for (chan = channels.begin(); chan != channels.end(); ++chan) {
990 if ((*chan).playback_buf->read_space() < distance) {
998 AudioDiskstream::internal_playback_seek (jack_nframes_t distance)
1000 ChannelList::iterator chan;
1002 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1003 (*chan).playback_buf->increment_read_ptr (distance);
1006 first_recordable_frame += distance;
1007 playback_sample += distance;
1013 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, jack_nframes_t& start, jack_nframes_t cnt,
1014 ChannelInfo& channel_info, int channel, bool reversed)
1016 jack_nframes_t this_read = 0;
1017 bool reloop = false;
1018 jack_nframes_t loop_end = 0;
1019 jack_nframes_t loop_start = 0;
1020 jack_nframes_t loop_length = 0;
1021 jack_nframes_t offset = 0;
1025 /* Make the use of a Location atomic for this read operation.
1027 Note: Locations don't get deleted, so all we care about
1028 when I say "atomic" is that we are always pointing to
1029 the same one and using a start/length values obtained
1033 if ((loc = loop_location) != 0) {
1034 loop_start = loc->start();
1035 loop_end = loc->end();
1036 loop_length = loop_end - loop_start;
1039 /* if we are looping, ensure that the first frame we read is at the correct
1040 position within the loop.
1043 if (loc && start >= loop_end) {
1044 //cerr << "start adjusted from " << start;
1045 start = loop_start + ((start - loop_start) % loop_length);
1046 //cerr << "to " << start << endl;
1048 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1053 /* take any loop into account. we can't read past the end of the loop. */
1055 if (loc && (loop_end - start < cnt)) {
1056 this_read = loop_end - start;
1057 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1064 if (this_read == 0) {
1068 this_read = min(cnt,this_read);
1070 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1071 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1076 _read_data_count = _playlist->read_data_count();
1080 /* don't adjust start, since caller has already done that
1083 swap_by_ptr (buf, buf + this_read - 1);
1087 /* if we read to the end of the loop, go back to the beginning */
1097 offset += this_read;
1104 AudioDiskstream::do_refill_with_alloc()
1106 Sample* mix_buf = new Sample[disk_io_chunk_frames];
1107 float* gain_buf = new float[disk_io_chunk_frames];
1109 int ret = _do_refill(mix_buf, gain_buf);
1118 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
1121 jack_nframes_t to_read;
1122 RingBufferNPT<Sample>::rw_vector vector;
1123 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1124 jack_nframes_t total_space;
1125 jack_nframes_t zero_fill;
1127 ChannelList::iterator i;
1130 assert(mixdown_buffer);
1131 assert(gain_buffer);
1133 channels.front().playback_buf->get_write_vector (&vector);
1135 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1139 /* if there are 2+ chunks of disk i/o possible for
1140 this track, let the caller know so that it can arrange
1141 for us to be called again, ASAP.
1144 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1148 /* if we're running close to normal speed and there isn't enough
1149 space to do disk_io_chunk_frames of I/O, then don't bother.
1151 at higher speeds, just do it because the sync between butler
1152 and audio thread may not be good enough.
1155 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1159 /* when slaved, don't try to get too close to the read pointer. this
1160 leaves space for the buffer reversal to have something useful to
1164 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1168 total_space = min (disk_io_chunk_frames, total_space);
1172 if (file_frame == 0) {
1174 /* at start: nothing to do but fill with silence */
1176 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1178 ChannelInfo& chan (*i);
1179 chan.playback_buf->get_write_vector (&vector);
1180 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1181 if (vector.len[1]) {
1182 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1184 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1189 if (file_frame < total_space) {
1191 /* too close to the start: read what we can,
1192 and then zero fill the rest
1195 zero_fill = total_space - file_frame;
1196 total_space = file_frame;
1201 /* move read position backwards because we are going
1202 to reverse the data.
1205 file_frame -= total_space;
1211 if (file_frame == max_frames) {
1213 /* at end: nothing to do but fill with silence */
1215 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1217 ChannelInfo& chan (*i);
1218 chan.playback_buf->get_write_vector (&vector);
1219 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1220 if (vector.len[1]) {
1221 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1223 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1228 if (file_frame > max_frames - total_space) {
1230 /* to close to the end: read what we can, and zero fill the rest */
1232 zero_fill = total_space - (max_frames - file_frame);
1233 total_space = max_frames - file_frame;
1240 jack_nframes_t file_frame_tmp = 0;
1242 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1244 ChannelInfo& chan (*i);
1247 jack_nframes_t len1, len2;
1249 chan.playback_buf->get_write_vector (&vector);
1252 file_frame_tmp = file_frame;
1255 buf1 = vector.buf[1];
1256 len1 = vector.len[1];
1257 buf2 = vector.buf[0];
1258 len2 = vector.len[0];
1260 buf1 = vector.buf[0];
1261 len1 = vector.len[0];
1262 buf2 = vector.buf[1];
1263 len2 = vector.len[1];
1267 to_read = min (ts, len1);
1268 to_read = min (to_read, disk_io_chunk_frames);
1272 if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1277 chan.playback_buf->increment_write_ptr (to_read);
1281 to_read = min (ts, len2);
1286 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1287 so read some or all of vector.len[1] as well.
1290 if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1295 chan.playback_buf->increment_write_ptr (to_read);
1304 file_frame = file_frame_tmp;
1311 /** Flush pending data to disk.
1313 * Important note: this function will write *AT MOST* disk_io_chunk_frames
1314 * of data to disk. it will never write more than that. If it writes that
1315 * much and there is more than that waiting to be written, it will return 1,
1316 * otherwise 0 on success or -1 on failure.
1318 * If there is less than disk_io_chunk_frames to be written, no data will be
1319 * written at all unless @a force_flush is true.
1322 AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
1326 RingBufferNPT<Sample>::rw_vector vector;
1327 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1328 jack_nframes_t total;
1330 _write_data_count = 0;
1332 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1334 (*chan).capture_buf->get_read_vector (&vector);
1336 total = vector.len[0] + vector.len[1];
1339 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1343 /* if there are 2+ chunks of disk i/o possible for
1344 this track, let the caller know so that it can arrange
1345 for us to be called again, ASAP.
1347 if we are forcing a flush, then if there is* any* extra
1348 work, let the caller know.
1350 if we are no longer recording and there is any extra work,
1351 let the caller know too.
1354 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1358 to_write = min (disk_io_chunk_frames, (jack_nframes_t) vector.len[0]);
1360 // check the transition buffer when recording destructive
1361 // important that we get this after the capture buf
1363 if (destructive()) {
1364 (*chan).capture_transition_buf->get_read_vector(&transvec);
1365 size_t transcount = transvec.len[0] + transvec.len[1];
1366 bool have_start = false;
1369 for (ti=0; ti < transcount; ++ti) {
1370 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1372 if (captrans.type == CaptureStart) {
1373 // by definition, the first data we got above represents the given capture pos
1375 (*chan).write_source->mark_capture_start (captrans.capture_val);
1376 (*chan).curr_capture_cnt = 0;
1380 else if (captrans.type == CaptureEnd) {
1382 // capture end, the capture_val represents total frames in capture
1384 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1386 // shorten to make the write a perfect fit
1387 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1389 if (nto_write < to_write) {
1390 ret = 1; // should we?
1392 to_write = nto_write;
1394 (*chan).write_source->mark_capture_end ();
1396 // increment past this transition, but go no further
1401 // actually ends just beyond this chunk, so force more work
1409 (*chan).capture_transition_buf->increment_read_ptr(ti);
1413 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write) != to_write) {
1414 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1418 (*chan).capture_buf->increment_read_ptr (to_write);
1419 (*chan).curr_capture_cnt += to_write;
1421 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1423 /* we wrote all of vector.len[0] but it wasn't an entire
1424 disk_io_chunk_frames of data, so arrange for some part
1425 of vector.len[1] to be flushed to disk as well.
1428 to_write = min ((jack_nframes_t)(disk_io_chunk_frames - to_write), (jack_nframes_t) vector.len[1]);
1430 if ((*chan).write_source->write (vector.buf[1], to_write) != to_write) {
1431 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1435 _write_data_count += (*chan).write_source->write_data_count();
1437 (*chan).capture_buf->increment_read_ptr (to_write);
1438 (*chan).curr_capture_cnt += to_write;
1447 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1449 uint32_t buffer_position;
1450 bool more_work = true;
1452 boost::shared_ptr<AudioRegion> region;
1453 jack_nframes_t total_capture;
1455 SourceList::iterator src;
1456 ChannelList::iterator chan;
1457 vector<CaptureInfo*>::iterator ci;
1459 bool mark_write_completed = false;
1461 finish_capture (true);
1463 /* butler is already stopped, but there may be work to do
1464 to flush remaining data to disk.
1467 while (more_work && !err) {
1468 switch (do_flush (Session::TransportContext, true)) {
1475 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1480 /* XXX is there anything we can do if err != 0 ? */
1481 Glib::Mutex::Lock lm (capture_info_lock);
1483 if (capture_info.empty()) {
1487 if (abort_capture) {
1489 ChannelList::iterator chan;
1491 list<boost::shared_ptr<Source> >* deletion_list = new list<boost::shared_ptr<Source> >;
1493 for ( chan = channels.begin(); chan != channels.end(); ++chan) {
1495 if ((*chan).write_source) {
1497 (*chan).write_source->mark_for_remove ();
1499 deletion_list->push_back ((*chan).write_source);
1501 (*chan).write_source.reset ();
1504 /* new source set up in "out" below */
1507 if (!deletion_list->empty()) {
1508 DeleteSources (deletion_list);
1510 delete deletion_list;
1516 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1517 total_capture += (*ci)->frames;
1520 /* figure out the name for this take */
1522 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1524 boost::shared_ptr<AudioFileSource> s = (*chan).write_source;
1528 s->update_header (capture_info.front()->start, when, twhen);
1529 s->set_captured_for (_name);
1534 /* destructive tracks have a single, never changing region */
1536 if (destructive()) {
1538 /* send a signal that any UI can pick up to do the right thing. there is
1539 a small problem here in that a UI may need the peak data to be ready
1540 for the data that was recorded and this isn't interlocked with that
1541 process. this problem is deferred to the UI.
1544 _playlist->Modified();
1548 /* Register a new region with the Session that
1549 describes the entire source. Do this first
1550 so that any sub-regions will obviously be
1551 children of this one (later!)
1555 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1556 region_name_from_path (channels[0].write_source->name()),
1557 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
1559 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1560 region->special_set_position (capture_info.front()->start);
1564 catch (failed_constructor& err) {
1565 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1569 _last_capture_regions.push_back (region);
1571 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1573 XMLNode &before = _playlist->get_state();
1574 _playlist->freeze ();
1576 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1579 _session.region_name (region_name, channels[0].write_source->name(), false);
1581 cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
1584 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1585 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1588 catch (failed_constructor& err) {
1589 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1590 continue; /* XXX is this OK? */
1593 _last_capture_regions.push_back (region);
1595 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1597 i_am_the_modifier++;
1598 _playlist->add_region (region, (*ci)->start);
1599 i_am_the_modifier--;
1601 buffer_position += (*ci)->frames;
1605 XMLNode &after = _playlist->get_state();
1606 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1609 mark_write_completed = true;
1611 reset_write_sources (mark_write_completed);
1614 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1618 capture_info.clear ();
1619 capture_start_frame = 0;
1623 AudioDiskstream::finish_capture (bool rec_monitors_input)
1625 was_recording = false;
1627 if (capture_captured == 0) {
1631 if (recordable() && destructive()) {
1632 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1634 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1635 (*chan).capture_transition_buf->get_write_vector(&transvec);
1638 if (transvec.len[0] > 0) {
1639 transvec.buf[0]->type = CaptureEnd;
1640 transvec.buf[0]->capture_val = capture_captured;
1641 (*chan).capture_transition_buf->increment_write_ptr(1);
1645 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1651 CaptureInfo* ci = new CaptureInfo;
1653 ci->start = capture_start_frame;
1654 ci->frames = capture_captured;
1656 /* XXX theoretical race condition here. Need atomic exchange ?
1657 However, the circumstances when this is called right
1658 now (either on record-disable or transport_stopped)
1659 mean that no actual race exists. I think ...
1660 We now have a capture_info_lock, but it is only to be used
1661 to synchronize in the transport_stop and the capture info
1662 accessors, so that invalidation will not occur (both non-realtime).
1665 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1667 capture_info.push_back (ci);
1668 capture_captured = 0;
1672 AudioDiskstream::set_record_enabled (bool yn)
1674 if (!recordable() || !_session.record_enabling_legal()) {
1678 /* can't rec-enable in destructive mode if transport is before start */
1680 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1684 if (yn && channels[0].source == 0) {
1686 /* pick up connections not initiated *from* the IO object
1687 we're associated with.
1690 get_input_sources ();
1693 /* yes, i know that this not proof against race conditions, but its
1694 good enough. i think.
1697 if (record_enabled() != yn) {
1699 engage_record_enable ();
1701 disengage_record_enable ();
1707 AudioDiskstream::engage_record_enable ()
1709 bool rolling = _session.transport_speed() != 0.0f;
1711 g_atomic_int_set (&_record_enabled, 1);
1712 capturing_sources.clear ();
1713 if (Config->get_use_hardware_monitoring()) {
1714 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1715 if ((*chan).source) {
1716 (*chan).source->ensure_monitor_input (!(_session.get_auto_input() && rolling));
1718 capturing_sources.push_back ((*chan).write_source);
1721 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1722 capturing_sources.push_back ((*chan).write_source);
1726 RecordEnableChanged (); /* EMIT SIGNAL */
1730 AudioDiskstream::disengage_record_enable ()
1732 g_atomic_int_set (&_record_enabled, 0);
1733 if (Config->get_use_hardware_monitoring()) {
1734 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1735 if ((*chan).source) {
1736 (*chan).source->ensure_monitor_input (false);
1740 capturing_sources.clear ();
1741 RecordEnableChanged (); /* EMIT SIGNAL */
1746 AudioDiskstream::get_state ()
1748 XMLNode* node = new XMLNode ("AudioDiskstream");
1750 LocaleGuard lg (X_("POSIX"));
1752 snprintf (buf, sizeof(buf), "0x%x", _flags);
1753 node->add_property ("flags", buf);
1755 snprintf (buf, sizeof(buf), "%zd", channels.size());
1756 node->add_property ("channels", buf);
1758 node->add_property ("playlist", _playlist->name());
1760 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1761 node->add_property ("speed", buf);
1763 node->add_property("name", _name);
1765 node->add_property("id", buf);
1767 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1769 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1770 XMLNode* cs_grandchild;
1772 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1773 cs_grandchild = new XMLNode (X_("file"));
1774 cs_grandchild->add_property (X_("path"), (*i)->path());
1775 cs_child->add_child_nocopy (*cs_grandchild);
1778 /* store the location where capture will start */
1782 if (_session.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1783 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1785 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1788 cs_child->add_property (X_("at"), buf);
1789 node->add_child_nocopy (*cs_child);
1793 node->add_child_copy (*_extra_xml);
1800 AudioDiskstream::set_state (const XMLNode& node)
1802 const XMLProperty* prop;
1803 XMLNodeList nlist = node.children();
1804 XMLNodeIterator niter;
1805 uint32_t nchans = 1;
1806 XMLNode* capture_pending_node = 0;
1807 LocaleGuard lg (X_("POSIX"));
1809 in_set_state = true;
1811 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1812 if ((*niter)->name() == IO::state_node_name) {
1813 deprecated_io_node = new XMLNode (**niter);
1816 if ((*niter)->name() == X_("CapturingSources")) {
1817 capture_pending_node = *niter;
1821 /* prevent write sources from being created */
1823 in_set_state = true;
1825 if ((prop = node.property ("name")) != 0) {
1826 _name = prop->value();
1829 if (deprecated_io_node) {
1830 if ((prop = deprecated_io_node->property ("id")) != 0) {
1831 _id = prop->value ();
1834 if ((prop = node.property ("id")) != 0) {
1835 _id = prop->value ();
1839 if ((prop = node.property ("flags")) != 0) {
1840 _flags = strtol (prop->value().c_str(), 0, 0);
1843 if ((prop = node.property ("channels")) != 0) {
1844 nchans = atoi (prop->value().c_str());
1847 // create necessary extra channels
1848 // we are always constructed with one and we always need one
1850 if (nchans > _n_channels) {
1852 // we need to add new channel infos
1853 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1855 int diff = nchans - channels.size();
1857 for (int i=0; i < diff; ++i) {
1861 } else if (nchans < _n_channels) {
1863 // we need to get rid of channels
1864 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1866 int diff = channels.size() - nchans;
1868 for (int i = 0; i < diff; ++i) {
1873 if ((prop = node.property ("playlist")) == 0) {
1878 bool had_playlist = (_playlist != 0);
1880 if (find_and_use_playlist (prop->value())) {
1884 if (!had_playlist) {
1885 _playlist->set_orig_diskstream_id (_id);
1888 if (!destructive() && capture_pending_node) {
1889 /* destructive streams have one and only one source per channel,
1890 and so they never end up in pending capture in any useful
1893 use_pending_capture_data (*capture_pending_node);
1898 if ((prop = node.property ("speed")) != 0) {
1899 double sp = atof (prop->value().c_str());
1901 if (realtime_set_speed (sp, false)) {
1902 non_realtime_set_speed ();
1906 _n_channels = channels.size();
1908 in_set_state = false;
1910 /* make sure this is clear before we do anything else */
1912 capturing_sources.clear ();
1914 /* write sources are handled when we handle the input set
1915 up of the IO that owns this DS (::non_realtime_input_change())
1918 in_set_state = false;
1924 AudioDiskstream::use_new_write_source (uint32_t n)
1926 if (!recordable()) {
1930 if (n >= channels.size()) {
1931 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1935 ChannelInfo &chan = channels[n];
1937 if (chan.write_source) {
1939 if (AudioFileSource::is_empty (chan.write_source->path())) {
1940 chan.write_source->mark_for_remove ();
1941 chan.write_source.reset ();
1943 chan.write_source.reset ();
1948 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
1949 throw failed_constructor();
1953 catch (failed_constructor &err) {
1954 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1955 chan.write_source.reset ();
1959 /* do not remove destructive files even if they are empty */
1961 chan.write_source->set_allow_remove_if_empty (!destructive());
1967 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1969 ChannelList::iterator chan;
1972 if (!recordable()) {
1976 capturing_sources.clear ();
1978 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
1979 if (!destructive()) {
1981 if ((*chan).write_source && mark_write_complete) {
1982 (*chan).write_source->mark_streaming_write_completed ();
1984 use_new_write_source (n);
1986 if (record_enabled()) {
1987 capturing_sources.push_back ((*chan).write_source);
1991 if ((*chan).write_source == 0) {
1992 use_new_write_source (n);
1997 if (destructive()) {
1999 /* we now have all our write sources set up, so create the
2000 playlist's single region.
2003 if (_playlist->empty()) {
2004 setup_destructive_playlist ();
2010 AudioDiskstream::rename_write_sources ()
2012 ChannelList::iterator chan;
2015 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2016 if ((*chan).write_source != 0) {
2017 (*chan).write_source->set_name (_name, destructive());
2018 /* XXX what to do if one of them fails ? */
2026 AudioDiskstream::set_block_size (jack_nframes_t nframes)
2028 if (_session.get_block_size() > speed_buffer_size) {
2029 speed_buffer_size = _session.get_block_size();
2031 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2032 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2033 (*chan).speed_buffer = new Sample[speed_buffer_size];
2036 allocate_temporary_buffers ();
2040 AudioDiskstream::allocate_temporary_buffers ()
2042 /* make sure the wrap buffer is at least large enough to deal
2043 with the speeds up to 1.2, to allow for micro-variation
2044 when slaving to MTC, SMPTE etc.
2047 double sp = max (fabsf (_actual_speed), 1.2f);
2048 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * sp) + 1;
2050 if (required_wrap_size > wrap_buffer_size) {
2052 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2053 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2054 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2055 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2056 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2059 wrap_buffer_size = required_wrap_size;
2064 AudioDiskstream::monitor_input (bool yn)
2066 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2068 if ((*chan).source) {
2069 (*chan).source->ensure_monitor_input (yn);
2075 AudioDiskstream::set_align_style_from_io ()
2077 bool have_physical = false;
2083 get_input_sources ();
2085 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2086 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2087 have_physical = true;
2092 if (have_physical) {
2093 set_align_style (ExistingMaterial);
2095 set_align_style (CaptureTime);
2100 AudioDiskstream::add_channel ()
2102 /* XXX need to take lock??? */
2106 init_channel (chan);
2108 chan.speed_buffer = new Sample[speed_buffer_size];
2109 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2110 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2112 channels.push_back (chan);
2114 _n_channels = channels.size();
2120 AudioDiskstream::remove_channel ()
2122 if (channels.size() > 1) {
2123 /* XXX need to take lock??? */
2124 ChannelInfo & chan = channels.back();
2125 destroy_channel (chan);
2126 channels.pop_back();
2128 _n_channels = channels.size();
2136 AudioDiskstream::playback_buffer_load () const
2138 return (float) ((double) channels.front().playback_buf->read_space()/
2139 (double) channels.front().playback_buf->bufsize());
2143 AudioDiskstream::capture_buffer_load () const
2145 return (float) ((double) channels.front().capture_buf->write_space()/
2146 (double) channels.front().capture_buf->bufsize());
2150 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2152 const XMLProperty* prop;
2153 XMLNodeList nlist = node.children();
2154 XMLNodeIterator niter;
2155 boost::shared_ptr<AudioFileSource> fs;
2156 boost::shared_ptr<AudioFileSource> first_fs;
2157 SourceList pending_sources;
2158 jack_nframes_t position;
2160 if ((prop = node.property (X_("at"))) == 0) {
2164 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2168 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2169 if ((*niter)->name() == X_("file")) {
2171 if ((prop = (*niter)->property (X_("path"))) == 0) {
2176 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (prop->value(), false, _session.frame_rate()));
2179 catch (failed_constructor& err) {
2180 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2181 _name, prop->value())
2186 pending_sources.push_back (fs);
2188 if (first_fs == 0) {
2192 fs->set_captured_for (_name);
2196 if (pending_sources.size() == 0) {
2197 /* nothing can be done */
2201 if (pending_sources.size() != _n_channels) {
2202 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2207 boost::shared_ptr<AudioRegion> region;
2210 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(),
2211 region_name_from_path (first_fs->name()),
2212 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
2213 region->special_set_position (0);
2216 catch (failed_constructor& err) {
2217 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2225 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name())));
2228 catch (failed_constructor& err) {
2229 error << string_compose (_("%1: cannot create region from pending capture sources"),
2236 _playlist->add_region (region, position);