2 Copyright (C) 2000-2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <pbd/error.h>
34 #include <pbd/basename.h>
35 #include <glibmm/thread.h>
36 #include <pbd/xml++.h>
37 #include <pbd/memento_command.h>
39 #include <ardour/ardour.h>
40 #include <ardour/audioengine.h>
41 #include <ardour/audio_diskstream.h>
42 #include <ardour/utils.h>
43 #include <ardour/configuration.h>
44 #include <ardour/audiofilesource.h>
45 #include <ardour/destructive_filesource.h>
46 #include <ardour/send.h>
47 #include <ardour/region_factory.h>
48 #include <ardour/audioplaylist.h>
49 #include <ardour/cycle_timer.h>
50 #include <ardour/audioregion.h>
51 #include <ardour/source_factory.h>
57 using namespace ARDOUR;
60 size_t AudioDiskstream::_working_buffers_size = 0;
61 Sample* AudioDiskstream::_mixdown_buffer = 0;
62 gain_t* AudioDiskstream::_gain_buffer = 0;
64 AudioDiskstream::AudioDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
65 : Diskstream(sess, name, flag)
66 , deprecated_io_node(NULL)
68 /* prevent any write sources from being created */
78 AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
79 : Diskstream(sess, node)
80 , deprecated_io_node(NULL)
85 if (set_state (node)) {
87 throw failed_constructor();
93 use_destructive_playlist ();
98 AudioDiskstream::init_channel (ChannelInfo &chan)
100 chan.playback_wrap_buffer = 0;
101 chan.capture_wrap_buffer = 0;
102 chan.speed_buffer = 0;
103 chan.peak_power = 0.0f;
105 chan.current_capture_buffer = 0;
106 chan.current_playback_buffer = 0;
107 chan.curr_capture_cnt = 0;
109 chan.playback_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
110 chan.capture_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
111 chan.capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
114 /* touch the ringbuffer buffers, which will cause
115 them to be mapped into locked physical RAM if
116 we're running with mlockall(). this doesn't do
119 memset (chan.playback_buf->buffer(), 0, sizeof (Sample) * chan.playback_buf->bufsize());
120 memset (chan.capture_buf->buffer(), 0, sizeof (Sample) * chan.capture_buf->bufsize());
121 memset (chan.capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * chan.capture_transition_buf->bufsize());
126 AudioDiskstream::init (Diskstream::Flag f)
130 /* there are no channels at this point, so these
131 two calls just get speed_buffer_size and wrap_buffer
132 size setup without duplicating their code.
135 set_block_size (_session.get_block_size());
136 allocate_temporary_buffers ();
139 assert(_n_channels == 1);
143 AudioDiskstream::destroy_channel (ChannelInfo &chan)
145 if (chan.write_source) {
146 chan.write_source.reset ();
149 if (chan.speed_buffer) {
150 delete [] chan.speed_buffer;
153 if (chan.playback_wrap_buffer) {
154 delete [] chan.playback_wrap_buffer;
156 if (chan.capture_wrap_buffer) {
157 delete [] chan.capture_wrap_buffer;
160 delete chan.playback_buf;
161 delete chan.capture_buf;
162 delete chan.capture_transition_buf;
164 chan.playback_buf = 0;
165 chan.capture_buf = 0;
168 AudioDiskstream::~AudioDiskstream ()
170 Glib::Mutex::Lock lm (state_lock);
172 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan)
173 destroy_channel((*chan));
179 AudioDiskstream::allocate_working_buffers()
181 assert(disk_io_frames() > 0);
183 _working_buffers_size = disk_io_frames();
184 _mixdown_buffer = new Sample[_working_buffers_size];
185 _gain_buffer = new gain_t[_working_buffers_size];
189 AudioDiskstream::free_working_buffers()
191 delete [] _mixdown_buffer;
192 delete [] _gain_buffer;
193 _working_buffers_size = 0;
199 AudioDiskstream::non_realtime_input_change ()
202 Glib::Mutex::Lock lm (state_lock);
204 if (input_change_pending == NoChange) {
208 if (input_change_pending & ConfigurationChanged) {
210 if (_io->n_inputs() > _n_channels) {
212 // we need to add new channel infos
214 int diff = _io->n_inputs() - channels.size();
216 for (int i = 0; i < diff; ++i) {
220 } else if (_io->n_inputs() < _n_channels) {
222 // we need to get rid of channels
224 int diff = channels.size() - _io->n_inputs();
226 for (int i = 0; i < diff; ++i) {
232 get_input_sources ();
233 set_capture_offset ();
235 if (first_input_change) {
236 set_align_style (_persistent_alignment_style);
237 first_input_change = false;
239 set_align_style_from_io ();
242 input_change_pending = NoChange;
245 /* reset capture files */
247 reset_write_sources (false);
249 /* now refill channel buffers */
251 if (speed() != 1.0f || speed() != -1.0f) {
252 seek ((nframes_t) (_session.transport_frame() * (double) speed()));
254 seek (_session.transport_frame());
259 AudioDiskstream::get_input_sources ()
261 uint32_t ni = _io->n_inputs();
263 for (uint32_t n = 0; n < ni; ++n) {
265 const char **connections = _io->input(n)->get_connections ();
266 ChannelInfo& chan = channels[n];
268 if (connections == 0 || connections[0] == 0) {
271 // _source->disable_metering ();
277 chan.source = _session.engine().get_port_by_name (connections[0]);
287 AudioDiskstream::find_and_use_playlist (const string& name)
290 AudioPlaylist* playlist;
292 if ((pl = _session.playlist_by_name (name)) == 0) {
293 playlist = new AudioPlaylist(_session, name);
297 if ((playlist = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
298 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
302 return use_playlist (playlist);
306 AudioDiskstream::use_playlist (Playlist* playlist)
308 assert(dynamic_cast<AudioPlaylist*>(playlist));
310 Diskstream::use_playlist(playlist);
316 AudioDiskstream::use_new_playlist ()
319 AudioPlaylist* playlist;
321 if (!in_set_state && destructive()) {
326 newname = Playlist::bump_name (_playlist->name(), _session);
328 newname = Playlist::bump_name (_name, _session);
331 if ((playlist = new AudioPlaylist (_session, newname, hidden())) != 0) {
332 playlist->set_orig_diskstream_id (id());
333 return use_playlist (playlist);
340 AudioDiskstream::use_copy_playlist ()
342 assert(audio_playlist());
348 if (_playlist == 0) {
349 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
354 AudioPlaylist* playlist;
356 newname = Playlist::bump_name (_playlist->name(), _session);
358 if ((playlist = new AudioPlaylist (*audio_playlist(), newname)) != 0) {
359 playlist->set_orig_diskstream_id (id());
360 return use_playlist (playlist);
367 AudioDiskstream::setup_destructive_playlist ()
371 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
372 srcs.push_back ((*chan).write_source);
375 /* a single full-sized region */
377 boost::shared_ptr<Region> region (RegionFactory::create (srcs, 0, max_frames - srcs.front()->natural_position(), _name));
378 _playlist->add_region (region, srcs.front()->natural_position());
382 AudioDiskstream::use_destructive_playlist ()
384 /* this is called from the XML-based constructor or ::set_destructive. when called,
385 we already have a playlist and a region, but we need to
386 set up our sources for write. we use the sources associated
387 with the (presumed single, full-extent) region.
390 boost::shared_ptr<Region> rp = _playlist->find_next_region (_session.current_start_frame(), Start, 1);
393 reset_write_sources (false, true);
397 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (rp);
400 throw failed_constructor();
403 /* be sure to stretch the region out to the maximum length */
405 region->set_length (max_frames - region->position(), this);
408 ChannelList::iterator chan;
410 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
411 (*chan).write_source = boost::dynamic_pointer_cast<AudioFileSource>(region->source (n));
412 assert((*chan).write_source);
413 (*chan).write_source->set_allow_remove_if_empty (false);
415 /* this might be false if we switched modes, so force it */
417 (*chan).write_source->set_destructive (true);
420 /* the source list will never be reset for a destructive track */
424 AudioDiskstream::check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record)
426 int possibly_recording;
429 const int transport_rolling = 0x4;
430 const int track_rec_enabled = 0x2;
431 const int global_rec_enabled = 0x1;
433 /* merge together the 3 factors that affect record status, and compute
437 rolling = _session.transport_speed() != 0.0f;
438 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
439 change = possibly_recording ^ last_possibly_recording;
441 if (possibly_recording == last_possibly_recording) {
447 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
449 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
450 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
452 /* starting to record: compute first+last frames */
454 first_recordable_frame = transport_frame + _capture_offset;
455 last_recordable_frame = max_frames;
456 capture_start_frame = transport_frame;
458 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
460 /* was stopped, now rolling (and recording) */
462 if (_alignment_style == ExistingMaterial) {
463 first_recordable_frame += _session.worst_output_latency();
465 first_recordable_frame += _roll_delay;
470 /* was rolling, but record state changed */
472 if (_alignment_style == ExistingMaterial) {
474 if (!Config->get_punch_in()) {
476 /* manual punch in happens at the correct transport frame
477 because the user hit a button. but to get alignment correct
478 we have to back up the position of the new region to the
479 appropriate spot given the roll delay.
482 capture_start_frame -= _roll_delay;
484 /* XXX paul notes (august 2005): i don't know why
488 first_recordable_frame += _capture_offset;
492 /* autopunch toggles recording at the precise
493 transport frame, and then the DS waits
494 to start recording for a time that depends
495 on the output latency.
498 first_recordable_frame += _session.worst_output_latency();
503 if (Config->get_punch_in()) {
504 first_recordable_frame += _roll_delay;
506 capture_start_frame -= _roll_delay;
512 if (_flags & Recordable) {
513 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
515 RingBufferNPT<CaptureTransition>::rw_vector transvec;
516 (*chan).capture_transition_buf->get_write_vector(&transvec);
518 if (transvec.len[0] > 0) {
519 transvec.buf[0]->type = CaptureStart;
520 transvec.buf[0]->capture_val = capture_start_frame;
521 (*chan).capture_transition_buf->increment_write_ptr(1);
525 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
531 } else if (!record_enabled() || !can_record) {
535 last_recordable_frame = transport_frame + _capture_offset;
537 if (_alignment_style == ExistingMaterial) {
538 last_recordable_frame += _session.worst_output_latency();
540 last_recordable_frame += _roll_delay;
544 last_possibly_recording = possibly_recording;
548 AudioDiskstream::process (nframes_t transport_frame, nframes_t nframes, nframes_t offset, bool can_record, bool rec_monitors_input)
551 ChannelList::iterator c;
553 nframes_t rec_offset = 0;
554 nframes_t rec_nframes = 0;
555 bool nominally_recording;
556 bool re = record_enabled ();
557 bool collect_playback = false;
559 /* if we've already processed the frames corresponding to this call,
560 just return. this allows multiple routes that are taking input
561 from this diskstream to call our ::process() method, but have
562 this stuff only happen once. more commonly, it allows both
563 the AudioTrack that is using this AudioDiskstream *and* the Session
564 to call process() without problems.
571 check_record_status (transport_frame, nframes, can_record);
573 nominally_recording = (can_record && re);
580 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
581 must always be called as a pair. The only exception is if this function
582 returns a non-zero value, in which case, ::commit should not be called.
585 // If we can't take the state lock return.
586 if (!state_lock.trylock()) {
590 adjust_capture_position = 0;
592 for (c = channels.begin(); c != channels.end(); ++c) {
593 (*c).current_capture_buffer = 0;
594 (*c).current_playback_buffer = 0;
597 if (nominally_recording || (_session.get_record_enabled() && Config->get_punch_in())) {
600 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
607 case OverlapInternal:
608 /* ---------- recrange
611 rec_nframes = nframes;
616 /* |--------| recrange
619 rec_nframes = transport_frame + nframes - first_recordable_frame;
621 rec_offset = first_recordable_frame - transport_frame;
626 /* |--------| recrange
629 rec_nframes = last_recordable_frame - transport_frame;
633 case OverlapExternal:
634 /* |--------| recrange
635 -------------- transrange
637 rec_nframes = last_recordable_frame - last_recordable_frame;
638 rec_offset = first_recordable_frame - transport_frame;
642 if (rec_nframes && !was_recording) {
643 capture_captured = 0;
644 was_recording = true;
649 if (can_record && !_last_capture_regions.empty()) {
650 _last_capture_regions.clear ();
653 if (nominally_recording || rec_nframes) {
655 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
657 ChannelInfo& chan (*c);
659 chan.capture_buf->get_write_vector (&chan.capture_vector);
661 if (rec_nframes <= chan.capture_vector.len[0]) {
663 chan.current_capture_buffer = chan.capture_vector.buf[0];
665 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
669 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
673 nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
675 if (rec_nframes > total) {
680 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
681 nframes_t first = chan.capture_vector.len[0];
683 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
684 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
685 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
686 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
688 chan.current_capture_buffer = chan.capture_wrap_buffer;
695 finish_capture (rec_monitors_input);
702 /* data will be written to disk */
704 if (rec_nframes == nframes && rec_offset == 0) {
706 for (c = channels.begin(); c != channels.end(); ++c) {
707 (*c).current_playback_buffer = (*c).current_capture_buffer;
710 playback_distance = nframes;
715 /* we can't use the capture buffer as the playback buffer, because
716 we recorded only a part of the current process' cycle data
720 collect_playback = true;
723 adjust_capture_position = rec_nframes;
725 } else if (nominally_recording) {
727 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
729 for (c = channels.begin(); c != channels.end(); ++c) {
730 (*c).current_playback_buffer = (*c).current_capture_buffer;
733 playback_distance = nframes;
737 collect_playback = true;
740 if (collect_playback) {
742 /* we're doing playback */
744 nframes_t necessary_samples;
746 /* no varispeed playback if we're recording, because the output .... TBD */
748 if (rec_nframes == 0 && _actual_speed != 1.0f) {
749 necessary_samples = (nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
751 necessary_samples = nframes;
754 for (c = channels.begin(); c != channels.end(); ++c) {
755 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
760 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
762 ChannelInfo& chan (*c);
764 if (necessary_samples <= chan.playback_vector.len[0]) {
766 chan.current_playback_buffer = chan.playback_vector.buf[0];
769 nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
771 if (necessary_samples > total) {
777 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
778 chan.playback_vector.len[0] * sizeof (Sample));
779 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
780 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
782 chan.current_playback_buffer = chan.playback_wrap_buffer;
787 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
789 uint64_t phase = last_phase;
792 // Linearly interpolate into the alt buffer
793 // using 40.24 fixp maths (swh)
795 for (c = channels.begin(); c != channels.end(); ++c) {
798 ChannelInfo& chan (*c);
803 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
805 fr = (phase & 0xFFFFFF) / 16777216.0f;
806 chan.speed_buffer[outsample] =
807 chan.current_playback_buffer[i] * (1.0f - fr) +
808 chan.current_playback_buffer[i+1] * fr;
812 chan.current_playback_buffer = chan.speed_buffer;
815 playback_distance = i + 1;
816 last_phase = (phase & 0xFFFFFF);
819 playback_distance = nframes;
831 /* we're exiting with failure, so ::commit will not
832 be called. unlock the state lock.
842 AudioDiskstream::commit (nframes_t nframes)
844 bool need_butler = false;
846 if (_actual_speed < 0.0) {
847 playback_sample -= playback_distance;
849 playback_sample += playback_distance;
852 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
854 (*chan).playback_buf->increment_read_ptr (playback_distance);
856 if (adjust_capture_position) {
857 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
861 if (adjust_capture_position != 0) {
862 capture_captured += adjust_capture_position;
863 adjust_capture_position = 0;
867 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
869 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
870 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
881 AudioDiskstream::set_pending_overwrite (bool yn)
883 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
885 pending_overwrite = yn;
887 overwrite_frame = playback_sample;
888 overwrite_offset = channels.front().playback_buf->get_read_ptr();
892 AudioDiskstream::overwrite_existing_buffers ()
894 Sample* mixdown_buffer;
897 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
899 overwrite_queued = false;
901 /* assume all are the same size */
902 nframes_t size = channels[0].playback_buf->bufsize();
904 mixdown_buffer = new Sample[size];
905 gain_buffer = new float[size];
907 /* reduce size so that we can fill the buffer correctly. */
913 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
915 start = overwrite_frame;
916 nframes_t cnt = size;
918 /* to fill the buffer without resetting the playback sample, we need to
919 do it one or two chunks (normally two).
921 |----------------------------------------------------------------------|
925 |<- second chunk->||<----------------- first chunk ------------------>|
929 nframes_t to_read = size - overwrite_offset;
931 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, start, to_read, *chan, n, reversed)) {
932 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
933 _id, size, playback_sample) << endmsg;
941 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer,
942 start, cnt, *chan, n, reversed)) {
943 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
944 _id, size, playback_sample) << endmsg;
953 pending_overwrite = false;
954 delete [] gain_buffer;
955 delete [] mixdown_buffer;
960 AudioDiskstream::seek (nframes_t frame, bool complete_refill)
962 Glib::Mutex::Lock lm (state_lock);
965 ChannelList::iterator chan;
967 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
968 (*chan).playback_buf->reset ();
969 (*chan).capture_buf->reset ();
972 /* can't rec-enable in destructive mode if transport is before start */
974 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
975 disengage_record_enable ();
978 playback_sample = frame;
981 if (complete_refill) {
982 while ((ret = do_refill_with_alloc ()) > 0) ;
984 ret = do_refill_with_alloc ();
991 AudioDiskstream::can_internal_playback_seek (nframes_t distance)
993 ChannelList::iterator chan;
995 for (chan = channels.begin(); chan != channels.end(); ++chan) {
996 if ((*chan).playback_buf->read_space() < distance) {
1004 AudioDiskstream::internal_playback_seek (nframes_t distance)
1006 ChannelList::iterator chan;
1008 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1009 (*chan).playback_buf->increment_read_ptr (distance);
1012 first_recordable_frame += distance;
1013 playback_sample += distance;
1019 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, nframes_t& start, nframes_t cnt,
1020 ChannelInfo& channel_info, int channel, bool reversed)
1022 nframes_t this_read = 0;
1023 bool reloop = false;
1024 nframes_t loop_end = 0;
1025 nframes_t loop_start = 0;
1026 nframes_t loop_length = 0;
1027 nframes_t offset = 0;
1031 /* Make the use of a Location atomic for this read operation.
1033 Note: Locations don't get deleted, so all we care about
1034 when I say "atomic" is that we are always pointing to
1035 the same one and using a start/length values obtained
1039 if ((loc = loop_location) != 0) {
1040 loop_start = loc->start();
1041 loop_end = loc->end();
1042 loop_length = loop_end - loop_start;
1045 /* if we are looping, ensure that the first frame we read is at the correct
1046 position within the loop.
1049 if (loc && start >= loop_end) {
1050 //cerr << "start adjusted from " << start;
1051 start = loop_start + ((start - loop_start) % loop_length);
1052 //cerr << "to " << start << endl;
1054 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1059 /* take any loop into account. we can't read past the end of the loop. */
1061 if (loc && (loop_end - start < cnt)) {
1062 this_read = loop_end - start;
1063 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1070 if (this_read == 0) {
1074 this_read = min(cnt,this_read);
1076 if (audio_playlist()->read (buf+offset, mixdown_buffer, gain_buffer, start, this_read, channel) != this_read) {
1077 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1082 _read_data_count = _playlist->read_data_count();
1086 /* don't adjust start, since caller has already done that
1089 swap_by_ptr (buf, buf + this_read - 1);
1093 /* if we read to the end of the loop, go back to the beginning */
1103 offset += this_read;
1110 AudioDiskstream::do_refill_with_alloc()
1112 Sample* mix_buf = new Sample[disk_io_chunk_frames];
1113 float* gain_buf = new float[disk_io_chunk_frames];
1115 int ret = _do_refill(mix_buf, gain_buf);
1124 AudioDiskstream::_do_refill (Sample* mixdown_buffer, float* gain_buffer)
1128 RingBufferNPT<Sample>::rw_vector vector;
1129 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1130 nframes_t total_space;
1131 nframes_t zero_fill;
1133 ChannelList::iterator i;
1136 assert(mixdown_buffer);
1137 assert(gain_buffer);
1139 channels.front().playback_buf->get_write_vector (&vector);
1141 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1145 /* if there are 2+ chunks of disk i/o possible for
1146 this track, let the caller know so that it can arrange
1147 for us to be called again, ASAP.
1150 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1154 /* if we're running close to normal speed and there isn't enough
1155 space to do disk_io_chunk_frames of I/O, then don't bother.
1157 at higher speeds, just do it because the sync between butler
1158 and audio thread may not be good enough.
1161 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1165 /* when slaved, don't try to get too close to the read pointer. this
1166 leaves space for the buffer reversal to have something useful to
1170 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1174 total_space = min (disk_io_chunk_frames, total_space);
1178 if (file_frame == 0) {
1180 /* at start: 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 < total_space) {
1197 /* too close to the start: read what we can,
1198 and then zero fill the rest
1201 zero_fill = total_space - file_frame;
1202 total_space = file_frame;
1207 /* move read position backwards because we are going
1208 to reverse the data.
1211 file_frame -= total_space;
1217 if (file_frame == max_frames) {
1219 /* at end: nothing to do but fill with silence */
1221 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1223 ChannelInfo& chan (*i);
1224 chan.playback_buf->get_write_vector (&vector);
1225 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1226 if (vector.len[1]) {
1227 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1229 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1234 if (file_frame > max_frames - total_space) {
1236 /* to close to the end: read what we can, and zero fill the rest */
1238 zero_fill = total_space - (max_frames - file_frame);
1239 total_space = max_frames - file_frame;
1246 nframes_t file_frame_tmp = 0;
1248 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1250 ChannelInfo& chan (*i);
1253 nframes_t len1, len2;
1255 chan.playback_buf->get_write_vector (&vector);
1258 file_frame_tmp = file_frame;
1261 buf1 = vector.buf[1];
1262 len1 = vector.len[1];
1263 buf2 = vector.buf[0];
1264 len2 = vector.len[0];
1266 buf1 = vector.buf[0];
1267 len1 = vector.len[0];
1268 buf2 = vector.buf[1];
1269 len2 = vector.len[1];
1273 to_read = min (ts, len1);
1274 to_read = min (to_read, disk_io_chunk_frames);
1278 if (read (buf1, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1283 chan.playback_buf->increment_write_ptr (to_read);
1287 to_read = min (ts, len2);
1292 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1293 so read some or all of vector.len[1] as well.
1296 if (read (buf2, mixdown_buffer, gain_buffer, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1301 chan.playback_buf->increment_write_ptr (to_read);
1310 file_frame = file_frame_tmp;
1317 /** Flush pending data to disk.
1319 * Important note: this function will write *AT MOST* disk_io_chunk_frames
1320 * of data to disk. it will never write more than that. If it writes that
1321 * much and there is more than that waiting to be written, it will return 1,
1322 * otherwise 0 on success or -1 on failure.
1324 * If there is less than disk_io_chunk_frames to be written, no data will be
1325 * written at all unless @a force_flush is true.
1328 AudioDiskstream::do_flush (Session::RunContext context, bool force_flush)
1332 RingBufferNPT<Sample>::rw_vector vector;
1333 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1336 _write_data_count = 0;
1338 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1340 (*chan).capture_buf->get_read_vector (&vector);
1342 total = vector.len[0] + vector.len[1];
1345 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1349 /* if there are 2+ chunks of disk i/o possible for
1350 this track, let the caller know so that it can arrange
1351 for us to be called again, ASAP.
1353 if we are forcing a flush, then if there is* any* extra
1354 work, let the caller know.
1356 if we are no longer recording and there is any extra work,
1357 let the caller know too.
1360 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1364 to_write = min (disk_io_chunk_frames, (nframes_t) vector.len[0]);
1366 // check the transition buffer when recording destructive
1367 // important that we get this after the capture buf
1369 if (destructive()) {
1370 (*chan).capture_transition_buf->get_read_vector(&transvec);
1371 size_t transcount = transvec.len[0] + transvec.len[1];
1372 bool have_start = false;
1375 for (ti=0; ti < transcount; ++ti) {
1376 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1378 if (captrans.type == CaptureStart) {
1379 // by definition, the first data we got above represents the given capture pos
1381 (*chan).write_source->mark_capture_start (captrans.capture_val);
1382 (*chan).curr_capture_cnt = 0;
1386 else if (captrans.type == CaptureEnd) {
1388 // capture end, the capture_val represents total frames in capture
1390 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1392 // shorten to make the write a perfect fit
1393 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1395 if (nto_write < to_write) {
1396 ret = 1; // should we?
1398 to_write = nto_write;
1400 (*chan).write_source->mark_capture_end ();
1402 // increment past this transition, but go no further
1407 // actually ends just beyond this chunk, so force more work
1415 (*chan).capture_transition_buf->increment_read_ptr(ti);
1419 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write) != to_write) {
1420 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1424 (*chan).capture_buf->increment_read_ptr (to_write);
1425 (*chan).curr_capture_cnt += to_write;
1427 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1429 /* we wrote all of vector.len[0] but it wasn't an entire
1430 disk_io_chunk_frames of data, so arrange for some part
1431 of vector.len[1] to be flushed to disk as well.
1434 to_write = min ((nframes_t)(disk_io_chunk_frames - to_write), (nframes_t) vector.len[1]);
1436 if ((*chan).write_source->write (vector.buf[1], to_write) != to_write) {
1437 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1441 _write_data_count += (*chan).write_source->write_data_count();
1443 (*chan).capture_buf->increment_read_ptr (to_write);
1444 (*chan).curr_capture_cnt += to_write;
1453 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1455 uint32_t buffer_position;
1456 bool more_work = true;
1458 boost::shared_ptr<AudioRegion> region;
1459 nframes_t total_capture;
1461 SourceList::iterator src;
1462 ChannelList::iterator chan;
1463 vector<CaptureInfo*>::iterator ci;
1465 bool mark_write_completed = false;
1467 finish_capture (true);
1469 /* butler is already stopped, but there may be work to do
1470 to flush remaining data to disk.
1473 while (more_work && !err) {
1474 switch (do_flush (Session::TransportContext, true)) {
1481 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1486 /* XXX is there anything we can do if err != 0 ? */
1487 Glib::Mutex::Lock lm (capture_info_lock);
1489 if (capture_info.empty()) {
1493 if (abort_capture) {
1495 if (destructive()) {
1499 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1501 if ((*chan).write_source) {
1503 (*chan).write_source->mark_for_remove ();
1504 (*chan).write_source->drop_references ();
1505 (*chan).write_source.reset ();
1508 /* new source set up in "out" below */
1514 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1515 total_capture += (*ci)->frames;
1518 /* figure out the name for this take */
1520 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1522 boost::shared_ptr<AudioFileSource> s = (*chan).write_source;
1526 s->update_header (capture_info.front()->start, when, twhen);
1527 s->set_captured_for (_name);
1532 /* destructive tracks have a single, never changing region */
1534 if (destructive()) {
1536 /* send a signal that any UI can pick up to do the right thing. there is
1537 a small problem here in that a UI may need the peak data to be ready
1538 for the data that was recorded and this isn't interlocked with that
1539 process. this problem is deferred to the UI.
1542 _playlist->Modified();
1546 string whole_file_region_name;
1547 whole_file_region_name = region_name_from_path (channels[0].write_source->name());
1549 /* Register a new region with the Session that
1550 describes the entire source. Do this first
1551 so that any sub-regions will obviously be
1552 children of this one (later!)
1556 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1557 whole_file_region_name,
1558 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
1560 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1561 region->special_set_position (capture_info.front()->start);
1565 catch (failed_constructor& err) {
1566 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1570 _last_capture_regions.push_back (region);
1572 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1574 XMLNode &before = _playlist->get_state();
1575 _playlist->freeze ();
1577 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1581 _session.region_name (region_name, whole_file_region_name, false);
1583 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
1586 boost::shared_ptr<Region> rx (RegionFactory::create (srcs, buffer_position, (*ci)->frames, region_name));
1587 region = boost::dynamic_pointer_cast<AudioRegion> (rx);
1590 catch (failed_constructor& err) {
1591 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1592 continue; /* XXX is this OK? */
1595 region->GoingAway.connect (bind (mem_fun (*this, &Diskstream::remove_region_from_last_capture), boost::weak_ptr<Region>(region)));
1597 _last_capture_regions.push_back (region);
1599 i_am_the_modifier++;
1600 _playlist->add_region (region, (*ci)->start);
1601 i_am_the_modifier--;
1603 buffer_position += (*ci)->frames;
1607 XMLNode &after = _playlist->get_state();
1608 _session.add_command (new MementoCommand<Playlist>(*_playlist, &before, &after));
1611 mark_write_completed = true;
1614 reset_write_sources (mark_write_completed);
1618 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1622 capture_info.clear ();
1623 capture_start_frame = 0;
1627 AudioDiskstream::finish_capture (bool rec_monitors_input)
1629 was_recording = false;
1631 if (capture_captured == 0) {
1635 if (recordable() && destructive()) {
1636 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1638 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1639 (*chan).capture_transition_buf->get_write_vector(&transvec);
1642 if (transvec.len[0] > 0) {
1643 transvec.buf[0]->type = CaptureEnd;
1644 transvec.buf[0]->capture_val = capture_captured;
1645 (*chan).capture_transition_buf->increment_write_ptr(1);
1649 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1655 CaptureInfo* ci = new CaptureInfo;
1657 ci->start = capture_start_frame;
1658 ci->frames = capture_captured;
1660 /* XXX theoretical race condition here. Need atomic exchange ?
1661 However, the circumstances when this is called right
1662 now (either on record-disable or transport_stopped)
1663 mean that no actual race exists. I think ...
1664 We now have a capture_info_lock, but it is only to be used
1665 to synchronize in the transport_stop and the capture info
1666 accessors, so that invalidation will not occur (both non-realtime).
1669 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1671 capture_info.push_back (ci);
1672 capture_captured = 0;
1676 AudioDiskstream::set_record_enabled (bool yn)
1678 if (!recordable() || !_session.record_enabling_legal()) {
1682 /* can't rec-enable in destructive mode if transport is before start */
1684 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1688 if (yn && channels[0].source == 0) {
1690 /* pick up connections not initiated *from* the IO object
1691 we're associated with.
1694 get_input_sources ();
1697 /* yes, i know that this not proof against race conditions, but its
1698 good enough. i think.
1701 if (record_enabled() != yn) {
1703 engage_record_enable ();
1705 disengage_record_enable ();
1711 AudioDiskstream::engage_record_enable ()
1713 bool rolling = _session.transport_speed() != 0.0f;
1715 g_atomic_int_set (&_record_enabled, 1);
1716 capturing_sources.clear ();
1717 if (Config->get_monitoring_model() == HardwareMonitoring) {
1718 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1719 if ((*chan).source) {
1720 (*chan).source->ensure_monitor_input (!(Config->get_auto_input() && rolling));
1722 capturing_sources.push_back ((*chan).write_source);
1725 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1726 capturing_sources.push_back ((*chan).write_source);
1730 RecordEnableChanged (); /* EMIT SIGNAL */
1734 AudioDiskstream::disengage_record_enable ()
1736 g_atomic_int_set (&_record_enabled, 0);
1737 if (Config->get_monitoring_model() == HardwareMonitoring) {
1738 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1739 if ((*chan).source) {
1740 (*chan).source->ensure_monitor_input (false);
1744 capturing_sources.clear ();
1745 RecordEnableChanged (); /* EMIT SIGNAL */
1750 AudioDiskstream::get_state ()
1752 XMLNode* node = new XMLNode ("AudioDiskstream");
1754 LocaleGuard lg (X_("POSIX"));
1756 snprintf (buf, sizeof(buf), "0x%x", _flags);
1757 node->add_property ("flags", buf);
1759 snprintf (buf, sizeof(buf), "%zd", channels.size());
1760 node->add_property ("channels", buf);
1762 node->add_property ("playlist", _playlist->name());
1764 snprintf (buf, sizeof(buf), "%.12g", _visible_speed);
1765 node->add_property ("speed", buf);
1767 node->add_property("name", _name);
1768 id().print (buf, sizeof (buf));
1769 node->add_property("id", buf);
1771 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1773 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1774 XMLNode* cs_grandchild;
1776 for (vector<boost::shared_ptr<AudioFileSource> >::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1777 cs_grandchild = new XMLNode (X_("file"));
1778 cs_grandchild->add_property (X_("path"), (*i)->path());
1779 cs_child->add_child_nocopy (*cs_grandchild);
1782 /* store the location where capture will start */
1786 if (Config->get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1787 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1789 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1792 cs_child->add_property (X_("at"), buf);
1793 node->add_child_nocopy (*cs_child);
1797 node->add_child_copy (*_extra_xml);
1804 AudioDiskstream::set_state (const XMLNode& node)
1806 const XMLProperty* prop;
1807 XMLNodeList nlist = node.children();
1808 XMLNodeIterator niter;
1809 uint32_t nchans = 1;
1810 XMLNode* capture_pending_node = 0;
1811 LocaleGuard lg (X_("POSIX"));
1813 in_set_state = true;
1815 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1816 if ((*niter)->name() == IO::state_node_name) {
1817 deprecated_io_node = new XMLNode (**niter);
1820 if ((*niter)->name() == X_("CapturingSources")) {
1821 capture_pending_node = *niter;
1825 /* prevent write sources from being created */
1827 in_set_state = true;
1829 if ((prop = node.property ("name")) != 0) {
1830 _name = prop->value();
1833 if (deprecated_io_node) {
1834 if ((prop = deprecated_io_node->property ("id")) != 0) {
1835 _id = prop->value ();
1838 if ((prop = node.property ("id")) != 0) {
1839 _id = prop->value ();
1843 if ((prop = node.property ("flags")) != 0) {
1844 _flags = strtol (prop->value().c_str(), 0, 0);
1847 if ((prop = node.property ("channels")) != 0) {
1848 nchans = atoi (prop->value().c_str());
1851 // create necessary extra channels
1852 // we are always constructed with one and we always need one
1854 if (nchans > _n_channels) {
1856 // we need to add new channel infos
1857 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1859 int diff = nchans - channels.size();
1861 for (int i=0; i < diff; ++i) {
1865 } else if (nchans < _n_channels) {
1867 // we need to get rid of channels
1868 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1870 int diff = channels.size() - nchans;
1872 for (int i = 0; i < diff; ++i) {
1877 if ((prop = node.property ("playlist")) == 0) {
1882 bool had_playlist = (_playlist != 0);
1884 if (find_and_use_playlist (prop->value())) {
1888 if (!had_playlist) {
1889 _playlist->set_orig_diskstream_id (_id);
1892 if (!destructive() && capture_pending_node) {
1893 /* destructive streams have one and only one source per channel,
1894 and so they never end up in pending capture in any useful
1897 use_pending_capture_data (*capture_pending_node);
1902 if ((prop = node.property ("speed")) != 0) {
1903 double sp = atof (prop->value().c_str());
1905 if (realtime_set_speed (sp, false)) {
1906 non_realtime_set_speed ();
1910 _n_channels = channels.size();
1912 in_set_state = false;
1914 /* make sure this is clear before we do anything else */
1916 capturing_sources.clear ();
1918 /* write sources are handled when we handle the input set
1919 up of the IO that owns this DS (::non_realtime_input_change())
1922 in_set_state = false;
1928 AudioDiskstream::use_new_write_source (uint32_t n)
1930 if (!recordable()) {
1934 if (n >= channels.size()) {
1935 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
1939 ChannelInfo &chan = channels[n];
1941 if (chan.write_source) {
1942 chan.write_source->set_allow_remove_if_empty (true);
1943 chan.write_source.reset ();
1947 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
1948 throw failed_constructor();
1952 catch (failed_constructor &err) {
1953 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
1954 chan.write_source.reset ();
1958 /* do not remove destructive files even if they are empty */
1960 chan.write_source->set_allow_remove_if_empty (!destructive());
1966 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
1968 ChannelList::iterator chan;
1971 if (!recordable()) {
1975 capturing_sources.clear ();
1977 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
1978 if (!destructive()) {
1980 if ((*chan).write_source && mark_write_complete) {
1981 (*chan).write_source->mark_streaming_write_completed ();
1983 use_new_write_source (n);
1985 if (record_enabled()) {
1986 capturing_sources.push_back ((*chan).write_source);
1990 if ((*chan).write_source == 0) {
1991 use_new_write_source (n);
1996 if (destructive()) {
1998 /* we now have all our write sources set up, so create the
1999 playlist's single region.
2002 if (_playlist->empty()) {
2003 setup_destructive_playlist ();
2009 AudioDiskstream::rename_write_sources ()
2011 ChannelList::iterator chan;
2014 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2015 if ((*chan).write_source != 0) {
2016 (*chan).write_source->set_name (_name, destructive());
2017 /* XXX what to do if one of them fails ? */
2025 AudioDiskstream::set_block_size (nframes_t nframes)
2027 if (_session.get_block_size() > speed_buffer_size) {
2028 speed_buffer_size = _session.get_block_size();
2030 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2031 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2032 (*chan).speed_buffer = new Sample[speed_buffer_size];
2035 allocate_temporary_buffers ();
2039 AudioDiskstream::allocate_temporary_buffers ()
2041 /* make sure the wrap buffer is at least large enough to deal
2042 with the speeds up to 1.2, to allow for micro-variation
2043 when slaving to MTC, SMPTE etc.
2046 double sp = max (fabsf (_actual_speed), 1.2f);
2047 nframes_t required_wrap_size = (nframes_t) floor (_session.get_block_size() * sp) + 1;
2049 if (required_wrap_size > wrap_buffer_size) {
2051 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2052 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2053 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2054 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2055 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2058 wrap_buffer_size = required_wrap_size;
2063 AudioDiskstream::monitor_input (bool yn)
2065 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2067 if ((*chan).source) {
2068 (*chan).source->ensure_monitor_input (yn);
2074 AudioDiskstream::set_align_style_from_io ()
2076 bool have_physical = false;
2082 get_input_sources ();
2084 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2085 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2086 have_physical = true;
2091 if (have_physical) {
2092 set_align_style (ExistingMaterial);
2094 set_align_style (CaptureTime);
2099 AudioDiskstream::add_channel ()
2101 /* XXX need to take lock??? */
2105 init_channel (chan);
2107 chan.speed_buffer = new Sample[speed_buffer_size];
2108 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2109 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2111 channels.push_back (chan);
2113 _n_channels = channels.size();
2119 AudioDiskstream::remove_channel ()
2121 if (channels.size() > 1) {
2122 /* XXX need to take lock??? */
2123 ChannelInfo & chan = channels.back();
2124 destroy_channel (chan);
2125 channels.pop_back();
2127 _n_channels = channels.size();
2135 AudioDiskstream::playback_buffer_load () const
2137 return (float) ((double) channels.front().playback_buf->read_space()/
2138 (double) channels.front().playback_buf->bufsize());
2142 AudioDiskstream::capture_buffer_load () const
2144 return (float) ((double) channels.front().capture_buf->write_space()/
2145 (double) channels.front().capture_buf->bufsize());
2149 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2151 const XMLProperty* prop;
2152 XMLNodeList nlist = node.children();
2153 XMLNodeIterator niter;
2154 boost::shared_ptr<AudioFileSource> fs;
2155 boost::shared_ptr<AudioFileSource> first_fs;
2156 SourceList pending_sources;
2159 if ((prop = node.property (X_("at"))) == 0) {
2163 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2167 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2168 if ((*niter)->name() == X_("file")) {
2170 if ((prop = (*niter)->property (X_("path"))) == 0) {
2175 fs = boost::dynamic_pointer_cast<AudioFileSource> (SourceFactory::createWritable (_session, prop->value(), false, _session.frame_rate()));
2178 catch (failed_constructor& err) {
2179 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2180 _name, prop->value())
2185 pending_sources.push_back (fs);
2187 if (first_fs == 0) {
2191 fs->set_captured_for (_name);
2195 if (pending_sources.size() == 0) {
2196 /* nothing can be done */
2200 if (pending_sources.size() != _n_channels) {
2201 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2206 boost::shared_ptr<AudioRegion> region;
2209 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(),
2210 region_name_from_path (first_fs->name()),
2211 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile)));
2212 region->special_set_position (0);
2215 catch (failed_constructor& err) {
2216 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2224 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name())));
2227 catch (failed_constructor& err) {
2228 error << string_compose (_("%1: cannot create region from pending capture sources"),
2235 _playlist->add_region (region, position);
2241 AudioDiskstream::set_destructive (bool yn)
2243 bool bounce_ignored;
2245 if (yn != destructive()) {
2248 /* requestor should already have checked this and
2249 bounced if necessary and desired
2251 if (!can_become_destructive (bounce_ignored)) {
2254 _flags |= Destructive;
2255 use_destructive_playlist ();
2257 _flags &= ~Destructive;
2258 reset_write_sources (true, true);
2266 AudioDiskstream::can_become_destructive (bool& requires_bounce) const
2269 requires_bounce = false;
2273 /* is there only one region ? */
2275 if (_playlist->n_regions() != 1) {
2276 requires_bounce = true;
2280 boost::shared_ptr<Region> first = _playlist->find_next_region (_session.current_start_frame(), Start, 1);
2283 /* do the source(s) for the region cover the session start position ? */
2285 if (first->position() != _session.current_start_frame()) {
2286 if (first->start() > _session.current_start_frame()) {
2287 requires_bounce = true;
2292 /* is the source used by only 1 playlist ? */
2294 boost::shared_ptr<AudioRegion> afirst = boost::dynamic_pointer_cast<AudioRegion> (first);
2298 if (afirst->source()->used() > 1) {
2299 requires_bounce = true;
2303 requires_bounce = false;