2 Copyright (C) 2000-2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <pbd/error.h>
34 #include <pbd/basename.h>
35 #include <glibmm/thread.h>
36 #include <pbd/xml++.h>
37 #include <pbd/memento_command.h>
39 #include <ardour/ardour.h>
40 #include <ardour/audioengine.h>
41 #include <ardour/audio_diskstream.h>
42 #include <ardour/utils.h>
43 #include <ardour/configuration.h>
44 #include <ardour/audiofilesource.h>
45 #include <ardour/destructive_filesource.h>
46 #include <ardour/send.h>
47 #include <ardour/region_factory.h>
48 #include <ardour/audioplaylist.h>
49 #include <ardour/cycle_timer.h>
50 #include <ardour/audioregion.h>
51 #include <ardour/source_factory.h>
57 using namespace ARDOUR;
60 size_t AudioDiskstream::_working_buffers_size = 0;
61 Sample* AudioDiskstream::_mixdown_buffer = 0;
62 gain_t* AudioDiskstream::_gain_buffer = 0;
64 AudioDiskstream::AudioDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
65 : Diskstream(sess, name, flag)
66 , deprecated_io_node(NULL)
68 /* prevent any write sources from being created */
78 AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
79 : Diskstream(sess, node)
80 , deprecated_io_node(NULL)
85 if (set_state (node)) {
87 throw failed_constructor();
93 use_destructive_playlist ();
98 AudioDiskstream::init_channel (ChannelInfo &chan)
100 chan.playback_wrap_buffer = 0;
101 chan.capture_wrap_buffer = 0;
102 chan.speed_buffer = 0;
103 chan.peak_power = 0.0f;
105 chan.current_capture_buffer = 0;
106 chan.current_playback_buffer = 0;
107 chan.curr_capture_cnt = 0;
109 chan.playback_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
110 chan.capture_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
111 chan.capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
114 /* touch the ringbuffer buffers, which will cause
115 them to be mapped into locked physical RAM if
116 we're running with mlockall(). this doesn't do
119 memset (chan.playback_buf->buffer(), 0, sizeof (Sample) * chan.playback_buf->bufsize());
120 memset (chan.capture_buf->buffer(), 0, sizeof (Sample) * chan.capture_buf->bufsize());
121 memset (chan.capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * chan.capture_transition_buf->bufsize());
126 AudioDiskstream::init (Diskstream::Flag f)
130 /* there are no channels at this point, so these
131 two calls just get speed_buffer_size and wrap_buffer
132 size setup without duplicating their code.
135 set_block_size (_session.get_block_size());
136 allocate_temporary_buffers ();
139 assert(_n_channels == 1);
143 AudioDiskstream::destroy_channel (ChannelInfo &chan)
145 if (chan.write_source) {
146 chan.write_source.reset ();
149 if (chan.speed_buffer) {
150 delete [] chan.speed_buffer;
153 if (chan.playback_wrap_buffer) {
154 delete [] chan.playback_wrap_buffer;
156 if (chan.capture_wrap_buffer) {
157 delete [] chan.capture_wrap_buffer;
160 delete chan.playback_buf;
161 delete chan.capture_buf;
162 delete chan.capture_transition_buf;
164 chan.playback_buf = 0;
165 chan.capture_buf = 0;
168 AudioDiskstream::~AudioDiskstream ()
170 Glib::Mutex::Lock lm (state_lock);
172 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan)
173 destroy_channel((*chan));
179 AudioDiskstream::allocate_working_buffers()
181 assert(disk_io_frames() > 0);
183 _working_buffers_size = disk_io_frames();
184 _mixdown_buffer = new Sample[_working_buffers_size];
185 _gain_buffer = new gain_t[_working_buffers_size];
189 AudioDiskstream::free_working_buffers()
191 delete [] _mixdown_buffer;
192 delete [] _gain_buffer;
193 _working_buffers_size = 0;
199 AudioDiskstream::non_realtime_input_change ()
202 Glib::Mutex::Lock lm (state_lock);
204 if (input_change_pending == NoChange) {
208 if (input_change_pending & ConfigurationChanged) {
210 if (_io->n_inputs() > _n_channels) {
212 // we need to add new channel infos
214 int diff = _io->n_inputs() - channels.size();
216 for (int i = 0; i < diff; ++i) {
220 } else if (_io->n_inputs() < _n_channels) {
222 // we need to get rid of channels
224 int diff = channels.size() - _io->n_inputs();
226 for (int i = 0; i < diff; ++i) {
232 get_input_sources ();
233 set_capture_offset ();
235 if (first_input_change) {
236 set_align_style (_persistent_alignment_style);
237 first_input_change = false;
239 set_align_style_from_io ();
242 input_change_pending = NoChange;
245 /* reset capture files */
247 reset_write_sources (false);
249 /* now refill channel buffers */
251 if (speed() != 1.0f || speed() != -1.0f) {
252 seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()));
254 seek (_session.transport_frame());
259 AudioDiskstream::get_input_sources ()
261 uint32_t ni = _io->n_inputs();
263 for (uint32_t n = 0; n < ni; ++n) {
265 const char **connections = _io->input(n)->get_connections ();
266 ChannelInfo& chan = channels[n];
268 if (connections == 0 || connections[0] == 0) {
271 // _source->disable_metering ();
277 chan.source = _session.engine().get_port_by_name (connections[0]);
287 AudioDiskstream::find_and_use_playlist (const string& name)
290 AudioPlaylist* playlist;
292 if ((pl = _session.playlist_by_name (name)) == 0) {
293 playlist = new AudioPlaylist(_session, name);
297 if ((playlist = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
298 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
302 return use_playlist (playlist);
306 AudioDiskstream::use_playlist (Playlist* playlist)
308 assert(dynamic_cast<AudioPlaylist*>(playlist));
310 Diskstream::use_playlist(playlist);
316 AudioDiskstream::use_new_playlist ()
319 AudioPlaylist* playlist;
321 if (!in_set_state && destructive()) {
326 newname = Playlist::bump_name (_playlist->name(), _session);
328 newname = Playlist::bump_name (_name, _session);
331 if ((playlist = new AudioPlaylist (_session, newname, hidden())) != 0) {
332 playlist->set_orig_diskstream_id (id());
333 return use_playlist (playlist);
340 AudioDiskstream::use_copy_playlist ()
342 assert(audio_playlist());
348 if (_playlist == 0) {
349 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
354 AudioPlaylist* playlist;
356 newname = Playlist::bump_name (_playlist->name(), _session);
358 if ((playlist = new AudioPlaylist (*audio_playlist(), newname)) != 0) {
359 playlist->set_orig_diskstream_id (id());
360 return use_playlist (playlist);
367 AudioDiskstream::setup_destructive_playlist ()
371 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
372 srcs.push_back ((*chan).write_source);
375 /* a single full-sized region */
377 cerr << "setup DS using " << srcs.front()->natural_position () << endl;
379 boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, max_frames, _name));
380 _playlist->add_region (region, srcs.front()->natural_position());
384 AudioDiskstream::use_destructive_playlist ()
386 /* this is called from the XML-based constructor. when its done,
387 we already have a playlist and a region, but we need to
388 set up our sources for write. we use the sources associated
389 with the (presumed single, full-extent) region.
392 Playlist::RegionList* rl = _playlist->regions_at (0);
395 reset_write_sources (false, true);
399 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rl->front());
402 throw failed_constructor();
408 ChannelList::iterator chan;
410 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
411 (*chan).write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
412 assert((*chan).write_source);
413 (*chan).write_source->set_allow_remove_if_empty (false);
416 /* the source list will never be reset for a destructive track */
420 AudioDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record)
422 int possibly_recording;
425 const int transport_rolling = 0x4;
426 const int track_rec_enabled = 0x2;
427 const int global_rec_enabled = 0x1;
429 /* merge together the 3 factors that affect record status, and compute
433 rolling = _session.transport_speed() != 0.0f;
434 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
435 change = possibly_recording ^ last_possibly_recording;
437 if (possibly_recording == last_possibly_recording) {
443 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
445 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
446 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
448 /* starting to record: compute first+last frames */
450 first_recordable_frame = transport_frame + _capture_offset;
451 last_recordable_frame = max_frames;
452 capture_start_frame = transport_frame;
454 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
456 /* was stopped, now rolling (and recording) */
458 if (_alignment_style == ExistingMaterial) {
459 first_recordable_frame += _session.worst_output_latency();
461 first_recordable_frame += _roll_delay;
466 /* was rolling, but record state changed */
468 if (_alignment_style == ExistingMaterial) {
470 if (!Config->get_punch_in()) {
472 /* manual punch in happens at the correct transport frame
473 because the user hit a button. but to get alignment correct
474 we have to back up the position of the new region to the
475 appropriate spot given the roll delay.
478 capture_start_frame -= _roll_delay;
480 /* XXX paul notes (august 2005): i don't know why
484 first_recordable_frame += _capture_offset;
488 /* autopunch toggles recording at the precise
489 transport frame, and then the DS waits
490 to start recording for a time that depends
491 on the output latency.
494 first_recordable_frame += _session.worst_output_latency();
499 if (Config->get_punch_in()) {
500 first_recordable_frame += _roll_delay;
502 capture_start_frame -= _roll_delay;
508 if (_flags & Recordable) {
509 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
511 RingBufferNPT<CaptureTransition>::rw_vector transvec;
512 (*chan).capture_transition_buf->get_write_vector(&transvec);
514 if (transvec.len[0] > 0) {
515 transvec.buf[0]->type = CaptureStart;
516 transvec.buf[0]->capture_val = capture_start_frame;
517 (*chan).capture_transition_buf->increment_write_ptr(1);
521 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
527 } else if (!record_enabled() || !can_record) {
531 last_recordable_frame = transport_frame + _capture_offset;
533 if (_alignment_style == ExistingMaterial) {
534 last_recordable_frame += _session.worst_output_latency();
536 last_recordable_frame += _roll_delay;
540 last_possibly_recording = possibly_recording;
544 AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input)
547 ChannelList::iterator c;
549 jack_nframes_t rec_offset = 0;
550 jack_nframes_t rec_nframes = 0;
551 bool nominally_recording;
552 bool re = record_enabled ();
553 bool collect_playback = false;
555 /* if we've already processed the frames corresponding to this call,
556 just return. this allows multiple routes that are taking input
557 from this diskstream to call our ::process() method, but have
558 this stuff only happen once. more commonly, it allows both
559 the AudioTrack that is using this AudioDiskstream *and* the Session
560 to call process() without problems.
567 check_record_status (transport_frame, nframes, can_record);
569 nominally_recording = (can_record && re);
576 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
577 must always be called as a pair. The only exception is if this function
578 returns a non-zero value, in which case, ::commit should not be called.
581 // If we can't take the state lock return.
582 if (!state_lock.trylock()) {
586 adjust_capture_position = 0;
588 for (c = channels.begin(); c != channels.end(); ++c) {
589 (*c).current_capture_buffer = 0;
590 (*c).current_playback_buffer = 0;
593 if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
596 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
603 case OverlapInternal:
604 /* ---------- recrange
607 rec_nframes = nframes;
612 /* |--------| recrange
615 rec_nframes = transport_frame + nframes - first_recordable_frame;
617 rec_offset = first_recordable_frame - transport_frame;
622 /* |--------| recrange
625 rec_nframes = last_recordable_frame - transport_frame;
629 case OverlapExternal:
630 /* |--------| recrange
631 -------------- transrange
633 rec_nframes = last_recordable_frame - last_recordable_frame;
634 rec_offset = first_recordable_frame - transport_frame;
638 if (rec_nframes && !was_recording) {
639 capture_captured = 0;
640 was_recording = true;
645 if (can_record && !_last_capture_regions.empty()) {
646 _last_capture_regions.clear ();
649 if (nominally_recording || rec_nframes) {
651 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
653 ChannelInfo& chan (*c);
655 chan.capture_buf->get_write_vector (&chan.capture_vector);
657 if (rec_nframes <= chan.capture_vector.len[0]) {
659 chan.current_capture_buffer = chan.capture_vector.buf[0];
661 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
665 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
669 jack_nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
671 if (rec_nframes > total) {
676 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
677 jack_nframes_t first = chan.capture_vector.len[0];
679 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
680 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
681 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
682 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
684 chan.current_capture_buffer = chan.capture_wrap_buffer;
691 finish_capture (rec_monitors_input);
698 /* data will be written to disk */
700 if (rec_nframes == nframes && rec_offset == 0) {
702 for (c = channels.begin(); c != channels.end(); ++c) {
703 (*c).current_playback_buffer = (*c).current_capture_buffer;
706 playback_distance = nframes;
711 /* we can't use the capture buffer as the playback buffer, because
712 we recorded only a part of the current process' cycle data
716 collect_playback = true;
719 adjust_capture_position = rec_nframes;
721 } else if (nominally_recording) {
723 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
725 for (c = channels.begin(); c != channels.end(); ++c) {
726 (*c).current_playback_buffer = (*c).current_capture_buffer;
729 playback_distance = nframes;
733 collect_playback = true;
736 if (collect_playback) {
738 /* we're doing playback */
740 jack_nframes_t necessary_samples;
742 /* no varispeed playback if we're recording, because the output .... TBD */
744 if (rec_nframes == 0 && _actual_speed != 1.0f) {
745 necessary_samples = (jack_nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
747 necessary_samples = nframes;
750 for (c = channels.begin(); c != channels.end(); ++c) {
751 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
756 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
758 ChannelInfo& chan (*c);
760 if (necessary_samples <= chan.playback_vector.len[0]) {
762 chan.current_playback_buffer = chan.playback_vector.buf[0];
765 jack_nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
767 if (necessary_samples > total) {
773 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
774 chan.playback_vector.len[0] * sizeof (Sample));
775 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
776 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
778 chan.current_playback_buffer = chan.playback_wrap_buffer;
783 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
785 uint64_t phase = last_phase;
786 jack_nframes_t i = 0;
788 // Linearly interpolate into the alt buffer
789 // using 40.24 fixp maths (swh)
791 for (c = channels.begin(); c != channels.end(); ++c) {
794 ChannelInfo& chan (*c);
799 for (jack_nframes_t outsample = 0; outsample < nframes; ++outsample) {
801 fr = (phase & 0xFFFFFF) / 16777216.0f;
802 chan.speed_buffer[outsample] =
803 chan.current_playback_buffer[i] * (1.0f - fr) +
804 chan.current_playback_buffer[i+1] * fr;
808 chan.current_playback_buffer = chan.speed_buffer;
811 playback_distance = i + 1;
812 last_phase = (phase & 0xFFFFFF);
815 playback_distance = nframes;
827 /* we're exiting with failure, so ::commit will not
828 be called. unlock the state lock.
838 AudioDiskstream::commit (jack_nframes_t nframes)
840 bool need_butler = false;
842 if (_actual_speed < 0.0) {
843 playback_sample -= playback_distance;
845 playback_sample += playback_distance;
848 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
850 (*chan).playback_buf->increment_read_ptr (playback_distance);
852 if (adjust_capture_position) {
853 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
857 if (adjust_capture_position != 0) {
858 capture_captured += adjust_capture_position;
859 adjust_capture_position = 0;
863 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
865 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
866 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
877 AudioDiskstream::set_pending_overwrite (bool yn)
879 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
881 pending_overwrite = yn;
883 overwrite_frame = playback_sample;
884 overwrite_offset = channels.front().playback_buf->get_read_ptr();
888 AudioDiskstream::overwrite_existing_buffers ()
890 Sample* mixdown_buffer;
893 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
895 overwrite_queued = false;
897 /* assume all are the same size */
898 jack_nframes_t size = channels[0].playback_buf->bufsize();
900 mixdown_buffer = new Sample[size];
901 gain_buffer = new float[size];
903 /* reduce size so that we can fill the buffer correctly. */
907 jack_nframes_t start;
909 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
911 start = overwrite_frame;
912 jack_nframes_t cnt = size;
914 /* to fill the buffer without resetting the playback sample, we need to
915 do it one or two chunks (normally two).
917 |----------------------------------------------------------------------|
921 |<- second chunk->||<----------------- first chunk ------------------>|
925 jack_nframes_t to_read = size - overwrite_offset;
927 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, *chan, n, reversed)) {
928 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
929 _id, size, playback_sample) << endmsg;
937 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer,
938 start, cnt, *chan, n, reversed)) {
939 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
940 _id, size, playback_sample) << endmsg;
949 pending_overwrite = false;
950 delete [] gain_buffer;
951 delete [] mixdown_buffer;
956 AudioDiskstream::seek (jack_nframes_t frame, bool complete_refill)
958 Glib::Mutex::Lock lm (state_lock);
961 ChannelList::iterator chan;
963 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
964 (*chan).playback_buf->reset ();
965 (*chan).capture_buf->reset ();
968 /* can't rec-enable in destructive mode if transport is before start */
970 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
971 disengage_record_enable ();
974 playback_sample = frame;
977 if (complete_refill) {
978 while ((ret = do_refill_with_alloc ()) > 0) ;
980 ret = do_refill_with_alloc ();
987 AudioDiskstream::can_internal_playback_seek (jack_nframes_t distance)
989 ChannelList::iterator chan;
991 for (chan = channels.begin(); chan != channels.end(); ++chan) {
992 if ((*chan).playback_buf->read_space() < distance) {
1000 AudioDiskstream::internal_playback_seek (jack_nframes_t distance)
1002 ChannelList::iterator chan;
1004 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1005 (*chan).playback_buf->increment_read_ptr (distance);
1008 first_recordable_frame += distance;
1009 playback_sample += distance;
1015 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, jack_nframes_t& start, jack_nframes_t cnt,
1016 ChannelInfo& channel_info, int channel, bool reversed)
1018 jack_nframes_t this_read = 0;
1019 bool reloop = false;
1020 jack_nframes_t loop_end = 0;
1021 jack_nframes_t loop_start = 0;
1022 jack_nframes_t loop_length = 0;
1023 jack_nframes_t offset = 0;
1027 /* Make the use of a Location atomic for this read operation.
1029 Note: Locations don't get deleted, so all we care about
1030 when I say "atomic" is that we are always pointing to
1031 the same one and using a start/length values obtained
1035 if ((loc = loop_location) != 0) {
1036 loop_start = loc->start();
1037 loop_end = loc->end();
1038 loop_length = loop_end - loop_start;
1041 /* if we are looping, ensure that the first frame we read is at the correct
1042 position within the loop.
1045 if (loc && start >= loop_end) {
1046 //cerr << "start adjusted from " << start;
1047 start = loop_start + ((start - loop_start) % loop_length);
1048 //cerr << "to " << start << endl;
1050 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1055 /* take any loop into account. we can't read past the end of the loop. */
1057 if (loc && (loop_end - start < cnt)) {
1058 this_read = loop_end - start;
1059 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1066 if (this_read == 0) {
1070 this_read = min(cnt,this_read);
1072 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1073 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1078 _read_data_count = _playlist->read_data_count();
1082 /* don't adjust start, since caller has already done that
1085 swap_by_ptr (buf, buf + this_read - 1);
1089 /* if we read to the end of the loop, go back to the beginning */
1099 offset += this_read;
1106 AudioDiskstream::do_refill_with_alloc()
1108 Sample* mix_buf = new Sample[disk_io_chunk_frames];
1109 float* gain_buf = new float[disk_io_chunk_frames];
1111 int ret = _do_refill(mix_buf, gain_buf);
1120 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
1123 jack_nframes_t to_read;
1124 RingBufferNPT<Sample>::rw_vector vector;
1125 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1126 jack_nframes_t total_space;
1127 jack_nframes_t zero_fill;
1129 ChannelList::iterator i;
1132 assert(mixdown_buffer);
1133 assert(gain_buffer);
1135 channels.front().playback_buf->get_write_vector (&vector);
1137 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1141 /* if there are 2+ chunks of disk i/o possible for
1142 this track, let the caller know so that it can arrange
1143 for us to be called again, ASAP.
1146 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1150 /* if we're running close to normal speed and there isn't enough
1151 space to do disk_io_chunk_frames of I/O, then don't bother.
1153 at higher speeds, just do it because the sync between butler
1154 and audio thread may not be good enough.
1157 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1161 /* when slaved, don't try to get too close to the read pointer. this
1162 leaves space for the buffer reversal to have something useful to
1166 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1170 total_space = min (disk_io_chunk_frames, total_space);
1174 if (file_frame == 0) {
1176 /* at start: nothing to do but fill with silence */
1178 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1180 ChannelInfo& chan (*i);
1181 chan.playback_buf->get_write_vector (&vector);
1182 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1183 if (vector.len[1]) {
1184 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1186 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1191 if (file_frame < total_space) {
1193 /* too close to the start: read what we can,
1194 and then zero fill the rest
1197 zero_fill = total_space - file_frame;
1198 total_space = file_frame;
1203 /* move read position backwards because we are going
1204 to reverse the data.
1207 file_frame -= total_space;
1213 if (file_frame == max_frames) {
1215 /* at end: nothing to do but fill with silence */
1217 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1219 ChannelInfo& chan (*i);
1220 chan.playback_buf->get_write_vector (&vector);
1221 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1222 if (vector.len[1]) {
1223 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1225 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1230 if (file_frame > max_frames - total_space) {
1232 /* to close to the end: read what we can, and zero fill the rest */
1234 zero_fill = total_space - (max_frames - file_frame);
1235 total_space = max_frames - file_frame;
1242 jack_nframes_t file_frame_tmp = 0;
1244 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1246 ChannelInfo& chan (*i);
1249 jack_nframes_t len1, len2;
1251 chan.playback_buf->get_write_vector (&vector);
1254 file_frame_tmp = file_frame;
1257 buf1 = vector.buf[1];
1258 len1 = vector.len[1];
1259 buf2 = vector.buf[0];
1260 len2 = vector.len[0];
1262 buf1 = vector.buf[0];
1263 len1 = vector.len[0];
1264 buf2 = vector.buf[1];
1265 len2 = vector.len[1];
1269 to_read = min (ts, len1);
1270 to_read = min (to_read, disk_io_chunk_frames);
1274 if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1279 chan.playback_buf->increment_write_ptr (to_read);
1283 to_read = min (ts, len2);
1288 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1289 so read some or all of vector.len[1] as well.
1292 if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1297 chan.playback_buf->increment_write_ptr (to_read);
1306 file_frame = file_frame_tmp;
1313 /** Flush pending data to disk.
1315 * Important note: this function will write *AT MOST* disk_io_chunk_frames
1316 * of data to disk. it will never write more than that. If it writes that
1317 * much and there is more than that waiting to be written, it will return 1,
1318 * otherwise 0 on success or -1 on failure.
1320 * If there is less than disk_io_chunk_frames to be written, no data will be
1321 * written at all unless @a force_flush is true.
1324 AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
1328 RingBufferNPT<Sample>::rw_vector vector;
1329 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1330 jack_nframes_t total;
1332 _write_data_count = 0;
1334 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1336 (*chan).capture_buf->get_read_vector (&vector);
1338 total = vector.len[0] + vector.len[1];
1341 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1345 /* if there are 2+ chunks of disk i/o possible for
1346 this track, let the caller know so that it can arrange
1347 for us to be called again, ASAP.
1349 if we are forcing a flush, then if there is* any* extra
1350 work, let the caller know.
1352 if we are no longer recording and there is any extra work,
1353 let the caller know too.
1356 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1360 to_write = min (disk_io_chunk_frames, (jack_nframes_t) vector.len[0]);
1362 // check the transition buffer when recording destructive
1363 // important that we get this after the capture buf
1365 if (destructive()) {
1366 (*chan).capture_transition_buf->get_read_vector(&transvec);
1367 size_t transcount = transvec.len[0] + transvec.len[1];
1368 bool have_start = false;
1371 for (ti=0; ti < transcount; ++ti) {
1372 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1374 if (captrans.type == CaptureStart) {
1375 // by definition, the first data we got above represents the given capture pos
1377 (*chan).write_source->mark_capture_start (captrans.capture_val);
1378 (*chan).curr_capture_cnt = 0;
1382 else if (captrans.type == CaptureEnd) {
1384 // capture end, the capture_val represents total frames in capture
1386 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1388 // shorten to make the write a perfect fit
1389 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1391 if (nto_write < to_write) {
1392 ret = 1; // should we?
1394 to_write = nto_write;
1396 (*chan).write_source->mark_capture_end ();
1398 // increment past this transition, but go no further
1403 // actually ends just beyond this chunk, so force more work
1411 (*chan).capture_transition_buf->increment_read_ptr(ti);
1415 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write) != to_write) {
1416 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1420 (*chan).capture_buf->increment_read_ptr (to_write);
1421 (*chan).curr_capture_cnt += to_write;
1423 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1425 /* we wrote all of vector.len[0] but it wasn't an entire
1426 disk_io_chunk_frames of data, so arrange for some part
1427 of vector.len[1] to be flushed to disk as well.
1430 to_write = min ((jack_nframes_t)(disk_io_chunk_frames - to_write), (jack_nframes_t) vector.len[1]);
1432 if ((*chan).write_source->write (vector.buf[1], to_write) != to_write) {
1433 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1437 _write_data_count += (*chan).write_source->write_data_count();
1439 (*chan).capture_buf->increment_read_ptr (to_write);
1440 (*chan).curr_capture_cnt += to_write;
1449 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1451 uint32_t buffer_position;
1452 bool more_work = true;
1454 boost::shared_ptr<AudioRegion> region;
1455 jack_nframes_t total_capture;
1457 SourceList::iterator src;
1458 ChannelList::iterator chan;
1459 vector<CaptureInfo*>::iterator ci;
1461 bool mark_write_completed = false;
1463 finish_capture (true);
1465 /* butler is already stopped, but there may be work to do
1466 to flush remaining data to disk.
1469 while (more_work && !err) {
1470 switch (do_flush (Session::TransportContext, true)) {
1477 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1482 /* XXX is there anything we can do if err != 0 ? */
1483 Glib::Mutex::Lock lm (capture_info_lock);
1485 if (capture_info.empty()) {
1489 if (abort_capture) {
1491 ChannelList::iterator chan;
1493 list<boost::shared_ptr<Source> >* deletion_list = new list<boost::shared_ptr<Source> >;
1495 for ( chan = channels.begin(); chan != channels.end(); ++chan) {
1497 if ((*chan).write_source) {
1499 (*chan).write_source->mark_for_remove ();
1501 deletion_list->push_back ((*chan).write_source);
1503 (*chan).write_source.reset ();
1506 /* new source set up in "out" below */
1509 if (!deletion_list->empty()) {
1510 DeleteSources (deletion_list);
1512 delete deletion_list;
1518 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1519 total_capture += (*ci)->frames;
1522 /* figure out the name for this take */
1524 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1526 boost::shared_ptr<AudioFileSource> s = (*chan).write_source;
1530 s->update_header (capture_info.front()->start, when, twhen);
1531 s->set_captured_for (_name);
1536 /* destructive tracks have a single, never changing region */
1538 if (destructive()) {
1540 /* send a signal that any UI can pick up to do the right thing. there is
1541 a small problem here in that a UI may need the peak data to be ready
1542 for the data that was recorded and this isn't interlocked with that
1543 process. this problem is deferred to the UI.
1546 _playlist->Modified();
1550 /* Register a new region with the Session that
1551 describes the entire source. Do this first
1552 so that any sub-regions will obviously be
1553 children of this one (later!)
1557 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1558 region_name_from_path (channels[0].write_source->name()),
1559 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
1561 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1562 region->special_set_position (capture_info.front()->start);
1566 catch (failed_constructor& err) {
1567 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1571 _last_capture_regions.push_back (region);
1573 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1575 XMLNode &before = _playlist->get_state();
1576 _playlist->freeze ();
1578 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1581 _session.region_name (region_name, channels[0].write_source->name(), false);
1583 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
1586 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1587 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1590 catch (failed_constructor& err) {
1591 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1592 continue; /* XXX is this OK? */
1595 _last_capture_regions.push_back (region);
1597 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1599 i_am_the_modifier++;
1600 _playlist->add_region (region, (*ci)->start);
1601 i_am_the_modifier--;
1603 buffer_position += (*ci)->frames;
1607 XMLNode &after = _playlist->get_state();
1608 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1611 mark_write_completed = true;
1613 reset_write_sources (mark_write_completed);
1616 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1620 capture_info.clear ();
1621 capture_start_frame = 0;
1625 AudioDiskstream::finish_capture (bool rec_monitors_input)
1627 was_recording = false;
1629 if (capture_captured == 0) {
1633 if (recordable() && destructive()) {
1634 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1636 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1637 (*chan).capture_transition_buf->get_write_vector(&transvec);
1640 if (transvec.len[0] > 0) {
1641 transvec.buf[0]->type = CaptureEnd;
1642 transvec.buf[0]->capture_val = capture_captured;
1643 (*chan).capture_transition_buf->increment_write_ptr(1);
1647 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1653 CaptureInfo* ci = new CaptureInfo;
1655 ci->start = capture_start_frame;
1656 ci->frames = capture_captured;
1658 /* XXX theoretical race condition here. Need atomic exchange ?
1659 However, the circumstances when this is called right
1660 now (either on record-disable or transport_stopped)
1661 mean that no actual race exists. I think ...
1662 We now have a capture_info_lock, but it is only to be used
1663 to synchronize in the transport_stop and the capture info
1664 accessors, so that invalidation will not occur (both non-realtime).
1667 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1669 capture_info.push_back (ci);
1670 capture_captured = 0;
1674 AudioDiskstream::set_record_enabled (bool yn)
1676 if (!recordable() || !_session.record_enabling_legal()) {
1680 /* can't rec-enable in destructive mode if transport is before start */
1682 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1686 if (yn && channels[0].source == 0) {
1688 /* pick up connections not initiated *from* the IO object
1689 we're associated with.
1692 get_input_sources ();
1695 /* yes, i know that this not proof against race conditions, but its
1696 good enough. i think.
1699 if (record_enabled() != yn) {
1701 engage_record_enable ();
1703 disengage_record_enable ();
1709 AudioDiskstream::engage_record_enable ()
1711 bool rolling = _session.transport_speed() != 0.0f;
1713 g_atomic_int_set (&_record_enabled, 1);
1714 capturing_sources.clear ();
1715 if (Config->get_use_hardware_monitoring()) {
1716 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1717 if ((*chan).source) {
1718 (*chan).source->ensure_monitor_input (!(Config->get_auto_input() && rolling));
1720 capturing_sources.push_back ((*chan).write_source);
1723 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1724 capturing_sources.push_back ((*chan).write_source);
1728 RecordEnableChanged (); /* EMIT SIGNAL */
1732 AudioDiskstream::disengage_record_enable ()
1734 g_atomic_int_set (&_record_enabled, 0);
1735 if (Config->get_use_hardware_monitoring()) {
1736 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1737 if ((*chan).source) {
1738 (*chan).source->ensure_monitor_input (false);
1742 capturing_sources.clear ();
1743 RecordEnableChanged (); /* EMIT SIGNAL */
1748 AudioDiskstream::get_state ()
1750 XMLNode* node = new XMLNode ("AudioDiskstream");
1752 LocaleGuard lg (X_("POSIX"));
1754 snprintf (buf, sizeof(buf), "0x%x", _flags);
1755 node->add_property ("flags", buf);
1757 snprintf (buf, sizeof(buf), "%zd", channels.size());
1758 node->add_property ("channels", buf);
1760 node->add_property ("playlist", _playlist->name());
1762 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1763 node->add_property ("speed", buf);
1765 node->add_property("name", _name);
1767 node->add_property("id", buf);
1769 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1771 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1772 XMLNode* cs_grandchild;
1774 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1775 cs_grandchild = new XMLNode (X_("file"));
1776 cs_grandchild->add_property (X_("path"), (*i)->path());
1777 cs_child->add_child_nocopy (*cs_grandchild);
1780 /* store the location where capture will start */
1784 if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1785 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1787 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1790 cs_child->add_property (X_("at"), buf);
1791 node->add_child_nocopy (*cs_child);
1795 node->add_child_copy (*_extra_xml);
1802 AudioDiskstream::set_state (const XMLNode& node)
1804 const XMLProperty* prop;
1805 XMLNodeList nlist = node.children();
1806 XMLNodeIterator niter;
1807 uint32_t nchans = 1;
1808 XMLNode* capture_pending_node = 0;
1809 LocaleGuard lg (X_("POSIX"));
1811 in_set_state = true;
1813 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1814 if ((*niter)->name() == IO::state_node_name) {
1815 deprecated_io_node = new XMLNode (**niter);
1818 if ((*niter)->name() == X_("CapturingSources")) {
1819 capture_pending_node = *niter;
1823 /* prevent write sources from being created */
1825 in_set_state = true;
1827 if ((prop = node.property ("name")) != 0) {
1828 _name = prop->value();
1831 if (deprecated_io_node) {
1832 if ((prop = deprecated_io_node->property ("id")) != 0) {
1833 _id = prop->value ();
1836 if ((prop = node.property ("id")) != 0) {
1837 _id = prop->value ();
1841 if ((prop = node.property ("flags")) != 0) {
1842 _flags = strtol (prop->value().c_str(), 0, 0);
1845 if ((prop = node.property ("channels")) != 0) {
1846 nchans = atoi (prop->value().c_str());
1849 // create necessary extra channels
1850 // we are always constructed with one and we always need one
1852 if (nchans > _n_channels) {
1854 // we need to add new channel infos
1855 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1857 int diff = nchans - channels.size();
1859 for (int i=0; i < diff; ++i) {
1863 } else if (nchans < _n_channels) {
1865 // we need to get rid of channels
1866 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1868 int diff = channels.size() - nchans;
1870 for (int i = 0; i < diff; ++i) {
1875 if ((prop = node.property ("playlist")) == 0) {
1880 bool had_playlist = (_playlist != 0);
1882 if (find_and_use_playlist (prop->value())) {
1886 if (!had_playlist) {
1887 _playlist->set_orig_diskstream_id (_id);
1890 if (!destructive() && capture_pending_node) {
1891 /* destructive streams have one and only one source per channel,
1892 and so they never end up in pending capture in any useful
1895 use_pending_capture_data (*capture_pending_node);
1900 if ((prop = node.property ("speed")) != 0) {
1901 double sp = atof (prop->value().c_str());
1903 if (realtime_set_speed (sp, false)) {
1904 non_realtime_set_speed ();
1908 _n_channels = channels.size();
1910 in_set_state = false;
1912 /* make sure this is clear before we do anything else */
1914 capturing_sources.clear ();
1916 /* write sources are handled when we handle the input set
1917 up of the IO that owns this DS (::non_realtime_input_change())
1920 in_set_state = false;
1926 AudioDiskstream::use_new_write_source (uint32_t n)
1928 if (!recordable()) {
1932 if (n >= channels.size()) {
1933 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1937 ChannelInfo &chan = channels[n];
1939 if (chan.write_source) {
1941 if (AudioFileSource::is_empty (_session, chan.write_source->path())) {
1942 chan.write_source->mark_for_remove ();
1943 chan.write_source.reset ();
1945 chan.write_source.reset ();
1950 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
1951 throw failed_constructor();
1955 catch (failed_constructor &err) {
1956 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1957 chan.write_source.reset ();
1961 /* do not remove destructive files even if they are empty */
1963 chan.write_source->set_allow_remove_if_empty (!destructive());
1969 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1971 ChannelList::iterator chan;
1974 if (!recordable()) {
1978 capturing_sources.clear ();
1980 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
1981 if (!destructive()) {
1983 if ((*chan).write_source && mark_write_complete) {
1984 (*chan).write_source->mark_streaming_write_completed ();
1986 use_new_write_source (n);
1988 if (record_enabled()) {
1989 capturing_sources.push_back ((*chan).write_source);
1993 if ((*chan).write_source == 0) {
1994 use_new_write_source (n);
1999 if (destructive()) {
2001 /* we now have all our write sources set up, so create the
2002 playlist's single region.
2005 if (_playlist->empty()) {
2006 setup_destructive_playlist ();
2012 AudioDiskstream::rename_write_sources ()
2014 ChannelList::iterator chan;
2017 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2018 if ((*chan).write_source != 0) {
2019 (*chan).write_source->set_name (_name, destructive());
2020 /* XXX what to do if one of them fails ? */
2028 AudioDiskstream::set_block_size (jack_nframes_t nframes)
2030 if (_session.get_block_size() > speed_buffer_size) {
2031 speed_buffer_size = _session.get_block_size();
2033 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2034 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2035 (*chan).speed_buffer = new Sample[speed_buffer_size];
2038 allocate_temporary_buffers ();
2042 AudioDiskstream::allocate_temporary_buffers ()
2044 /* make sure the wrap buffer is at least large enough to deal
2045 with the speeds up to 1.2, to allow for micro-variation
2046 when slaving to MTC, SMPTE etc.
2049 double sp = max (fabsf (_actual_speed), 1.2f);
2050 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * sp) + 1;
2052 if (required_wrap_size > wrap_buffer_size) {
2054 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2055 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2056 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2057 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2058 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2061 wrap_buffer_size = required_wrap_size;
2066 AudioDiskstream::monitor_input (bool yn)
2068 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2070 if ((*chan).source) {
2071 (*chan).source->ensure_monitor_input (yn);
2077 AudioDiskstream::set_align_style_from_io ()
2079 bool have_physical = false;
2085 get_input_sources ();
2087 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2088 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2089 have_physical = true;
2094 if (have_physical) {
2095 set_align_style (ExistingMaterial);
2097 set_align_style (CaptureTime);
2102 AudioDiskstream::add_channel ()
2104 /* XXX need to take lock??? */
2108 init_channel (chan);
2110 chan.speed_buffer = new Sample[speed_buffer_size];
2111 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2112 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2114 channels.push_back (chan);
2116 _n_channels = channels.size();
2122 AudioDiskstream::remove_channel ()
2124 if (channels.size() > 1) {
2125 /* XXX need to take lock??? */
2126 ChannelInfo & chan = channels.back();
2127 destroy_channel (chan);
2128 channels.pop_back();
2130 _n_channels = channels.size();
2138 AudioDiskstream::playback_buffer_load () const
2140 return (float) ((double) channels.front().playback_buf->read_space()/
2141 (double) channels.front().playback_buf->bufsize());
2145 AudioDiskstream::capture_buffer_load () const
2147 return (float) ((double) channels.front().capture_buf->write_space()/
2148 (double) channels.front().capture_buf->bufsize());
2152 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2154 const XMLProperty* prop;
2155 XMLNodeList nlist = node.children();
2156 XMLNodeIterator niter;
2157 boost::shared_ptr<AudioFileSource> fs;
2158 boost::shared_ptr<AudioFileSource> first_fs;
2159 SourceList pending_sources;
2160 jack_nframes_t position;
2162 if ((prop = node.property (X_("at"))) == 0) {
2166 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2170 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2171 if ((*niter)->name() == X_("file")) {
2173 if ((prop = (*niter)->property (X_("path"))) == 0) {
2178 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (_session, prop->value(), false, _session.frame_rate()));
2181 catch (failed_constructor& err) {
2182 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2183 _name, prop->value())
2188 pending_sources.push_back (fs);
2190 if (first_fs == 0) {
2194 fs->set_captured_for (_name);
2198 if (pending_sources.size() == 0) {
2199 /* nothing can be done */
2203 if (pending_sources.size() != _n_channels) {
2204 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2209 boost::shared_ptr<AudioRegion> region;
2212 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(),
2213 region_name_from_path (first_fs->name()),
2214 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
2215 region->special_set_position (0);
2218 catch (failed_constructor& err) {
2219 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2227 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name())));
2230 catch (failed_constructor& err) {
2231 error << string_compose (_("%1: cannot create region from pending capture sources"),
2238 _playlist->add_region (region, position);