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>
38 #include <pbd/enumwriter.h>
40 #include <ardour/ardour.h>
41 #include <ardour/audioengine.h>
42 #include <ardour/audio_diskstream.h>
43 #include <ardour/utils.h>
44 #include <ardour/configuration.h>
45 #include <ardour/audiofilesource.h>
46 #include <ardour/send.h>
47 #include <ardour/region_factory.h>
48 #include <ardour/audioplaylist.h>
49 #include <ardour/playlist_factory.h>
50 #include <ardour/cycle_timer.h>
51 #include <ardour/audioregion.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 (Diskstream::Flag f)
103 /* there are no channels at this point, so these
104 two calls just get speed_buffer_size and wrap_buffer
105 size setup without duplicating their code.
108 set_block_size (_session.get_block_size());
109 allocate_temporary_buffers ();
112 assert(_n_channels == 1);
115 AudioDiskstream::~AudioDiskstream ()
120 /* don't be holding this lock as we exit the destructor, glib will wince
121 visibly since the mutex gets destroyed before we release it.
124 Glib::Mutex::Lock lm (state_lock);
125 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
133 AudioDiskstream::allocate_working_buffers()
135 assert(disk_io_frames() > 0);
137 _working_buffers_size = disk_io_frames();
138 _mixdown_buffer = new Sample[_working_buffers_size];
139 _gain_buffer = new gain_t[_working_buffers_size];
143 AudioDiskstream::free_working_buffers()
145 delete [] _mixdown_buffer;
146 delete [] _gain_buffer;
147 _working_buffers_size = 0;
153 AudioDiskstream::non_realtime_input_change ()
156 Glib::Mutex::Lock lm (state_lock);
158 if (input_change_pending == NoChange) {
162 if (input_change_pending & ConfigurationChanged) {
164 if (_io->n_inputs() > _n_channels) {
166 // we need to add new channel infos
168 int diff = _io->n_inputs() - channels.size();
170 for (int i = 0; i < diff; ++i) {
174 } else if (_io->n_inputs() < _n_channels) {
176 // we need to get rid of channels
178 int diff = channels.size() - _io->n_inputs();
180 for (int i = 0; i < diff; ++i) {
186 get_input_sources ();
187 set_capture_offset ();
189 if (first_input_change) {
190 set_align_style (_persistent_alignment_style);
191 first_input_change = false;
193 set_align_style_from_io ();
196 input_change_pending = NoChange;
199 /* reset capture files */
201 reset_write_sources (false);
203 /* now refill channel buffers */
205 if (speed() != 1.0f || speed() != -1.0f) {
206 seek ((nframes_t) (_session.transport_frame() * (double) speed()));
208 seek (_session.transport_frame());
213 AudioDiskstream::get_input_sources ()
215 uint32_t ni = _io->n_inputs();
217 for (uint32_t n = 0; n < ni; ++n) {
219 const char **connections = _io->input(n)->get_connections ();
220 ChannelInfo& chan = channels[n];
222 if (connections == 0 || connections[0] == 0) {
225 // _source->disable_metering ();
231 chan.source = _session.engine().get_port_by_name (connections[0]);
241 AudioDiskstream::find_and_use_playlist (const string& name)
243 boost::shared_ptr<AudioPlaylist> playlist;
245 if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (_session.playlist_by_name (name))) == 0) {
246 playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (_session, name));
250 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
254 return use_playlist (playlist);
258 AudioDiskstream::use_playlist (boost::shared_ptr<Playlist> playlist)
260 assert(boost::dynamic_pointer_cast<AudioPlaylist>(playlist));
262 Diskstream::use_playlist(playlist);
268 AudioDiskstream::use_new_playlist ()
271 boost::shared_ptr<AudioPlaylist> playlist;
273 if (!in_set_state && destructive()) {
278 newname = Playlist::bump_name (_playlist->name(), _session);
280 newname = Playlist::bump_name (_name, _session);
283 if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist> (PlaylistFactory::create (_session, newname, hidden()))) != 0) {
285 playlist->set_orig_diskstream_id (id());
286 return use_playlist (playlist);
294 AudioDiskstream::use_copy_playlist ()
296 assert(audio_playlist());
302 if (_playlist == 0) {
303 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
308 boost::shared_ptr<AudioPlaylist> playlist;
310 newname = Playlist::bump_name (_playlist->name(), _session);
312 if ((playlist = boost::dynamic_pointer_cast<AudioPlaylist>(PlaylistFactory::create (audio_playlist(), newname))) != 0) {
313 playlist->set_orig_diskstream_id (id());
314 return use_playlist (playlist);
321 AudioDiskstream::setup_destructive_playlist ()
325 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
326 srcs.push_back ((*chan).write_source);
329 /* a single full-sized region */
331 boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, max_frames - srcs.front()->natural_position(), _name));
332 _playlist->add_region (region, srcs.front()->natural_position());
336 AudioDiskstream::use_destructive_playlist ()
338 /* this is called from the XML-based constructor or ::set_destructive. when called,
339 we already have a playlist and a region, but we need to
340 set up our sources for write. we use the sources associated
341 with the (presumed single, full-extent) region.
344 boost::shared_ptr<Region> rp = _playlist->find_next_region (_session.current_start_frame(), Start, 1);
347 reset_write_sources (false, true);
351 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rp);
354 throw failed_constructor();
357 /* be sure to stretch the region out to the maximum length */
359 region->set_length (max_frames - region->position(), this);
362 ChannelList::iterator chan;
364 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
365 (*chan).write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
366 assert((*chan).write_source);
367 (*chan).write_source->set_allow_remove_if_empty (false);
369 /* this might be false if we switched modes, so force it */
371 (*chan).write_source->set_destructive (true);
374 /* the source list will never be reset for a destructive track */
378 AudioDiskstream::check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record)
380 int possibly_recording;
383 const int transport_rolling = 0x4;
384 const int track_rec_enabled = 0x2;
385 const int global_rec_enabled = 0x1;
387 /* merge together the 3 factors that affect record status, and compute
391 rolling = _session.transport_speed() != 0.0f;
392 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
393 change = possibly_recording ^ last_possibly_recording;
395 if (possibly_recording == last_possibly_recording) {
401 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
403 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
404 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
406 /* starting to record: compute first+last frames */
408 first_recordable_frame = transport_frame + _capture_offset;
409 last_recordable_frame = max_frames;
410 capture_start_frame = transport_frame;
412 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
414 /* was stopped, now rolling (and recording) */
416 if (_alignment_style == ExistingMaterial) {
417 first_recordable_frame += _session.worst_output_latency();
419 first_recordable_frame += _roll_delay;
424 /* was rolling, but record state changed */
426 if (_alignment_style == ExistingMaterial) {
428 if (!Config->get_punch_in()) {
430 /* manual punch in happens at the correct transport frame
431 because the user hit a button. but to get alignment correct
432 we have to back up the position of the new region to the
433 appropriate spot given the roll delay.
436 capture_start_frame -= _roll_delay;
438 /* XXX paul notes (august 2005): i don't know why
442 first_recordable_frame += _capture_offset;
446 /* autopunch toggles recording at the precise
447 transport frame, and then the DS waits
448 to start recording for a time that depends
449 on the output latency.
452 first_recordable_frame += _session.worst_output_latency();
457 if (Config->get_punch_in()) {
458 first_recordable_frame += _roll_delay;
460 capture_start_frame -= _roll_delay;
466 if (_flags & Recordable) {
467 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
469 RingBufferNPT<CaptureTransition>::rw_vector transvec;
470 (*chan).capture_transition_buf->get_write_vector(&transvec);
472 if (transvec.len[0] > 0) {
473 transvec.buf[0]->type = CaptureStart;
474 transvec.buf[0]->capture_val = capture_start_frame;
475 (*chan).capture_transition_buf->increment_write_ptr(1);
479 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
485 } else if (!record_enabled() || !can_record) {
489 last_recordable_frame = transport_frame + _capture_offset;
491 if (_alignment_style == ExistingMaterial) {
492 last_recordable_frame += _session.worst_output_latency();
494 last_recordable_frame += _roll_delay;
498 last_possibly_recording = possibly_recording;
502 AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_t offset, bool can_record, bool rec_monitors_input)
505 ChannelList::iterator c;
507 nframes_t rec_offset = 0;
508 nframes_t rec_nframes = 0;
509 bool nominally_recording;
510 bool re = record_enabled ();
511 bool collect_playback = false;
513 /* if we've already processed the frames corresponding to this call,
514 just return. this allows multiple routes that are taking input
515 from this diskstream to call our ::process() method, but have
516 this stuff only happen once. more commonly, it allows both
517 the AudioTrack that is using this AudioDiskstream *and* the Session
518 to call process() without problems.
525 check_record_status (transport_frame, nframes, can_record);
527 nominally_recording = (can_record && re);
534 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
535 must always be called as a pair. The only exception is if this function
536 returns a non-zero value, in which case, ::commit should not be called.
539 // If we can't take the state lock return.
540 if (!state_lock.trylock()) {
544 adjust_capture_position = 0;
546 for (c = channels.begin(); c != channels.end(); ++c) {
547 (*c).current_capture_buffer = 0;
548 (*c).current_playback_buffer = 0;
551 if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
554 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
561 case OverlapInternal:
562 /* ---------- recrange
565 rec_nframes = nframes;
570 /* |--------| recrange
573 rec_nframes = transport_frame + nframes - first_recordable_frame;
575 rec_offset = first_recordable_frame - transport_frame;
580 /* |--------| recrange
583 rec_nframes = last_recordable_frame - transport_frame;
587 case OverlapExternal:
588 /* |--------| recrange
589 -------------- transrange
591 rec_nframes = last_recordable_frame - last_recordable_frame;
592 rec_offset = first_recordable_frame - transport_frame;
596 if (rec_nframes && !was_recording) {
597 capture_captured = 0;
598 was_recording = true;
603 if (can_record && !_last_capture_regions.empty()) {
604 _last_capture_regions.clear ();
607 if (nominally_recording || rec_nframes) {
609 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
611 ChannelInfo& chan (*c);
613 chan.capture_buf->get_write_vector (&chan.capture_vector);
615 if (rec_nframes <= chan.capture_vector.len[0]) {
617 chan.current_capture_buffer = chan.capture_vector.buf[0];
619 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
623 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
627 nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
629 if (rec_nframes > total) {
634 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
635 nframes_t first = chan.capture_vector.len[0];
637 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
638 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
639 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
640 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
642 chan.current_capture_buffer = chan.capture_wrap_buffer;
649 finish_capture (rec_monitors_input);
656 /* data will be written to disk */
658 if (rec_nframes == nframes && rec_offset == 0) {
660 for (c = channels.begin(); c != channels.end(); ++c) {
661 (*c).current_playback_buffer = (*c).current_capture_buffer;
664 playback_distance = nframes;
669 /* we can't use the capture buffer as the playback buffer, because
670 we recorded only a part of the current process' cycle data
674 collect_playback = true;
677 adjust_capture_position = rec_nframes;
679 } else if (nominally_recording) {
681 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
683 for (c = channels.begin(); c != channels.end(); ++c) {
684 (*c).current_playback_buffer = (*c).current_capture_buffer;
687 playback_distance = nframes;
691 collect_playback = true;
694 if (collect_playback) {
696 /* we're doing playback */
698 nframes_t necessary_samples;
700 /* no varispeed playback if we're recording, because the output .... TBD */
702 if (rec_nframes == 0 && _actual_speed != 1.0f) {
703 necessary_samples = (nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
705 necessary_samples = nframes;
708 for (c = channels.begin(); c != channels.end(); ++c) {
709 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
714 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
716 ChannelInfo& chan (*c);
718 if (necessary_samples <= chan.playback_vector.len[0]) {
720 chan.current_playback_buffer = chan.playback_vector.buf[0];
723 nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
725 if (necessary_samples > total) {
731 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
732 chan.playback_vector.len[0] * sizeof (Sample));
733 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
734 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
736 chan.current_playback_buffer = chan.playback_wrap_buffer;
741 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
743 uint64_t phase = last_phase;
746 // Linearly interpolate into the alt buffer
747 // using 40.24 fixp maths (swh)
749 for (c = channels.begin(); c != channels.end(); ++c) {
752 ChannelInfo& chan (*c);
757 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
759 fr = (phase & 0xFFFFFF) / 16777216.0f;
760 chan.speed_buffer[outsample] =
761 chan.current_playback_buffer[i] * (1.0f - fr) +
762 chan.current_playback_buffer[i+1] * fr;
766 chan.current_playback_buffer = chan.speed_buffer;
769 playback_distance = i + 1;
770 last_phase = (phase & 0xFFFFFF);
773 playback_distance = nframes;
785 /* we're exiting with failure, so ::commit will not
786 be called. unlock the state lock.
796 AudioDiskstream::commit (nframes_t nframes)
798 bool need_butler = false;
800 if (_actual_speed < 0.0) {
801 playback_sample -= playback_distance;
803 playback_sample += playback_distance;
806 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
808 (*chan).playback_buf->increment_read_ptr (playback_distance);
810 if (adjust_capture_position) {
811 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
815 if (adjust_capture_position != 0) {
816 capture_captured += adjust_capture_position;
817 adjust_capture_position = 0;
821 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
823 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
824 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
834 AudioDiskstream::set_pending_overwrite (bool yn)
836 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
838 pending_overwrite = yn;
840 overwrite_frame = playback_sample;
841 overwrite_offset = channels.front().playback_buf->get_read_ptr();
845 AudioDiskstream::overwrite_existing_buffers ()
847 Sample* mixdown_buffer;
850 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
852 overwrite_queued = false;
854 /* assume all are the same size */
855 nframes_t size = channels[0].playback_buf->bufsize();
857 mixdown_buffer = new Sample[size];
858 gain_buffer = new float[size];
860 /* reduce size so that we can fill the buffer correctly. */
866 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
868 start = overwrite_frame;
869 nframes_t cnt = size;
871 /* to fill the buffer without resetting the playback sample, we need to
872 do it one or two chunks (normally two).
874 |----------------------------------------------------------------------|
878 |<- second chunk->||<----------------- first chunk ------------------>|
882 nframes_t to_read = size - overwrite_offset;
884 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, *chan, n, reversed)) {
885 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
886 _id, size, playback_sample) << endmsg;
894 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer,
895 start, cnt, *chan, n, reversed)) {
896 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
897 _id, size, playback_sample) << endmsg;
906 pending_overwrite = false;
907 delete [] gain_buffer;
908 delete [] mixdown_buffer;
913 AudioDiskstream::seek (nframes_t frame, bool complete_refill)
915 Glib::Mutex::Lock lm (state_lock);
918 ChannelList::iterator chan;
920 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
921 (*chan).playback_buf->reset ();
922 (*chan).capture_buf->reset ();
925 /* can't rec-enable in destructive mode if transport is before start */
927 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
928 disengage_record_enable ();
931 playback_sample = frame;
934 if (complete_refill) {
935 while ((ret = do_refill_with_alloc ()) > 0) ;
937 ret = do_refill_with_alloc ();
944 AudioDiskstream::can_internal_playback_seek (nframes_t distance)
946 ChannelList::iterator chan;
948 for (chan = channels.begin(); chan != channels.end(); ++chan) {
949 if ((*chan).playback_buf->read_space() < distance) {
957 AudioDiskstream::internal_playback_seek (nframes_t distance)
959 ChannelList::iterator chan;
961 for (chan = channels.begin(); chan != channels.end(); ++chan) {
962 (*chan).playback_buf->increment_read_ptr (distance);
965 first_recordable_frame += distance;
966 playback_sample += distance;
972 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, nframes_t& start, nframes_t cnt,
973 ChannelInfo& channel_info, int channel, bool reversed)
975 nframes_t this_read = 0;
977 nframes_t loop_end = 0;
978 nframes_t loop_start = 0;
979 nframes_t loop_length = 0;
980 nframes_t offset = 0;
983 /* XXX we don't currently play loops in reverse. not sure why */
987 /* Make the use of a Location atomic for this read operation.
989 Note: Locations don't get deleted, so all we care about
990 when I say "atomic" is that we are always pointing to
991 the same one and using a start/length values obtained
995 if ((loc = loop_location) != 0) {
996 loop_start = loc->start();
997 loop_end = loc->end();
998 loop_length = loop_end - loop_start;
1001 /* if we are looping, ensure that the first frame we read is at the correct
1002 position within the loop.
1005 if (loc && start >= loop_end) {
1006 //cerr << "start adjusted from " << start;
1007 start = loop_start + ((start - loop_start) % loop_length);
1008 //cerr << "to " << start << endl;
1011 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1020 /* take any loop into account. we can't read past the end of the loop. */
1022 if (loc && (loop_end - start < cnt)) {
1023 this_read = loop_end - start;
1024 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1031 if (this_read == 0) {
1035 this_read = min(cnt,this_read);
1037 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1038 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1043 _read_data_count = _playlist->read_data_count();
1047 swap_by_ptr (buf, buf + this_read - 1);
1051 /* if we read to the end of the loop, go back to the beginning */
1061 offset += this_read;
1068 AudioDiskstream::do_refill_with_alloc()
1070 Sample* mix_buf = new Sample[disk_io_chunk_frames];
1071 float* gain_buf = new float[disk_io_chunk_frames];
1073 int ret = _do_refill(mix_buf, gain_buf);
1082 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
1086 RingBufferNPT<Sample>::rw_vector vector;
1087 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1088 nframes_t total_space;
1089 nframes_t zero_fill;
1091 ChannelList::iterator i;
1094 assert(mixdown_buffer);
1095 assert(gain_buffer);
1102 channels.front().playback_buf->get_write_vector (&vector);
1105 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1109 /* if there are 2+ chunks of disk i/o possible for
1110 this track, let the caller know so that it can arrange
1111 for us to be called again, ASAP.
1114 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1118 /* if we're running close to normal speed and there isn't enough
1119 space to do disk_io_chunk_frames of I/O, then don't bother.
1121 at higher speeds, just do it because the sync between butler
1122 and audio thread may not be good enough.
1125 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1129 /* when slaved, don't try to get too close to the read pointer. this
1130 leaves space for the buffer reversal to have something useful to
1134 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1138 /* never do more than disk_io_chunk_frames worth of disk input per call (limit doesn't apply for memset) */
1140 total_space = min (disk_io_chunk_frames, total_space);
1144 if (file_frame == 0) {
1146 /* at start: nothing to do but fill with silence */
1148 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1150 ChannelInfo& chan (*i);
1151 chan.playback_buf->get_write_vector (&vector);
1152 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1153 if (vector.len[1]) {
1154 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1156 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1161 if (file_frame < total_space) {
1163 /* too close to the start: read what we can,
1164 and then zero fill the rest
1167 zero_fill = total_space - file_frame;
1168 total_space = file_frame;
1178 if (file_frame == max_frames) {
1180 /* at end: nothing to do but fill with silence */
1182 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1184 ChannelInfo& chan (*i);
1185 chan.playback_buf->get_write_vector (&vector);
1186 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1187 if (vector.len[1]) {
1188 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1190 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1195 if (file_frame > max_frames - total_space) {
1197 /* to close to the end: read what we can, and zero fill the rest */
1199 zero_fill = total_space - (max_frames - file_frame);
1200 total_space = max_frames - file_frame;
1207 nframes_t file_frame_tmp = 0;
1209 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1211 ChannelInfo& chan (*i);
1214 nframes_t len1, len2;
1216 chan.playback_buf->get_write_vector (&vector);
1218 if (vector.len[0] > disk_io_chunk_frames) {
1220 /* we're not going to fill the first chunk, so certainly do not bother with the
1221 other part. it won't be connected with the part we do fill, as in:
1223 .... => writable space
1224 ++++ => readable space
1225 ^^^^ => 1 x disk_io_chunk_frames that would be filled
1227 |......|+++++++++++++|...............................|
1232 So, just pretend that the buf1 part isn't there.
1242 file_frame_tmp = file_frame;
1244 buf1 = vector.buf[0];
1245 len1 = vector.len[0];
1246 buf2 = vector.buf[1];
1247 len2 = vector.len[1];
1249 to_read = min (ts, len1);
1250 to_read = min (to_read, disk_io_chunk_frames);
1254 if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1259 chan.playback_buf->increment_write_ptr (to_read);
1263 to_read = min (ts, len2);
1267 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1268 so read some or all of vector.len[1] as well.
1271 if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1276 chan.playback_buf->increment_write_ptr (to_read);
1285 file_frame = file_frame_tmp;
1292 /** Flush pending data to disk.
1294 * Important note: this function will write *AT MOST* disk_io_chunk_frames
1295 * of data to disk. it will never write more than that. If it writes that
1296 * much and there is more than that waiting to be written, it will return 1,
1297 * otherwise 0 on success or -1 on failure.
1299 * If there is less than disk_io_chunk_frames to be written, no data will be
1300 * written at all unless @a force_flush is true.
1303 AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
1307 RingBufferNPT<Sample>::rw_vector vector;
1308 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1311 _write_data_count = 0;
1313 transvec.buf[0] = 0;
1314 transvec.buf[1] = 0;
1318 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1320 (*chan).capture_buf->get_read_vector (&vector);
1322 total = vector.len[0] + vector.len[1];
1324 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1328 /* if there are 2+ chunks of disk i/o possible for
1329 this track, let the caller know so that it can arrange
1330 for us to be called again, ASAP.
1332 if we are forcing a flush, then if there is* any* extra
1333 work, let the caller know.
1335 if we are no longer recording and there is any extra work,
1336 let the caller know too.
1339 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1343 to_write = min (disk_io_chunk_frames, (nframes_t) vector.len[0]);
1345 // check the transition buffer when recording destructive
1346 // important that we get this after the capture buf
1348 if (destructive()) {
1349 (*chan).capture_transition_buf->get_read_vector(&transvec);
1350 size_t transcount = transvec.len[0] + transvec.len[1];
1351 bool have_start = false;
1354 for (ti=0; ti < transcount; ++ti) {
1355 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1357 if (captrans.type == CaptureStart) {
1358 // by definition, the first data we got above represents the given capture pos
1360 (*chan).write_source->mark_capture_start (captrans.capture_val);
1361 (*chan).curr_capture_cnt = 0;
1365 else if (captrans.type == CaptureEnd) {
1367 // capture end, the capture_val represents total frames in capture
1369 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1371 // shorten to make the write a perfect fit
1372 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1374 if (nto_write < to_write) {
1375 ret = 1; // should we?
1377 to_write = nto_write;
1379 (*chan).write_source->mark_capture_end ();
1381 // increment past this transition, but go no further
1386 // actually ends just beyond this chunk, so force more work
1394 (*chan).capture_transition_buf->increment_read_ptr(ti);
1398 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write) != to_write) {
1399 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1403 (*chan).capture_buf->increment_read_ptr (to_write);
1404 (*chan).curr_capture_cnt += to_write;
1406 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1408 /* we wrote all of vector.len[0] but it wasn't an entire
1409 disk_io_chunk_frames of data, so arrange for some part
1410 of vector.len[1] to be flushed to disk as well.
1413 to_write = min ((nframes_t)(disk_io_chunk_frames - to_write), (nframes_t) vector.len[1]);
1415 if ((*chan).write_source->write (vector.buf[1], to_write) != to_write) {
1416 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1420 _write_data_count += (*chan).write_source->write_data_count();
1422 (*chan).capture_buf->increment_read_ptr (to_write);
1423 (*chan).curr_capture_cnt += to_write;
1432 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1434 uint32_t buffer_position;
1435 bool more_work = true;
1437 boost::shared_ptr<AudioRegion> region;
1438 nframes_t total_capture;
1440 SourceList::iterator src;
1441 ChannelList::iterator chan;
1442 vector<CaptureInfo*>::iterator ci;
1444 bool mark_write_completed = false;
1446 finish_capture (true);
1448 /* butler is already stopped, but there may be work to do
1449 to flush remaining data to disk.
1452 while (more_work && !err) {
1453 switch (do_flush (Session::TransportContext, true)) {
1460 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1465 /* XXX is there anything we can do if err != 0 ? */
1466 Glib::Mutex::Lock lm (capture_info_lock);
1468 if (capture_info.empty()) {
1472 if (abort_capture) {
1474 if (destructive()) {
1478 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1480 if ((*chan).write_source) {
1482 (*chan).write_source->mark_for_remove ();
1483 (*chan).write_source->drop_references ();
1484 (*chan).write_source.reset ();
1487 /* new source set up in "out" below */
1493 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1494 total_capture += (*ci)->frames;
1497 /* figure out the name for this take */
1499 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1501 boost::shared_ptr<AudioFileSource> s = (*chan).write_source;
1505 s->update_header (capture_info.front()->start, when, twhen);
1506 s->set_captured_for (_name);
1507 s->mark_immutable ();
1511 /* destructive tracks have a single, never changing region */
1513 if (destructive()) {
1515 /* send a signal that any UI can pick up to do the right thing. there is
1516 a small problem here in that a UI may need the peak data to be ready
1517 for the data that was recorded and this isn't interlocked with that
1518 process. this problem is deferred to the UI.
1521 _playlist->Modified();
1525 string whole_file_region_name;
1526 whole_file_region_name = region_name_from_path (channels[0].write_source->name(), true);
1528 /* Register a new region with the Session that
1529 describes the entire source. Do this first
1530 so that any sub-regions will obviously be
1531 children of this one (later!)
1535 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1536 whole_file_region_name,
1537 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
1539 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1540 region->special_set_position (capture_info.front()->start);
1544 catch (failed_constructor& err) {
1545 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1549 _last_capture_regions.push_back (region);
1551 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1553 XMLNode &before = _playlist->get_state();
1554 _playlist->freeze ();
1556 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1560 _session.region_name (region_name, whole_file_region_name, false);
1562 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
1565 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1566 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1569 catch (failed_constructor& err) {
1570 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1571 continue; /* XXX is this OK? */
1574 region->GoingAway.connect (bind (mem_fun (*this, &Diskstream::remove_region_from_last_capture), boost::weak_ptr<Region>(region)));
1576 _last_capture_regions.push_back (region);
1578 i_am_the_modifier++;
1579 _playlist->add_region (region, (*ci)->start);
1580 i_am_the_modifier--;
1582 buffer_position += (*ci)->frames;
1586 XMLNode &after = _playlist->get_state();
1587 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1590 mark_write_completed = true;
1593 reset_write_sources (mark_write_completed);
1597 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1601 capture_info.clear ();
1602 capture_start_frame = 0;
1606 AudioDiskstream::finish_capture (bool rec_monitors_input)
1608 was_recording = false;
1610 if (capture_captured == 0) {
1614 if (recordable() && destructive()) {
1615 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1617 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1618 (*chan).capture_transition_buf->get_write_vector(&transvec);
1621 if (transvec.len[0] > 0) {
1622 transvec.buf[0]->type = CaptureEnd;
1623 transvec.buf[0]->capture_val = capture_captured;
1624 (*chan).capture_transition_buf->increment_write_ptr(1);
1628 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1634 CaptureInfo* ci = new CaptureInfo;
1636 ci->start = capture_start_frame;
1637 ci->frames = capture_captured;
1639 /* XXX theoretical race condition here. Need atomic exchange ?
1640 However, the circumstances when this is called right
1641 now (either on record-disable or transport_stopped)
1642 mean that no actual race exists. I think ...
1643 We now have a capture_info_lock, but it is only to be used
1644 to synchronize in the transport_stop and the capture info
1645 accessors, so that invalidation will not occur (both non-realtime).
1648 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1650 capture_info.push_back (ci);
1651 capture_captured = 0;
1655 AudioDiskstream::set_record_enabled (bool yn)
1657 if (!recordable() || !_session.record_enabling_legal() || _io->n_inputs() == 0) {
1661 /* can't rec-enable in destructive mode if transport is before start */
1663 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1667 if (yn && channels[0].source == 0) {
1669 /* pick up connections not initiated *from* the IO object
1670 we're associated with.
1673 get_input_sources ();
1676 /* yes, i know that this not proof against race conditions, but its
1677 good enough. i think.
1680 if (record_enabled() != yn) {
1682 engage_record_enable ();
1684 disengage_record_enable ();
1690 AudioDiskstream::engage_record_enable ()
1692 bool rolling = _session.transport_speed() != 0.0f;
1694 g_atomic_int_set (&_record_enabled, 1);
1695 capturing_sources.clear ();
1697 if (Config->get_monitoring_model() == HardwareMonitoring) {
1699 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1700 if ((*chan).source) {
1701 (*chan).source->ensure_monitor_input (!(Config->get_auto_input() && rolling));
1703 capturing_sources.push_back ((*chan).write_source);
1707 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1708 capturing_sources.push_back ((*chan).write_source);
1712 RecordEnableChanged (); /* EMIT SIGNAL */
1716 AudioDiskstream::disengage_record_enable ()
1718 g_atomic_int_set (&_record_enabled, 0);
1719 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1720 if (Config->get_monitoring_model() == HardwareMonitoring) {
1721 if ((*chan).source) {
1722 (*chan).source->ensure_monitor_input (false);
1726 capturing_sources.clear ();
1727 RecordEnableChanged (); /* EMIT SIGNAL */
1731 AudioDiskstream::get_state ()
1733 XMLNode* node = new XMLNode ("AudioDiskstream");
1735 LocaleGuard lg (X_("POSIX"));
1737 node->add_property ("flags", enum_2_string (_flags));
1739 snprintf (buf, sizeof(buf), "%zd", channels.size());
1740 node->add_property ("channels", buf);
1742 node->add_property ("playlist", _playlist->name());
1744 snprintf (buf, sizeof(buf), "%.12g", _visible_speed);
1745 node->add_property ("speed", buf);
1747 node->add_property("name", _name);
1748 id().print (buf, sizeof (buf));
1749 node->add_property("id", buf);
1751 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1753 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1754 XMLNode* cs_grandchild;
1756 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1757 cs_grandchild = new XMLNode (X_("file"));
1758 cs_grandchild->add_property (X_("path"), (*i)->path());
1759 cs_child->add_child_nocopy (*cs_grandchild);
1762 /* store the location where capture will start */
1766 if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1767 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1769 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1772 cs_child->add_property (X_("at"), buf);
1773 node->add_child_nocopy (*cs_child);
1777 node->add_child_copy (*_extra_xml);
1784 AudioDiskstream::set_state (const XMLNode& node)
1786 const XMLProperty* prop;
1787 XMLNodeList nlist = node.children();
1788 XMLNodeIterator niter;
1789 uint32_t nchans = 1;
1790 XMLNode* capture_pending_node = 0;
1791 LocaleGuard lg (X_("POSIX"));
1793 in_set_state = true;
1795 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1796 if ((*niter)->name() == IO::state_node_name) {
1797 deprecated_io_node = new XMLNode (**niter);
1800 if ((*niter)->name() == X_("CapturingSources")) {
1801 capture_pending_node = *niter;
1805 /* prevent write sources from being created */
1807 in_set_state = true;
1809 if ((prop = node.property ("name")) != 0) {
1810 _name = prop->value();
1813 if (deprecated_io_node) {
1814 if ((prop = deprecated_io_node->property ("id")) != 0) {
1815 _id = prop->value ();
1818 if ((prop = node.property ("id")) != 0) {
1819 _id = prop->value ();
1823 if ((prop = node.property ("flags")) != 0) {
1824 _flags = Flag (string_2_enum (prop->value(), _flags));
1827 if ((prop = node.property ("channels")) != 0) {
1828 nchans = atoi (prop->value().c_str());
1831 // create necessary extra channels
1832 // we are always constructed with one and we always need one
1834 if (nchans > _n_channels) {
1836 // we need to add new channel infos
1837 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1839 int diff = nchans - channels.size();
1841 for (int i=0; i < diff; ++i) {
1845 } else if (nchans < _n_channels) {
1847 // we need to get rid of channels
1848 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1850 int diff = channels.size() - nchans;
1852 for (int i = 0; i < diff; ++i) {
1857 if ((prop = node.property ("playlist")) == 0) {
1862 bool had_playlist = (_playlist != 0);
1864 if (find_and_use_playlist (prop->value())) {
1868 if (!had_playlist) {
1869 _playlist->set_orig_diskstream_id (_id);
1872 if (!destructive() && capture_pending_node) {
1873 /* destructive streams have one and only one source per channel,
1874 and so they never end up in pending capture in any useful
1877 use_pending_capture_data (*capture_pending_node);
1882 if ((prop = node.property ("speed")) != 0) {
1883 double sp = atof (prop->value().c_str());
1885 if (realtime_set_speed (sp, false)) {
1886 non_realtime_set_speed ();
1890 _n_channels = channels.size();
1892 in_set_state = false;
1894 /* make sure this is clear before we do anything else */
1896 capturing_sources.clear ();
1898 /* write sources are handled when we handle the input set
1899 up of the IO that owns this DS (::non_realtime_input_change())
1902 in_set_state = false;
1908 AudioDiskstream::use_new_write_source (uint32_t n)
1910 if (!recordable()) {
1914 if (n >= channels.size()) {
1915 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1919 ChannelInfo &chan = channels[n];
1921 if (chan.write_source) {
1922 chan.write_source->done_with_peakfile_writes ();
1923 chan.write_source->set_allow_remove_if_empty (true);
1924 chan.write_source.reset ();
1928 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
1929 throw failed_constructor();
1933 catch (failed_constructor &err) {
1934 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1935 chan.write_source.reset ();
1939 /* do not remove destructive files even if they are empty */
1941 chan.write_source->set_allow_remove_if_empty (!destructive());
1947 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1949 ChannelList::iterator chan;
1952 if (!recordable()) {
1956 capturing_sources.clear ();
1958 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
1959 if (!destructive()) {
1961 if ((*chan).write_source && mark_write_complete) {
1962 (*chan).write_source->mark_streaming_write_completed ();
1964 use_new_write_source (n);
1966 if (record_enabled()) {
1967 capturing_sources.push_back ((*chan).write_source);
1971 if ((*chan).write_source == 0) {
1972 use_new_write_source (n);
1977 if (destructive()) {
1979 /* we now have all our write sources set up, so create the
1980 playlist's single region.
1983 if (_playlist->empty()) {
1984 setup_destructive_playlist ();
1990 AudioDiskstream::rename_write_sources ()
1992 ChannelList::iterator chan;
1995 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
1996 if ((*chan).write_source != 0) {
1997 (*chan).write_source->set_name (_name, destructive());
1998 /* XXX what to do if one of them fails ? */
2006 AudioDiskstream::set_block_size (nframes_t nframes)
2008 if (_session.get_block_size() > speed_buffer_size) {
2009 speed_buffer_size = _session.get_block_size();
2011 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2012 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2013 (*chan).speed_buffer = new Sample[speed_buffer_size];
2016 allocate_temporary_buffers ();
2020 AudioDiskstream::allocate_temporary_buffers ()
2022 /* make sure the wrap buffer is at least large enough to deal
2023 with the speeds up to 1.2, to allow for micro-variation
2024 when slaving to MTC, SMPTE etc.
2027 double sp = max (fabsf (_actual_speed), 1.2f);
2028 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() * sp) + 1;
2030 if (required_wrap_size > wrap_buffer_size) {
2032 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2033 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2034 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2035 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2036 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2039 wrap_buffer_size = required_wrap_size;
2044 AudioDiskstream::monitor_input (bool yn)
2046 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2048 if ((*chan).source) {
2049 (*chan).source->ensure_monitor_input (yn);
2055 AudioDiskstream::set_align_style_from_io ()
2057 bool have_physical = false;
2063 get_input_sources ();
2065 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2066 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2067 have_physical = true;
2072 if (have_physical) {
2073 set_align_style (ExistingMaterial);
2075 set_align_style (CaptureTime);
2080 AudioDiskstream::add_channel ()
2082 /* XXX need to take lock??? */
2084 /* this copies the ChannelInfo, which currently has no buffers. kind
2085 of pointless really, but we want the channels list to contain
2086 actual objects, not pointers to objects. mostly for convenience,
2087 which isn't much in evidence.
2090 channels.push_back (ChannelInfo());
2092 /* now allocate the buffers */
2094 channels.back().init (_session.diskstream_buffer_size(),
2098 _n_channels = channels.size();
2104 AudioDiskstream::remove_channel ()
2106 if (channels.size() > 1) {
2107 /* XXX need to take lock??? */
2108 channels.back().release ();
2109 channels.pop_back();
2110 _n_channels = channels.size();
2118 AudioDiskstream::playback_buffer_load () const
2120 return (float) ((double) channels.front().playback_buf->read_space()/
2121 (double) channels.front().playback_buf->bufsize());
2125 AudioDiskstream::capture_buffer_load () const
2127 return (float) ((double) channels.front().capture_buf->write_space()/
2128 (double) channels.front().capture_buf->bufsize());
2132 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2134 const XMLProperty* prop;
2135 XMLNodeList nlist = node.children();
2136 XMLNodeIterator niter;
2137 boost::shared_ptr<AudioFileSource> fs;
2138 boost::shared_ptr<AudioFileSource> first_fs;
2139 SourceList pending_sources;
2142 if ((prop = node.property (X_("at"))) == 0) {
2146 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2150 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2151 if ((*niter)->name() == X_("file")) {
2153 if ((prop = (*niter)->property (X_("path"))) == 0) {
2158 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (_session, prop->value(), false, _session.frame_rate()));
2161 catch (failed_constructor& err) {
2162 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2163 _name, prop->value())
2168 pending_sources.push_back (fs);
2170 if (first_fs == 0) {
2174 fs->set_captured_for (_name);
2178 if (pending_sources.size() == 0) {
2179 /* nothing can be done */
2183 if (pending_sources.size() != _n_channels) {
2184 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2189 boost::shared_ptr<AudioRegion> region;
2192 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(),
2193 region_name_from_path (first_fs->name(), true),
2194 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
2195 region->special_set_position (0);
2198 catch (failed_constructor& err) {
2199 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2207 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name(), true)));
2210 catch (failed_constructor& err) {
2211 error << string_compose (_("%1: cannot create region from pending capture sources"),
2218 _playlist->add_region (region, position);
2224 AudioDiskstream::set_destructive (bool yn)
2226 bool bounce_ignored;
2228 if (yn != destructive()) {
2231 /* requestor should already have checked this and
2232 bounced if necessary and desired
2234 if (!can_become_destructive (bounce_ignored)) {
2237 _flags = Flag (_flags | Destructive);
2238 use_destructive_playlist ();
2240 _flags = Flag (_flags & ~Destructive);
2241 reset_write_sources (true, true);
2249 AudioDiskstream::can_become_destructive (bool& requires_bounce) const
2252 requires_bounce = false;
2256 /* is there only one region ? */
2258 if (_playlist->n_regions() != 1) {
2259 requires_bounce = true;
2263 boost::shared_ptr<Region> first = _playlist->find_next_region (_session.current_start_frame(), Start, 1);
2266 /* do the source(s) for the region cover the session start position ? */
2268 if (first->position() != _session.current_start_frame()) {
2269 if (first->start() > _session.current_start_frame()) {
2270 requires_bounce = true;
2275 /* is the source used by only 1 playlist ? */
2277 boost::shared_ptr<AudioRegion> afirst = boost::dynamic_pointer_cast<AudioRegion> (first);
2281 if (afirst->source()->used() > 1) {
2282 requires_bounce = true;
2286 requires_bounce = false;
2290 AudioDiskstream::ChannelInfo::ChannelInfo ()
2292 playback_wrap_buffer = 0;
2293 capture_wrap_buffer = 0;
2297 current_capture_buffer = 0;
2298 current_playback_buffer = 0;
2299 curr_capture_cnt = 0;
2302 capture_transition_buf = 0;
2306 AudioDiskstream::ChannelInfo::init (nframes_t bufsize, nframes_t speed_size, nframes_t wrap_size)
2308 speed_buffer = new Sample[speed_size];
2309 playback_wrap_buffer = new Sample[wrap_size];
2310 capture_wrap_buffer = new Sample[wrap_size];
2312 playback_buf = new RingBufferNPT<Sample> (bufsize);
2313 capture_buf = new RingBufferNPT<Sample> (bufsize);
2314 capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
2316 /* touch the ringbuffer buffers, which will cause
2317 them to be mapped into locked physical RAM if
2318 we're running with mlockall(). this doesn't do
2322 memset (playback_buf->buffer(), 0, sizeof (Sample) * playback_buf->bufsize());
2323 memset (capture_buf->buffer(), 0, sizeof (Sample) * capture_buf->bufsize());
2324 memset (capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * capture_transition_buf->bufsize());
2327 AudioDiskstream::ChannelInfo::~ChannelInfo ()
2332 AudioDiskstream::ChannelInfo::release ()
2335 write_source.reset ();
2339 delete [] speed_buffer;
2343 if (playback_wrap_buffer) {
2344 delete [] playback_wrap_buffer;
2345 playback_wrap_buffer = 0;
2348 if (capture_wrap_buffer) {
2349 delete [] capture_wrap_buffer;
2350 capture_wrap_buffer = 0;
2354 delete playback_buf;
2363 if (capture_transition_buf) {
2364 delete capture_transition_buf;
2365 capture_transition_buf = 0;