2 Copyright (C) 2000-2003 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.
18 $Id: diskstream.cc 567 2006-06-07 14:54:12Z trutkin $
34 #include <pbd/error.h>
35 #include <pbd/basename.h>
36 #include <glibmm/thread.h>
37 #include <pbd/xml++.h>
39 #include <ardour/ardour.h>
40 #include <ardour/audioengine.h>
41 #include <ardour/audio_diskstream.h>
42 #include <ardour/utils.h>
43 #include <ardour/configuration.h>
44 #include <ardour/audiofilesource.h>
45 #include <ardour/destructive_filesource.h>
46 #include <ardour/send.h>
47 #include <ardour/audioplaylist.h>
48 #include <ardour/cycle_timer.h>
49 #include <ardour/audioregion.h>
55 using namespace ARDOUR;
58 sigc::signal<void,list<AudioFileSource*>*> AudioDiskstream::DeleteSources;
60 AudioDiskstream::AudioDiskstream (Session &sess, const string &name, Diskstream::Flag flag)
61 : Diskstream(sess, name, flag)
64 /* prevent any write sources from being created */
73 DiskstreamCreated (this); /* EMIT SIGNAL */
76 AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
77 : Diskstream(sess, node)
83 if (set_state (node)) {
85 throw failed_constructor();
91 use_destructive_playlist ();
94 DiskstreamCreated (this); /* EMIT SIGNAL */
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;
104 chan.write_source = 0;
106 chan.current_capture_buffer = 0;
107 chan.current_playback_buffer = 0;
108 chan.curr_capture_cnt = 0;
110 chan.playback_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
111 chan.capture_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
112 chan.capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
115 /* touch the ringbuffer buffers, which will cause
116 them to be mapped into locked physical RAM if
117 we're running with mlockall(). this doesn't do
120 memset (chan.playback_buf->buffer(), 0, sizeof (Sample) * chan.playback_buf->bufsize());
121 memset (chan.capture_buf->buffer(), 0, sizeof (Sample) * chan.capture_buf->bufsize());
122 memset (chan.capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * chan.capture_transition_buf->bufsize());
127 AudioDiskstream::init (Diskstream::Flag f)
131 /* there are no channels at this point, so these
132 two calls just get speed_buffer_size and wrap_buffer
133 size setup without duplicating their code.
136 set_block_size (_session.get_block_size());
137 allocate_temporary_buffers ();
139 pending_overwrite = false;
141 overwrite_queued = false;
142 input_change_pending = NoChange;
149 AudioDiskstream::destroy_channel (ChannelInfo &chan)
151 if (chan.write_source) {
152 chan.write_source->release ();
153 chan.write_source = 0;
156 if (chan.speed_buffer) {
157 delete [] chan.speed_buffer;
160 if (chan.playback_wrap_buffer) {
161 delete [] chan.playback_wrap_buffer;
163 if (chan.capture_wrap_buffer) {
164 delete [] chan.capture_wrap_buffer;
167 delete chan.playback_buf;
168 delete chan.capture_buf;
169 delete chan.capture_transition_buf;
171 chan.playback_buf = 0;
172 chan.capture_buf = 0;
175 AudioDiskstream::~AudioDiskstream ()
177 Glib::Mutex::Lock lm (state_lock);
182 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
183 destroy_channel((*chan));
190 AudioDiskstream::handle_input_change (IOChange change, void *src)
192 Glib::Mutex::Lock lm (state_lock);
194 if (!(input_change_pending & change)) {
195 input_change_pending = IOChange (input_change_pending|change);
196 _session.request_input_change_handling ();
201 AudioDiskstream::non_realtime_input_change ()
204 Glib::Mutex::Lock lm (state_lock);
206 if (input_change_pending == NoChange) {
210 if (input_change_pending & ConfigurationChanged) {
212 if (_io->n_inputs() > _n_channels) {
214 // we need to add new channel infos
216 int diff = _io->n_inputs() - channels.size();
218 for (int i = 0; i < diff; ++i) {
222 } else if (_io->n_inputs() < _n_channels) {
224 // we need to get rid of channels
226 int diff = channels.size() - _io->n_inputs();
228 for (int i = 0; i < diff; ++i) {
234 get_input_sources ();
235 set_capture_offset ();
237 if (first_input_change) {
238 set_align_style (_persistent_alignment_style);
239 first_input_change = false;
241 set_align_style_from_io ();
244 input_change_pending = NoChange;
247 /* reset capture files */
249 reset_write_sources (false);
251 /* now refill channel buffers */
253 if (speed() != 1.0f || speed() != -1.0f) {
254 seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()));
257 seek (_session.transport_frame());
262 AudioDiskstream::get_input_sources ()
264 uint32_t ni = _io->n_inputs();
266 for (uint32_t n = 0; n < ni; ++n) {
268 const char **connections = _io->input(n)->get_connections ();
269 ChannelInfo& chan = channels[n];
271 if (connections == 0 || connections[0] == 0) {
274 // _source->disable_metering ();
280 chan.source = _session.engine().get_port_by_name (connections[0]);
290 AudioDiskstream::find_and_use_playlist (const string& name)
293 AudioPlaylist* playlist;
295 if ((pl = _session.playlist_by_name (name)) == 0) {
296 playlist = new AudioPlaylist(_session, name);
300 if ((playlist = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
301 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
305 return use_playlist (playlist);
309 AudioDiskstream::use_playlist (Playlist* playlist)
311 assert(dynamic_cast<AudioPlaylist*>(playlist));
314 Glib::Mutex::Lock lm (state_lock);
316 if (playlist == _playlist) {
320 plstate_connection.disconnect();
321 plmod_connection.disconnect ();
322 plgone_connection.disconnect ();
328 _playlist = dynamic_cast<AudioPlaylist*>(playlist);
331 if (!in_set_state && recordable()) {
332 reset_write_sources (false);
335 plstate_connection = _playlist->StateChanged.connect (mem_fun (*this, &AudioDiskstream::playlist_changed));
336 plmod_connection = _playlist->Modified.connect (mem_fun (*this, &AudioDiskstream::playlist_modified));
337 plgone_connection = _playlist->GoingAway.connect (mem_fun (*this, &AudioDiskstream::playlist_deleted));
340 if (!overwrite_queued) {
341 _session.request_overwrite_buffer (this);
342 overwrite_queued = true;
345 PlaylistChanged (); /* EMIT SIGNAL */
346 _session.set_dirty ();
352 AudioDiskstream::use_new_playlist ()
355 AudioPlaylist* playlist;
357 if (!in_set_state && destructive()) {
362 newname = Playlist::bump_name (_playlist->name(), _session);
364 newname = Playlist::bump_name (_name, _session);
367 if ((playlist = new AudioPlaylist (_session, newname, hidden())) != 0) {
368 playlist->set_orig_diskstream_id (id());
369 return use_playlist (playlist);
376 AudioDiskstream::use_copy_playlist ()
382 if (_playlist == 0) {
383 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
388 AudioPlaylist* playlist;
390 newname = Playlist::bump_name (_playlist->name(), _session);
392 if ((playlist = new AudioPlaylist (*_playlist, newname)) != 0) {
393 playlist->set_orig_diskstream_id (id());
394 return use_playlist (playlist);
402 AudioDiskstream::playlist_deleted (Playlist* pl)
404 /* this catches an ordering issue with session destruction. playlists
405 are destroyed before diskstreams. we have to invalidate any handles
406 we have to the playlist.
414 AudioDiskstream::setup_destructive_playlist ()
416 AudioRegion::SourceList srcs;
418 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
419 srcs.push_back ((*chan).write_source);
422 /* a single full-sized region */
424 cerr << "setup DS using " << srcs.front()->natural_position () << endl;
426 AudioRegion* region = new AudioRegion (srcs, 0, max_frames, _name);
427 _playlist->add_region (*region, srcs.front()->natural_position());
431 AudioDiskstream::use_destructive_playlist ()
433 /* use the sources associated with the single full-extent region */
435 Playlist::RegionList* rl = _playlist->regions_at (0);
438 reset_write_sources (false, true);
442 AudioRegion* region = dynamic_cast<AudioRegion*> (rl->front());
445 throw failed_constructor();
451 ChannelList::iterator chan;
453 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
454 (*chan).write_source = dynamic_cast<AudioFileSource*>(®ion->source (n));
455 (*chan).write_source->set_allow_remove_if_empty (false);
458 /* the source list will never be reset for a destructive track */
462 AudioDiskstream::set_io (IO& io)
465 set_align_style_from_io ();
469 AudioDiskstream::non_realtime_set_speed ()
471 if (_buffer_reallocation_required)
473 Glib::Mutex::Lock lm (state_lock);
474 allocate_temporary_buffers ();
476 _buffer_reallocation_required = false;
479 if (_seek_required) {
480 if (speed() != 1.0f || speed() != -1.0f) {
481 seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()), true);
484 seek (_session.transport_frame(), true);
487 _seek_required = false;
492 AudioDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record)
494 int possibly_recording;
497 const int transport_rolling = 0x4;
498 const int track_rec_enabled = 0x2;
499 const int global_rec_enabled = 0x1;
501 /* merge together the 3 factors that affect record status, and compute
505 rolling = _session.transport_speed() != 0.0f;
506 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
507 change = possibly_recording ^ last_possibly_recording;
509 if (possibly_recording == last_possibly_recording) {
515 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
517 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
518 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
520 /* starting to record: compute first+last frames */
522 first_recordable_frame = transport_frame + _capture_offset;
523 last_recordable_frame = max_frames;
524 capture_start_frame = transport_frame;
526 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
528 /* was stopped, now rolling (and recording) */
530 if (_alignment_style == ExistingMaterial) {
531 first_recordable_frame += _session.worst_output_latency();
533 first_recordable_frame += _roll_delay;
538 /* was rolling, but record state changed */
540 if (_alignment_style == ExistingMaterial) {
543 if (!_session.get_punch_in()) {
545 /* manual punch in happens at the correct transport frame
546 because the user hit a button. but to get alignment correct
547 we have to back up the position of the new region to the
548 appropriate spot given the roll delay.
551 capture_start_frame -= _roll_delay;
553 /* XXX paul notes (august 2005): i don't know why
557 first_recordable_frame += _capture_offset;
561 /* autopunch toggles recording at the precise
562 transport frame, and then the DS waits
563 to start recording for a time that depends
564 on the output latency.
567 first_recordable_frame += _session.worst_output_latency();
572 if (_session.get_punch_in()) {
573 first_recordable_frame += _roll_delay;
575 capture_start_frame -= _roll_delay;
581 if (_flags & Recordable) {
582 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
584 RingBufferNPT<CaptureTransition>::rw_vector transvec;
585 (*chan).capture_transition_buf->get_write_vector(&transvec);
587 if (transvec.len[0] > 0) {
588 transvec.buf[0]->type = CaptureStart;
589 transvec.buf[0]->capture_val = capture_start_frame;
590 (*chan).capture_transition_buf->increment_write_ptr(1);
594 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
600 } else if (!record_enabled() || !can_record) {
604 last_recordable_frame = transport_frame + _capture_offset;
606 if (_alignment_style == ExistingMaterial) {
607 last_recordable_frame += _session.worst_output_latency();
609 last_recordable_frame += _roll_delay;
613 last_possibly_recording = possibly_recording;
617 AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input)
620 ChannelList::iterator c;
622 jack_nframes_t rec_offset = 0;
623 jack_nframes_t rec_nframes = 0;
624 bool nominally_recording;
625 bool re = record_enabled ();
626 bool collect_playback = false;
628 /* if we've already processed the frames corresponding to this call,
629 just return. this allows multiple routes that are taking input
630 from this diskstream to call our ::process() method, but have
631 this stuff only happen once. more commonly, it allows both
632 the AudioTrack that is using this AudioDiskstream *and* the Session
633 to call process() without problems.
640 check_record_status (transport_frame, nframes, can_record);
642 nominally_recording = (can_record && re);
649 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
650 must always be called as a pair. The only exception is if this function
651 returns a non-zero value, in which case, ::commit should not be called.
654 // If we can't take the state lock return.
655 if (!state_lock.trylock()) {
659 adjust_capture_position = 0;
661 for (c = channels.begin(); c != channels.end(); ++c) {
662 (*c).current_capture_buffer = 0;
663 (*c).current_playback_buffer = 0;
666 if (nominally_recording || (_session.get_record_enabled() && _session.get_punch_in())) {
669 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
676 case OverlapInternal:
677 /* ---------- recrange
680 rec_nframes = nframes;
685 /* |--------| recrange
688 rec_nframes = transport_frame + nframes - first_recordable_frame;
690 rec_offset = first_recordable_frame - transport_frame;
695 /* |--------| recrange
698 rec_nframes = last_recordable_frame - transport_frame;
702 case OverlapExternal:
703 /* |--------| recrange
704 -------------- transrange
706 rec_nframes = last_recordable_frame - last_recordable_frame;
707 rec_offset = first_recordable_frame - transport_frame;
711 if (rec_nframes && !was_recording) {
712 capture_captured = 0;
713 was_recording = true;
718 if (can_record && !_last_capture_regions.empty()) {
719 _last_capture_regions.clear ();
722 if (nominally_recording || rec_nframes) {
724 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
726 ChannelInfo& chan (*c);
728 chan.capture_buf->get_write_vector (&chan.capture_vector);
730 if (rec_nframes <= chan.capture_vector.len[0]) {
732 chan.current_capture_buffer = chan.capture_vector.buf[0];
734 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
738 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
742 jack_nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
744 if (rec_nframes > total) {
749 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
750 jack_nframes_t first = chan.capture_vector.len[0];
752 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
753 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
754 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
755 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
757 chan.current_capture_buffer = chan.capture_wrap_buffer;
764 finish_capture (rec_monitors_input);
771 /* data will be written to disk */
773 if (rec_nframes == nframes && rec_offset == 0) {
775 for (c = channels.begin(); c != channels.end(); ++c) {
776 (*c).current_playback_buffer = (*c).current_capture_buffer;
779 playback_distance = nframes;
784 /* we can't use the capture buffer as the playback buffer, because
785 we recorded only a part of the current process' cycle data
789 collect_playback = true;
792 adjust_capture_position = rec_nframes;
794 } else if (nominally_recording) {
796 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
798 for (c = channels.begin(); c != channels.end(); ++c) {
799 (*c).current_playback_buffer = (*c).current_capture_buffer;
802 playback_distance = nframes;
806 collect_playback = true;
809 if (collect_playback) {
811 /* we're doing playback */
813 jack_nframes_t necessary_samples;
815 /* no varispeed playback if we're recording, because the output .... TBD */
817 if (rec_nframes == 0 && _actual_speed != 1.0f) {
818 necessary_samples = (jack_nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
820 necessary_samples = nframes;
823 for (c = channels.begin(); c != channels.end(); ++c) {
824 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
829 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
831 ChannelInfo& chan (*c);
833 if (necessary_samples <= chan.playback_vector.len[0]) {
835 chan.current_playback_buffer = chan.playback_vector.buf[0];
838 jack_nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
840 if (necessary_samples > total) {
846 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
847 chan.playback_vector.len[0] * sizeof (Sample));
848 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
849 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
851 chan.current_playback_buffer = chan.playback_wrap_buffer;
856 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
858 uint64_t phase = last_phase;
859 jack_nframes_t i = 0;
861 // Linearly interpolate into the alt buffer
862 // using 40.24 fixp maths (swh)
864 for (c = channels.begin(); c != channels.end(); ++c) {
867 ChannelInfo& chan (*c);
872 for (jack_nframes_t outsample = 0; outsample < nframes; ++outsample) {
874 fr = (phase & 0xFFFFFF) / 16777216.0f;
875 chan.speed_buffer[outsample] =
876 chan.current_playback_buffer[i] * (1.0f - fr) +
877 chan.current_playback_buffer[i+1] * fr;
881 chan.current_playback_buffer = chan.speed_buffer;
884 playback_distance = i + 1;
885 last_phase = (phase & 0xFFFFFF);
888 playback_distance = nframes;
900 /* we're exiting with failure, so ::commit will not
901 be called. unlock the state lock.
911 AudioDiskstream::commit (jack_nframes_t nframes)
913 bool need_butler = false;
915 if (_actual_speed < 0.0) {
916 playback_sample -= playback_distance;
918 playback_sample += playback_distance;
921 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
923 (*chan).playback_buf->increment_read_ptr (playback_distance);
925 if (adjust_capture_position) {
926 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
930 if (adjust_capture_position != 0) {
931 capture_captured += adjust_capture_position;
932 adjust_capture_position = 0;
936 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
938 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
939 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
950 AudioDiskstream::set_pending_overwrite (bool yn)
952 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
954 pending_overwrite = yn;
956 overwrite_frame = playback_sample;
957 overwrite_offset = channels.front().playback_buf->get_read_ptr();
961 AudioDiskstream::overwrite_existing_buffers ()
963 Sample* mixdown_buffer;
967 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
969 overwrite_queued = false;
971 /* assume all are the same size */
972 jack_nframes_t size = channels[0].playback_buf->bufsize();
974 mixdown_buffer = new Sample[size];
975 gain_buffer = new float[size];
976 workbuf = new char[size*4];
978 /* reduce size so that we can fill the buffer correctly. */
982 jack_nframes_t start;
984 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
986 start = overwrite_frame;
987 jack_nframes_t cnt = size;
989 /* to fill the buffer without resetting the playback sample, we need to
990 do it one or two chunks (normally two).
992 |----------------------------------------------------------------------|
996 |<- second chunk->||<----------------- first chunk ------------------>|
1000 jack_nframes_t to_read = size - overwrite_offset;
1002 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, workbuf,
1003 start, to_read, *chan, n, reversed)) {
1004 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
1005 _id, size, playback_sample) << endmsg;
1009 if (cnt > to_read) {
1013 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer, workbuf,
1014 start, cnt, *chan, n, reversed)) {
1015 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
1016 _id, size, playback_sample) << endmsg;
1025 pending_overwrite = false;
1026 delete [] gain_buffer;
1027 delete [] mixdown_buffer;
1033 AudioDiskstream::seek (jack_nframes_t frame, bool complete_refill)
1035 Glib::Mutex::Lock lm (state_lock);
1038 ChannelList::iterator chan;
1040 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1041 (*chan).playback_buf->reset ();
1042 (*chan).capture_buf->reset ();
1045 /* can't rec-enable in destructive mode if transport is before start */
1047 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
1048 disengage_record_enable (this);
1051 playback_sample = frame;
1054 if (complete_refill) {
1055 while ((ret = non_realtime_do_refill ()) > 0);
1057 ret = non_realtime_do_refill ();
1064 AudioDiskstream::can_internal_playback_seek (jack_nframes_t distance)
1066 ChannelList::iterator chan;
1068 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1069 if ((*chan).playback_buf->read_space() < distance) {
1077 AudioDiskstream::internal_playback_seek (jack_nframes_t distance)
1079 ChannelList::iterator chan;
1081 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1082 (*chan).playback_buf->increment_read_ptr (distance);
1085 first_recordable_frame += distance;
1086 playback_sample += distance;
1092 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, char * workbuf, jack_nframes_t& start, jack_nframes_t cnt,
1093 ChannelInfo& channel_info, int channel, bool reversed)
1095 jack_nframes_t this_read = 0;
1096 bool reloop = false;
1097 jack_nframes_t loop_end = 0;
1098 jack_nframes_t loop_start = 0;
1099 jack_nframes_t loop_length = 0;
1100 jack_nframes_t offset = 0;
1104 /* Make the use of a Location atomic for this read operation.
1106 Note: Locations don't get deleted, so all we care about
1107 when I say "atomic" is that we are always pointing to
1108 the same one and using a start/length values obtained
1112 if ((loc = loop_location) != 0) {
1113 loop_start = loc->start();
1114 loop_end = loc->end();
1115 loop_length = loop_end - loop_start;
1118 /* if we are looping, ensure that the first frame we read is at the correct
1119 position within the loop.
1122 if (loc && start >= loop_end) {
1123 //cerr << "start adjusted from " << start;
1124 start = loop_start + ((start - loop_start) % loop_length);
1125 //cerr << "to " << start << endl;
1127 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1132 /* take any loop into account. we can't read past the end of the loop. */
1134 if (loc && (loop_end - start < cnt)) {
1135 this_read = loop_end - start;
1136 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1143 if (this_read == 0) {
1147 this_read = min(cnt,this_read);
1149 if (_playlist->read (buf+offset, mixdown_buffer, gain_buffer, workbuf, start, this_read, channel) != this_read) {
1150 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1155 _read_data_count = _playlist->read_data_count();
1159 /* don't adjust start, since caller has already done that
1162 swap_by_ptr (buf, buf + this_read - 1);
1166 /* if we read to the end of the loop, go back to the beginning */
1176 offset += this_read;
1183 AudioDiskstream::do_refill (Sample* mixdown_buffer, float* gain_buffer, char * workbuf)
1186 jack_nframes_t to_read;
1187 RingBufferNPT<Sample>::rw_vector vector;
1191 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1192 jack_nframes_t total_space;
1193 jack_nframes_t zero_fill;
1195 ChannelList::iterator i;
1198 channels.front().playback_buf->get_write_vector (&vector);
1200 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1204 /* if there are 2+ chunks of disk i/o possible for
1205 this track, let the caller know so that it can arrange
1206 for us to be called again, ASAP.
1209 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1213 /* if we're running close to normal speed and there isn't enough
1214 space to do disk_io_chunk_frames of I/O, then don't bother.
1216 at higher speeds, just do it because the sync between butler
1217 and audio thread may not be good enough.
1220 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1224 /* when slaved, don't try to get too close to the read pointer. this
1225 leaves space for the buffer reversal to have something useful to
1229 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1233 total_space = min (disk_io_chunk_frames, total_space);
1237 if (file_frame == 0) {
1239 /* at start: nothing to do but fill with silence */
1241 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1243 ChannelInfo& chan (*i);
1244 chan.playback_buf->get_write_vector (&vector);
1245 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1246 if (vector.len[1]) {
1247 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1249 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1254 if (file_frame < total_space) {
1256 /* too close to the start: read what we can,
1257 and then zero fill the rest
1260 zero_fill = total_space - file_frame;
1261 total_space = file_frame;
1266 /* move read position backwards because we are going
1267 to reverse the data.
1270 file_frame -= total_space;
1276 if (file_frame == max_frames) {
1278 /* at end: nothing to do but fill with silence */
1280 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1282 ChannelInfo& chan (*i);
1283 chan.playback_buf->get_write_vector (&vector);
1284 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1285 if (vector.len[1]) {
1286 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1288 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1293 if (file_frame > max_frames - total_space) {
1295 /* to close to the end: read what we can, and zero fill the rest */
1297 zero_fill = total_space - (max_frames - file_frame);
1298 total_space = max_frames - file_frame;
1305 /* Please note: the code to allocate buffers isn't run
1306 during normal butler thread operation. Its there
1307 for other times when we need to call do_refill()
1308 from somewhere other than the butler thread.
1311 if (mixdown_buffer == 0) {
1312 mixdown_buffer = new Sample[disk_io_chunk_frames];
1313 free_mixdown = true;
1315 free_mixdown = false;
1318 if (gain_buffer == 0) {
1319 gain_buffer = new float[disk_io_chunk_frames];
1326 workbuf = new char[disk_io_chunk_frames * 4];
1327 free_workbuf = true;
1329 free_workbuf = false;
1332 jack_nframes_t file_frame_tmp = 0;
1334 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1336 ChannelInfo& chan (*i);
1339 jack_nframes_t len1, len2;
1341 chan.playback_buf->get_write_vector (&vector);
1344 file_frame_tmp = file_frame;
1347 buf1 = vector.buf[1];
1348 len1 = vector.len[1];
1349 buf2 = vector.buf[0];
1350 len2 = vector.len[0];
1352 buf1 = vector.buf[0];
1353 len1 = vector.len[0];
1354 buf2 = vector.buf[1];
1355 len2 = vector.len[1];
1359 to_read = min (ts, len1);
1360 to_read = min (to_read, disk_io_chunk_frames);
1364 if (read (buf1, mixdown_buffer, gain_buffer, workbuf, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1369 chan.playback_buf->increment_write_ptr (to_read);
1373 to_read = min (ts, len2);
1378 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1379 so read some or all of vector.len[1] as well.
1382 if (read (buf2, mixdown_buffer, gain_buffer, workbuf, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1387 chan.playback_buf->increment_write_ptr (to_read);
1396 file_frame = file_frame_tmp;
1400 delete [] mixdown_buffer;
1403 delete [] gain_buffer;
1413 AudioDiskstream::do_flush (char * workbuf, bool force_flush)
1417 RingBufferNPT<Sample>::rw_vector vector;
1418 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1419 jack_nframes_t total;
1421 /* important note: this function will write *AT MOST*
1422 disk_io_chunk_frames of data to disk. it will never
1423 write more than that. if its writes that much and there
1424 is more than that waiting to be written, it will return 1,
1425 otherwise 0 on success or -1 on failure.
1427 if there is less than disk_io_chunk_frames to be written,
1428 no data will be written at all unless `force_flush' is true.
1431 _write_data_count = 0;
1433 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1435 (*chan).capture_buf->get_read_vector (&vector);
1437 total = vector.len[0] + vector.len[1];
1440 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1445 /* if there are 2+ chunks of disk i/o possible for
1446 this track, let the caller know so that it can arrange
1447 for us to be called again, ASAP.
1449 if we are forcing a flush, then if there is* any* extra
1450 work, let the caller know.
1452 if we are no longer recording and there is any extra work,
1453 let the caller know too.
1456 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1460 to_write = min (disk_io_chunk_frames, (jack_nframes_t) vector.len[0]);
1462 // check the transition buffer when recording destructive
1463 // important that we get this after the capture buf
1465 if (destructive()) {
1466 (*chan).capture_transition_buf->get_read_vector(&transvec);
1467 size_t transcount = transvec.len[0] + transvec.len[1];
1468 bool have_start = false;
1471 for (ti=0; ti < transcount; ++ti) {
1472 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1474 if (captrans.type == CaptureStart) {
1475 // by definition, the first data we got above represents the given capture pos
1477 (*chan).write_source->mark_capture_start (captrans.capture_val);
1478 (*chan).curr_capture_cnt = 0;
1482 else if (captrans.type == CaptureEnd) {
1484 // capture end, the capture_val represents total frames in capture
1486 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1488 // shorten to make the write a perfect fit
1489 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1491 if (nto_write < to_write) {
1492 ret = 1; // should we?
1494 to_write = nto_write;
1496 (*chan).write_source->mark_capture_end ();
1498 // increment past this transition, but go no further
1503 // actually ends just beyond this chunk, so force more work
1511 (*chan).capture_transition_buf->increment_read_ptr(ti);
1515 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write, workbuf) != to_write) {
1516 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1520 (*chan).capture_buf->increment_read_ptr (to_write);
1521 (*chan).curr_capture_cnt += to_write;
1523 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1525 /* we wrote all of vector.len[0] but it wasn't an entire
1526 disk_io_chunk_frames of data, so arrange for some part
1527 of vector.len[1] to be flushed to disk as well.
1530 to_write = min ((jack_nframes_t)(disk_io_chunk_frames - to_write), (jack_nframes_t) vector.len[1]);
1532 if ((*chan).write_source->write (vector.buf[1], to_write, workbuf) != to_write) {
1533 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1537 _write_data_count += (*chan).write_source->write_data_count();
1539 (*chan).capture_buf->increment_read_ptr (to_write);
1540 (*chan).curr_capture_cnt += to_write;
1549 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1551 uint32_t buffer_position;
1552 bool more_work = true;
1554 AudioRegion* region = 0;
1555 jack_nframes_t total_capture;
1556 AudioRegion::SourceList srcs;
1557 AudioRegion::SourceList::iterator src;
1558 ChannelList::iterator chan;
1559 vector<CaptureInfo*>::iterator ci;
1561 list<AudioFileSource*>* deletion_list;
1562 bool mark_write_completed = false;
1564 finish_capture (true);
1566 /* butler is already stopped, but there may be work to do
1567 to flush remaining data to disk.
1570 while (more_work && !err) {
1571 switch (do_flush ( _session.conversion_buffer(Session::TransportContext), true)) {
1578 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1583 /* XXX is there anything we can do if err != 0 ? */
1584 Glib::Mutex::Lock lm (capture_info_lock);
1586 if (capture_info.empty()) {
1590 if (abort_capture) {
1592 ChannelList::iterator chan;
1594 deletion_list = new list<AudioFileSource*>;
1596 for ( chan = channels.begin(); chan != channels.end(); ++chan) {
1598 if ((*chan).write_source) {
1600 (*chan).write_source->mark_for_remove ();
1601 (*chan).write_source->release ();
1603 deletion_list->push_back ((*chan).write_source);
1605 (*chan).write_source = 0;
1608 /* new source set up in "out" below */
1611 if (!deletion_list->empty()) {
1612 DeleteSources (deletion_list);
1614 delete deletion_list;
1620 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1621 total_capture += (*ci)->frames;
1624 /* figure out the name for this take */
1626 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1628 AudioFileSource* s = (*chan).write_source;
1632 AudioFileSource* fsrc;
1636 if ((fsrc = dynamic_cast<AudioFileSource *>(s)) != 0) {
1637 cerr << "updating source after capture\n";
1638 fsrc->update_header (capture_info.front()->start, when, twhen);
1641 s->set_captured_for (_name);
1646 /* destructive tracks have a single, never changing region */
1648 if (destructive()) {
1650 /* send a signal that any UI can pick up to do the right thing. there is
1651 a small problem here in that a UI may need the peak data to be ready
1652 for the data that was recorded and this isn't interlocked with that
1653 process. this problem is deferred to the UI.
1656 _playlist->Modified();
1660 /* Register a new region with the Session that
1661 describes the entire source. Do this first
1662 so that any sub-regions will obviously be
1663 children of this one (later!)
1667 region = new AudioRegion (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1668 region_name_from_path (channels[0].write_source->name()),
1669 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
1671 region->special_set_position (capture_info.front()->start);
1675 catch (failed_constructor& err) {
1676 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1680 _last_capture_regions.push_back (region);
1682 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1684 _session.add_undo (_playlist->get_memento());
1685 _playlist->freeze ();
1687 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1690 _session.region_name (region_name, channels[0].write_source->name(), false);
1692 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1695 region = new AudioRegion (srcs, buffer_position, (*ci)->frames, region_name);
1698 catch (failed_constructor& err) {
1699 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1700 continue; /* XXX is this OK? */
1703 _last_capture_regions.push_back (region);
1705 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1707 i_am_the_modifier++;
1708 _playlist->add_region (*region, (*ci)->start);
1709 i_am_the_modifier--;
1711 buffer_position += (*ci)->frames;
1715 _session.add_redo_no_execute (_playlist->get_memento());
1718 mark_write_completed = true;
1720 reset_write_sources (mark_write_completed);
1723 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1727 capture_info.clear ();
1728 capture_start_frame = 0;
1732 AudioDiskstream::finish_capture (bool rec_monitors_input)
1734 was_recording = false;
1736 if (capture_captured == 0) {
1740 if (recordable() && destructive()) {
1741 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1743 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1744 (*chan).capture_transition_buf->get_write_vector(&transvec);
1747 if (transvec.len[0] > 0) {
1748 transvec.buf[0]->type = CaptureEnd;
1749 transvec.buf[0]->capture_val = capture_captured;
1750 (*chan).capture_transition_buf->increment_write_ptr(1);
1754 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1760 CaptureInfo* ci = new CaptureInfo;
1762 ci->start = capture_start_frame;
1763 ci->frames = capture_captured;
1765 /* XXX theoretical race condition here. Need atomic exchange ?
1766 However, the circumstances when this is called right
1767 now (either on record-disable or transport_stopped)
1768 mean that no actual race exists. I think ...
1769 We now have a capture_info_lock, but it is only to be used
1770 to synchronize in the transport_stop and the capture info
1771 accessors, so that invalidation will not occur (both non-realtime).
1774 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1776 capture_info.push_back (ci);
1777 capture_captured = 0;
1781 AudioDiskstream::set_record_enabled (bool yn, void* src)
1783 if (!recordable() || !_session.record_enabling_legal()) {
1787 /* can't rec-enable in destructive mode if transport is before start */
1789 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1793 if (yn && channels[0].source == 0) {
1795 /* pick up connections not initiated *from* the IO object
1796 we're associated with.
1799 get_input_sources ();
1802 /* yes, i know that this not proof against race conditions, but its
1803 good enough. i think.
1806 if (record_enabled() != yn) {
1808 engage_record_enable (src);
1810 disengage_record_enable (src);
1816 AudioDiskstream::engage_record_enable (void* src)
1818 bool rolling = _session.transport_speed() != 0.0f;
1820 g_atomic_int_set (&_record_enabled, 1);
1821 capturing_sources.clear ();
1822 if (Config->get_use_hardware_monitoring()) {
1823 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1824 if ((*chan).source) {
1825 (*chan).source->request_monitor_input (!(_session.get_auto_input() && rolling));
1827 capturing_sources.push_back ((*chan).write_source);
1830 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1831 capturing_sources.push_back ((*chan).write_source);
1835 record_enable_changed (src); /* EMIT SIGNAL */
1839 AudioDiskstream::disengage_record_enable (void* src)
1841 g_atomic_int_set (&_record_enabled, 0);
1842 if (Config->get_use_hardware_monitoring()) {
1843 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1844 if ((*chan).source) {
1845 (*chan).source->request_monitor_input (false);
1849 capturing_sources.clear ();
1850 record_enable_changed (src); /* EMIT SIGNAL */
1855 AudioDiskstream::get_state ()
1857 XMLNode* node = new XMLNode ("AudioDiskstream");
1859 LocaleGuard lg (X_("POSIX"));
1861 snprintf (buf, sizeof(buf), "0x%x", _flags);
1862 node->add_property ("flags", buf);
1864 snprintf (buf, sizeof(buf), "%zd", channels.size());
1865 node->add_property ("channels", buf);
1867 node->add_property ("playlist", _playlist->name());
1869 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1870 node->add_property ("speed", buf);
1872 node->add_property("name", _name);
1874 node->add_property("id", buf);
1876 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1878 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1879 XMLNode* cs_grandchild;
1881 for (vector<AudioFileSource*>::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1882 cs_grandchild = new XMLNode (X_("file"));
1883 cs_grandchild->add_property (X_("path"), (*i)->path());
1884 cs_child->add_child_nocopy (*cs_grandchild);
1887 /* store the location where capture will start */
1891 if (_session.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1892 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1894 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1897 cs_child->add_property (X_("at"), buf);
1898 node->add_child_nocopy (*cs_child);
1902 node->add_child_copy (*_extra_xml);
1909 AudioDiskstream::set_state (const XMLNode& node)
1911 const XMLProperty* prop;
1912 XMLNodeList nlist = node.children();
1913 XMLNodeIterator niter;
1914 uint32_t nchans = 1;
1915 XMLNode* capture_pending_node = 0;
1916 LocaleGuard lg (X_("POSIX"));
1918 in_set_state = true;
1920 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1921 if ((*niter)->name() == IO::state_node_name) {
1922 deprecated_io_node = new XMLNode (**niter);
1925 if ((*niter)->name() == X_("CapturingSources")) {
1926 capture_pending_node = *niter;
1930 /* prevent write sources from being created */
1932 in_set_state = true;
1934 if ((prop = node.property ("name")) != 0) {
1935 _name = prop->value();
1938 if (deprecated_io_node) {
1939 if ((prop = deprecated_io_node->property ("id")) != 0) {
1940 _id = prop->value ();
1943 if ((prop = node.property ("id")) != 0) {
1944 _id = prop->value ();
1948 if ((prop = node.property ("flags")) != 0) {
1949 _flags = strtol (prop->value().c_str(), 0, 0);
1952 if ((prop = node.property ("channels")) != 0) {
1953 nchans = atoi (prop->value().c_str());
1956 // create necessary extra channels
1957 // we are always constructed with one
1958 // and we always need one
1960 if (nchans > _n_channels) {
1962 // we need to add new channel infos
1963 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1965 int diff = nchans - channels.size();
1967 for (int i=0; i < diff; ++i) {
1971 } else if (nchans < _n_channels) {
1973 // we need to get rid of channels
1974 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1976 int diff = channels.size() - nchans;
1978 for (int i = 0; i < diff; ++i) {
1983 if ((prop = node.property ("playlist")) == 0) {
1988 bool had_playlist = (_playlist != 0);
1990 if (find_and_use_playlist (prop->value())) {
1994 if (!had_playlist) {
1995 _playlist->set_orig_diskstream_id (_id);
1998 if (!destructive() && capture_pending_node) {
1999 /* destructive streams have one and only one source per channel,
2000 and so they never end up in pending capture in any useful
2003 use_pending_capture_data (*capture_pending_node);
2008 if ((prop = node.property ("speed")) != 0) {
2009 double sp = atof (prop->value().c_str());
2011 if (realtime_set_speed (sp, false)) {
2012 non_realtime_set_speed ();
2016 _n_channels = channels.size();
2018 in_set_state = false;
2020 /* make sure this is clear before we do anything else */
2022 capturing_sources.clear ();
2024 /* write sources are handled when we handle the input set
2025 up of the IO that owns this DS (::non_realtime_input_change())
2028 in_set_state = false;
2034 AudioDiskstream::use_new_write_source (uint32_t n)
2036 if (!recordable()) {
2040 if (n >= channels.size()) {
2041 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
2045 ChannelInfo &chan = channels[n];
2047 if (chan.write_source) {
2049 if (AudioFileSource::is_empty (chan.write_source->path())) {
2050 chan.write_source->mark_for_remove ();
2051 chan.write_source->release();
2052 delete chan.write_source;
2054 chan.write_source->release();
2055 chan.write_source = 0;
2060 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
2061 throw failed_constructor();
2065 catch (failed_constructor &err) {
2066 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
2067 chan.write_source = 0;
2071 chan.write_source->use ();
2073 /* do not remove destructive files even if they are empty */
2075 chan.write_source->set_allow_remove_if_empty (!destructive());
2081 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
2083 ChannelList::iterator chan;
2086 if (!recordable()) {
2090 capturing_sources.clear ();
2092 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2093 if (!destructive()) {
2095 if ((*chan).write_source && mark_write_complete) {
2096 (*chan).write_source->mark_streaming_write_completed ();
2098 use_new_write_source (n);
2100 if (record_enabled()) {
2101 capturing_sources.push_back ((*chan).write_source);
2105 if ((*chan).write_source == 0) {
2106 use_new_write_source (n);
2111 if (destructive()) {
2113 /* we now have all our write sources set up, so create the
2114 playlist's single region.
2117 if (_playlist->empty()) {
2118 setup_destructive_playlist ();
2124 AudioDiskstream::rename_write_sources ()
2126 ChannelList::iterator chan;
2129 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2130 if ((*chan).write_source != 0) {
2131 (*chan).write_source->set_name (_name, destructive());
2132 /* XXX what to do if one of them fails ? */
2140 AudioDiskstream::set_block_size (jack_nframes_t nframes)
2142 if (_session.get_block_size() > speed_buffer_size) {
2143 speed_buffer_size = _session.get_block_size();
2145 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2146 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2147 (*chan).speed_buffer = new Sample[speed_buffer_size];
2150 allocate_temporary_buffers ();
2154 AudioDiskstream::allocate_temporary_buffers ()
2156 /* make sure the wrap buffer is at least large enough to deal
2157 with the speeds up to 1.2, to allow for micro-variation
2158 when slaving to MTC, SMPTE etc.
2161 double sp = max (fabsf (_actual_speed), 1.2f);
2162 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * sp) + 1;
2164 if (required_wrap_size > wrap_buffer_size) {
2166 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2167 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2168 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2169 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2170 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2173 wrap_buffer_size = required_wrap_size;
2178 AudioDiskstream::monitor_input (bool yn)
2180 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2182 if ((*chan).source) {
2183 (*chan).source->request_monitor_input (yn);
2189 AudioDiskstream::set_align_style_from_io ()
2191 bool have_physical = false;
2197 get_input_sources ();
2199 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2200 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2201 have_physical = true;
2206 if (have_physical) {
2207 set_align_style (ExistingMaterial);
2209 set_align_style (CaptureTime);
2214 AudioDiskstream::add_channel ()
2216 /* XXX need to take lock??? */
2220 init_channel (chan);
2222 chan.speed_buffer = new Sample[speed_buffer_size];
2223 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2224 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2226 channels.push_back (chan);
2228 _n_channels = channels.size();
2234 AudioDiskstream::remove_channel ()
2236 if (channels.size() > 1) {
2237 /* XXX need to take lock??? */
2238 ChannelInfo & chan = channels.back();
2239 destroy_channel (chan);
2240 channels.pop_back();
2242 _n_channels = channels.size();
2250 AudioDiskstream::playback_buffer_load () const
2252 return (float) ((double) channels.front().playback_buf->read_space()/
2253 (double) channels.front().playback_buf->bufsize());
2257 AudioDiskstream::capture_buffer_load () const
2259 return (float) ((double) channels.front().capture_buf->write_space()/
2260 (double) channels.front().capture_buf->bufsize());
2264 AudioDiskstream::set_loop (Location *location)
2267 if (location->start() >= location->end()) {
2268 error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
2273 loop_location = location;
2275 LoopSet (location); /* EMIT SIGNAL */
2280 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2282 const XMLProperty* prop;
2283 XMLNodeList nlist = node.children();
2284 XMLNodeIterator niter;
2285 AudioFileSource* fs;
2286 AudioFileSource* first_fs = 0;
2287 AudioRegion::SourceList pending_sources;
2288 jack_nframes_t position;
2290 if ((prop = node.property (X_("at"))) == 0) {
2294 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2298 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2299 if ((*niter)->name() == X_("file")) {
2301 if ((prop = (*niter)->property (X_("path"))) == 0) {
2306 fs = new SndFileSource (prop->value(),
2307 Config->get_native_file_data_format(),
2308 Config->get_native_file_header_format(),
2309 _session.frame_rate());
2312 catch (failed_constructor& err) {
2313 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2314 _name, prop->value())
2319 pending_sources.push_back (fs);
2321 if (first_fs == 0) {
2325 fs->set_captured_for (_name);
2329 if (pending_sources.size() == 0) {
2330 /* nothing can be done */
2334 if (pending_sources.size() != _n_channels) {
2335 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2340 AudioRegion* region;
2343 region = new AudioRegion (pending_sources, 0, first_fs->length(),
2344 region_name_from_path (first_fs->name()),
2345 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
2347 region->special_set_position (0);
2350 catch (failed_constructor& err) {
2351 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2359 region = new AudioRegion (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name()));
2362 catch (failed_constructor& err) {
2363 error << string_compose (_("%1: cannot create region from pending capture sources"),
2370 _playlist->add_region (*region, position);