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 ((jack_nframes_t) (_session.transport_frame() * (double) speed()));
256 seek (_session.transport_frame());
261 AudioDiskstream::get_input_sources ()
263 uint32_t ni = _io->n_inputs().get(DataType::AUDIO);
265 for (uint32_t n = 0; n < ni; ++n) {
267 const char **connections = _io->input(n)->get_connections ();
268 ChannelInfo& chan = channels[n];
270 if (connections == 0 || connections[0] == 0) {
273 // _source->disable_metering ();
279 chan.source = dynamic_cast<AudioPort*>(
280 _session.engine().get_port_by_name (connections[0]) );
290 AudioDiskstream::find_and_use_playlist (const string& name)
293 AudioPlaylist* playlist;
295 if ((pl = _session.playlist_by_name (name)) == 0) {
296 playlist = new AudioPlaylist(_session, name);
300 if ((playlist = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
301 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
305 return use_playlist (playlist);
309 AudioDiskstream::use_playlist (Playlist* playlist)
311 assert(dynamic_cast<AudioPlaylist*>(playlist));
313 Diskstream::use_playlist(playlist);
319 AudioDiskstream::use_new_playlist ()
322 AudioPlaylist* playlist;
324 if (!in_set_state && destructive()) {
329 newname = Playlist::bump_name (_playlist->name(), _session);
331 newname = Playlist::bump_name (_name, _session);
334 if ((playlist = new AudioPlaylist (_session, newname, hidden())) != 0) {
335 playlist->set_orig_diskstream_id (id());
336 return use_playlist (playlist);
343 AudioDiskstream::use_copy_playlist ()
345 assert(audio_playlist());
351 if (_playlist == 0) {
352 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
357 AudioPlaylist* playlist;
359 newname = Playlist::bump_name (_playlist->name(), _session);
361 if ((playlist = new AudioPlaylist (*audio_playlist(), newname)) != 0) {
362 playlist->set_orig_diskstream_id (id());
363 return use_playlist (playlist);
370 AudioDiskstream::setup_destructive_playlist ()
374 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
375 srcs.push_back ((*chan).write_source);
378 /* a single full-sized region */
380 cerr << "setup DS using " << srcs.front()->natural_position () << endl;
382 boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, max_frames, _name));
383 _playlist->add_region (region, srcs.front()->natural_position());
387 AudioDiskstream::use_destructive_playlist ()
389 /* use the sources associated with the single full-extent region */
391 Playlist::RegionList* rl = _playlist->regions_at (0);
394 reset_write_sources (false, true);
398 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rl->front());
401 throw failed_constructor();
407 ChannelList::iterator chan;
409 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
410 (*chan).write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
411 assert((*chan).write_source);
412 (*chan).write_source->set_allow_remove_if_empty (false);
415 /* the source list will never be reset for a destructive track */
419 AudioDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record)
421 int possibly_recording;
424 const int transport_rolling = 0x4;
425 const int track_rec_enabled = 0x2;
426 const int global_rec_enabled = 0x1;
428 /* merge together the 3 factors that affect record status, and compute
432 rolling = _session.transport_speed() != 0.0f;
433 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
434 change = possibly_recording ^ last_possibly_recording;
436 if (possibly_recording == last_possibly_recording) {
442 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
444 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
445 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
447 /* starting to record: compute first+last frames */
449 first_recordable_frame = transport_frame + _capture_offset;
450 last_recordable_frame = max_frames;
451 capture_start_frame = transport_frame;
453 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
455 /* was stopped, now rolling (and recording) */
457 if (_alignment_style == ExistingMaterial) {
458 first_recordable_frame += _session.worst_output_latency();
460 first_recordable_frame += _roll_delay;
465 /* was rolling, but record state changed */
467 if (_alignment_style == ExistingMaterial) {
470 if (!_session.get_punch_in()) {
472 /* manual punch in happens at the correct transport frame
473 because the user hit a button. but to get alignment correct
474 we have to back up the position of the new region to the
475 appropriate spot given the roll delay.
478 capture_start_frame -= _roll_delay;
480 /* XXX paul notes (august 2005): i don't know why
484 first_recordable_frame += _capture_offset;
488 /* autopunch toggles recording at the precise
489 transport frame, and then the DS waits
490 to start recording for a time that depends
491 on the output latency.
494 first_recordable_frame += _session.worst_output_latency();
499 if (_session.get_punch_in()) {
500 first_recordable_frame += _roll_delay;
502 capture_start_frame -= _roll_delay;
508 if (_flags & Recordable) {
509 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
511 RingBufferNPT<CaptureTransition>::rw_vector transvec;
512 (*chan).capture_transition_buf->get_write_vector(&transvec);
514 if (transvec.len[0] > 0) {
515 transvec.buf[0]->type = CaptureStart;
516 transvec.buf[0]->capture_val = capture_start_frame;
517 (*chan).capture_transition_buf->increment_write_ptr(1);
521 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
527 } else if (!record_enabled() || !can_record) {
531 last_recordable_frame = transport_frame + _capture_offset;
533 if (_alignment_style == ExistingMaterial) {
534 last_recordable_frame += _session.worst_output_latency();
536 last_recordable_frame += _roll_delay;
540 last_possibly_recording = possibly_recording;
544 AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input)
547 ChannelList::iterator c;
549 jack_nframes_t rec_offset = 0;
550 jack_nframes_t rec_nframes = 0;
551 bool nominally_recording;
552 bool re = record_enabled ();
553 bool collect_playback = false;
555 /* if we've already processed the frames corresponding to this call,
556 just return. this allows multiple routes that are taking input
557 from this diskstream to call our ::process() method, but have
558 this stuff only happen once. more commonly, it allows both
559 the AudioTrack that is using this AudioDiskstream *and* the Session
560 to call process() without problems.
567 check_record_status (transport_frame, nframes, can_record);
569 nominally_recording = (can_record && re);
576 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
577 must always be called as a pair. The only exception is if this function
578 returns a non-zero value, in which case, ::commit should not be called.
581 // If we can't take the state lock return.
582 if (!state_lock.trylock()) {
586 adjust_capture_position = 0;
588 for (c = channels.begin(); c != channels.end(); ++c) {
589 (*c).current_capture_buffer = 0;
590 (*c).current_playback_buffer = 0;
593 if (nominally_recording || (_session.get_record_enabled() && _session.get_punch_in())) {
596 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
603 case OverlapInternal:
604 /* ---------- recrange
607 rec_nframes = nframes;
612 /* |--------| recrange
615 rec_nframes = transport_frame + nframes - first_recordable_frame;
617 rec_offset = first_recordable_frame - transport_frame;
622 /* |--------| recrange
625 rec_nframes = last_recordable_frame - transport_frame;
629 case OverlapExternal:
630 /* |--------| recrange
631 -------------- transrange
633 rec_nframes = last_recordable_frame - last_recordable_frame;
634 rec_offset = first_recordable_frame - transport_frame;
638 if (rec_nframes && !was_recording) {
639 capture_captured = 0;
640 was_recording = true;
645 if (can_record && !_last_capture_regions.empty()) {
646 _last_capture_regions.clear ();
649 if (nominally_recording || rec_nframes) {
651 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
653 ChannelInfo& chan (*c);
655 chan.capture_buf->get_write_vector (&chan.capture_vector);
657 if (rec_nframes <= chan.capture_vector.len[0]) {
659 chan.current_capture_buffer = chan.capture_vector.buf[0];
661 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
665 AudioPort* const ap = _io->audio_input(n);
667 assert(rec_nframes <= ap->get_audio_buffer().capacity());
669 memcpy (chan.current_capture_buffer, ap->get_audio_buffer().data(rec_nframes, offset + rec_offset), sizeof (Sample) * rec_nframes);
673 jack_nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
675 if (rec_nframes > total) {
680 AudioPort* const ap = _io->audio_input(n);
683 Sample* buf = ap->get_audio_buffer().data(nframes, offset);
684 jack_nframes_t first = chan.capture_vector.len[0];
686 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
687 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
688 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
689 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
691 chan.current_capture_buffer = chan.capture_wrap_buffer;
698 finish_capture (rec_monitors_input);
705 /* data will be written to disk */
707 if (rec_nframes == nframes && rec_offset == 0) {
709 for (c = channels.begin(); c != channels.end(); ++c) {
710 (*c).current_playback_buffer = (*c).current_capture_buffer;
713 playback_distance = nframes;
718 /* we can't use the capture buffer as the playback buffer, because
719 we recorded only a part of the current process' cycle data
723 collect_playback = true;
726 adjust_capture_position = rec_nframes;
728 } else if (nominally_recording) {
730 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
732 for (c = channels.begin(); c != channels.end(); ++c) {
733 (*c).current_playback_buffer = (*c).current_capture_buffer;
736 playback_distance = nframes;
740 collect_playback = true;
743 if (collect_playback) {
745 /* we're doing playback */
747 jack_nframes_t necessary_samples;
749 /* no varispeed playback if we're recording, because the output .... TBD */
751 if (rec_nframes == 0 && _actual_speed != 1.0f) {
752 necessary_samples = (jack_nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
754 necessary_samples = nframes;
757 for (c = channels.begin(); c != channels.end(); ++c) {
758 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
763 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
765 ChannelInfo& chan (*c);
767 if (necessary_samples <= chan.playback_vector.len[0]) {
769 chan.current_playback_buffer = chan.playback_vector.buf[0];
772 jack_nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
774 if (necessary_samples > total) {
780 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
781 chan.playback_vector.len[0] * sizeof (Sample));
782 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
783 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
785 chan.current_playback_buffer = chan.playback_wrap_buffer;
790 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
792 uint64_t phase = last_phase;
793 jack_nframes_t i = 0;
795 // Linearly interpolate into the alt buffer
796 // using 40.24 fixp maths (swh)
798 for (c = channels.begin(); c != channels.end(); ++c) {
801 ChannelInfo& chan (*c);
806 for (jack_nframes_t outsample = 0; outsample < nframes; ++outsample) {
808 fr = (phase & 0xFFFFFF) / 16777216.0f;
809 chan.speed_buffer[outsample] =
810 chan.current_playback_buffer[i] * (1.0f - fr) +
811 chan.current_playback_buffer[i+1] * fr;
815 chan.current_playback_buffer = chan.speed_buffer;
818 playback_distance = i + 1;
819 last_phase = (phase & 0xFFFFFF);
822 playback_distance = nframes;
834 /* we're exiting with failure, so ::commit will not
835 be called. unlock the state lock.
845 AudioDiskstream::commit (jack_nframes_t nframes)
847 bool need_butler = false;
849 if (_actual_speed < 0.0) {
850 playback_sample -= playback_distance;
852 playback_sample += playback_distance;
855 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
857 (*chan).playback_buf->increment_read_ptr (playback_distance);
859 if (adjust_capture_position) {
860 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
864 if (adjust_capture_position != 0) {
865 capture_captured += adjust_capture_position;
866 adjust_capture_position = 0;
870 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
872 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
873 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
884 AudioDiskstream::set_pending_overwrite (bool yn)
886 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
888 pending_overwrite = yn;
890 overwrite_frame = playback_sample;
891 overwrite_offset = channels.front().playback_buf->get_read_ptr();
895 AudioDiskstream::overwrite_existing_buffers ()
897 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];
910 /* reduce size so that we can fill the buffer correctly. */
914 jack_nframes_t start;
916 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
918 start = overwrite_frame;
919 jack_nframes_t cnt = size;
921 /* to fill the buffer without resetting the playback sample, we need to
922 do it one or two chunks (normally two).
924 |----------------------------------------------------------------------|
928 |<- second chunk->||<----------------- first chunk ------------------>|
932 jack_nframes_t to_read = size - overwrite_offset;
934 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, *chan, n, reversed)) {
935 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
936 _id, size, playback_sample) << endmsg;
944 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer,
945 start, cnt, *chan, n, reversed)) {
946 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
947 _id, size, playback_sample) << endmsg;
956 pending_overwrite = false;
957 delete [] gain_buffer;
958 delete [] mixdown_buffer;
963 AudioDiskstream::seek (jack_nframes_t frame, bool complete_refill)
965 Glib::Mutex::Lock lm (state_lock);
968 ChannelList::iterator chan;
970 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
971 (*chan).playback_buf->reset ();
972 (*chan).capture_buf->reset ();
975 /* can't rec-enable in destructive mode if transport is before start */
977 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
978 disengage_record_enable ();
981 playback_sample = frame;
984 if (complete_refill) {
985 while ((ret = do_refill_with_alloc ()) > 0) ;
987 ret = do_refill_with_alloc ();
994 AudioDiskstream::can_internal_playback_seek (jack_nframes_t distance)
996 ChannelList::iterator chan;
998 for (chan = channels.begin(); chan != channels.end(); ++chan) {
999 if ((*chan).playback_buf->read_space() < distance) {
1007 AudioDiskstream::internal_playback_seek (jack_nframes_t distance)
1009 ChannelList::iterator chan;
1011 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1012 (*chan).playback_buf->increment_read_ptr (distance);
1015 first_recordable_frame += distance;
1016 playback_sample += distance;
1022 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, jack_nframes_t& start, jack_nframes_t cnt,
1023 ChannelInfo& channel_info, int channel, bool reversed)
1025 jack_nframes_t this_read = 0;
1026 bool reloop = false;
1027 jack_nframes_t loop_end = 0;
1028 jack_nframes_t loop_start = 0;
1029 jack_nframes_t loop_length = 0;
1030 jack_nframes_t offset = 0;
1034 /* Make the use of a Location atomic for this read operation.
1036 Note: Locations don't get deleted, so all we care about
1037 when I say "atomic" is that we are always pointing to
1038 the same one and using a start/length values obtained
1042 if ((loc = loop_location) != 0) {
1043 loop_start = loc->start();
1044 loop_end = loc->end();
1045 loop_length = loop_end - loop_start;
1048 /* if we are looping, ensure that the first frame we read is at the correct
1049 position within the loop.
1052 if (loc && start >= loop_end) {
1053 //cerr << "start adjusted from " << start;
1054 start = loop_start + ((start - loop_start) % loop_length);
1055 //cerr << "to " << start << endl;
1057 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1062 /* take any loop into account. we can't read past the end of the loop. */
1064 if (loc && (loop_end - start < cnt)) {
1065 this_read = loop_end - start;
1066 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1073 if (this_read == 0) {
1077 this_read = min(cnt,this_read);
1079 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1080 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1085 _read_data_count = _playlist->read_data_count();
1089 /* don't adjust start, since caller has already done that
1092 swap_by_ptr (buf, buf + this_read - 1);
1096 /* if we read to the end of the loop, go back to the beginning */
1106 offset += this_read;
1113 AudioDiskstream::do_refill_with_alloc()
1115 Sample* mix_buf = new Sample[disk_io_chunk_frames];
1116 float* gain_buf = new float[disk_io_chunk_frames];
1118 int ret = _do_refill(mix_buf, gain_buf);
1127 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
1130 jack_nframes_t to_read;
1131 RingBufferNPT<Sample>::rw_vector vector;
1132 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1133 jack_nframes_t total_space;
1134 jack_nframes_t zero_fill;
1136 ChannelList::iterator i;
1139 assert(mixdown_buffer);
1140 assert(gain_buffer);
1142 channels.front().playback_buf->get_write_vector (&vector);
1144 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1148 /* if there are 2+ chunks of disk i/o possible for
1149 this track, let the caller know so that it can arrange
1150 for us to be called again, ASAP.
1153 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1157 /* if we're running close to normal speed and there isn't enough
1158 space to do disk_io_chunk_frames of I/O, then don't bother.
1160 at higher speeds, just do it because the sync between butler
1161 and audio thread may not be good enough.
1164 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1168 /* when slaved, don't try to get too close to the read pointer. this
1169 leaves space for the buffer reversal to have something useful to
1173 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1177 total_space = min (disk_io_chunk_frames, total_space);
1181 if (file_frame == 0) {
1183 /* at start: nothing to do but fill with silence */
1185 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1187 ChannelInfo& chan (*i);
1188 chan.playback_buf->get_write_vector (&vector);
1189 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1190 if (vector.len[1]) {
1191 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1193 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1198 if (file_frame < total_space) {
1200 /* too close to the start: read what we can,
1201 and then zero fill the rest
1204 zero_fill = total_space - file_frame;
1205 total_space = file_frame;
1210 /* move read position backwards because we are going
1211 to reverse the data.
1214 file_frame -= total_space;
1220 if (file_frame == max_frames) {
1222 /* at end: nothing to do but fill with silence */
1224 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1226 ChannelInfo& chan (*i);
1227 chan.playback_buf->get_write_vector (&vector);
1228 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1229 if (vector.len[1]) {
1230 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1232 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1237 if (file_frame > max_frames - total_space) {
1239 /* to close to the end: read what we can, and zero fill the rest */
1241 zero_fill = total_space - (max_frames - file_frame);
1242 total_space = max_frames - file_frame;
1249 jack_nframes_t file_frame_tmp = 0;
1251 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1253 ChannelInfo& chan (*i);
1256 jack_nframes_t len1, len2;
1258 chan.playback_buf->get_write_vector (&vector);
1261 file_frame_tmp = file_frame;
1264 buf1 = vector.buf[1];
1265 len1 = vector.len[1];
1266 buf2 = vector.buf[0];
1267 len2 = vector.len[0];
1269 buf1 = vector.buf[0];
1270 len1 = vector.len[0];
1271 buf2 = vector.buf[1];
1272 len2 = vector.len[1];
1276 to_read = min (ts, len1);
1277 to_read = min (to_read, disk_io_chunk_frames);
1281 if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1286 chan.playback_buf->increment_write_ptr (to_read);
1290 to_read = min (ts, len2);
1295 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1296 so read some or all of vector.len[1] as well.
1299 if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1304 chan.playback_buf->increment_write_ptr (to_read);
1313 file_frame = file_frame_tmp;
1320 /** Flush pending data to disk.
1322 * Important note: this function will write *AT MOST* disk_io_chunk_frames
1323 * of data to disk. it will never write more than that. If it writes that
1324 * much and there is more than that waiting to be written, it will return 1,
1325 * otherwise 0 on success or -1 on failure.
1327 * If there is less than disk_io_chunk_frames to be written, no data will be
1328 * written at all unless @a force_flush is true.
1331 AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
1335 RingBufferNPT<Sample>::rw_vector vector;
1336 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1337 jack_nframes_t total;
1339 _write_data_count = 0;
1341 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1343 (*chan).capture_buf->get_read_vector (&vector);
1345 total = vector.len[0] + vector.len[1];
1348 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1352 /* if there are 2+ chunks of disk i/o possible for
1353 this track, let the caller know so that it can arrange
1354 for us to be called again, ASAP.
1356 if we are forcing a flush, then if there is* any* extra
1357 work, let the caller know.
1359 if we are no longer recording and there is any extra work,
1360 let the caller know too.
1363 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1367 to_write = min (disk_io_chunk_frames, (jack_nframes_t) vector.len[0]);
1369 // check the transition buffer when recording destructive
1370 // important that we get this after the capture buf
1372 if (destructive()) {
1373 (*chan).capture_transition_buf->get_read_vector(&transvec);
1374 size_t transcount = transvec.len[0] + transvec.len[1];
1375 bool have_start = false;
1378 for (ti=0; ti < transcount; ++ti) {
1379 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1381 if (captrans.type == CaptureStart) {
1382 // by definition, the first data we got above represents the given capture pos
1384 (*chan).write_source->mark_capture_start (captrans.capture_val);
1385 (*chan).curr_capture_cnt = 0;
1389 else if (captrans.type == CaptureEnd) {
1391 // capture end, the capture_val represents total frames in capture
1393 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1395 // shorten to make the write a perfect fit
1396 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1398 if (nto_write < to_write) {
1399 ret = 1; // should we?
1401 to_write = nto_write;
1403 (*chan).write_source->mark_capture_end ();
1405 // increment past this transition, but go no further
1410 // actually ends just beyond this chunk, so force more work
1418 (*chan).capture_transition_buf->increment_read_ptr(ti);
1422 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write) != to_write) {
1423 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1427 (*chan).capture_buf->increment_read_ptr (to_write);
1428 (*chan).curr_capture_cnt += to_write;
1430 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1432 /* we wrote all of vector.len[0] but it wasn't an entire
1433 disk_io_chunk_frames of data, so arrange for some part
1434 of vector.len[1] to be flushed to disk as well.
1437 to_write = min ((jack_nframes_t)(disk_io_chunk_frames - to_write), (jack_nframes_t) vector.len[1]);
1439 if ((*chan).write_source->write (vector.buf[1], to_write) != to_write) {
1440 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1444 _write_data_count += (*chan).write_source->write_data_count();
1446 (*chan).capture_buf->increment_read_ptr (to_write);
1447 (*chan).curr_capture_cnt += to_write;
1456 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1458 uint32_t buffer_position;
1459 bool more_work = true;
1461 boost::shared_ptr<AudioRegion> region;
1462 jack_nframes_t total_capture;
1464 SourceList::iterator src;
1465 ChannelList::iterator chan;
1466 vector<CaptureInfo*>::iterator ci;
1468 bool mark_write_completed = false;
1470 finish_capture (true);
1472 /* butler is already stopped, but there may be work to do
1473 to flush remaining data to disk.
1476 while (more_work && !err) {
1477 switch (do_flush (Session::TransportContext, true)) {
1484 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1489 /* XXX is there anything we can do if err != 0 ? */
1490 Glib::Mutex::Lock lm (capture_info_lock);
1492 if (capture_info.empty()) {
1496 if (abort_capture) {
1498 ChannelList::iterator chan;
1500 list<boost::shared_ptr<Source> >* deletion_list = new list<boost::shared_ptr<Source> >;
1502 for ( chan = channels.begin(); chan != channels.end(); ++chan) {
1504 if ((*chan).write_source) {
1506 (*chan).write_source->mark_for_remove ();
1508 deletion_list->push_back ((*chan).write_source);
1510 (*chan).write_source.reset ();
1513 /* new source set up in "out" below */
1516 if (!deletion_list->empty()) {
1517 DeleteSources (deletion_list);
1519 delete deletion_list;
1525 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1526 total_capture += (*ci)->frames;
1529 /* figure out the name for this take */
1531 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1533 boost::shared_ptr<AudioFileSource> s = (*chan).write_source;
1537 s->update_header (capture_info.front()->start, when, twhen);
1538 s->set_captured_for (_name);
1543 /* destructive tracks have a single, never changing region */
1545 if (destructive()) {
1547 /* send a signal that any UI can pick up to do the right thing. there is
1548 a small problem here in that a UI may need the peak data to be ready
1549 for the data that was recorded and this isn't interlocked with that
1550 process. this problem is deferred to the UI.
1553 _playlist->Modified();
1557 /* Register a new region with the Session that
1558 describes the entire source. Do this first
1559 so that any sub-regions will obviously be
1560 children of this one (later!)
1564 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1565 region_name_from_path (channels[0].write_source->name()),
1566 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
1568 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1569 region->special_set_position (capture_info.front()->start);
1573 catch (failed_constructor& err) {
1574 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1578 _last_capture_regions.push_back (region);
1580 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1582 XMLNode &before = _playlist->get_state();
1583 _playlist->freeze ();
1585 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1588 _session.region_name (region_name, channels[0].write_source->name(), false);
1590 cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
1593 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1594 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1597 catch (failed_constructor& err) {
1598 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1599 continue; /* XXX is this OK? */
1602 _last_capture_regions.push_back (region);
1604 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1606 i_am_the_modifier++;
1607 _playlist->add_region (region, (*ci)->start);
1608 i_am_the_modifier--;
1610 buffer_position += (*ci)->frames;
1614 XMLNode &after = _playlist->get_state();
1615 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1618 mark_write_completed = true;
1620 reset_write_sources (mark_write_completed);
1623 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1627 capture_info.clear ();
1628 capture_start_frame = 0;
1632 AudioDiskstream::finish_capture (bool rec_monitors_input)
1634 was_recording = false;
1636 if (capture_captured == 0) {
1640 if (recordable() && destructive()) {
1641 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1643 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1644 (*chan).capture_transition_buf->get_write_vector(&transvec);
1647 if (transvec.len[0] > 0) {
1648 transvec.buf[0]->type = CaptureEnd;
1649 transvec.buf[0]->capture_val = capture_captured;
1650 (*chan).capture_transition_buf->increment_write_ptr(1);
1654 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1660 CaptureInfo* ci = new CaptureInfo;
1662 ci->start = capture_start_frame;
1663 ci->frames = capture_captured;
1665 /* XXX theoretical race condition here. Need atomic exchange ?
1666 However, the circumstances when this is called right
1667 now (either on record-disable or transport_stopped)
1668 mean that no actual race exists. I think ...
1669 We now have a capture_info_lock, but it is only to be used
1670 to synchronize in the transport_stop and the capture info
1671 accessors, so that invalidation will not occur (both non-realtime).
1674 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1676 capture_info.push_back (ci);
1677 capture_captured = 0;
1681 AudioDiskstream::set_record_enabled (bool yn)
1683 if (!recordable() || !_session.record_enabling_legal()) {
1687 /* can't rec-enable in destructive mode if transport is before start */
1689 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1693 if (yn && channels[0].source == 0) {
1695 /* pick up connections not initiated *from* the IO object
1696 we're associated with.
1699 get_input_sources ();
1702 /* yes, i know that this not proof against race conditions, but its
1703 good enough. i think.
1706 if (record_enabled() != yn) {
1708 engage_record_enable ();
1710 disengage_record_enable ();
1716 AudioDiskstream::engage_record_enable ()
1718 bool rolling = _session.transport_speed() != 0.0f;
1720 g_atomic_int_set (&_record_enabled, 1);
1721 capturing_sources.clear ();
1722 if (Config->get_use_hardware_monitoring()) {
1723 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1724 if ((*chan).source) {
1725 (*chan).source->request_monitor_input (!(_session.get_auto_input() && rolling));
1727 capturing_sources.push_back ((*chan).write_source);
1730 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1731 capturing_sources.push_back ((*chan).write_source);
1735 RecordEnableChanged (); /* EMIT SIGNAL */
1739 AudioDiskstream::disengage_record_enable ()
1741 g_atomic_int_set (&_record_enabled, 0);
1742 if (Config->get_use_hardware_monitoring()) {
1743 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1744 if ((*chan).source) {
1745 (*chan).source->request_monitor_input (false);
1749 capturing_sources.clear ();
1750 RecordEnableChanged (); /* EMIT SIGNAL */
1755 AudioDiskstream::get_state ()
1757 XMLNode* node = new XMLNode ("AudioDiskstream");
1759 LocaleGuard lg (X_("POSIX"));
1761 snprintf (buf, sizeof(buf), "0x%x", _flags);
1762 node->add_property ("flags", buf);
1764 snprintf (buf, sizeof(buf), "%zd", channels.size());
1765 node->add_property ("channels", buf);
1767 node->add_property ("playlist", _playlist->name());
1769 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1770 node->add_property ("speed", buf);
1772 node->add_property("name", _name);
1774 node->add_property("id", buf);
1776 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1778 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1779 XMLNode* cs_grandchild;
1781 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1782 cs_grandchild = new XMLNode (X_("file"));
1783 cs_grandchild->add_property (X_("path"), (*i)->path());
1784 cs_child->add_child_nocopy (*cs_grandchild);
1787 /* store the location where capture will start */
1791 if (_session.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1792 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1794 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1797 cs_child->add_property (X_("at"), buf);
1798 node->add_child_nocopy (*cs_child);
1802 node->add_child_copy (*_extra_xml);
1809 AudioDiskstream::set_state (const XMLNode& node)
1811 const XMLProperty* prop;
1812 XMLNodeList nlist = node.children();
1813 XMLNodeIterator niter;
1814 uint32_t nchans = 1;
1815 XMLNode* capture_pending_node = 0;
1816 LocaleGuard lg (X_("POSIX"));
1818 in_set_state = true;
1820 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1821 if ((*niter)->name() == IO::state_node_name) {
1822 deprecated_io_node = new XMLNode (**niter);
1825 if ((*niter)->name() == X_("CapturingSources")) {
1826 capture_pending_node = *niter;
1830 /* prevent write sources from being created */
1832 in_set_state = true;
1834 if ((prop = node.property ("name")) != 0) {
1835 _name = prop->value();
1838 if (deprecated_io_node) {
1839 if ((prop = deprecated_io_node->property ("id")) != 0) {
1840 _id = prop->value ();
1843 if ((prop = node.property ("id")) != 0) {
1844 _id = prop->value ();
1848 if ((prop = node.property ("flags")) != 0) {
1849 _flags = strtol (prop->value().c_str(), 0, 0);
1852 if ((prop = node.property ("channels")) != 0) {
1853 nchans = atoi (prop->value().c_str());
1856 // create necessary extra channels
1857 // we are always constructed with one and we always need one
1859 if (nchans > _n_channels.get(DataType::AUDIO)) {
1861 // we need to add new channel infos
1862 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1864 int diff = nchans - channels.size();
1866 for (int i=0; i < diff; ++i) {
1870 } else if (nchans < _n_channels.get(DataType::AUDIO)) {
1872 // we need to get rid of channels
1873 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1875 int diff = channels.size() - nchans;
1877 for (int i = 0; i < diff; ++i) {
1882 if ((prop = node.property ("playlist")) == 0) {
1887 bool had_playlist = (_playlist != 0);
1889 if (find_and_use_playlist (prop->value())) {
1893 if (!had_playlist) {
1894 _playlist->set_orig_diskstream_id (_id);
1897 if (!destructive() && capture_pending_node) {
1898 /* destructive streams have one and only one source per channel,
1899 and so they never end up in pending capture in any useful
1902 use_pending_capture_data (*capture_pending_node);
1907 if ((prop = node.property ("speed")) != 0) {
1908 double sp = atof (prop->value().c_str());
1910 if (realtime_set_speed (sp, false)) {
1911 non_realtime_set_speed ();
1915 _n_channels.set(DataType::AUDIO, channels.size());
1917 in_set_state = false;
1919 /* make sure this is clear before we do anything else */
1921 capturing_sources.clear ();
1923 /* write sources are handled when we handle the input set
1924 up of the IO that owns this DS (::non_realtime_input_change())
1927 in_set_state = false;
1933 AudioDiskstream::use_new_write_source (uint32_t n)
1935 if (!recordable()) {
1939 if (n >= channels.size()) {
1940 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1944 ChannelInfo &chan = channels[n];
1946 if (chan.write_source) {
1948 if (AudioFileSource::is_empty (chan.write_source->path())) {
1949 chan.write_source->mark_for_remove ();
1950 chan.write_source.reset ();
1952 chan.write_source.reset ();
1957 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
1958 throw failed_constructor();
1962 catch (failed_constructor &err) {
1963 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1964 chan.write_source.reset ();
1968 /* do not remove destructive files even if they are empty */
1970 chan.write_source->set_allow_remove_if_empty (!destructive());
1976 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1978 ChannelList::iterator chan;
1981 if (!recordable()) {
1985 capturing_sources.clear ();
1987 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
1988 if (!destructive()) {
1990 if ((*chan).write_source && mark_write_complete) {
1991 (*chan).write_source->mark_streaming_write_completed ();
1993 use_new_write_source (n);
1995 if (record_enabled()) {
1996 capturing_sources.push_back ((*chan).write_source);
2000 if ((*chan).write_source == 0) {
2001 use_new_write_source (n);
2006 if (destructive()) {
2008 /* we now have all our write sources set up, so create the
2009 playlist's single region.
2012 if (_playlist->empty()) {
2013 setup_destructive_playlist ();
2019 AudioDiskstream::rename_write_sources ()
2021 ChannelList::iterator chan;
2024 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2025 if ((*chan).write_source != 0) {
2026 (*chan).write_source->set_name (_name, destructive());
2027 /* XXX what to do if one of them fails ? */
2035 AudioDiskstream::set_block_size (jack_nframes_t nframes)
2037 if (_session.get_block_size() > speed_buffer_size) {
2038 speed_buffer_size = _session.get_block_size();
2040 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2041 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2042 (*chan).speed_buffer = new Sample[speed_buffer_size];
2045 allocate_temporary_buffers ();
2049 AudioDiskstream::allocate_temporary_buffers ()
2051 /* make sure the wrap buffer is at least large enough to deal
2052 with the speeds up to 1.2, to allow for micro-variation
2053 when slaving to MTC, SMPTE etc.
2056 double sp = max (fabsf (_actual_speed), 1.2f);
2057 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * sp) + 1;
2059 if (required_wrap_size > wrap_buffer_size) {
2061 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2062 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2063 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2064 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2065 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2068 wrap_buffer_size = required_wrap_size;
2073 AudioDiskstream::monitor_input (bool yn)
2075 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2077 if ((*chan).source) {
2078 (*chan).source->request_monitor_input (yn);
2084 AudioDiskstream::set_align_style_from_io ()
2086 bool have_physical = false;
2092 get_input_sources ();
2094 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2095 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2096 have_physical = true;
2101 if (have_physical) {
2102 set_align_style (ExistingMaterial);
2104 set_align_style (CaptureTime);
2109 AudioDiskstream::add_channel ()
2111 /* XXX need to take lock??? */
2115 init_channel (chan);
2117 chan.speed_buffer = new Sample[speed_buffer_size];
2118 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2119 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2121 channels.push_back (chan);
2123 _n_channels.set(DataType::AUDIO, channels.size());
2129 AudioDiskstream::remove_channel ()
2131 if (channels.size() > 1) {
2132 /* XXX need to take lock??? */
2133 ChannelInfo & chan = channels.back();
2134 destroy_channel (chan);
2135 channels.pop_back();
2137 _n_channels.set(DataType::AUDIO, channels.size());
2145 AudioDiskstream::playback_buffer_load () const
2147 return (float) ((double) channels.front().playback_buf->read_space()/
2148 (double) channels.front().playback_buf->bufsize());
2152 AudioDiskstream::capture_buffer_load () const
2154 return (float) ((double) channels.front().capture_buf->write_space()/
2155 (double) channels.front().capture_buf->bufsize());
2159 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2161 const XMLProperty* prop;
2162 XMLNodeList nlist = node.children();
2163 XMLNodeIterator niter;
2164 boost::shared_ptr<AudioFileSource> fs;
2165 boost::shared_ptr<AudioFileSource> first_fs;
2166 SourceList pending_sources;
2167 jack_nframes_t position;
2169 if ((prop = node.property (X_("at"))) == 0) {
2173 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2177 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2178 if ((*niter)->name() == X_("file")) {
2180 if ((prop = (*niter)->property (X_("path"))) == 0) {
2185 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (DataType::AUDIO, prop->value(), false, _session.frame_rate()));
2188 catch (failed_constructor& err) {
2189 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2190 _name, prop->value())
2195 pending_sources.push_back (fs);
2197 if (first_fs == 0) {
2201 fs->set_captured_for (_name);
2205 if (pending_sources.size() == 0) {
2206 /* nothing can be done */
2210 if (pending_sources.size() != _n_channels.get(DataType::AUDIO)) {
2211 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2216 boost::shared_ptr<AudioRegion> region;
2219 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(),
2220 region_name_from_path (first_fs->name()),
2221 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
2222 region->special_set_position (0);
2225 catch (failed_constructor& err) {
2226 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2234 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name())));
2237 catch (failed_constructor& err) {
2238 error << string_compose (_("%1: cannot create region from pending capture sources"),
2245 _playlist->add_region (region, position);