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/audioplaylist.h>
48 #include <ardour/cycle_timer.h>
49 #include <ardour/audioregion.h>
55 using namespace ARDOUR;
58 size_t AudioDiskstream::_working_buffers_size = 0;
59 Sample* AudioDiskstream::_mixdown_buffer = 0;
60 gain_t* AudioDiskstream::_gain_buffer = 0;
62 AudioDiskstream::AudioDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
63 : Diskstream(sess, name, flag)
64 , deprecated_io_node(NULL)
66 /* prevent any write sources from being created */
75 DiskstreamCreated (this); /* EMIT SIGNAL */
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 ();
96 DiskstreamCreated (this); /* EMIT SIGNAL */
100 AudioDiskstream::init_channel (ChannelInfo &chan)
102 chan.playback_wrap_buffer = 0;
103 chan.capture_wrap_buffer = 0;
104 chan.speed_buffer = 0;
105 chan.peak_power = 0.0f;
106 chan.write_source = 0;
108 chan.current_capture_buffer = 0;
109 chan.current_playback_buffer = 0;
110 chan.curr_capture_cnt = 0;
112 chan.playback_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
113 chan.capture_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
114 chan.capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
117 /* touch the ringbuffer buffers, which will cause
118 them to be mapped into locked physical RAM if
119 we're running with mlockall(). this doesn't do
122 memset (chan.playback_buf->buffer(), 0, sizeof (Sample) * chan.playback_buf->bufsize());
123 memset (chan.capture_buf->buffer(), 0, sizeof (Sample) * chan.capture_buf->bufsize());
124 memset (chan.capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * chan.capture_transition_buf->bufsize());
129 AudioDiskstream::init (Diskstream::Flag f)
133 /* there are no channels at this point, so these
134 two calls just get speed_buffer_size and wrap_buffer
135 size setup without duplicating their code.
138 set_block_size (_session.get_block_size());
139 allocate_temporary_buffers ();
142 assert(_n_channels == 1);
146 AudioDiskstream::destroy_channel (ChannelInfo &chan)
148 if (chan.write_source) {
149 chan.write_source->release ();
150 chan.write_source = 0;
153 if (chan.speed_buffer) {
154 delete [] chan.speed_buffer;
157 if (chan.playback_wrap_buffer) {
158 delete [] chan.playback_wrap_buffer;
160 if (chan.capture_wrap_buffer) {
161 delete [] chan.capture_wrap_buffer;
164 delete chan.playback_buf;
165 delete chan.capture_buf;
166 delete chan.capture_transition_buf;
168 chan.playback_buf = 0;
169 chan.capture_buf = 0;
172 AudioDiskstream::~AudioDiskstream ()
174 Glib::Mutex::Lock lm (state_lock);
176 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan)
177 destroy_channel((*chan));
183 AudioDiskstream::allocate_working_buffers()
185 assert(disk_io_frames() > 0);
187 _working_buffers_size = disk_io_frames();
188 _mixdown_buffer = new Sample[_working_buffers_size];
189 _gain_buffer = new gain_t[_working_buffers_size];
193 AudioDiskstream::free_working_buffers()
195 delete _mixdown_buffer;
197 _working_buffers_size = 0;
203 AudioDiskstream::non_realtime_input_change ()
206 Glib::Mutex::Lock lm (state_lock);
208 if (input_change_pending == NoChange) {
212 if (input_change_pending & ConfigurationChanged) {
214 if (_io->n_inputs() > _n_channels) {
216 // we need to add new channel infos
218 int diff = _io->n_inputs() - channels.size();
220 for (int i = 0; i < diff; ++i) {
224 } else if (_io->n_inputs() < _n_channels) {
226 // we need to get rid of channels
228 int diff = channels.size() - _io->n_inputs();
230 for (int i = 0; i < diff; ++i) {
236 get_input_sources ();
237 set_capture_offset ();
239 if (first_input_change) {
240 set_align_style (_persistent_alignment_style);
241 first_input_change = false;
243 set_align_style_from_io ();
246 input_change_pending = NoChange;
249 /* reset capture files */
251 reset_write_sources (false);
253 /* now refill channel buffers */
255 if (speed() != 1.0f || speed() != -1.0f) {
256 seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()));
259 seek (_session.transport_frame());
264 AudioDiskstream::get_input_sources ()
266 uint32_t ni = _io->n_inputs();
268 for (uint32_t n = 0; n < ni; ++n) {
270 const char **connections = _io->input(n)->get_connections ();
271 ChannelInfo& chan = channels[n];
273 if (connections == 0 || connections[0] == 0) {
276 // _source->disable_metering ();
282 chan.source = _session.engine().get_port_by_name (connections[0]);
292 AudioDiskstream::find_and_use_playlist (const string& name)
295 AudioPlaylist* playlist;
297 if ((pl = _session.playlist_by_name (name)) == 0) {
298 playlist = new AudioPlaylist(_session, name);
302 if ((playlist = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
303 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
307 return use_playlist (playlist);
311 AudioDiskstream::use_playlist (Playlist* playlist)
313 assert(dynamic_cast<AudioPlaylist*>(playlist));
315 Diskstream::use_playlist(playlist);
321 AudioDiskstream::use_new_playlist ()
324 AudioPlaylist* playlist;
326 if (!in_set_state && destructive()) {
331 newname = Playlist::bump_name (_playlist->name(), _session);
333 newname = Playlist::bump_name (_name, _session);
336 if ((playlist = new AudioPlaylist (_session, newname, hidden())) != 0) {
337 playlist->set_orig_diskstream_id (id());
338 return use_playlist (playlist);
345 AudioDiskstream::use_copy_playlist ()
347 assert(audio_playlist());
353 if (_playlist == 0) {
354 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
359 AudioPlaylist* playlist;
361 newname = Playlist::bump_name (_playlist->name(), _session);
363 if ((playlist = new AudioPlaylist (*audio_playlist(), newname)) != 0) {
364 playlist->set_orig_diskstream_id (id());
365 return use_playlist (playlist);
372 AudioDiskstream::setup_destructive_playlist ()
374 AudioRegion::SourceList srcs;
376 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
377 srcs.push_back ((*chan).write_source);
380 /* a single full-sized region */
382 cerr << "setup DS using " << srcs.front()->natural_position () << endl;
384 AudioRegion* region = new AudioRegion (srcs, 0, max_frames, _name);
385 _playlist->add_region (*region, srcs.front()->natural_position());
389 AudioDiskstream::use_destructive_playlist ()
391 /* use the sources associated with the single full-extent region */
393 Playlist::RegionList* rl = _playlist->regions_at (0);
396 reset_write_sources (false, true);
400 AudioRegion* region = dynamic_cast<AudioRegion*> (rl->front());
403 throw failed_constructor();
409 ChannelList::iterator chan;
411 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
412 (*chan).write_source = dynamic_cast<AudioFileSource*>(®ion->source (n));
413 assert((*chan).write_source);
414 (*chan).write_source->set_allow_remove_if_empty (false);
417 /* the source list will never be reset for a destructive track */
421 AudioDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record)
423 int possibly_recording;
426 const int transport_rolling = 0x4;
427 const int track_rec_enabled = 0x2;
428 const int global_rec_enabled = 0x1;
430 /* merge together the 3 factors that affect record status, and compute
434 rolling = _session.transport_speed() != 0.0f;
435 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
436 change = possibly_recording ^ last_possibly_recording;
438 if (possibly_recording == last_possibly_recording) {
444 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
446 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
447 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
449 /* starting to record: compute first+last frames */
451 first_recordable_frame = transport_frame + _capture_offset;
452 last_recordable_frame = max_frames;
453 capture_start_frame = transport_frame;
455 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
457 /* was stopped, now rolling (and recording) */
459 if (_alignment_style == ExistingMaterial) {
460 first_recordable_frame += _session.worst_output_latency();
462 first_recordable_frame += _roll_delay;
467 /* was rolling, but record state changed */
469 if (_alignment_style == ExistingMaterial) {
472 if (!_session.get_punch_in()) {
474 /* manual punch in happens at the correct transport frame
475 because the user hit a button. but to get alignment correct
476 we have to back up the position of the new region to the
477 appropriate spot given the roll delay.
480 capture_start_frame -= _roll_delay;
482 /* XXX paul notes (august 2005): i don't know why
486 first_recordable_frame += _capture_offset;
490 /* autopunch toggles recording at the precise
491 transport frame, and then the DS waits
492 to start recording for a time that depends
493 on the output latency.
496 first_recordable_frame += _session.worst_output_latency();
501 if (_session.get_punch_in()) {
502 first_recordable_frame += _roll_delay;
504 capture_start_frame -= _roll_delay;
510 if (_flags & Recordable) {
511 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
513 RingBufferNPT<CaptureTransition>::rw_vector transvec;
514 (*chan).capture_transition_buf->get_write_vector(&transvec);
516 if (transvec.len[0] > 0) {
517 transvec.buf[0]->type = CaptureStart;
518 transvec.buf[0]->capture_val = capture_start_frame;
519 (*chan).capture_transition_buf->increment_write_ptr(1);
523 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
529 } else if (!record_enabled() || !can_record) {
533 last_recordable_frame = transport_frame + _capture_offset;
535 if (_alignment_style == ExistingMaterial) {
536 last_recordable_frame += _session.worst_output_latency();
538 last_recordable_frame += _roll_delay;
542 last_possibly_recording = possibly_recording;
546 AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input)
549 ChannelList::iterator c;
551 jack_nframes_t rec_offset = 0;
552 jack_nframes_t rec_nframes = 0;
553 bool nominally_recording;
554 bool re = record_enabled ();
555 bool collect_playback = false;
557 /* if we've already processed the frames corresponding to this call,
558 just return. this allows multiple routes that are taking input
559 from this diskstream to call our ::process() method, but have
560 this stuff only happen once. more commonly, it allows both
561 the AudioTrack that is using this AudioDiskstream *and* the Session
562 to call process() without problems.
569 check_record_status (transport_frame, nframes, can_record);
571 nominally_recording = (can_record && re);
578 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
579 must always be called as a pair. The only exception is if this function
580 returns a non-zero value, in which case, ::commit should not be called.
583 // If we can't take the state lock return.
584 if (!state_lock.trylock()) {
588 adjust_capture_position = 0;
590 for (c = channels.begin(); c != channels.end(); ++c) {
591 (*c).current_capture_buffer = 0;
592 (*c).current_playback_buffer = 0;
595 if (nominally_recording || (_session.get_record_enabled() && _session.get_punch_in())) {
598 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
605 case OverlapInternal:
606 /* ---------- recrange
609 rec_nframes = nframes;
614 /* |--------| recrange
617 rec_nframes = transport_frame + nframes - first_recordable_frame;
619 rec_offset = first_recordable_frame - transport_frame;
624 /* |--------| recrange
627 rec_nframes = last_recordable_frame - transport_frame;
631 case OverlapExternal:
632 /* |--------| recrange
633 -------------- transrange
635 rec_nframes = last_recordable_frame - last_recordable_frame;
636 rec_offset = first_recordable_frame - transport_frame;
640 if (rec_nframes && !was_recording) {
641 capture_captured = 0;
642 was_recording = true;
647 if (can_record && !_last_capture_regions.empty()) {
648 _last_capture_regions.clear ();
651 if (nominally_recording || rec_nframes) {
653 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
655 ChannelInfo& chan (*c);
657 chan.capture_buf->get_write_vector (&chan.capture_vector);
659 if (rec_nframes <= chan.capture_vector.len[0]) {
661 chan.current_capture_buffer = chan.capture_vector.buf[0];
663 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
667 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
671 jack_nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
673 if (rec_nframes > total) {
678 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
679 jack_nframes_t first = chan.capture_vector.len[0];
681 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
682 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
683 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
684 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
686 chan.current_capture_buffer = chan.capture_wrap_buffer;
693 finish_capture (rec_monitors_input);
700 /* data will be written to disk */
702 if (rec_nframes == nframes && rec_offset == 0) {
704 for (c = channels.begin(); c != channels.end(); ++c) {
705 (*c).current_playback_buffer = (*c).current_capture_buffer;
708 playback_distance = nframes;
713 /* we can't use the capture buffer as the playback buffer, because
714 we recorded only a part of the current process' cycle data
718 collect_playback = true;
721 adjust_capture_position = rec_nframes;
723 } else if (nominally_recording) {
725 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
727 for (c = channels.begin(); c != channels.end(); ++c) {
728 (*c).current_playback_buffer = (*c).current_capture_buffer;
731 playback_distance = nframes;
735 collect_playback = true;
738 if (collect_playback) {
740 /* we're doing playback */
742 jack_nframes_t necessary_samples;
744 /* no varispeed playback if we're recording, because the output .... TBD */
746 if (rec_nframes == 0 && _actual_speed != 1.0f) {
747 necessary_samples = (jack_nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
749 necessary_samples = nframes;
752 for (c = channels.begin(); c != channels.end(); ++c) {
753 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
758 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
760 ChannelInfo& chan (*c);
762 if (necessary_samples <= chan.playback_vector.len[0]) {
764 chan.current_playback_buffer = chan.playback_vector.buf[0];
767 jack_nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
769 if (necessary_samples > total) {
775 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
776 chan.playback_vector.len[0] * sizeof (Sample));
777 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
778 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
780 chan.current_playback_buffer = chan.playback_wrap_buffer;
785 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
787 uint64_t phase = last_phase;
788 jack_nframes_t i = 0;
790 // Linearly interpolate into the alt buffer
791 // using 40.24 fixp maths (swh)
793 for (c = channels.begin(); c != channels.end(); ++c) {
796 ChannelInfo& chan (*c);
801 for (jack_nframes_t outsample = 0; outsample < nframes; ++outsample) {
803 fr = (phase & 0xFFFFFF) / 16777216.0f;
804 chan.speed_buffer[outsample] =
805 chan.current_playback_buffer[i] * (1.0f - fr) +
806 chan.current_playback_buffer[i+1] * fr;
810 chan.current_playback_buffer = chan.speed_buffer;
813 playback_distance = i + 1;
814 last_phase = (phase & 0xFFFFFF);
817 playback_distance = nframes;
829 /* we're exiting with failure, so ::commit will not
830 be called. unlock the state lock.
840 AudioDiskstream::commit (jack_nframes_t nframes)
842 bool need_butler = false;
844 if (_actual_speed < 0.0) {
845 playback_sample -= playback_distance;
847 playback_sample += playback_distance;
850 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
852 (*chan).playback_buf->increment_read_ptr (playback_distance);
854 if (adjust_capture_position) {
855 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
859 if (adjust_capture_position != 0) {
860 capture_captured += adjust_capture_position;
861 adjust_capture_position = 0;
865 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
867 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
868 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
879 AudioDiskstream::set_pending_overwrite (bool yn)
881 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
883 pending_overwrite = yn;
885 overwrite_frame = playback_sample;
886 overwrite_offset = channels.front().playback_buf->get_read_ptr();
890 AudioDiskstream::overwrite_existing_buffers ()
892 Sample* mixdown_buffer;
895 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
897 overwrite_queued = false;
899 /* assume all are the same size */
900 jack_nframes_t size = channels[0].playback_buf->bufsize();
902 mixdown_buffer = new Sample[size];
903 gain_buffer = new float[size];
905 /* reduce size so that we can fill the buffer correctly. */
909 jack_nframes_t start;
911 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
913 start = overwrite_frame;
914 jack_nframes_t cnt = size;
916 /* to fill the buffer without resetting the playback sample, we need to
917 do it one or two chunks (normally two).
919 |----------------------------------------------------------------------|
923 |<- second chunk->||<----------------- first chunk ------------------>|
927 jack_nframes_t to_read = size - overwrite_offset;
929 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, *chan, n, reversed)) {
930 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
931 _id, size, playback_sample) << endmsg;
939 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer,
940 start, cnt, *chan, n, reversed)) {
941 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
942 _id, size, playback_sample) << endmsg;
951 pending_overwrite = false;
952 delete [] gain_buffer;
953 delete [] mixdown_buffer;
958 AudioDiskstream::seek (jack_nframes_t frame, bool complete_refill)
960 Glib::Mutex::Lock lm (state_lock);
963 ChannelList::iterator chan;
965 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
966 (*chan).playback_buf->reset ();
967 (*chan).capture_buf->reset ();
970 /* can't rec-enable in destructive mode if transport is before start */
972 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
973 disengage_record_enable ();
976 playback_sample = frame;
979 if (complete_refill) {
980 while ((ret = do_refill_with_alloc ()) > 0) ;
982 ret = do_refill_with_alloc ();
989 AudioDiskstream::can_internal_playback_seek (jack_nframes_t distance)
991 ChannelList::iterator chan;
993 for (chan = channels.begin(); chan != channels.end(); ++chan) {
994 if ((*chan).playback_buf->read_space() < distance) {
1002 AudioDiskstream::internal_playback_seek (jack_nframes_t distance)
1004 ChannelList::iterator chan;
1006 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1007 (*chan).playback_buf->increment_read_ptr (distance);
1010 first_recordable_frame += distance;
1011 playback_sample += distance;
1017 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, jack_nframes_t& start, jack_nframes_t cnt,
1018 ChannelInfo& channel_info, int channel, bool reversed)
1020 jack_nframes_t this_read = 0;
1021 bool reloop = false;
1022 jack_nframes_t loop_end = 0;
1023 jack_nframes_t loop_start = 0;
1024 jack_nframes_t loop_length = 0;
1025 jack_nframes_t offset = 0;
1029 /* Make the use of a Location atomic for this read operation.
1031 Note: Locations don't get deleted, so all we care about
1032 when I say "atomic" is that we are always pointing to
1033 the same one and using a start/length values obtained
1037 if ((loc = loop_location) != 0) {
1038 loop_start = loc->start();
1039 loop_end = loc->end();
1040 loop_length = loop_end - loop_start;
1043 /* if we are looping, ensure that the first frame we read is at the correct
1044 position within the loop.
1047 if (loc && start >= loop_end) {
1048 //cerr << "start adjusted from " << start;
1049 start = loop_start + ((start - loop_start) % loop_length);
1050 //cerr << "to " << start << endl;
1052 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1057 /* take any loop into account. we can't read past the end of the loop. */
1059 if (loc && (loop_end - start < cnt)) {
1060 this_read = loop_end - start;
1061 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1068 if (this_read == 0) {
1072 this_read = min(cnt,this_read);
1074 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1075 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1080 _read_data_count = _playlist->read_data_count();
1084 /* don't adjust start, since caller has already done that
1087 swap_by_ptr (buf, buf + this_read - 1);
1091 /* if we read to the end of the loop, go back to the beginning */
1101 offset += this_read;
1108 AudioDiskstream::do_refill_with_alloc()
1110 Sample* mix_buf = new Sample[disk_io_chunk_frames];
1111 float* gain_buf = new float[disk_io_chunk_frames];
1113 int ret = _do_refill(mix_buf, gain_buf);
1122 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
1125 jack_nframes_t to_read;
1126 RingBufferNPT<Sample>::rw_vector vector;
1127 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1128 jack_nframes_t total_space;
1129 jack_nframes_t zero_fill;
1131 ChannelList::iterator i;
1134 assert(mixdown_buffer);
1135 assert(gain_buffer);
1137 channels.front().playback_buf->get_write_vector (&vector);
1139 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1143 /* if there are 2+ chunks of disk i/o possible for
1144 this track, let the caller know so that it can arrange
1145 for us to be called again, ASAP.
1148 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1152 /* if we're running close to normal speed and there isn't enough
1153 space to do disk_io_chunk_frames of I/O, then don't bother.
1155 at higher speeds, just do it because the sync between butler
1156 and audio thread may not be good enough.
1159 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1163 /* when slaved, don't try to get too close to the read pointer. this
1164 leaves space for the buffer reversal to have something useful to
1168 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1172 total_space = min (disk_io_chunk_frames, total_space);
1176 if (file_frame == 0) {
1178 /* at start: nothing to do but fill with silence */
1180 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1182 ChannelInfo& chan (*i);
1183 chan.playback_buf->get_write_vector (&vector);
1184 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1185 if (vector.len[1]) {
1186 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1188 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1193 if (file_frame < total_space) {
1195 /* too close to the start: read what we can,
1196 and then zero fill the rest
1199 zero_fill = total_space - file_frame;
1200 total_space = file_frame;
1205 /* move read position backwards because we are going
1206 to reverse the data.
1209 file_frame -= total_space;
1215 if (file_frame == max_frames) {
1217 /* at end: nothing to do but fill with silence */
1219 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1221 ChannelInfo& chan (*i);
1222 chan.playback_buf->get_write_vector (&vector);
1223 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1224 if (vector.len[1]) {
1225 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1227 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1232 if (file_frame > max_frames - total_space) {
1234 /* to close to the end: read what we can, and zero fill the rest */
1236 zero_fill = total_space - (max_frames - file_frame);
1237 total_space = max_frames - file_frame;
1244 jack_nframes_t file_frame_tmp = 0;
1246 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1248 ChannelInfo& chan (*i);
1251 jack_nframes_t len1, len2;
1253 chan.playback_buf->get_write_vector (&vector);
1256 file_frame_tmp = file_frame;
1259 buf1 = vector.buf[1];
1260 len1 = vector.len[1];
1261 buf2 = vector.buf[0];
1262 len2 = vector.len[0];
1264 buf1 = vector.buf[0];
1265 len1 = vector.len[0];
1266 buf2 = vector.buf[1];
1267 len2 = vector.len[1];
1271 to_read = min (ts, len1);
1272 to_read = min (to_read, disk_io_chunk_frames);
1276 if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1281 chan.playback_buf->increment_write_ptr (to_read);
1285 to_read = min (ts, len2);
1290 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1291 so read some or all of vector.len[1] as well.
1294 if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1299 chan.playback_buf->increment_write_ptr (to_read);
1308 file_frame = file_frame_tmp;
1315 /** Flush pending data to disk.
1317 * Important note: this function will write *AT MOST* disk_io_chunk_frames
1318 * of data to disk. it will never write more than that. If it writes that
1319 * much and there is more than that waiting to be written, it will return 1,
1320 * otherwise 0 on success or -1 on failure.
1322 * If there is less than disk_io_chunk_frames to be written, no data will be
1323 * written at all unless @a force_flush is true.
1326 AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
1330 RingBufferNPT<Sample>::rw_vector vector;
1331 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1332 jack_nframes_t total;
1334 _write_data_count = 0;
1336 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1338 (*chan).capture_buf->get_read_vector (&vector);
1340 total = vector.len[0] + vector.len[1];
1343 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1347 /* if there are 2+ chunks of disk i/o possible for
1348 this track, let the caller know so that it can arrange
1349 for us to be called again, ASAP.
1351 if we are forcing a flush, then if there is* any* extra
1352 work, let the caller know.
1354 if we are no longer recording and there is any extra work,
1355 let the caller know too.
1358 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1362 to_write = min (disk_io_chunk_frames, (jack_nframes_t) vector.len[0]);
1364 // check the transition buffer when recording destructive
1365 // important that we get this after the capture buf
1367 if (destructive()) {
1368 (*chan).capture_transition_buf->get_read_vector(&transvec);
1369 size_t transcount = transvec.len[0] + transvec.len[1];
1370 bool have_start = false;
1373 for (ti=0; ti < transcount; ++ti) {
1374 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1376 if (captrans.type == CaptureStart) {
1377 // by definition, the first data we got above represents the given capture pos
1379 (*chan).write_source->mark_capture_start (captrans.capture_val);
1380 (*chan).curr_capture_cnt = 0;
1384 else if (captrans.type == CaptureEnd) {
1386 // capture end, the capture_val represents total frames in capture
1388 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1390 // shorten to make the write a perfect fit
1391 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1393 if (nto_write < to_write) {
1394 ret = 1; // should we?
1396 to_write = nto_write;
1398 (*chan).write_source->mark_capture_end ();
1400 // increment past this transition, but go no further
1405 // actually ends just beyond this chunk, so force more work
1413 (*chan).capture_transition_buf->increment_read_ptr(ti);
1417 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write) != to_write) {
1418 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1422 (*chan).capture_buf->increment_read_ptr (to_write);
1423 (*chan).curr_capture_cnt += to_write;
1425 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1427 /* we wrote all of vector.len[0] but it wasn't an entire
1428 disk_io_chunk_frames of data, so arrange for some part
1429 of vector.len[1] to be flushed to disk as well.
1432 to_write = min ((jack_nframes_t)(disk_io_chunk_frames - to_write), (jack_nframes_t) vector.len[1]);
1434 if ((*chan).write_source->write (vector.buf[1], to_write) != to_write) {
1435 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1439 _write_data_count += (*chan).write_source->write_data_count();
1441 (*chan).capture_buf->increment_read_ptr (to_write);
1442 (*chan).curr_capture_cnt += to_write;
1451 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1453 uint32_t buffer_position;
1454 bool more_work = true;
1456 AudioRegion* region = 0;
1457 jack_nframes_t total_capture;
1458 AudioRegion::SourceList srcs;
1459 AudioRegion::SourceList::iterator src;
1460 ChannelList::iterator chan;
1461 vector<CaptureInfo*>::iterator ci;
1463 bool mark_write_completed = false;
1465 finish_capture (true);
1467 /* butler is already stopped, but there may be work to do
1468 to flush remaining data to disk.
1471 while (more_work && !err) {
1472 switch (do_flush (Session::TransportContext, true)) {
1479 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1484 /* XXX is there anything we can do if err != 0 ? */
1485 Glib::Mutex::Lock lm (capture_info_lock);
1487 if (capture_info.empty()) {
1491 if (abort_capture) {
1493 ChannelList::iterator chan;
1495 list<Source*>* deletion_list = new list<Source*>;
1497 for ( chan = channels.begin(); chan != channels.end(); ++chan) {
1499 if ((*chan).write_source) {
1501 (*chan).write_source->mark_for_remove ();
1502 (*chan).write_source->release ();
1504 deletion_list->push_back ((*chan).write_source);
1506 (*chan).write_source = 0;
1509 /* new source set up in "out" below */
1512 if (!deletion_list->empty()) {
1513 DeleteSources (deletion_list);
1515 delete deletion_list;
1521 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1522 total_capture += (*ci)->frames;
1525 /* figure out the name for this take */
1527 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1529 AudioFileSource* s = (*chan).write_source;
1533 AudioFileSource* fsrc;
1537 if ((fsrc = dynamic_cast<AudioFileSource *>(s)) != 0) {
1538 cerr << "updating source after capture\n";
1539 fsrc->update_header (capture_info.front()->start, when, twhen);
1542 s->set_captured_for (_name);
1547 /* destructive tracks have a single, never changing region */
1549 if (destructive()) {
1551 /* send a signal that any UI can pick up to do the right thing. there is
1552 a small problem here in that a UI may need the peak data to be ready
1553 for the data that was recorded and this isn't interlocked with that
1554 process. this problem is deferred to the UI.
1557 _playlist->Modified();
1561 /* Register a new region with the Session that
1562 describes the entire source. Do this first
1563 so that any sub-regions will obviously be
1564 children of this one (later!)
1568 region = new AudioRegion (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1569 region_name_from_path (channels[0].write_source->name()),
1570 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
1572 region->special_set_position (capture_info.front()->start);
1576 catch (failed_constructor& err) {
1577 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1581 _last_capture_regions.push_back (region);
1583 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1585 XMLNode &before = _playlist->get_state();
1586 _playlist->freeze ();
1588 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1591 _session.region_name (region_name, channels[0].write_source->name(), false);
1593 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1596 region = new AudioRegion (srcs, buffer_position, (*ci)->frames, region_name);
1599 catch (failed_constructor& err) {
1600 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1601 continue; /* XXX is this OK? */
1604 _last_capture_regions.push_back (region);
1606 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1608 i_am_the_modifier++;
1609 _playlist->add_region (*region, (*ci)->start);
1610 i_am_the_modifier--;
1612 buffer_position += (*ci)->frames;
1616 XMLNode &after = _playlist->get_state();
1617 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1620 mark_write_completed = true;
1622 reset_write_sources (mark_write_completed);
1625 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1629 capture_info.clear ();
1630 capture_start_frame = 0;
1634 AudioDiskstream::finish_capture (bool rec_monitors_input)
1636 was_recording = false;
1638 if (capture_captured == 0) {
1642 if (recordable() && destructive()) {
1643 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1645 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1646 (*chan).capture_transition_buf->get_write_vector(&transvec);
1649 if (transvec.len[0] > 0) {
1650 transvec.buf[0]->type = CaptureEnd;
1651 transvec.buf[0]->capture_val = capture_captured;
1652 (*chan).capture_transition_buf->increment_write_ptr(1);
1656 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1662 CaptureInfo* ci = new CaptureInfo;
1664 ci->start = capture_start_frame;
1665 ci->frames = capture_captured;
1667 /* XXX theoretical race condition here. Need atomic exchange ?
1668 However, the circumstances when this is called right
1669 now (either on record-disable or transport_stopped)
1670 mean that no actual race exists. I think ...
1671 We now have a capture_info_lock, but it is only to be used
1672 to synchronize in the transport_stop and the capture info
1673 accessors, so that invalidation will not occur (both non-realtime).
1676 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1678 capture_info.push_back (ci);
1679 capture_captured = 0;
1683 AudioDiskstream::set_record_enabled (bool yn)
1685 if (!recordable() || !_session.record_enabling_legal()) {
1689 /* can't rec-enable in destructive mode if transport is before start */
1691 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1695 if (yn && channels[0].source == 0) {
1697 /* pick up connections not initiated *from* the IO object
1698 we're associated with.
1701 get_input_sources ();
1704 /* yes, i know that this not proof against race conditions, but its
1705 good enough. i think.
1708 if (record_enabled() != yn) {
1710 engage_record_enable ();
1712 disengage_record_enable ();
1718 AudioDiskstream::engage_record_enable ()
1720 bool rolling = _session.transport_speed() != 0.0f;
1722 g_atomic_int_set (&_record_enabled, 1);
1723 capturing_sources.clear ();
1724 if (Config->get_use_hardware_monitoring()) {
1725 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1726 if ((*chan).source) {
1727 (*chan).source->request_monitor_input (!(_session.get_auto_input() && rolling));
1729 capturing_sources.push_back ((*chan).write_source);
1732 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1733 capturing_sources.push_back ((*chan).write_source);
1737 RecordEnableChanged (); /* EMIT SIGNAL */
1741 AudioDiskstream::disengage_record_enable ()
1743 g_atomic_int_set (&_record_enabled, 0);
1744 if (Config->get_use_hardware_monitoring()) {
1745 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1746 if ((*chan).source) {
1747 (*chan).source->request_monitor_input (false);
1751 capturing_sources.clear ();
1752 RecordEnableChanged (); /* EMIT SIGNAL */
1757 AudioDiskstream::get_state ()
1759 XMLNode* node = new XMLNode ("AudioDiskstream");
1761 LocaleGuard lg (X_("POSIX"));
1763 snprintf (buf, sizeof(buf), "0x%x", _flags);
1764 node->add_property ("flags", buf);
1766 snprintf (buf, sizeof(buf), "%zd", channels.size());
1767 node->add_property ("channels", buf);
1769 node->add_property ("playlist", _playlist->name());
1771 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1772 node->add_property ("speed", buf);
1774 node->add_property("name", _name);
1776 node->add_property("id", buf);
1778 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1780 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1781 XMLNode* cs_grandchild;
1783 for (vector<AudioFileSource*>::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1784 cs_grandchild = new XMLNode (X_("file"));
1785 cs_grandchild->add_property (X_("path"), (*i)->path());
1786 cs_child->add_child_nocopy (*cs_grandchild);
1789 /* store the location where capture will start */
1793 if (_session.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1794 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1796 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1799 cs_child->add_property (X_("at"), buf);
1800 node->add_child_nocopy (*cs_child);
1804 node->add_child_copy (*_extra_xml);
1811 AudioDiskstream::set_state (const XMLNode& node)
1813 const XMLProperty* prop;
1814 XMLNodeList nlist = node.children();
1815 XMLNodeIterator niter;
1816 uint32_t nchans = 1;
1817 XMLNode* capture_pending_node = 0;
1818 LocaleGuard lg (X_("POSIX"));
1820 in_set_state = true;
1822 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1823 if ((*niter)->name() == IO::state_node_name) {
1824 deprecated_io_node = new XMLNode (**niter);
1827 if ((*niter)->name() == X_("CapturingSources")) {
1828 capture_pending_node = *niter;
1832 /* prevent write sources from being created */
1834 in_set_state = true;
1836 if ((prop = node.property ("name")) != 0) {
1837 _name = prop->value();
1840 if (deprecated_io_node) {
1841 if ((prop = deprecated_io_node->property ("id")) != 0) {
1842 _id = prop->value ();
1845 if ((prop = node.property ("id")) != 0) {
1846 _id = prop->value ();
1850 if ((prop = node.property ("flags")) != 0) {
1851 _flags = strtol (prop->value().c_str(), 0, 0);
1854 if ((prop = node.property ("channels")) != 0) {
1855 nchans = atoi (prop->value().c_str());
1858 // create necessary extra channels
1859 // we are always constructed with one and we always need one
1861 if (nchans > _n_channels) {
1863 // we need to add new channel infos
1864 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1866 int diff = nchans - channels.size();
1868 for (int i=0; i < diff; ++i) {
1872 } else if (nchans < _n_channels) {
1874 // we need to get rid of channels
1875 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1877 int diff = channels.size() - nchans;
1879 for (int i = 0; i < diff; ++i) {
1884 if ((prop = node.property ("playlist")) == 0) {
1889 bool had_playlist = (_playlist != 0);
1891 if (find_and_use_playlist (prop->value())) {
1895 if (!had_playlist) {
1896 _playlist->set_orig_diskstream_id (_id);
1899 if (!destructive() && capture_pending_node) {
1900 /* destructive streams have one and only one source per channel,
1901 and so they never end up in pending capture in any useful
1904 use_pending_capture_data (*capture_pending_node);
1909 if ((prop = node.property ("speed")) != 0) {
1910 double sp = atof (prop->value().c_str());
1912 if (realtime_set_speed (sp, false)) {
1913 non_realtime_set_speed ();
1917 _n_channels = channels.size();
1919 in_set_state = false;
1921 /* make sure this is clear before we do anything else */
1923 capturing_sources.clear ();
1925 /* write sources are handled when we handle the input set
1926 up of the IO that owns this DS (::non_realtime_input_change())
1929 in_set_state = false;
1935 AudioDiskstream::use_new_write_source (uint32_t n)
1937 if (!recordable()) {
1941 if (n >= channels.size()) {
1942 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1946 ChannelInfo &chan = channels[n];
1948 if (chan.write_source) {
1950 if (AudioFileSource::is_empty (chan.write_source->path())) {
1951 chan.write_source->mark_for_remove ();
1952 chan.write_source->release();
1953 delete chan.write_source;
1955 chan.write_source->release();
1956 chan.write_source = 0;
1961 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
1962 throw failed_constructor();
1966 catch (failed_constructor &err) {
1967 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1968 chan.write_source = 0;
1972 chan.write_source->use ();
1974 /* do not remove destructive files even if they are empty */
1976 chan.write_source->set_allow_remove_if_empty (!destructive());
1982 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1984 ChannelList::iterator chan;
1987 if (!recordable()) {
1991 capturing_sources.clear ();
1993 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
1994 if (!destructive()) {
1996 if ((*chan).write_source && mark_write_complete) {
1997 (*chan).write_source->mark_streaming_write_completed ();
1999 use_new_write_source (n);
2001 if (record_enabled()) {
2002 capturing_sources.push_back ((*chan).write_source);
2006 if ((*chan).write_source == 0) {
2007 use_new_write_source (n);
2012 if (destructive()) {
2014 /* we now have all our write sources set up, so create the
2015 playlist's single region.
2018 if (_playlist->empty()) {
2019 setup_destructive_playlist ();
2025 AudioDiskstream::rename_write_sources ()
2027 ChannelList::iterator chan;
2030 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2031 if ((*chan).write_source != 0) {
2032 (*chan).write_source->set_name (_name, destructive());
2033 /* XXX what to do if one of them fails ? */
2041 AudioDiskstream::set_block_size (jack_nframes_t nframes)
2043 if (_session.get_block_size() > speed_buffer_size) {
2044 speed_buffer_size = _session.get_block_size();
2046 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2047 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2048 (*chan).speed_buffer = new Sample[speed_buffer_size];
2051 allocate_temporary_buffers ();
2055 AudioDiskstream::allocate_temporary_buffers ()
2057 /* make sure the wrap buffer is at least large enough to deal
2058 with the speeds up to 1.2, to allow for micro-variation
2059 when slaving to MTC, SMPTE etc.
2062 double sp = max (fabsf (_actual_speed), 1.2f);
2063 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * sp) + 1;
2065 if (required_wrap_size > wrap_buffer_size) {
2067 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2068 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2069 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2070 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2071 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2074 wrap_buffer_size = required_wrap_size;
2079 AudioDiskstream::monitor_input (bool yn)
2081 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2083 if ((*chan).source) {
2084 (*chan).source->request_monitor_input (yn);
2090 AudioDiskstream::set_align_style_from_io ()
2092 bool have_physical = false;
2098 get_input_sources ();
2100 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2101 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2102 have_physical = true;
2107 if (have_physical) {
2108 set_align_style (ExistingMaterial);
2110 set_align_style (CaptureTime);
2115 AudioDiskstream::add_channel ()
2117 /* XXX need to take lock??? */
2121 init_channel (chan);
2123 chan.speed_buffer = new Sample[speed_buffer_size];
2124 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2125 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2127 channels.push_back (chan);
2129 _n_channels = channels.size();
2135 AudioDiskstream::remove_channel ()
2137 if (channels.size() > 1) {
2138 /* XXX need to take lock??? */
2139 ChannelInfo & chan = channels.back();
2140 destroy_channel (chan);
2141 channels.pop_back();
2143 _n_channels = channels.size();
2151 AudioDiskstream::playback_buffer_load () const
2153 return (float) ((double) channels.front().playback_buf->read_space()/
2154 (double) channels.front().playback_buf->bufsize());
2158 AudioDiskstream::capture_buffer_load () const
2160 return (float) ((double) channels.front().capture_buf->write_space()/
2161 (double) channels.front().capture_buf->bufsize());
2165 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2167 const XMLProperty* prop;
2168 XMLNodeList nlist = node.children();
2169 XMLNodeIterator niter;
2170 AudioFileSource* fs;
2171 AudioFileSource* first_fs = 0;
2172 AudioRegion::SourceList pending_sources;
2173 jack_nframes_t position;
2175 if ((prop = node.property (X_("at"))) == 0) {
2179 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2183 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2184 if ((*niter)->name() == X_("file")) {
2186 if ((prop = (*niter)->property (X_("path"))) == 0) {
2191 fs = new SndFileSource (prop->value(),
2192 Config->get_native_file_data_format(),
2193 Config->get_native_file_header_format(),
2194 _session.frame_rate());
2197 catch (failed_constructor& err) {
2198 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2199 _name, prop->value())
2204 pending_sources.push_back (fs);
2206 if (first_fs == 0) {
2210 fs->set_captured_for (_name);
2214 if (pending_sources.size() == 0) {
2215 /* nothing can be done */
2219 if (pending_sources.size() != _n_channels) {
2220 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2225 AudioRegion* region;
2228 region = new AudioRegion (pending_sources, 0, first_fs->length(),
2229 region_name_from_path (first_fs->name()),
2230 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
2232 region->special_set_position (0);
2235 catch (failed_constructor& err) {
2236 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2244 region = new AudioRegion (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name()));
2247 catch (failed_constructor& err) {
2248 error << string_compose (_("%1: cannot create region from pending capture sources"),
2255 _playlist->add_region (*region, position);