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/audio_port.h>
52 #include <ardour/source_factory.h>
58 using namespace ARDOUR;
61 size_t AudioDiskstream::_working_buffers_size = 0;
62 Sample* AudioDiskstream::_mixdown_buffer = 0;
63 gain_t* AudioDiskstream::_gain_buffer = 0;
65 AudioDiskstream::AudioDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
66 : Diskstream(sess, name, flag)
67 , deprecated_io_node(NULL)
69 /* prevent any write sources from being created */
79 AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
80 : Diskstream(sess, node)
81 , deprecated_io_node(NULL)
86 if (set_state (node)) {
88 throw failed_constructor();
94 use_destructive_playlist ();
99 AudioDiskstream::init_channel (ChannelInfo &chan)
101 chan.playback_wrap_buffer = 0;
102 chan.capture_wrap_buffer = 0;
103 chan.speed_buffer = 0;
104 chan.peak_power = 0.0f;
106 chan.current_capture_buffer = 0;
107 chan.current_playback_buffer = 0;
108 chan.curr_capture_cnt = 0;
110 chan.playback_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
111 chan.capture_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
112 chan.capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
115 /* touch the ringbuffer buffers, which will cause
116 them to be mapped into locked physical RAM if
117 we're running with mlockall(). this doesn't do
120 memset (chan.playback_buf->buffer(), 0, sizeof (Sample) * chan.playback_buf->bufsize());
121 memset (chan.capture_buf->buffer(), 0, sizeof (Sample) * chan.capture_buf->bufsize());
122 memset (chan.capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * chan.capture_transition_buf->bufsize());
127 AudioDiskstream::init (Diskstream::Flag f)
131 /* there are no channels at this point, so these
132 two calls just get speed_buffer_size and wrap_buffer
133 size setup without duplicating their code.
136 set_block_size (_session.get_block_size());
137 allocate_temporary_buffers ();
140 assert(_n_channels == ChanCount(DataType::AUDIO, 1));
144 AudioDiskstream::destroy_channel (ChannelInfo &chan)
146 if (chan.write_source) {
147 chan.write_source.reset ();
150 if (chan.speed_buffer) {
151 delete [] chan.speed_buffer;
154 if (chan.playback_wrap_buffer) {
155 delete [] chan.playback_wrap_buffer;
157 if (chan.capture_wrap_buffer) {
158 delete [] chan.capture_wrap_buffer;
161 delete chan.playback_buf;
162 delete chan.capture_buf;
163 delete chan.capture_transition_buf;
165 chan.playback_buf = 0;
166 chan.capture_buf = 0;
169 AudioDiskstream::~AudioDiskstream ()
171 Glib::Mutex::Lock lm (state_lock);
173 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan)
174 destroy_channel((*chan));
180 AudioDiskstream::allocate_working_buffers()
182 assert(disk_io_frames() > 0);
184 _working_buffers_size = disk_io_frames();
185 _mixdown_buffer = new Sample[_working_buffers_size];
186 _gain_buffer = new gain_t[_working_buffers_size];
190 AudioDiskstream::free_working_buffers()
192 delete [] _mixdown_buffer;
193 delete [] _gain_buffer;
194 _working_buffers_size = 0;
200 AudioDiskstream::non_realtime_input_change ()
203 Glib::Mutex::Lock lm (state_lock);
205 if (input_change_pending == NoChange) {
209 if (input_change_pending & ConfigurationChanged) {
211 if (_io->n_inputs().get(DataType::AUDIO) > _n_channels.get(DataType::AUDIO)) {
213 // we need to add new channel infos
215 int diff = _io->n_inputs().get(DataType::AUDIO) - channels.size();
217 for (int i = 0; i < diff; ++i) {
221 } else if (_io->n_inputs().get(DataType::AUDIO) < _n_channels.get(DataType::AUDIO)) {
223 // we need to get rid of channels
225 int diff = channels.size() - _io->n_inputs().get(DataType::AUDIO);
227 for (int i = 0; i < diff; ++i) {
233 get_input_sources ();
234 set_capture_offset ();
236 if (first_input_change) {
237 set_align_style (_persistent_alignment_style);
238 first_input_change = false;
240 set_align_style_from_io ();
243 input_change_pending = NoChange;
246 /* reset capture files */
248 reset_write_sources (false);
250 /* now refill channel buffers */
252 if (speed() != 1.0f || speed() != -1.0f) {
253 seek ((nframes_t) (_session.transport_frame() * (double) speed()));
255 seek (_session.transport_frame());
260 AudioDiskstream::get_input_sources ()
262 uint32_t ni = _io->n_inputs().get(DataType::AUDIO);
264 for (uint32_t n = 0; n < ni; ++n) {
266 const char **connections = _io->input(n)->get_connections ();
267 ChannelInfo& chan = channels[n];
269 if (connections == 0 || connections[0] == 0) {
272 // _source->disable_metering ();
278 chan.source = dynamic_cast<AudioPort*>(
279 _session.engine().get_port_by_name (connections[0]) );
289 AudioDiskstream::find_and_use_playlist (const string& name)
292 AudioPlaylist* playlist;
294 if ((pl = _session.playlist_by_name (name)) == 0) {
295 playlist = new AudioPlaylist(_session, name);
299 if ((playlist = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
300 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
304 return use_playlist (playlist);
308 AudioDiskstream::use_playlist (Playlist* playlist)
310 assert(dynamic_cast<AudioPlaylist*>(playlist));
312 Diskstream::use_playlist(playlist);
318 AudioDiskstream::use_new_playlist ()
321 AudioPlaylist* playlist;
323 if (!in_set_state && destructive()) {
328 newname = Playlist::bump_name (_playlist->name(), _session);
330 newname = Playlist::bump_name (_name, _session);
333 if ((playlist = new AudioPlaylist (_session, newname, hidden())) != 0) {
334 playlist->set_orig_diskstream_id (id());
335 return use_playlist (playlist);
342 AudioDiskstream::use_copy_playlist ()
344 assert(audio_playlist());
350 if (_playlist == 0) {
351 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
356 AudioPlaylist* playlist;
358 newname = Playlist::bump_name (_playlist->name(), _session);
360 if ((playlist = new AudioPlaylist (*audio_playlist(), newname)) != 0) {
361 playlist->set_orig_diskstream_id (id());
362 return use_playlist (playlist);
369 AudioDiskstream::setup_destructive_playlist ()
373 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
374 srcs.push_back ((*chan).write_source);
377 /* a single full-sized region */
379 boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, max_frames - srcs.front()->natural_position(), _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 boost::shared_ptr<Region> rp = _playlist->find_next_region (_session.current_start_frame(), Start, 1);
395 reset_write_sources (false, true);
399 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rp);
402 throw failed_constructor();
406 ChannelList::iterator chan;
408 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
409 (*chan).write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
410 assert((*chan).write_source);
411 (*chan).write_source->set_allow_remove_if_empty (false);
414 /* the source list will never be reset for a destructive track */
418 AudioDiskstream::check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record)
420 int possibly_recording;
423 const int transport_rolling = 0x4;
424 const int track_rec_enabled = 0x2;
425 const int global_rec_enabled = 0x1;
427 /* merge together the 3 factors that affect record status, and compute
431 rolling = _session.transport_speed() != 0.0f;
432 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
433 change = possibly_recording ^ last_possibly_recording;
435 if (possibly_recording == last_possibly_recording) {
441 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
443 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
444 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
446 /* starting to record: compute first+last frames */
448 first_recordable_frame = transport_frame + _capture_offset;
449 last_recordable_frame = max_frames;
450 capture_start_frame = transport_frame;
452 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
454 /* was stopped, now rolling (and recording) */
456 if (_alignment_style == ExistingMaterial) {
457 first_recordable_frame += _session.worst_output_latency();
459 first_recordable_frame += _roll_delay;
464 /* was rolling, but record state changed */
466 if (_alignment_style == ExistingMaterial) {
468 if (!Config->get_punch_in()) {
470 /* manual punch in happens at the correct transport frame
471 because the user hit a button. but to get alignment correct
472 we have to back up the position of the new region to the
473 appropriate spot given the roll delay.
476 capture_start_frame -= _roll_delay;
478 /* XXX paul notes (august 2005): i don't know why
482 first_recordable_frame += _capture_offset;
486 /* autopunch toggles recording at the precise
487 transport frame, and then the DS waits
488 to start recording for a time that depends
489 on the output latency.
492 first_recordable_frame += _session.worst_output_latency();
497 if (Config->get_punch_in()) {
498 first_recordable_frame += _roll_delay;
500 capture_start_frame -= _roll_delay;
506 if (_flags & Recordable) {
507 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
509 RingBufferNPT<CaptureTransition>::rw_vector transvec;
510 (*chan).capture_transition_buf->get_write_vector(&transvec);
512 if (transvec.len[0] > 0) {
513 transvec.buf[0]->type = CaptureStart;
514 transvec.buf[0]->capture_val = capture_start_frame;
515 (*chan).capture_transition_buf->increment_write_ptr(1);
519 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
525 } else if (!record_enabled() || !can_record) {
529 last_recordable_frame = transport_frame + _capture_offset;
531 if (_alignment_style == ExistingMaterial) {
532 last_recordable_frame += _session.worst_output_latency();
534 last_recordable_frame += _roll_delay;
538 last_possibly_recording = possibly_recording;
542 AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_t offset, bool can_record, bool rec_monitors_input)
545 ChannelList::iterator c;
547 nframes_t rec_offset = 0;
548 nframes_t rec_nframes = 0;
549 bool nominally_recording;
550 bool re = record_enabled ();
551 bool collect_playback = false;
553 /* if we've already processed the frames corresponding to this call,
554 just return. this allows multiple routes that are taking input
555 from this diskstream to call our ::process() method, but have
556 this stuff only happen once. more commonly, it allows both
557 the AudioTrack that is using this AudioDiskstream *and* the Session
558 to call process() without problems.
565 check_record_status (transport_frame, nframes, can_record);
567 nominally_recording = (can_record && re);
574 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
575 must always be called as a pair. The only exception is if this function
576 returns a non-zero value, in which case, ::commit should not be called.
579 // If we can't take the state lock return.
580 if (!state_lock.trylock()) {
584 adjust_capture_position = 0;
586 for (c = channels.begin(); c != channels.end(); ++c) {
587 (*c).current_capture_buffer = 0;
588 (*c).current_playback_buffer = 0;
591 if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
594 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
601 case OverlapInternal:
602 /* ---------- recrange
605 rec_nframes = nframes;
610 /* |--------| recrange
613 rec_nframes = transport_frame + nframes - first_recordable_frame;
615 rec_offset = first_recordable_frame - transport_frame;
620 /* |--------| recrange
623 rec_nframes = last_recordable_frame - transport_frame;
627 case OverlapExternal:
628 /* |--------| recrange
629 -------------- transrange
631 rec_nframes = last_recordable_frame - last_recordable_frame;
632 rec_offset = first_recordable_frame - transport_frame;
636 if (rec_nframes && !was_recording) {
637 capture_captured = 0;
638 was_recording = true;
643 if (can_record && !_last_capture_regions.empty()) {
644 _last_capture_regions.clear ();
647 if (nominally_recording || rec_nframes) {
649 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
651 ChannelInfo& chan (*c);
653 chan.capture_buf->get_write_vector (&chan.capture_vector);
655 if (rec_nframes <= chan.capture_vector.len[0]) {
657 chan.current_capture_buffer = chan.capture_vector.buf[0];
659 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
663 AudioPort* const ap = _io->audio_input(n);
665 assert(rec_nframes <= ap->get_audio_buffer().capacity());
667 memcpy (chan.current_capture_buffer, ap->get_audio_buffer().data(rec_nframes, offset + rec_offset), sizeof (Sample) * rec_nframes);
671 nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
673 if (rec_nframes > total) {
678 AudioPort* const ap = _io->audio_input(n);
681 Sample* buf = ap->get_audio_buffer().data(nframes, offset);
682 nframes_t first = chan.capture_vector.len[0];
684 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
685 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
686 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
687 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
689 chan.current_capture_buffer = chan.capture_wrap_buffer;
696 finish_capture (rec_monitors_input);
703 /* data will be written to disk */
705 if (rec_nframes == nframes && rec_offset == 0) {
707 for (c = channels.begin(); c != channels.end(); ++c) {
708 (*c).current_playback_buffer = (*c).current_capture_buffer;
711 playback_distance = nframes;
716 /* we can't use the capture buffer as the playback buffer, because
717 we recorded only a part of the current process' cycle data
721 collect_playback = true;
724 adjust_capture_position = rec_nframes;
726 } else if (nominally_recording) {
728 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
730 for (c = channels.begin(); c != channels.end(); ++c) {
731 (*c).current_playback_buffer = (*c).current_capture_buffer;
734 playback_distance = nframes;
738 collect_playback = true;
741 if (collect_playback) {
743 /* we're doing playback */
745 nframes_t necessary_samples;
747 /* no varispeed playback if we're recording, because the output .... TBD */
749 if (rec_nframes == 0 && _actual_speed != 1.0f) {
750 necessary_samples = (nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
752 necessary_samples = nframes;
755 for (c = channels.begin(); c != channels.end(); ++c) {
756 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
761 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
763 ChannelInfo& chan (*c);
765 if (necessary_samples <= chan.playback_vector.len[0]) {
767 chan.current_playback_buffer = chan.playback_vector.buf[0];
770 nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
772 if (necessary_samples > total) {
778 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
779 chan.playback_vector.len[0] * sizeof (Sample));
780 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
781 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
783 chan.current_playback_buffer = chan.playback_wrap_buffer;
788 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
790 uint64_t phase = last_phase;
793 // Linearly interpolate into the alt buffer
794 // using 40.24 fixp maths (swh)
796 for (c = channels.begin(); c != channels.end(); ++c) {
799 ChannelInfo& chan (*c);
804 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
806 fr = (phase & 0xFFFFFF) / 16777216.0f;
807 chan.speed_buffer[outsample] =
808 chan.current_playback_buffer[i] * (1.0f - fr) +
809 chan.current_playback_buffer[i+1] * fr;
813 chan.current_playback_buffer = chan.speed_buffer;
816 playback_distance = i + 1;
817 last_phase = (phase & 0xFFFFFF);
820 playback_distance = nframes;
832 /* we're exiting with failure, so ::commit will not
833 be called. unlock the state lock.
843 AudioDiskstream::commit (nframes_t nframes)
845 bool need_butler = false;
847 if (_actual_speed < 0.0) {
848 playback_sample -= playback_distance;
850 playback_sample += playback_distance;
853 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
855 (*chan).playback_buf->increment_read_ptr (playback_distance);
857 if (adjust_capture_position) {
858 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
862 if (adjust_capture_position != 0) {
863 capture_captured += adjust_capture_position;
864 adjust_capture_position = 0;
868 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
870 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
871 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
882 AudioDiskstream::set_pending_overwrite (bool yn)
884 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
886 pending_overwrite = yn;
888 overwrite_frame = playback_sample;
889 overwrite_offset = channels.front().playback_buf->get_read_ptr();
893 AudioDiskstream::overwrite_existing_buffers ()
895 Sample* mixdown_buffer;
898 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
900 overwrite_queued = false;
902 /* assume all are the same size */
903 nframes_t size = channels[0].playback_buf->bufsize();
905 mixdown_buffer = new Sample[size];
906 gain_buffer = new float[size];
908 /* reduce size so that we can fill the buffer correctly. */
914 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
916 start = overwrite_frame;
917 nframes_t cnt = size;
919 /* to fill the buffer without resetting the playback sample, we need to
920 do it one or two chunks (normally two).
922 |----------------------------------------------------------------------|
926 |<- second chunk->||<----------------- first chunk ------------------>|
930 nframes_t to_read = size - overwrite_offset;
932 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, *chan, n, reversed)) {
933 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
934 _id, size, playback_sample) << endmsg;
942 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer,
943 start, cnt, *chan, n, reversed)) {
944 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
945 _id, size, playback_sample) << endmsg;
954 pending_overwrite = false;
955 delete [] gain_buffer;
956 delete [] mixdown_buffer;
961 AudioDiskstream::seek (nframes_t frame, bool complete_refill)
963 Glib::Mutex::Lock lm (state_lock);
966 ChannelList::iterator chan;
968 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
969 (*chan).playback_buf->reset ();
970 (*chan).capture_buf->reset ();
973 /* can't rec-enable in destructive mode if transport is before start */
975 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
976 disengage_record_enable ();
979 playback_sample = frame;
982 if (complete_refill) {
983 while ((ret = do_refill_with_alloc ()) > 0) ;
985 ret = do_refill_with_alloc ();
992 AudioDiskstream::can_internal_playback_seek (nframes_t distance)
994 ChannelList::iterator chan;
996 for (chan = channels.begin(); chan != channels.end(); ++chan) {
997 if ((*chan).playback_buf->read_space() < distance) {
1005 AudioDiskstream::internal_playback_seek (nframes_t distance)
1007 ChannelList::iterator chan;
1009 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1010 (*chan).playback_buf->increment_read_ptr (distance);
1013 first_recordable_frame += distance;
1014 playback_sample += distance;
1020 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, nframes_t& start, nframes_t cnt,
1021 ChannelInfo& channel_info, int channel, bool reversed)
1023 nframes_t this_read = 0;
1024 bool reloop = false;
1025 nframes_t loop_end = 0;
1026 nframes_t loop_start = 0;
1027 nframes_t loop_length = 0;
1028 nframes_t offset = 0;
1032 /* Make the use of a Location atomic for this read operation.
1034 Note: Locations don't get deleted, so all we care about
1035 when I say "atomic" is that we are always pointing to
1036 the same one and using a start/length values obtained
1040 if ((loc = loop_location) != 0) {
1041 loop_start = loc->start();
1042 loop_end = loc->end();
1043 loop_length = loop_end - loop_start;
1046 /* if we are looping, ensure that the first frame we read is at the correct
1047 position within the loop.
1050 if (loc && start >= loop_end) {
1051 //cerr << "start adjusted from " << start;
1052 start = loop_start + ((start - loop_start) % loop_length);
1053 //cerr << "to " << start << endl;
1055 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1060 /* take any loop into account. we can't read past the end of the loop. */
1062 if (loc && (loop_end - start < cnt)) {
1063 this_read = loop_end - start;
1064 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1071 if (this_read == 0) {
1075 this_read = min(cnt,this_read);
1077 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1078 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1083 _read_data_count = _playlist->read_data_count();
1087 /* don't adjust start, since caller has already done that
1090 swap_by_ptr (buf, buf + this_read - 1);
1094 /* if we read to the end of the loop, go back to the beginning */
1104 offset += this_read;
1111 AudioDiskstream::do_refill_with_alloc()
1113 Sample* mix_buf = new Sample[disk_io_chunk_frames];
1114 float* gain_buf = new float[disk_io_chunk_frames];
1116 int ret = _do_refill(mix_buf, gain_buf);
1125 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
1129 RingBufferNPT<Sample>::rw_vector vector;
1130 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1131 nframes_t total_space;
1132 nframes_t zero_fill;
1134 ChannelList::iterator i;
1137 assert(mixdown_buffer);
1138 assert(gain_buffer);
1140 channels.front().playback_buf->get_write_vector (&vector);
1142 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1146 /* if there are 2+ chunks of disk i/o possible for
1147 this track, let the caller know so that it can arrange
1148 for us to be called again, ASAP.
1151 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1155 /* if we're running close to normal speed and there isn't enough
1156 space to do disk_io_chunk_frames of I/O, then don't bother.
1158 at higher speeds, just do it because the sync between butler
1159 and audio thread may not be good enough.
1162 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1166 /* when slaved, don't try to get too close to the read pointer. this
1167 leaves space for the buffer reversal to have something useful to
1171 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1175 total_space = min (disk_io_chunk_frames, total_space);
1179 if (file_frame == 0) {
1181 /* at start: nothing to do but fill with silence */
1183 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1185 ChannelInfo& chan (*i);
1186 chan.playback_buf->get_write_vector (&vector);
1187 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1188 if (vector.len[1]) {
1189 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1191 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1196 if (file_frame < total_space) {
1198 /* too close to the start: read what we can,
1199 and then zero fill the rest
1202 zero_fill = total_space - file_frame;
1203 total_space = file_frame;
1208 /* move read position backwards because we are going
1209 to reverse the data.
1212 file_frame -= total_space;
1218 if (file_frame == max_frames) {
1220 /* at end: nothing to do but fill with silence */
1222 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1224 ChannelInfo& chan (*i);
1225 chan.playback_buf->get_write_vector (&vector);
1226 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1227 if (vector.len[1]) {
1228 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1230 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1235 if (file_frame > max_frames - total_space) {
1237 /* to close to the end: read what we can, and zero fill the rest */
1239 zero_fill = total_space - (max_frames - file_frame);
1240 total_space = max_frames - file_frame;
1247 nframes_t file_frame_tmp = 0;
1249 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1251 ChannelInfo& chan (*i);
1254 nframes_t len1, len2;
1256 chan.playback_buf->get_write_vector (&vector);
1259 file_frame_tmp = file_frame;
1262 buf1 = vector.buf[1];
1263 len1 = vector.len[1];
1264 buf2 = vector.buf[0];
1265 len2 = vector.len[0];
1267 buf1 = vector.buf[0];
1268 len1 = vector.len[0];
1269 buf2 = vector.buf[1];
1270 len2 = vector.len[1];
1274 to_read = min (ts, len1);
1275 to_read = min (to_read, disk_io_chunk_frames);
1279 if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1284 chan.playback_buf->increment_write_ptr (to_read);
1288 to_read = min (ts, len2);
1293 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1294 so read some or all of vector.len[1] as well.
1297 if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1302 chan.playback_buf->increment_write_ptr (to_read);
1311 file_frame = file_frame_tmp;
1318 /** Flush pending data to disk.
1320 * Important note: this function will write *AT MOST* disk_io_chunk_frames
1321 * of data to disk. it will never write more than that. If it writes that
1322 * much and there is more than that waiting to be written, it will return 1,
1323 * otherwise 0 on success or -1 on failure.
1325 * If there is less than disk_io_chunk_frames to be written, no data will be
1326 * written at all unless @a force_flush is true.
1329 AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
1333 RingBufferNPT<Sample>::rw_vector vector;
1334 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1337 _write_data_count = 0;
1339 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1341 (*chan).capture_buf->get_read_vector (&vector);
1343 total = vector.len[0] + vector.len[1];
1346 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1350 /* if there are 2+ chunks of disk i/o possible for
1351 this track, let the caller know so that it can arrange
1352 for us to be called again, ASAP.
1354 if we are forcing a flush, then if there is* any* extra
1355 work, let the caller know.
1357 if we are no longer recording and there is any extra work,
1358 let the caller know too.
1361 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1365 to_write = min (disk_io_chunk_frames, (nframes_t) vector.len[0]);
1367 // check the transition buffer when recording destructive
1368 // important that we get this after the capture buf
1370 if (destructive()) {
1371 (*chan).capture_transition_buf->get_read_vector(&transvec);
1372 size_t transcount = transvec.len[0] + transvec.len[1];
1373 bool have_start = false;
1376 for (ti=0; ti < transcount; ++ti) {
1377 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1379 if (captrans.type == CaptureStart) {
1380 // by definition, the first data we got above represents the given capture pos
1382 (*chan).write_source->mark_capture_start (captrans.capture_val);
1383 (*chan).curr_capture_cnt = 0;
1387 else if (captrans.type == CaptureEnd) {
1389 // capture end, the capture_val represents total frames in capture
1391 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1393 // shorten to make the write a perfect fit
1394 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1396 if (nto_write < to_write) {
1397 ret = 1; // should we?
1399 to_write = nto_write;
1401 (*chan).write_source->mark_capture_end ();
1403 // increment past this transition, but go no further
1408 // actually ends just beyond this chunk, so force more work
1416 (*chan).capture_transition_buf->increment_read_ptr(ti);
1420 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write) != to_write) {
1421 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1425 (*chan).capture_buf->increment_read_ptr (to_write);
1426 (*chan).curr_capture_cnt += to_write;
1428 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1430 /* we wrote all of vector.len[0] but it wasn't an entire
1431 disk_io_chunk_frames of data, so arrange for some part
1432 of vector.len[1] to be flushed to disk as well.
1435 to_write = min ((nframes_t)(disk_io_chunk_frames - to_write), (nframes_t) vector.len[1]);
1437 if ((*chan).write_source->write (vector.buf[1], to_write) != to_write) {
1438 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1442 _write_data_count += (*chan).write_source->write_data_count();
1444 (*chan).capture_buf->increment_read_ptr (to_write);
1445 (*chan).curr_capture_cnt += to_write;
1454 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1456 uint32_t buffer_position;
1457 bool more_work = true;
1459 boost::shared_ptr<AudioRegion> region;
1460 nframes_t total_capture;
1462 SourceList::iterator src;
1463 ChannelList::iterator chan;
1464 vector<CaptureInfo*>::iterator ci;
1466 bool mark_write_completed = false;
1468 finish_capture (true);
1470 /* butler is already stopped, but there may be work to do
1471 to flush remaining data to disk.
1474 while (more_work && !err) {
1475 switch (do_flush (Session::TransportContext, true)) {
1482 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1487 /* XXX is there anything we can do if err != 0 ? */
1488 Glib::Mutex::Lock lm (capture_info_lock);
1490 if (capture_info.empty()) {
1494 if (abort_capture) {
1496 if (destructive()) {
1500 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1502 if ((*chan).write_source) {
1504 (*chan).write_source->mark_for_remove ();
1505 (*chan).write_source->drop_references ();
1506 (*chan).write_source.reset ();
1509 /* new source set up in "out" below */
1515 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1516 total_capture += (*ci)->frames;
1519 /* figure out the name for this take */
1521 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1523 boost::shared_ptr<AudioFileSource> s = (*chan).write_source;
1527 s->update_header (capture_info.front()->start, when, twhen);
1528 s->set_captured_for (_name);
1533 /* destructive tracks have a single, never changing region */
1535 if (destructive()) {
1537 /* send a signal that any UI can pick up to do the right thing. there is
1538 a small problem here in that a UI may need the peak data to be ready
1539 for the data that was recorded and this isn't interlocked with that
1540 process. this problem is deferred to the UI.
1543 _playlist->Modified();
1547 /* Register a new region with the Session that
1548 describes the entire source. Do this first
1549 so that any sub-regions will obviously be
1550 children of this one (later!)
1554 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1555 region_name_from_path (channels[0].write_source->name()),
1556 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
1558 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1559 region->special_set_position (capture_info.front()->start);
1563 catch (failed_constructor& err) {
1564 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1568 _last_capture_regions.push_back (region);
1570 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1572 XMLNode &before = _playlist->get_state();
1573 _playlist->freeze ();
1575 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1578 _session.region_name (region_name, channels[0].write_source->name(), false);
1580 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
1583 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1584 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1587 catch (failed_constructor& err) {
1588 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1589 continue; /* XXX is this OK? */
1592 _last_capture_regions.push_back (region);
1594 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1596 i_am_the_modifier++;
1597 _playlist->add_region (region, (*ci)->start);
1598 i_am_the_modifier--;
1600 buffer_position += (*ci)->frames;
1604 XMLNode &after = _playlist->get_state();
1605 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1608 mark_write_completed = true;
1611 reset_write_sources (mark_write_completed);
1615 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1619 capture_info.clear ();
1620 capture_start_frame = 0;
1624 AudioDiskstream::finish_capture (bool rec_monitors_input)
1626 was_recording = false;
1628 if (capture_captured == 0) {
1632 if (recordable() && destructive()) {
1633 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1635 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1636 (*chan).capture_transition_buf->get_write_vector(&transvec);
1639 if (transvec.len[0] > 0) {
1640 transvec.buf[0]->type = CaptureEnd;
1641 transvec.buf[0]->capture_val = capture_captured;
1642 (*chan).capture_transition_buf->increment_write_ptr(1);
1646 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1652 CaptureInfo* ci = new CaptureInfo;
1654 ci->start = capture_start_frame;
1655 ci->frames = capture_captured;
1657 /* XXX theoretical race condition here. Need atomic exchange ?
1658 However, the circumstances when this is called right
1659 now (either on record-disable or transport_stopped)
1660 mean that no actual race exists. I think ...
1661 We now have a capture_info_lock, but it is only to be used
1662 to synchronize in the transport_stop and the capture info
1663 accessors, so that invalidation will not occur (both non-realtime).
1666 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1668 capture_info.push_back (ci);
1669 capture_captured = 0;
1673 AudioDiskstream::set_record_enabled (bool yn)
1675 if (!recordable() || !_session.record_enabling_legal()) {
1679 /* can't rec-enable in destructive mode if transport is before start */
1681 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1685 if (yn && channels[0].source == 0) {
1687 /* pick up connections not initiated *from* the IO object
1688 we're associated with.
1691 get_input_sources ();
1694 /* yes, i know that this not proof against race conditions, but its
1695 good enough. i think.
1698 if (record_enabled() != yn) {
1700 engage_record_enable ();
1702 disengage_record_enable ();
1708 AudioDiskstream::engage_record_enable ()
1710 bool rolling = _session.transport_speed() != 0.0f;
1712 g_atomic_int_set (&_record_enabled, 1);
1713 capturing_sources.clear ();
1714 if (Config->get_monitoring_model() == HardwareMonitoring) {
1715 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1716 if ((*chan).source) {
1717 (*chan).source->ensure_monitor_input (!(Config->get_auto_input() && rolling));
1719 capturing_sources.push_back ((*chan).write_source);
1722 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1723 capturing_sources.push_back ((*chan).write_source);
1727 RecordEnableChanged (); /* EMIT SIGNAL */
1731 AudioDiskstream::disengage_record_enable ()
1733 g_atomic_int_set (&_record_enabled, 0);
1734 if (Config->get_monitoring_model() == HardwareMonitoring) {
1735 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1736 if ((*chan).source) {
1737 (*chan).source->ensure_monitor_input (false);
1741 capturing_sources.clear ();
1742 RecordEnableChanged (); /* EMIT SIGNAL */
1747 AudioDiskstream::get_state ()
1749 XMLNode* node = new XMLNode ("AudioDiskstream");
1751 LocaleGuard lg (X_("POSIX"));
1753 snprintf (buf, sizeof(buf), "0x%x", _flags);
1754 node->add_property ("flags", buf);
1756 snprintf (buf, sizeof(buf), "%zd", channels.size());
1757 node->add_property ("channels", buf);
1759 node->add_property ("playlist", _playlist->name());
1761 snprintf (buf, sizeof(buf), "%.12g", _visible_speed);
1762 node->add_property ("speed", buf);
1764 node->add_property("name", _name);
1765 id().print (buf, sizeof (buf));
1766 node->add_property("id", buf);
1768 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1770 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1771 XMLNode* cs_grandchild;
1773 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1774 cs_grandchild = new XMLNode (X_("file"));
1775 cs_grandchild->add_property (X_("path"), (*i)->path());
1776 cs_child->add_child_nocopy (*cs_grandchild);
1779 /* store the location where capture will start */
1783 if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1784 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1786 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1789 cs_child->add_property (X_("at"), buf);
1790 node->add_child_nocopy (*cs_child);
1794 node->add_child_copy (*_extra_xml);
1801 AudioDiskstream::set_state (const XMLNode& node)
1803 const XMLProperty* prop;
1804 XMLNodeList nlist = node.children();
1805 XMLNodeIterator niter;
1806 uint32_t nchans = 1;
1807 XMLNode* capture_pending_node = 0;
1808 LocaleGuard lg (X_("POSIX"));
1810 in_set_state = true;
1812 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1813 if ((*niter)->name() == IO::state_node_name) {
1814 deprecated_io_node = new XMLNode (**niter);
1817 if ((*niter)->name() == X_("CapturingSources")) {
1818 capture_pending_node = *niter;
1822 /* prevent write sources from being created */
1824 in_set_state = true;
1826 if ((prop = node.property ("name")) != 0) {
1827 _name = prop->value();
1830 if (deprecated_io_node) {
1831 if ((prop = deprecated_io_node->property ("id")) != 0) {
1832 _id = prop->value ();
1835 if ((prop = node.property ("id")) != 0) {
1836 _id = prop->value ();
1840 if ((prop = node.property ("flags")) != 0) {
1841 _flags = strtol (prop->value().c_str(), 0, 0);
1844 if ((prop = node.property ("channels")) != 0) {
1845 nchans = atoi (prop->value().c_str());
1848 // create necessary extra channels
1849 // we are always constructed with one and we always need one
1851 if (nchans > _n_channels.get(DataType::AUDIO)) {
1853 // we need to add new channel infos
1854 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1856 int diff = nchans - channels.size();
1858 for (int i=0; i < diff; ++i) {
1862 } else if (nchans < _n_channels.get(DataType::AUDIO)) {
1864 // we need to get rid of channels
1865 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1867 int diff = channels.size() - nchans;
1869 for (int i = 0; i < diff; ++i) {
1874 if ((prop = node.property ("playlist")) == 0) {
1879 bool had_playlist = (_playlist != 0);
1881 if (find_and_use_playlist (prop->value())) {
1885 if (!had_playlist) {
1886 _playlist->set_orig_diskstream_id (_id);
1889 if (!destructive() && capture_pending_node) {
1890 /* destructive streams have one and only one source per channel,
1891 and so they never end up in pending capture in any useful
1894 use_pending_capture_data (*capture_pending_node);
1899 if ((prop = node.property ("speed")) != 0) {
1900 double sp = atof (prop->value().c_str());
1902 if (realtime_set_speed (sp, false)) {
1903 non_realtime_set_speed ();
1907 _n_channels.set(DataType::AUDIO, channels.size());
1909 in_set_state = false;
1911 /* make sure this is clear before we do anything else */
1913 capturing_sources.clear ();
1915 /* write sources are handled when we handle the input set
1916 up of the IO that owns this DS (::non_realtime_input_change())
1919 in_set_state = false;
1925 AudioDiskstream::use_new_write_source (uint32_t n)
1927 if (!recordable()) {
1931 if (n >= channels.size()) {
1932 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1936 ChannelInfo &chan = channels[n];
1938 if (chan.write_source) {
1939 chan.write_source->set_allow_remove_if_empty (true);
1940 chan.write_source.reset ();
1944 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
1945 throw failed_constructor();
1949 catch (failed_constructor &err) {
1950 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1951 chan.write_source.reset ();
1955 /* do not remove destructive files even if they are empty */
1957 chan.write_source->set_allow_remove_if_empty (!destructive());
1963 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1965 ChannelList::iterator chan;
1968 if (!recordable()) {
1972 capturing_sources.clear ();
1974 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
1975 if (!destructive()) {
1977 if ((*chan).write_source && mark_write_complete) {
1978 (*chan).write_source->mark_streaming_write_completed ();
1980 use_new_write_source (n);
1982 if (record_enabled()) {
1983 capturing_sources.push_back ((*chan).write_source);
1987 if ((*chan).write_source == 0) {
1988 use_new_write_source (n);
1993 if (destructive()) {
1995 /* we now have all our write sources set up, so create the
1996 playlist's single region.
1999 if (_playlist->empty()) {
2000 setup_destructive_playlist ();
2006 AudioDiskstream::rename_write_sources ()
2008 ChannelList::iterator chan;
2011 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2012 if ((*chan).write_source != 0) {
2013 (*chan).write_source->set_name (_name, destructive());
2014 /* XXX what to do if one of them fails ? */
2022 AudioDiskstream::set_block_size (nframes_t nframes)
2024 if (_session.get_block_size() > speed_buffer_size) {
2025 speed_buffer_size = _session.get_block_size();
2027 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2028 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2029 (*chan).speed_buffer = new Sample[speed_buffer_size];
2032 allocate_temporary_buffers ();
2036 AudioDiskstream::allocate_temporary_buffers ()
2038 /* make sure the wrap buffer is at least large enough to deal
2039 with the speeds up to 1.2, to allow for micro-variation
2040 when slaving to MTC, SMPTE etc.
2043 double sp = max (fabsf (_actual_speed), 1.2f);
2044 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() * sp) + 1;
2046 if (required_wrap_size > wrap_buffer_size) {
2048 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2049 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2050 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2051 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2052 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2055 wrap_buffer_size = required_wrap_size;
2060 AudioDiskstream::monitor_input (bool yn)
2062 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2064 if ((*chan).source) {
2065 (*chan).source->ensure_monitor_input (yn);
2071 AudioDiskstream::set_align_style_from_io ()
2073 bool have_physical = false;
2079 get_input_sources ();
2081 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2082 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2083 have_physical = true;
2088 if (have_physical) {
2089 set_align_style (ExistingMaterial);
2091 set_align_style (CaptureTime);
2096 AudioDiskstream::add_channel ()
2098 /* XXX need to take lock??? */
2102 init_channel (chan);
2104 chan.speed_buffer = new Sample[speed_buffer_size];
2105 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2106 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2108 channels.push_back (chan);
2110 _n_channels.set(DataType::AUDIO, channels.size());
2116 AudioDiskstream::remove_channel ()
2118 if (channels.size() > 1) {
2119 /* XXX need to take lock??? */
2120 ChannelInfo & chan = channels.back();
2121 destroy_channel (chan);
2122 channels.pop_back();
2124 _n_channels.set(DataType::AUDIO, channels.size());
2132 AudioDiskstream::playback_buffer_load () const
2134 return (float) ((double) channels.front().playback_buf->read_space()/
2135 (double) channels.front().playback_buf->bufsize());
2139 AudioDiskstream::capture_buffer_load () const
2141 return (float) ((double) channels.front().capture_buf->write_space()/
2142 (double) channels.front().capture_buf->bufsize());
2146 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2148 const XMLProperty* prop;
2149 XMLNodeList nlist = node.children();
2150 XMLNodeIterator niter;
2151 boost::shared_ptr<AudioFileSource> fs;
2152 boost::shared_ptr<AudioFileSource> first_fs;
2153 SourceList pending_sources;
2156 if ((prop = node.property (X_("at"))) == 0) {
2160 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2164 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2165 if ((*niter)->name() == X_("file")) {
2167 if ((prop = (*niter)->property (X_("path"))) == 0) {
2172 fs = boost::dynamic_pointer_cast<AudioFileSource> (
2173 SourceFactory::createWritable (DataType::AUDIO, _session, prop->value(), false, _session.frame_rate()));
2176 catch (failed_constructor& err) {
2177 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2178 _name, prop->value())
2183 pending_sources.push_back (fs);
2185 if (first_fs == 0) {
2189 fs->set_captured_for (_name);
2193 if (pending_sources.size() == 0) {
2194 /* nothing can be done */
2198 if (pending_sources.size() != _n_channels.get(DataType::AUDIO)) {
2199 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2204 boost::shared_ptr<AudioRegion> region;
2207 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(),
2208 region_name_from_path (first_fs->name()),
2209 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
2210 region->special_set_position (0);
2213 catch (failed_constructor& err) {
2214 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2222 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name())));
2225 catch (failed_constructor& err) {
2226 error << string_compose (_("%1: cannot create region from pending capture sources"),
2233 _playlist->add_region (region, position);