2 Copyright (C) 2000-2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <pbd/error.h>
34 #include <pbd/basename.h>
35 #include <glibmm/thread.h>
36 #include <pbd/xml++.h>
37 #include <pbd/memento_command.h>
39 #include <ardour/ardour.h>
40 #include <ardour/audioengine.h>
41 #include <ardour/audio_diskstream.h>
42 #include <ardour/utils.h>
43 #include <ardour/configuration.h>
44 #include <ardour/audiofilesource.h>
45 #include <ardour/destructive_filesource.h>
46 #include <ardour/send.h>
47 #include <ardour/audioplaylist.h>
48 #include <ardour/cycle_timer.h>
49 #include <ardour/audioregion.h>
55 using namespace ARDOUR;
58 size_t AudioDiskstream::_working_buffers_size = 0;
59 Sample* AudioDiskstream::_mixdown_buffer = 0;
60 gain_t* AudioDiskstream::_gain_buffer = 0;
61 char* AudioDiskstream::_conversion_buffer = 0;
63 AudioDiskstream::AudioDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
64 : Diskstream(sess, name, flag)
65 , deprecated_io_node(NULL)
67 /* prevent any write sources from being created */
76 DiskstreamCreated (this); /* EMIT SIGNAL */
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 ();
97 DiskstreamCreated (this); /* EMIT SIGNAL */
101 AudioDiskstream::init_channel (ChannelInfo &chan)
103 chan.playback_wrap_buffer = 0;
104 chan.capture_wrap_buffer = 0;
105 chan.speed_buffer = 0;
106 chan.peak_power = 0.0f;
107 chan.write_source = 0;
109 chan.current_capture_buffer = 0;
110 chan.current_playback_buffer = 0;
111 chan.curr_capture_cnt = 0;
113 chan.playback_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
114 chan.capture_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
115 chan.capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
118 /* touch the ringbuffer buffers, which will cause
119 them to be mapped into locked physical RAM if
120 we're running with mlockall(). this doesn't do
123 memset (chan.playback_buf->buffer(), 0, sizeof (Sample) * chan.playback_buf->bufsize());
124 memset (chan.capture_buf->buffer(), 0, sizeof (Sample) * chan.capture_buf->bufsize());
125 memset (chan.capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * chan.capture_transition_buf->bufsize());
130 AudioDiskstream::init (Diskstream::Flag f)
134 /* there are no channels at this point, so these
135 two calls just get speed_buffer_size and wrap_buffer
136 size setup without duplicating their code.
139 set_block_size (_session.get_block_size());
140 allocate_temporary_buffers ();
143 assert(_n_channels == 1);
147 AudioDiskstream::destroy_channel (ChannelInfo &chan)
149 if (chan.write_source) {
150 chan.write_source->release ();
151 chan.write_source = 0;
154 if (chan.speed_buffer) {
155 delete [] chan.speed_buffer;
158 if (chan.playback_wrap_buffer) {
159 delete [] chan.playback_wrap_buffer;
161 if (chan.capture_wrap_buffer) {
162 delete [] chan.capture_wrap_buffer;
165 delete chan.playback_buf;
166 delete chan.capture_buf;
167 delete chan.capture_transition_buf;
169 chan.playback_buf = 0;
170 chan.capture_buf = 0;
173 AudioDiskstream::~AudioDiskstream ()
175 Glib::Mutex::Lock lm (state_lock);
177 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan)
178 destroy_channel((*chan));
184 AudioDiskstream::allocate_working_buffers()
186 assert(disk_io_frames() > 0);
188 _working_buffers_size = disk_io_frames();
189 _mixdown_buffer = new Sample[_working_buffers_size];
190 _gain_buffer = new gain_t[_working_buffers_size];
191 _conversion_buffer = new char[_working_buffers_size * 4];
195 AudioDiskstream::free_working_buffers()
197 delete _mixdown_buffer;
199 delete _conversion_buffer;
200 _working_buffers_size = 0;
203 _conversion_buffer = 0;
207 AudioDiskstream::non_realtime_input_change ()
210 Glib::Mutex::Lock lm (state_lock);
212 if (input_change_pending == NoChange) {
216 if (input_change_pending & ConfigurationChanged) {
218 if (_io->n_inputs() > _n_channels) {
220 // we need to add new channel infos
222 int diff = _io->n_inputs() - channels.size();
224 for (int i = 0; i < diff; ++i) {
228 } else if (_io->n_inputs() < _n_channels) {
230 // we need to get rid of channels
232 int diff = channels.size() - _io->n_inputs();
234 for (int i = 0; i < diff; ++i) {
240 get_input_sources ();
241 set_capture_offset ();
243 if (first_input_change) {
244 set_align_style (_persistent_alignment_style);
245 first_input_change = false;
247 set_align_style_from_io ();
250 input_change_pending = NoChange;
253 /* reset capture files */
255 reset_write_sources (false);
257 /* now refill channel buffers */
259 if (speed() != 1.0f || speed() != -1.0f) {
260 seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()));
263 seek (_session.transport_frame());
268 AudioDiskstream::get_input_sources ()
270 uint32_t ni = _io->n_inputs();
272 for (uint32_t n = 0; n < ni; ++n) {
274 const char **connections = _io->input(n)->get_connections ();
275 ChannelInfo& chan = channels[n];
277 if (connections == 0 || connections[0] == 0) {
280 // _source->disable_metering ();
286 chan.source = _session.engine().get_port_by_name (connections[0]);
296 AudioDiskstream::find_and_use_playlist (const string& name)
299 AudioPlaylist* playlist;
301 if ((pl = _session.playlist_by_name (name)) == 0) {
302 playlist = new AudioPlaylist(_session, name);
306 if ((playlist = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
307 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
311 return use_playlist (playlist);
315 AudioDiskstream::use_playlist (Playlist* playlist)
317 assert(dynamic_cast<AudioPlaylist*>(playlist));
319 Diskstream::use_playlist(playlist);
325 AudioDiskstream::use_new_playlist ()
328 AudioPlaylist* playlist;
330 if (!in_set_state && destructive()) {
335 newname = Playlist::bump_name (_playlist->name(), _session);
337 newname = Playlist::bump_name (_name, _session);
340 if ((playlist = new AudioPlaylist (_session, newname, hidden())) != 0) {
341 playlist->set_orig_diskstream_id (id());
342 return use_playlist (playlist);
349 AudioDiskstream::use_copy_playlist ()
351 assert(audio_playlist());
357 if (_playlist == 0) {
358 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
363 AudioPlaylist* playlist;
365 newname = Playlist::bump_name (_playlist->name(), _session);
367 if ((playlist = new AudioPlaylist (*audio_playlist(), newname)) != 0) {
368 playlist->set_orig_diskstream_id (id());
369 return use_playlist (playlist);
376 AudioDiskstream::setup_destructive_playlist ()
378 AudioRegion::SourceList srcs;
380 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
381 srcs.push_back ((*chan).write_source);
384 /* a single full-sized region */
386 cerr << "setup DS using " << srcs.front()->natural_position () << endl;
388 AudioRegion* region = new AudioRegion (srcs, 0, max_frames, _name);
389 _playlist->add_region (*region, srcs.front()->natural_position());
393 AudioDiskstream::use_destructive_playlist ()
395 /* use the sources associated with the single full-extent region */
397 Playlist::RegionList* rl = _playlist->regions_at (0);
400 reset_write_sources (false, true);
404 AudioRegion* region = dynamic_cast<AudioRegion*> (rl->front());
407 throw failed_constructor();
413 ChannelList::iterator chan;
415 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
416 (*chan).write_source = dynamic_cast<AudioFileSource*>(®ion->source (n));
417 assert((*chan).write_source);
418 (*chan).write_source->set_allow_remove_if_empty (false);
421 /* the source list will never be reset for a destructive track */
425 AudioDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record)
427 int possibly_recording;
430 const int transport_rolling = 0x4;
431 const int track_rec_enabled = 0x2;
432 const int global_rec_enabled = 0x1;
434 /* merge together the 3 factors that affect record status, and compute
438 rolling = _session.transport_speed() != 0.0f;
439 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
440 change = possibly_recording ^ last_possibly_recording;
442 if (possibly_recording == last_possibly_recording) {
448 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
450 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
451 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
453 /* starting to record: compute first+last frames */
455 first_recordable_frame = transport_frame + _capture_offset;
456 last_recordable_frame = max_frames;
457 capture_start_frame = transport_frame;
459 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
461 /* was stopped, now rolling (and recording) */
463 if (_alignment_style == ExistingMaterial) {
464 first_recordable_frame += _session.worst_output_latency();
466 first_recordable_frame += _roll_delay;
471 /* was rolling, but record state changed */
473 if (_alignment_style == ExistingMaterial) {
476 if (!_session.get_punch_in()) {
478 /* manual punch in happens at the correct transport frame
479 because the user hit a button. but to get alignment correct
480 we have to back up the position of the new region to the
481 appropriate spot given the roll delay.
484 capture_start_frame -= _roll_delay;
486 /* XXX paul notes (august 2005): i don't know why
490 first_recordable_frame += _capture_offset;
494 /* autopunch toggles recording at the precise
495 transport frame, and then the DS waits
496 to start recording for a time that depends
497 on the output latency.
500 first_recordable_frame += _session.worst_output_latency();
505 if (_session.get_punch_in()) {
506 first_recordable_frame += _roll_delay;
508 capture_start_frame -= _roll_delay;
514 if (_flags & Recordable) {
515 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
517 RingBufferNPT<CaptureTransition>::rw_vector transvec;
518 (*chan).capture_transition_buf->get_write_vector(&transvec);
520 if (transvec.len[0] > 0) {
521 transvec.buf[0]->type = CaptureStart;
522 transvec.buf[0]->capture_val = capture_start_frame;
523 (*chan).capture_transition_buf->increment_write_ptr(1);
527 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
533 } else if (!record_enabled() || !can_record) {
537 last_recordable_frame = transport_frame + _capture_offset;
539 if (_alignment_style == ExistingMaterial) {
540 last_recordable_frame += _session.worst_output_latency();
542 last_recordable_frame += _roll_delay;
546 last_possibly_recording = possibly_recording;
550 AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input)
553 ChannelList::iterator c;
555 jack_nframes_t rec_offset = 0;
556 jack_nframes_t rec_nframes = 0;
557 bool nominally_recording;
558 bool re = record_enabled ();
559 bool collect_playback = false;
561 /* if we've already processed the frames corresponding to this call,
562 just return. this allows multiple routes that are taking input
563 from this diskstream to call our ::process() method, but have
564 this stuff only happen once. more commonly, it allows both
565 the AudioTrack that is using this AudioDiskstream *and* the Session
566 to call process() without problems.
573 check_record_status (transport_frame, nframes, can_record);
575 nominally_recording = (can_record && re);
582 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
583 must always be called as a pair. The only exception is if this function
584 returns a non-zero value, in which case, ::commit should not be called.
587 // If we can't take the state lock return.
588 if (!state_lock.trylock()) {
592 adjust_capture_position = 0;
594 for (c = channels.begin(); c != channels.end(); ++c) {
595 (*c).current_capture_buffer = 0;
596 (*c).current_playback_buffer = 0;
599 if (nominally_recording || (_session.get_record_enabled() && _session.get_punch_in())) {
602 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
609 case OverlapInternal:
610 /* ---------- recrange
613 rec_nframes = nframes;
618 /* |--------| recrange
621 rec_nframes = transport_frame + nframes - first_recordable_frame;
623 rec_offset = first_recordable_frame - transport_frame;
628 /* |--------| recrange
631 rec_nframes = last_recordable_frame - transport_frame;
635 case OverlapExternal:
636 /* |--------| recrange
637 -------------- transrange
639 rec_nframes = last_recordable_frame - last_recordable_frame;
640 rec_offset = first_recordable_frame - transport_frame;
644 if (rec_nframes && !was_recording) {
645 capture_captured = 0;
646 was_recording = true;
651 if (can_record && !_last_capture_regions.empty()) {
652 _last_capture_regions.clear ();
655 if (nominally_recording || rec_nframes) {
657 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
659 ChannelInfo& chan (*c);
661 chan.capture_buf->get_write_vector (&chan.capture_vector);
663 if (rec_nframes <= chan.capture_vector.len[0]) {
665 chan.current_capture_buffer = chan.capture_vector.buf[0];
667 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
671 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
675 jack_nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
677 if (rec_nframes > total) {
682 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
683 jack_nframes_t first = chan.capture_vector.len[0];
685 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
686 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
687 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
688 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
690 chan.current_capture_buffer = chan.capture_wrap_buffer;
697 finish_capture (rec_monitors_input);
704 /* data will be written to disk */
706 if (rec_nframes == nframes && rec_offset == 0) {
708 for (c = channels.begin(); c != channels.end(); ++c) {
709 (*c).current_playback_buffer = (*c).current_capture_buffer;
712 playback_distance = nframes;
717 /* we can't use the capture buffer as the playback buffer, because
718 we recorded only a part of the current process' cycle data
722 collect_playback = true;
725 adjust_capture_position = rec_nframes;
727 } else if (nominally_recording) {
729 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
731 for (c = channels.begin(); c != channels.end(); ++c) {
732 (*c).current_playback_buffer = (*c).current_capture_buffer;
735 playback_distance = nframes;
739 collect_playback = true;
742 if (collect_playback) {
744 /* we're doing playback */
746 jack_nframes_t necessary_samples;
748 /* no varispeed playback if we're recording, because the output .... TBD */
750 if (rec_nframes == 0 && _actual_speed != 1.0f) {
751 necessary_samples = (jack_nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
753 necessary_samples = nframes;
756 for (c = channels.begin(); c != channels.end(); ++c) {
757 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
762 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
764 ChannelInfo& chan (*c);
766 if (necessary_samples <= chan.playback_vector.len[0]) {
768 chan.current_playback_buffer = chan.playback_vector.buf[0];
771 jack_nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
773 if (necessary_samples > total) {
779 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
780 chan.playback_vector.len[0] * sizeof (Sample));
781 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
782 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
784 chan.current_playback_buffer = chan.playback_wrap_buffer;
789 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
791 uint64_t phase = last_phase;
792 jack_nframes_t i = 0;
794 // Linearly interpolate into the alt buffer
795 // using 40.24 fixp maths (swh)
797 for (c = channels.begin(); c != channels.end(); ++c) {
800 ChannelInfo& chan (*c);
805 for (jack_nframes_t outsample = 0; outsample < nframes; ++outsample) {
807 fr = (phase & 0xFFFFFF) / 16777216.0f;
808 chan.speed_buffer[outsample] =
809 chan.current_playback_buffer[i] * (1.0f - fr) +
810 chan.current_playback_buffer[i+1] * fr;
814 chan.current_playback_buffer = chan.speed_buffer;
817 playback_distance = i + 1;
818 last_phase = (phase & 0xFFFFFF);
821 playback_distance = nframes;
833 /* we're exiting with failure, so ::commit will not
834 be called. unlock the state lock.
844 AudioDiskstream::commit (jack_nframes_t nframes)
846 bool need_butler = false;
848 if (_actual_speed < 0.0) {
849 playback_sample -= playback_distance;
851 playback_sample += playback_distance;
854 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
856 (*chan).playback_buf->increment_read_ptr (playback_distance);
858 if (adjust_capture_position) {
859 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
863 if (adjust_capture_position != 0) {
864 capture_captured += adjust_capture_position;
865 adjust_capture_position = 0;
869 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
871 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
872 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
883 AudioDiskstream::set_pending_overwrite (bool yn)
885 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
887 pending_overwrite = yn;
889 overwrite_frame = playback_sample;
890 overwrite_offset = channels.front().playback_buf->get_read_ptr();
894 AudioDiskstream::overwrite_existing_buffers ()
896 Sample* mixdown_buffer;
900 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
902 overwrite_queued = false;
904 /* assume all are the same size */
905 jack_nframes_t size = channels[0].playback_buf->bufsize();
907 mixdown_buffer = new Sample[size];
908 gain_buffer = new float[size];
909 workbuf = new char[size*4];
911 /* reduce size so that we can fill the buffer correctly. */
915 jack_nframes_t start;
917 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
919 start = overwrite_frame;
920 jack_nframes_t cnt = size;
922 /* to fill the buffer without resetting the playback sample, we need to
923 do it one or two chunks (normally two).
925 |----------------------------------------------------------------------|
929 |<- second chunk->||<----------------- first chunk ------------------>|
933 jack_nframes_t to_read = size - overwrite_offset;
935 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, workbuf,
936 start, to_read, *chan, n, reversed)) {
937 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
938 _id, size, playback_sample) << endmsg;
946 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer, workbuf,
947 start, cnt, *chan, n, reversed)) {
948 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
949 _id, size, playback_sample) << endmsg;
958 pending_overwrite = false;
959 delete [] gain_buffer;
960 delete [] mixdown_buffer;
966 AudioDiskstream::seek (jack_nframes_t frame, bool complete_refill)
968 Glib::Mutex::Lock lm (state_lock);
971 ChannelList::iterator chan;
973 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
974 (*chan).playback_buf->reset ();
975 (*chan).capture_buf->reset ();
978 /* can't rec-enable in destructive mode if transport is before start */
980 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
981 disengage_record_enable ();
984 playback_sample = frame;
987 if (complete_refill) {
988 while ((ret = do_refill_with_alloc ()) > 0) ;
990 ret = do_refill_with_alloc ();
997 AudioDiskstream::can_internal_playback_seek (jack_nframes_t distance)
999 ChannelList::iterator chan;
1001 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1002 if ((*chan).playback_buf->read_space() < distance) {
1010 AudioDiskstream::internal_playback_seek (jack_nframes_t distance)
1012 ChannelList::iterator chan;
1014 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1015 (*chan).playback_buf->increment_read_ptr (distance);
1018 first_recordable_frame += distance;
1019 playback_sample += distance;
1025 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, char * workbuf, jack_nframes_t& start, jack_nframes_t cnt,
1026 ChannelInfo& channel_info, int channel, bool reversed)
1028 jack_nframes_t this_read = 0;
1029 bool reloop = false;
1030 jack_nframes_t loop_end = 0;
1031 jack_nframes_t loop_start = 0;
1032 jack_nframes_t loop_length = 0;
1033 jack_nframes_t offset = 0;
1037 /* Make the use of a Location atomic for this read operation.
1039 Note: Locations don't get deleted, so all we care about
1040 when I say "atomic" is that we are always pointing to
1041 the same one and using a start/length values obtained
1045 if ((loc = loop_location) != 0) {
1046 loop_start = loc->start();
1047 loop_end = loc->end();
1048 loop_length = loop_end - loop_start;
1051 /* if we are looping, ensure that the first frame we read is at the correct
1052 position within the loop.
1055 if (loc && start >= loop_end) {
1056 //cerr << "start adjusted from " << start;
1057 start = loop_start + ((start - loop_start) % loop_length);
1058 //cerr << "to " << start << endl;
1060 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1065 /* take any loop into account. we can't read past the end of the loop. */
1067 if (loc && (loop_end - start < cnt)) {
1068 this_read = loop_end - start;
1069 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1076 if (this_read == 0) {
1080 this_read = min(cnt,this_read);
1082 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, workbuf, start, this_read, channel) != this_read) {
1083 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1088 _read_data_count = _playlist->read_data_count();
1092 /* don't adjust start, since caller has already done that
1095 swap_by_ptr (buf, buf + this_read - 1);
1099 /* if we read to the end of the loop, go back to the beginning */
1109 offset += this_read;
1116 AudioDiskstream::do_refill_with_alloc()
1118 Sample* mix_buf = new Sample[disk_io_chunk_frames];
1119 float* gain_buf = new float[disk_io_chunk_frames];
1120 char* work_buf = new char[disk_io_chunk_frames * 4];
1122 int ret = _do_refill(mix_buf, gain_buf, work_buf);
1132 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer, char * workbuf)
1135 jack_nframes_t to_read;
1136 RingBufferNPT<Sample>::rw_vector vector;
1137 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1138 jack_nframes_t total_space;
1139 jack_nframes_t zero_fill;
1141 ChannelList::iterator i;
1144 assert(mixdown_buffer);
1145 assert(gain_buffer);
1148 channels.front().playback_buf->get_write_vector (&vector);
1150 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1154 /* if there are 2+ chunks of disk i/o possible for
1155 this track, let the caller know so that it can arrange
1156 for us to be called again, ASAP.
1159 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1163 /* if we're running close to normal speed and there isn't enough
1164 space to do disk_io_chunk_frames of I/O, then don't bother.
1166 at higher speeds, just do it because the sync between butler
1167 and audio thread may not be good enough.
1170 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1174 /* when slaved, don't try to get too close to the read pointer. this
1175 leaves space for the buffer reversal to have something useful to
1179 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1183 total_space = min (disk_io_chunk_frames, total_space);
1187 if (file_frame == 0) {
1189 /* at start: nothing to do but fill with silence */
1191 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1193 ChannelInfo& chan (*i);
1194 chan.playback_buf->get_write_vector (&vector);
1195 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1196 if (vector.len[1]) {
1197 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1199 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1204 if (file_frame < total_space) {
1206 /* too close to the start: read what we can,
1207 and then zero fill the rest
1210 zero_fill = total_space - file_frame;
1211 total_space = file_frame;
1216 /* move read position backwards because we are going
1217 to reverse the data.
1220 file_frame -= total_space;
1226 if (file_frame == max_frames) {
1228 /* at end: nothing to do but fill with silence */
1230 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1232 ChannelInfo& chan (*i);
1233 chan.playback_buf->get_write_vector (&vector);
1234 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1235 if (vector.len[1]) {
1236 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1238 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1243 if (file_frame > max_frames - total_space) {
1245 /* to close to the end: read what we can, and zero fill the rest */
1247 zero_fill = total_space - (max_frames - file_frame);
1248 total_space = max_frames - file_frame;
1255 jack_nframes_t file_frame_tmp = 0;
1257 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1259 ChannelInfo& chan (*i);
1262 jack_nframes_t len1, len2;
1264 chan.playback_buf->get_write_vector (&vector);
1267 file_frame_tmp = file_frame;
1270 buf1 = vector.buf[1];
1271 len1 = vector.len[1];
1272 buf2 = vector.buf[0];
1273 len2 = vector.len[0];
1275 buf1 = vector.buf[0];
1276 len1 = vector.len[0];
1277 buf2 = vector.buf[1];
1278 len2 = vector.len[1];
1282 to_read = min (ts, len1);
1283 to_read = min (to_read, disk_io_chunk_frames);
1287 if (read (buf1, mixdown_buffer, gain_buffer, workbuf, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1292 chan.playback_buf->increment_write_ptr (to_read);
1296 to_read = min (ts, len2);
1301 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1302 so read some or all of vector.len[1] as well.
1305 if (read (buf2, mixdown_buffer, gain_buffer, workbuf, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1310 chan.playback_buf->increment_write_ptr (to_read);
1319 file_frame = file_frame_tmp;
1326 /** Flush pending data to disk.
1328 * Important note: this function will write *AT MOST* disk_io_chunk_frames
1329 * of data to disk. it will never write more than that. If it writes that
1330 * much and there is more than that waiting to be written, it will return 1,
1331 * otherwise 0 on success or -1 on failure.
1333 * If there is less than disk_io_chunk_frames to be written, no data will be
1334 * written at all unless @a force_flush is true.
1337 AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
1339 char* workbuf = _session.conversion_buffer(context);
1343 RingBufferNPT<Sample>::rw_vector vector;
1344 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1345 jack_nframes_t total;
1347 _write_data_count = 0;
1349 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1351 (*chan).capture_buf->get_read_vector (&vector);
1353 total = vector.len[0] + vector.len[1];
1356 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1360 /* if there are 2+ chunks of disk i/o possible for
1361 this track, let the caller know so that it can arrange
1362 for us to be called again, ASAP.
1364 if we are forcing a flush, then if there is* any* extra
1365 work, let the caller know.
1367 if we are no longer recording and there is any extra work,
1368 let the caller know too.
1371 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1375 to_write = min (disk_io_chunk_frames, (jack_nframes_t) vector.len[0]);
1377 // check the transition buffer when recording destructive
1378 // important that we get this after the capture buf
1380 if (destructive()) {
1381 (*chan).capture_transition_buf->get_read_vector(&transvec);
1382 size_t transcount = transvec.len[0] + transvec.len[1];
1383 bool have_start = false;
1386 for (ti=0; ti < transcount; ++ti) {
1387 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1389 if (captrans.type == CaptureStart) {
1390 // by definition, the first data we got above represents the given capture pos
1392 (*chan).write_source->mark_capture_start (captrans.capture_val);
1393 (*chan).curr_capture_cnt = 0;
1397 else if (captrans.type == CaptureEnd) {
1399 // capture end, the capture_val represents total frames in capture
1401 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1403 // shorten to make the write a perfect fit
1404 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1406 if (nto_write < to_write) {
1407 ret = 1; // should we?
1409 to_write = nto_write;
1411 (*chan).write_source->mark_capture_end ();
1413 // increment past this transition, but go no further
1418 // actually ends just beyond this chunk, so force more work
1426 (*chan).capture_transition_buf->increment_read_ptr(ti);
1430 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write, workbuf) != to_write) {
1431 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1435 (*chan).capture_buf->increment_read_ptr (to_write);
1436 (*chan).curr_capture_cnt += to_write;
1438 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1440 /* we wrote all of vector.len[0] but it wasn't an entire
1441 disk_io_chunk_frames of data, so arrange for some part
1442 of vector.len[1] to be flushed to disk as well.
1445 to_write = min ((jack_nframes_t)(disk_io_chunk_frames - to_write), (jack_nframes_t) vector.len[1]);
1447 if ((*chan).write_source->write (vector.buf[1], to_write, workbuf) != to_write) {
1448 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1452 _write_data_count += (*chan).write_source->write_data_count();
1454 (*chan).capture_buf->increment_read_ptr (to_write);
1455 (*chan).curr_capture_cnt += to_write;
1464 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1466 uint32_t buffer_position;
1467 bool more_work = true;
1469 AudioRegion* region = 0;
1470 jack_nframes_t total_capture;
1471 AudioRegion::SourceList srcs;
1472 AudioRegion::SourceList::iterator src;
1473 ChannelList::iterator chan;
1474 vector<CaptureInfo*>::iterator ci;
1476 bool mark_write_completed = false;
1478 finish_capture (true);
1480 /* butler is already stopped, but there may be work to do
1481 to flush remaining data to disk.
1484 while (more_work && !err) {
1485 switch (do_flush (Session::TransportContext, true)) {
1492 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1497 /* XXX is there anything we can do if err != 0 ? */
1498 Glib::Mutex::Lock lm (capture_info_lock);
1500 if (capture_info.empty()) {
1504 if (abort_capture) {
1506 ChannelList::iterator chan;
1508 list<Source*>* deletion_list = new list<Source*>;
1510 for ( chan = channels.begin(); chan != channels.end(); ++chan) {
1512 if ((*chan).write_source) {
1514 (*chan).write_source->mark_for_remove ();
1515 (*chan).write_source->release ();
1517 deletion_list->push_back ((*chan).write_source);
1519 (*chan).write_source = 0;
1522 /* new source set up in "out" below */
1525 if (!deletion_list->empty()) {
1526 DeleteSources (deletion_list);
1528 delete deletion_list;
1534 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1535 total_capture += (*ci)->frames;
1538 /* figure out the name for this take */
1540 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1542 AudioFileSource* s = (*chan).write_source;
1546 AudioFileSource* fsrc;
1550 if ((fsrc = dynamic_cast<AudioFileSource *>(s)) != 0) {
1551 cerr << "updating source after capture\n";
1552 fsrc->update_header (capture_info.front()->start, when, twhen);
1555 s->set_captured_for (_name);
1560 /* destructive tracks have a single, never changing region */
1562 if (destructive()) {
1564 /* send a signal that any UI can pick up to do the right thing. there is
1565 a small problem here in that a UI may need the peak data to be ready
1566 for the data that was recorded and this isn't interlocked with that
1567 process. this problem is deferred to the UI.
1570 _playlist->Modified();
1574 /* Register a new region with the Session that
1575 describes the entire source. Do this first
1576 so that any sub-regions will obviously be
1577 children of this one (later!)
1581 region = new AudioRegion (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1582 region_name_from_path (channels[0].write_source->name()),
1583 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
1585 region->special_set_position (capture_info.front()->start);
1589 catch (failed_constructor& err) {
1590 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1594 _last_capture_regions.push_back (region);
1596 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1598 XMLNode &before = _playlist->get_state();
1599 _playlist->freeze ();
1601 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1604 _session.region_name (region_name, channels[0].write_source->name(), false);
1606 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1609 region = new AudioRegion (srcs, buffer_position, (*ci)->frames, region_name);
1612 catch (failed_constructor& err) {
1613 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1614 continue; /* XXX is this OK? */
1617 _last_capture_regions.push_back (region);
1619 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1621 i_am_the_modifier++;
1622 _playlist->add_region (*region, (*ci)->start);
1623 i_am_the_modifier--;
1625 buffer_position += (*ci)->frames;
1629 XMLNode &after = _playlist->get_state();
1630 _session.add_command (new MementoCommand<Playlist>(*_playlist, before, after));
1633 mark_write_completed = true;
1635 reset_write_sources (mark_write_completed);
1638 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1642 capture_info.clear ();
1643 capture_start_frame = 0;
1647 AudioDiskstream::finish_capture (bool rec_monitors_input)
1649 was_recording = false;
1651 if (capture_captured == 0) {
1655 if (recordable() && destructive()) {
1656 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1658 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1659 (*chan).capture_transition_buf->get_write_vector(&transvec);
1662 if (transvec.len[0] > 0) {
1663 transvec.buf[0]->type = CaptureEnd;
1664 transvec.buf[0]->capture_val = capture_captured;
1665 (*chan).capture_transition_buf->increment_write_ptr(1);
1669 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1675 CaptureInfo* ci = new CaptureInfo;
1677 ci->start = capture_start_frame;
1678 ci->frames = capture_captured;
1680 /* XXX theoretical race condition here. Need atomic exchange ?
1681 However, the circumstances when this is called right
1682 now (either on record-disable or transport_stopped)
1683 mean that no actual race exists. I think ...
1684 We now have a capture_info_lock, but it is only to be used
1685 to synchronize in the transport_stop and the capture info
1686 accessors, so that invalidation will not occur (both non-realtime).
1689 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1691 capture_info.push_back (ci);
1692 capture_captured = 0;
1696 AudioDiskstream::set_record_enabled (bool yn)
1698 if (!recordable() || !_session.record_enabling_legal()) {
1702 /* can't rec-enable in destructive mode if transport is before start */
1704 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1708 if (yn && channels[0].source == 0) {
1710 /* pick up connections not initiated *from* the IO object
1711 we're associated with.
1714 get_input_sources ();
1717 /* yes, i know that this not proof against race conditions, but its
1718 good enough. i think.
1721 if (record_enabled() != yn) {
1723 engage_record_enable ();
1725 disengage_record_enable ();
1731 AudioDiskstream::engage_record_enable ()
1733 bool rolling = _session.transport_speed() != 0.0f;
1735 g_atomic_int_set (&_record_enabled, 1);
1736 capturing_sources.clear ();
1737 if (Config->get_use_hardware_monitoring()) {
1738 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1739 if ((*chan).source) {
1740 (*chan).source->request_monitor_input (!(_session.get_auto_input() && rolling));
1742 capturing_sources.push_back ((*chan).write_source);
1745 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1746 capturing_sources.push_back ((*chan).write_source);
1750 RecordEnableChanged (); /* EMIT SIGNAL */
1754 AudioDiskstream::disengage_record_enable ()
1756 g_atomic_int_set (&_record_enabled, 0);
1757 if (Config->get_use_hardware_monitoring()) {
1758 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1759 if ((*chan).source) {
1760 (*chan).source->request_monitor_input (false);
1764 capturing_sources.clear ();
1765 RecordEnableChanged (); /* EMIT SIGNAL */
1770 AudioDiskstream::get_state ()
1772 XMLNode* node = new XMLNode ("AudioDiskstream");
1774 LocaleGuard lg (X_("POSIX"));
1776 snprintf (buf, sizeof(buf), "0x%x", _flags);
1777 node->add_property ("flags", buf);
1779 snprintf (buf, sizeof(buf), "%zd", channels.size());
1780 node->add_property ("channels", buf);
1782 node->add_property ("playlist", _playlist->name());
1784 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1785 node->add_property ("speed", buf);
1787 node->add_property("name", _name);
1789 node->add_property("id", buf);
1791 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1793 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1794 XMLNode* cs_grandchild;
1796 for (vector<AudioFileSource*>::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1797 cs_grandchild = new XMLNode (X_("file"));
1798 cs_grandchild->add_property (X_("path"), (*i)->path());
1799 cs_child->add_child_nocopy (*cs_grandchild);
1802 /* store the location where capture will start */
1806 if (_session.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1807 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1809 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1812 cs_child->add_property (X_("at"), buf);
1813 node->add_child_nocopy (*cs_child);
1817 node->add_child_copy (*_extra_xml);
1824 AudioDiskstream::set_state (const XMLNode& node)
1826 const XMLProperty* prop;
1827 XMLNodeList nlist = node.children();
1828 XMLNodeIterator niter;
1829 uint32_t nchans = 1;
1830 XMLNode* capture_pending_node = 0;
1831 LocaleGuard lg (X_("POSIX"));
1833 in_set_state = true;
1835 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1836 if ((*niter)->name() == IO::state_node_name) {
1837 deprecated_io_node = new XMLNode (**niter);
1840 if ((*niter)->name() == X_("CapturingSources")) {
1841 capture_pending_node = *niter;
1845 /* prevent write sources from being created */
1847 in_set_state = true;
1849 if ((prop = node.property ("name")) != 0) {
1850 _name = prop->value();
1853 if (deprecated_io_node) {
1854 if ((prop = deprecated_io_node->property ("id")) != 0) {
1855 _id = prop->value ();
1858 if ((prop = node.property ("id")) != 0) {
1859 _id = prop->value ();
1863 if ((prop = node.property ("flags")) != 0) {
1864 _flags = strtol (prop->value().c_str(), 0, 0);
1867 if ((prop = node.property ("channels")) != 0) {
1868 nchans = atoi (prop->value().c_str());
1871 // create necessary extra channels
1872 // we are always constructed with one and we always need one
1874 if (nchans > _n_channels) {
1876 // we need to add new channel infos
1877 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1879 int diff = nchans - channels.size();
1881 for (int i=0; i < diff; ++i) {
1885 } else if (nchans < _n_channels) {
1887 // we need to get rid of channels
1888 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1890 int diff = channels.size() - nchans;
1892 for (int i = 0; i < diff; ++i) {
1897 if ((prop = node.property ("playlist")) == 0) {
1902 bool had_playlist = (_playlist != 0);
1904 if (find_and_use_playlist (prop->value())) {
1908 if (!had_playlist) {
1909 _playlist->set_orig_diskstream_id (_id);
1912 if (!destructive() && capture_pending_node) {
1913 /* destructive streams have one and only one source per channel,
1914 and so they never end up in pending capture in any useful
1917 use_pending_capture_data (*capture_pending_node);
1922 if ((prop = node.property ("speed")) != 0) {
1923 double sp = atof (prop->value().c_str());
1925 if (realtime_set_speed (sp, false)) {
1926 non_realtime_set_speed ();
1930 _n_channels = channels.size();
1932 in_set_state = false;
1934 /* make sure this is clear before we do anything else */
1936 capturing_sources.clear ();
1938 /* write sources are handled when we handle the input set
1939 up of the IO that owns this DS (::non_realtime_input_change())
1942 in_set_state = false;
1948 AudioDiskstream::use_new_write_source (uint32_t n)
1950 if (!recordable()) {
1954 if (n >= channels.size()) {
1955 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1959 ChannelInfo &chan = channels[n];
1961 if (chan.write_source) {
1963 if (AudioFileSource::is_empty (chan.write_source->path())) {
1964 chan.write_source->mark_for_remove ();
1965 chan.write_source->release();
1966 delete chan.write_source;
1968 chan.write_source->release();
1969 chan.write_source = 0;
1974 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
1975 throw failed_constructor();
1979 catch (failed_constructor &err) {
1980 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1981 chan.write_source = 0;
1985 chan.write_source->use ();
1987 /* do not remove destructive files even if they are empty */
1989 chan.write_source->set_allow_remove_if_empty (!destructive());
1995 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1997 ChannelList::iterator chan;
2000 if (!recordable()) {
2004 capturing_sources.clear ();
2006 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2007 if (!destructive()) {
2009 if ((*chan).write_source && mark_write_complete) {
2010 (*chan).write_source->mark_streaming_write_completed ();
2012 use_new_write_source (n);
2014 if (record_enabled()) {
2015 capturing_sources.push_back ((*chan).write_source);
2019 if ((*chan).write_source == 0) {
2020 use_new_write_source (n);
2025 if (destructive()) {
2027 /* we now have all our write sources set up, so create the
2028 playlist's single region.
2031 if (_playlist->empty()) {
2032 setup_destructive_playlist ();
2038 AudioDiskstream::rename_write_sources ()
2040 ChannelList::iterator chan;
2043 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2044 if ((*chan).write_source != 0) {
2045 (*chan).write_source->set_name (_name, destructive());
2046 /* XXX what to do if one of them fails ? */
2054 AudioDiskstream::set_block_size (jack_nframes_t nframes)
2056 if (_session.get_block_size() > speed_buffer_size) {
2057 speed_buffer_size = _session.get_block_size();
2059 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2060 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2061 (*chan).speed_buffer = new Sample[speed_buffer_size];
2064 allocate_temporary_buffers ();
2068 AudioDiskstream::allocate_temporary_buffers ()
2070 /* make sure the wrap buffer is at least large enough to deal
2071 with the speeds up to 1.2, to allow for micro-variation
2072 when slaving to MTC, SMPTE etc.
2075 double sp = max (fabsf (_actual_speed), 1.2f);
2076 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * sp) + 1;
2078 if (required_wrap_size > wrap_buffer_size) {
2080 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2081 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2082 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2083 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2084 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2087 wrap_buffer_size = required_wrap_size;
2092 AudioDiskstream::monitor_input (bool yn)
2094 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2096 if ((*chan).source) {
2097 (*chan).source->request_monitor_input (yn);
2103 AudioDiskstream::set_align_style_from_io ()
2105 bool have_physical = false;
2111 get_input_sources ();
2113 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2114 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2115 have_physical = true;
2120 if (have_physical) {
2121 set_align_style (ExistingMaterial);
2123 set_align_style (CaptureTime);
2128 AudioDiskstream::add_channel ()
2130 /* XXX need to take lock??? */
2134 init_channel (chan);
2136 chan.speed_buffer = new Sample[speed_buffer_size];
2137 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2138 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2140 channels.push_back (chan);
2142 _n_channels = channels.size();
2148 AudioDiskstream::remove_channel ()
2150 if (channels.size() > 1) {
2151 /* XXX need to take lock??? */
2152 ChannelInfo & chan = channels.back();
2153 destroy_channel (chan);
2154 channels.pop_back();
2156 _n_channels = channels.size();
2164 AudioDiskstream::playback_buffer_load () const
2166 return (float) ((double) channels.front().playback_buf->read_space()/
2167 (double) channels.front().playback_buf->bufsize());
2171 AudioDiskstream::capture_buffer_load () const
2173 return (float) ((double) channels.front().capture_buf->write_space()/
2174 (double) channels.front().capture_buf->bufsize());
2178 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2180 const XMLProperty* prop;
2181 XMLNodeList nlist = node.children();
2182 XMLNodeIterator niter;
2183 AudioFileSource* fs;
2184 AudioFileSource* first_fs = 0;
2185 AudioRegion::SourceList pending_sources;
2186 jack_nframes_t position;
2188 if ((prop = node.property (X_("at"))) == 0) {
2192 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2196 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2197 if ((*niter)->name() == X_("file")) {
2199 if ((prop = (*niter)->property (X_("path"))) == 0) {
2204 fs = new SndFileSource (prop->value(),
2205 Config->get_native_file_data_format(),
2206 Config->get_native_file_header_format(),
2207 _session.frame_rate());
2210 catch (failed_constructor& err) {
2211 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2212 _name, prop->value())
2217 pending_sources.push_back (fs);
2219 if (first_fs == 0) {
2223 fs->set_captured_for (_name);
2227 if (pending_sources.size() == 0) {
2228 /* nothing can be done */
2232 if (pending_sources.size() != _n_channels) {
2233 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2238 AudioRegion* region;
2241 region = new AudioRegion (pending_sources, 0, first_fs->length(),
2242 region_name_from_path (first_fs->name()),
2243 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
2245 region->special_set_position (0);
2248 catch (failed_constructor& err) {
2249 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2257 region = new AudioRegion (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name()));
2260 catch (failed_constructor& err) {
2261 error << string_compose (_("%1: cannot create region from pending capture sources"),
2268 _playlist->add_region (*region, position);