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)
62 , deprecated_io_node(NULL)
65 /* prevent any write sources from being created */
74 DiskstreamCreated (this); /* EMIT SIGNAL */
77 AudioDiskstream::AudioDiskstream (Session& sess, const XMLNode& node)
78 : Diskstream(sess, node)
79 , deprecated_io_node(NULL)
85 if (set_state (node)) {
87 throw failed_constructor();
93 use_destructive_playlist ();
96 DiskstreamCreated (this); /* EMIT SIGNAL */
100 AudioDiskstream::init_channel (ChannelInfo &chan)
102 chan.playback_wrap_buffer = 0;
103 chan.capture_wrap_buffer = 0;
104 chan.speed_buffer = 0;
105 chan.peak_power = 0.0f;
106 chan.write_source = 0;
108 chan.current_capture_buffer = 0;
109 chan.current_playback_buffer = 0;
110 chan.curr_capture_cnt = 0;
112 chan.playback_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
113 chan.capture_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
114 chan.capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
117 /* touch the ringbuffer buffers, which will cause
118 them to be mapped into locked physical RAM if
119 we're running with mlockall(). this doesn't do
122 memset (chan.playback_buf->buffer(), 0, sizeof (Sample) * chan.playback_buf->bufsize());
123 memset (chan.capture_buf->buffer(), 0, sizeof (Sample) * chan.capture_buf->bufsize());
124 memset (chan.capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * chan.capture_transition_buf->bufsize());
129 AudioDiskstream::init (Diskstream::Flag f)
133 /* there are no channels at this point, so these
134 two calls just get speed_buffer_size and wrap_buffer
135 size setup without duplicating their code.
138 set_block_size (_session.get_block_size());
139 allocate_temporary_buffers ();
141 pending_overwrite = false;
143 overwrite_queued = false;
144 input_change_pending = NoChange;
151 AudioDiskstream::destroy_channel (ChannelInfo &chan)
153 if (chan.write_source) {
154 chan.write_source->release ();
155 chan.write_source = 0;
158 if (chan.speed_buffer) {
159 delete [] chan.speed_buffer;
162 if (chan.playback_wrap_buffer) {
163 delete [] chan.playback_wrap_buffer;
165 if (chan.capture_wrap_buffer) {
166 delete [] chan.capture_wrap_buffer;
169 delete chan.playback_buf;
170 delete chan.capture_buf;
171 delete chan.capture_transition_buf;
173 chan.playback_buf = 0;
174 chan.capture_buf = 0;
177 AudioDiskstream::~AudioDiskstream ()
179 Glib::Mutex::Lock lm (state_lock);
184 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
185 destroy_channel((*chan));
192 AudioDiskstream::handle_input_change (IOChange change, void *src)
194 Glib::Mutex::Lock lm (state_lock);
196 if (!(input_change_pending & change)) {
197 input_change_pending = IOChange (input_change_pending|change);
198 _session.request_input_change_handling ();
203 AudioDiskstream::non_realtime_input_change ()
206 Glib::Mutex::Lock lm (state_lock);
208 if (input_change_pending == NoChange) {
212 if (input_change_pending & ConfigurationChanged) {
214 if (_io->n_inputs() > _n_channels) {
216 // we need to add new channel infos
218 int diff = _io->n_inputs() - channels.size();
220 for (int i = 0; i < diff; ++i) {
224 } else if (_io->n_inputs() < _n_channels) {
226 // we need to get rid of channels
228 int diff = channels.size() - _io->n_inputs();
230 for (int i = 0; i < diff; ++i) {
236 get_input_sources ();
237 set_capture_offset ();
239 if (first_input_change) {
240 set_align_style (_persistent_alignment_style);
241 first_input_change = false;
243 set_align_style_from_io ();
246 input_change_pending = NoChange;
249 /* reset capture files */
251 reset_write_sources (false);
253 /* now refill channel buffers */
255 if (speed() != 1.0f || speed() != -1.0f) {
256 seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()));
259 seek (_session.transport_frame());
264 AudioDiskstream::get_input_sources ()
266 uint32_t ni = _io->n_inputs();
268 for (uint32_t n = 0; n < ni; ++n) {
270 const char **connections = _io->input(n)->get_connections ();
271 ChannelInfo& chan = channels[n];
273 if (connections == 0 || connections[0] == 0) {
276 // _source->disable_metering ();
282 chan.source = _session.engine().get_port_by_name (connections[0]);
292 AudioDiskstream::find_and_use_playlist (const string& name)
295 AudioPlaylist* playlist;
297 if ((pl = _session.playlist_by_name (name)) == 0) {
298 playlist = new AudioPlaylist(_session, name);
302 if ((playlist = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
303 error << string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
307 return use_playlist (playlist);
311 AudioDiskstream::use_playlist (Playlist* playlist)
313 assert(dynamic_cast<AudioPlaylist*>(playlist));
316 Glib::Mutex::Lock lm (state_lock);
318 if (playlist == _playlist) {
322 plstate_connection.disconnect();
323 plmod_connection.disconnect ();
324 plgone_connection.disconnect ();
330 _playlist = dynamic_cast<AudioPlaylist*>(playlist);
333 if (!in_set_state && recordable()) {
334 reset_write_sources (false);
337 plstate_connection = _playlist->StateChanged.connect (mem_fun (*this, &AudioDiskstream::playlist_changed));
338 plmod_connection = _playlist->Modified.connect (mem_fun (*this, &AudioDiskstream::playlist_modified));
339 plgone_connection = _playlist->GoingAway.connect (mem_fun (*this, &AudioDiskstream::playlist_deleted));
342 if (!overwrite_queued) {
343 _session.request_overwrite_buffer (this);
344 overwrite_queued = true;
347 PlaylistChanged (); /* EMIT SIGNAL */
348 _session.set_dirty ();
354 AudioDiskstream::use_new_playlist ()
357 AudioPlaylist* playlist;
359 if (!in_set_state && destructive()) {
364 newname = Playlist::bump_name (_playlist->name(), _session);
366 newname = Playlist::bump_name (_name, _session);
369 if ((playlist = new AudioPlaylist (_session, newname, hidden())) != 0) {
370 playlist->set_orig_diskstream_id (id());
371 return use_playlist (playlist);
378 AudioDiskstream::use_copy_playlist ()
384 if (_playlist == 0) {
385 error << string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
390 AudioPlaylist* playlist;
392 newname = Playlist::bump_name (_playlist->name(), _session);
394 if ((playlist = new AudioPlaylist (*_playlist, newname)) != 0) {
395 playlist->set_orig_diskstream_id (id());
396 return use_playlist (playlist);
404 AudioDiskstream::playlist_deleted (Playlist* pl)
406 /* this catches an ordering issue with session destruction. playlists
407 are destroyed before diskstreams. we have to invalidate any handles
408 we have to the playlist.
416 AudioDiskstream::setup_destructive_playlist ()
418 AudioRegion::SourceList srcs;
420 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
421 srcs.push_back ((*chan).write_source);
424 /* a single full-sized region */
426 cerr << "setup DS using " << srcs.front()->natural_position () << endl;
428 AudioRegion* region = new AudioRegion (srcs, 0, max_frames, _name);
429 _playlist->add_region (*region, srcs.front()->natural_position());
433 AudioDiskstream::use_destructive_playlist ()
435 /* use the sources associated with the single full-extent region */
437 Playlist::RegionList* rl = _playlist->regions_at (0);
440 reset_write_sources (false, true);
444 AudioRegion* region = dynamic_cast<AudioRegion*> (rl->front());
447 throw failed_constructor();
453 ChannelList::iterator chan;
455 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
456 (*chan).write_source = dynamic_cast<AudioFileSource*>(®ion->source (n));
457 (*chan).write_source->set_allow_remove_if_empty (false);
460 /* the source list will never be reset for a destructive track */
464 AudioDiskstream::set_io (IO& io)
467 set_align_style_from_io ();
471 AudioDiskstream::non_realtime_set_speed ()
473 if (_buffer_reallocation_required)
475 Glib::Mutex::Lock lm (state_lock);
476 allocate_temporary_buffers ();
478 _buffer_reallocation_required = false;
481 if (_seek_required) {
482 if (speed() != 1.0f || speed() != -1.0f) {
483 seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()), true);
486 seek (_session.transport_frame(), true);
489 _seek_required = false;
494 AudioDiskstream::check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record)
496 int possibly_recording;
499 const int transport_rolling = 0x4;
500 const int track_rec_enabled = 0x2;
501 const int global_rec_enabled = 0x1;
503 /* merge together the 3 factors that affect record status, and compute
507 rolling = _session.transport_speed() != 0.0f;
508 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
509 change = possibly_recording ^ last_possibly_recording;
511 if (possibly_recording == last_possibly_recording) {
517 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
519 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
520 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
522 /* starting to record: compute first+last frames */
524 first_recordable_frame = transport_frame + _capture_offset;
525 last_recordable_frame = max_frames;
526 capture_start_frame = transport_frame;
528 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
530 /* was stopped, now rolling (and recording) */
532 if (_alignment_style == ExistingMaterial) {
533 first_recordable_frame += _session.worst_output_latency();
535 first_recordable_frame += _roll_delay;
540 /* was rolling, but record state changed */
542 if (_alignment_style == ExistingMaterial) {
545 if (!_session.get_punch_in()) {
547 /* manual punch in happens at the correct transport frame
548 because the user hit a button. but to get alignment correct
549 we have to back up the position of the new region to the
550 appropriate spot given the roll delay.
553 capture_start_frame -= _roll_delay;
555 /* XXX paul notes (august 2005): i don't know why
559 first_recordable_frame += _capture_offset;
563 /* autopunch toggles recording at the precise
564 transport frame, and then the DS waits
565 to start recording for a time that depends
566 on the output latency.
569 first_recordable_frame += _session.worst_output_latency();
574 if (_session.get_punch_in()) {
575 first_recordable_frame += _roll_delay;
577 capture_start_frame -= _roll_delay;
583 if (_flags & Recordable) {
584 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
586 RingBufferNPT<CaptureTransition>::rw_vector transvec;
587 (*chan).capture_transition_buf->get_write_vector(&transvec);
589 if (transvec.len[0] > 0) {
590 transvec.buf[0]->type = CaptureStart;
591 transvec.buf[0]->capture_val = capture_start_frame;
592 (*chan).capture_transition_buf->increment_write_ptr(1);
596 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
602 } else if (!record_enabled() || !can_record) {
606 last_recordable_frame = transport_frame + _capture_offset;
608 if (_alignment_style == ExistingMaterial) {
609 last_recordable_frame += _session.worst_output_latency();
611 last_recordable_frame += _roll_delay;
615 last_possibly_recording = possibly_recording;
619 AudioDiskstream::process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input)
622 ChannelList::iterator c;
624 jack_nframes_t rec_offset = 0;
625 jack_nframes_t rec_nframes = 0;
626 bool nominally_recording;
627 bool re = record_enabled ();
628 bool collect_playback = false;
630 /* if we've already processed the frames corresponding to this call,
631 just return. this allows multiple routes that are taking input
632 from this diskstream to call our ::process() method, but have
633 this stuff only happen once. more commonly, it allows both
634 the AudioTrack that is using this AudioDiskstream *and* the Session
635 to call process() without problems.
642 check_record_status (transport_frame, nframes, can_record);
644 nominally_recording = (can_record && re);
651 /* This lock is held until the end of AudioDiskstream::commit, so these two functions
652 must always be called as a pair. The only exception is if this function
653 returns a non-zero value, in which case, ::commit should not be called.
656 // If we can't take the state lock return.
657 if (!state_lock.trylock()) {
661 adjust_capture_position = 0;
663 for (c = channels.begin(); c != channels.end(); ++c) {
664 (*c).current_capture_buffer = 0;
665 (*c).current_playback_buffer = 0;
668 if (nominally_recording || (_session.get_record_enabled() && _session.get_punch_in())) {
671 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
678 case OverlapInternal:
679 /* ---------- recrange
682 rec_nframes = nframes;
687 /* |--------| recrange
690 rec_nframes = transport_frame + nframes - first_recordable_frame;
692 rec_offset = first_recordable_frame - transport_frame;
697 /* |--------| recrange
700 rec_nframes = last_recordable_frame - transport_frame;
704 case OverlapExternal:
705 /* |--------| recrange
706 -------------- transrange
708 rec_nframes = last_recordable_frame - last_recordable_frame;
709 rec_offset = first_recordable_frame - transport_frame;
713 if (rec_nframes && !was_recording) {
714 capture_captured = 0;
715 was_recording = true;
720 if (can_record && !_last_capture_regions.empty()) {
721 _last_capture_regions.clear ();
724 if (nominally_recording || rec_nframes) {
726 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
728 ChannelInfo& chan (*c);
730 chan.capture_buf->get_write_vector (&chan.capture_vector);
732 if (rec_nframes <= chan.capture_vector.len[0]) {
734 chan.current_capture_buffer = chan.capture_vector.buf[0];
736 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
740 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
744 jack_nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
746 if (rec_nframes > total) {
751 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
752 jack_nframes_t first = chan.capture_vector.len[0];
754 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
755 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
756 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
757 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
759 chan.current_capture_buffer = chan.capture_wrap_buffer;
766 finish_capture (rec_monitors_input);
773 /* data will be written to disk */
775 if (rec_nframes == nframes && rec_offset == 0) {
777 for (c = channels.begin(); c != channels.end(); ++c) {
778 (*c).current_playback_buffer = (*c).current_capture_buffer;
781 playback_distance = nframes;
786 /* we can't use the capture buffer as the playback buffer, because
787 we recorded only a part of the current process' cycle data
791 collect_playback = true;
794 adjust_capture_position = rec_nframes;
796 } else if (nominally_recording) {
798 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
800 for (c = channels.begin(); c != channels.end(); ++c) {
801 (*c).current_playback_buffer = (*c).current_capture_buffer;
804 playback_distance = nframes;
808 collect_playback = true;
811 if (collect_playback) {
813 /* we're doing playback */
815 jack_nframes_t necessary_samples;
817 /* no varispeed playback if we're recording, because the output .... TBD */
819 if (rec_nframes == 0 && _actual_speed != 1.0f) {
820 necessary_samples = (jack_nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
822 necessary_samples = nframes;
825 for (c = channels.begin(); c != channels.end(); ++c) {
826 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
831 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
833 ChannelInfo& chan (*c);
835 if (necessary_samples <= chan.playback_vector.len[0]) {
837 chan.current_playback_buffer = chan.playback_vector.buf[0];
840 jack_nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
842 if (necessary_samples > total) {
848 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
849 chan.playback_vector.len[0] * sizeof (Sample));
850 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
851 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
853 chan.current_playback_buffer = chan.playback_wrap_buffer;
858 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
860 uint64_t phase = last_phase;
861 jack_nframes_t i = 0;
863 // Linearly interpolate into the alt buffer
864 // using 40.24 fixp maths (swh)
866 for (c = channels.begin(); c != channels.end(); ++c) {
869 ChannelInfo& chan (*c);
874 for (jack_nframes_t outsample = 0; outsample < nframes; ++outsample) {
876 fr = (phase & 0xFFFFFF) / 16777216.0f;
877 chan.speed_buffer[outsample] =
878 chan.current_playback_buffer[i] * (1.0f - fr) +
879 chan.current_playback_buffer[i+1] * fr;
883 chan.current_playback_buffer = chan.speed_buffer;
886 playback_distance = i + 1;
887 last_phase = (phase & 0xFFFFFF);
890 playback_distance = nframes;
902 /* we're exiting with failure, so ::commit will not
903 be called. unlock the state lock.
913 AudioDiskstream::commit (jack_nframes_t nframes)
915 bool need_butler = false;
917 if (_actual_speed < 0.0) {
918 playback_sample -= playback_distance;
920 playback_sample += playback_distance;
923 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
925 (*chan).playback_buf->increment_read_ptr (playback_distance);
927 if (adjust_capture_position) {
928 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
932 if (adjust_capture_position != 0) {
933 capture_captured += adjust_capture_position;
934 adjust_capture_position = 0;
938 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
940 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
941 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
952 AudioDiskstream::set_pending_overwrite (bool yn)
954 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
956 pending_overwrite = yn;
958 overwrite_frame = playback_sample;
959 overwrite_offset = channels.front().playback_buf->get_read_ptr();
963 AudioDiskstream::overwrite_existing_buffers ()
965 Sample* mixdown_buffer;
969 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
971 overwrite_queued = false;
973 /* assume all are the same size */
974 jack_nframes_t size = channels[0].playback_buf->bufsize();
976 mixdown_buffer = new Sample[size];
977 gain_buffer = new float[size];
978 workbuf = new char[size*4];
980 /* reduce size so that we can fill the buffer correctly. */
984 jack_nframes_t start;
986 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
988 start = overwrite_frame;
989 jack_nframes_t cnt = size;
991 /* to fill the buffer without resetting the playback sample, we need to
992 do it one or two chunks (normally two).
994 |----------------------------------------------------------------------|
998 |<- second chunk->||<----------------- first chunk ------------------>|
1002 jack_nframes_t to_read = size - overwrite_offset;
1004 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, workbuf,
1005 start, to_read, *chan, n, reversed)) {
1006 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
1007 _id, size, playback_sample) << endmsg;
1011 if (cnt > to_read) {
1015 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer, workbuf,
1016 start, cnt, *chan, n, reversed)) {
1017 error << string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
1018 _id, size, playback_sample) << endmsg;
1027 pending_overwrite = false;
1028 delete [] gain_buffer;
1029 delete [] mixdown_buffer;
1035 AudioDiskstream::seek (jack_nframes_t frame, bool complete_refill)
1037 Glib::Mutex::Lock lm (state_lock);
1040 ChannelList::iterator chan;
1042 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1043 (*chan).playback_buf->reset ();
1044 (*chan).capture_buf->reset ();
1047 /* can't rec-enable in destructive mode if transport is before start */
1049 if (destructive() && record_enabled() && frame < _session.current_start_frame()) {
1050 disengage_record_enable (this);
1053 playback_sample = frame;
1056 if (complete_refill) {
1057 while ((ret = non_realtime_do_refill ()) > 0);
1059 ret = non_realtime_do_refill ();
1066 AudioDiskstream::can_internal_playback_seek (jack_nframes_t distance)
1068 ChannelList::iterator chan;
1070 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1071 if ((*chan).playback_buf->read_space() < distance) {
1079 AudioDiskstream::internal_playback_seek (jack_nframes_t distance)
1081 ChannelList::iterator chan;
1083 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1084 (*chan).playback_buf->increment_read_ptr (distance);
1087 first_recordable_frame += distance;
1088 playback_sample += distance;
1094 AudioDiskstream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, char * workbuf, jack_nframes_t& start, jack_nframes_t cnt,
1095 ChannelInfo& channel_info, int channel, bool reversed)
1097 jack_nframes_t this_read = 0;
1098 bool reloop = false;
1099 jack_nframes_t loop_end = 0;
1100 jack_nframes_t loop_start = 0;
1101 jack_nframes_t loop_length = 0;
1102 jack_nframes_t offset = 0;
1106 /* Make the use of a Location atomic for this read operation.
1108 Note: Locations don't get deleted, so all we care about
1109 when I say "atomic" is that we are always pointing to
1110 the same one and using a start/length values obtained
1114 if ((loc = loop_location) != 0) {
1115 loop_start = loc->start();
1116 loop_end = loc->end();
1117 loop_length = loop_end - loop_start;
1120 /* if we are looping, ensure that the first frame we read is at the correct
1121 position within the loop.
1124 if (loc && start >= loop_end) {
1125 //cerr << "start adjusted from " << start;
1126 start = loop_start + ((start - loop_start) % loop_length);
1127 //cerr << "to " << start << endl;
1129 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1134 /* take any loop into account. we can't read past the end of the loop. */
1136 if (loc && (loop_end - start < cnt)) {
1137 this_read = loop_end - start;
1138 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1145 if (this_read == 0) {
1149 this_read = min(cnt,this_read);
1151 if (_playlist->read (buf+offset, mixdown_buffer, gain_buffer, workbuf, start, this_read, channel) != this_read) {
1152 error << string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1157 _read_data_count = _playlist->read_data_count();
1161 /* don't adjust start, since caller has already done that
1164 swap_by_ptr (buf, buf + this_read - 1);
1168 /* if we read to the end of the loop, go back to the beginning */
1178 offset += this_read;
1185 AudioDiskstream::do_refill (Sample* mixdown_buffer, float* gain_buffer, char * workbuf)
1188 jack_nframes_t to_read;
1189 RingBufferNPT<Sample>::rw_vector vector;
1193 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1194 jack_nframes_t total_space;
1195 jack_nframes_t zero_fill;
1197 ChannelList::iterator i;
1200 channels.front().playback_buf->get_write_vector (&vector);
1202 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1206 /* if there are 2+ chunks of disk i/o possible for
1207 this track, let the caller know so that it can arrange
1208 for us to be called again, ASAP.
1211 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1215 /* if we're running close to normal speed and there isn't enough
1216 space to do disk_io_chunk_frames of I/O, then don't bother.
1218 at higher speeds, just do it because the sync between butler
1219 and audio thread may not be good enough.
1222 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1226 /* when slaved, don't try to get too close to the read pointer. this
1227 leaves space for the buffer reversal to have something useful to
1231 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1235 total_space = min (disk_io_chunk_frames, total_space);
1239 if (file_frame == 0) {
1241 /* at start: nothing to do but fill with silence */
1243 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1245 ChannelInfo& chan (*i);
1246 chan.playback_buf->get_write_vector (&vector);
1247 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1248 if (vector.len[1]) {
1249 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1251 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1256 if (file_frame < total_space) {
1258 /* too close to the start: read what we can,
1259 and then zero fill the rest
1262 zero_fill = total_space - file_frame;
1263 total_space = file_frame;
1268 /* move read position backwards because we are going
1269 to reverse the data.
1272 file_frame -= total_space;
1278 if (file_frame == max_frames) {
1280 /* at end: nothing to do but fill with silence */
1282 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1284 ChannelInfo& chan (*i);
1285 chan.playback_buf->get_write_vector (&vector);
1286 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1287 if (vector.len[1]) {
1288 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1290 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1295 if (file_frame > max_frames - total_space) {
1297 /* to close to the end: read what we can, and zero fill the rest */
1299 zero_fill = total_space - (max_frames - file_frame);
1300 total_space = max_frames - file_frame;
1307 /* Please note: the code to allocate buffers isn't run
1308 during normal butler thread operation. Its there
1309 for other times when we need to call do_refill()
1310 from somewhere other than the butler thread.
1313 if (mixdown_buffer == 0) {
1314 mixdown_buffer = new Sample[disk_io_chunk_frames];
1315 free_mixdown = true;
1317 free_mixdown = false;
1320 if (gain_buffer == 0) {
1321 gain_buffer = new float[disk_io_chunk_frames];
1328 workbuf = new char[disk_io_chunk_frames * 4];
1329 free_workbuf = true;
1331 free_workbuf = false;
1334 jack_nframes_t file_frame_tmp = 0;
1336 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1338 ChannelInfo& chan (*i);
1341 jack_nframes_t len1, len2;
1343 chan.playback_buf->get_write_vector (&vector);
1346 file_frame_tmp = file_frame;
1349 buf1 = vector.buf[1];
1350 len1 = vector.len[1];
1351 buf2 = vector.buf[0];
1352 len2 = vector.len[0];
1354 buf1 = vector.buf[0];
1355 len1 = vector.len[0];
1356 buf2 = vector.buf[1];
1357 len2 = vector.len[1];
1361 to_read = min (ts, len1);
1362 to_read = min (to_read, disk_io_chunk_frames);
1366 if (read (buf1, mixdown_buffer, gain_buffer, workbuf, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1371 chan.playback_buf->increment_write_ptr (to_read);
1375 to_read = min (ts, len2);
1380 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1381 so read some or all of vector.len[1] as well.
1384 if (read (buf2, mixdown_buffer, gain_buffer, workbuf, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1389 chan.playback_buf->increment_write_ptr (to_read);
1398 file_frame = file_frame_tmp;
1402 delete [] mixdown_buffer;
1405 delete [] gain_buffer;
1415 AudioDiskstream::do_flush (char * workbuf, bool force_flush)
1419 RingBufferNPT<Sample>::rw_vector vector;
1420 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1421 jack_nframes_t total;
1423 /* important note: this function will write *AT MOST*
1424 disk_io_chunk_frames of data to disk. it will never
1425 write more than that. if its writes that much and there
1426 is more than that waiting to be written, it will return 1,
1427 otherwise 0 on success or -1 on failure.
1429 if there is less than disk_io_chunk_frames to be written,
1430 no data will be written at all unless `force_flush' is true.
1433 _write_data_count = 0;
1435 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1437 (*chan).capture_buf->get_read_vector (&vector);
1439 total = vector.len[0] + vector.len[1];
1442 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1447 /* if there are 2+ chunks of disk i/o possible for
1448 this track, let the caller know so that it can arrange
1449 for us to be called again, ASAP.
1451 if we are forcing a flush, then if there is* any* extra
1452 work, let the caller know.
1454 if we are no longer recording and there is any extra work,
1455 let the caller know too.
1458 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1462 to_write = min (disk_io_chunk_frames, (jack_nframes_t) vector.len[0]);
1464 // check the transition buffer when recording destructive
1465 // important that we get this after the capture buf
1467 if (destructive()) {
1468 (*chan).capture_transition_buf->get_read_vector(&transvec);
1469 size_t transcount = transvec.len[0] + transvec.len[1];
1470 bool have_start = false;
1473 for (ti=0; ti < transcount; ++ti) {
1474 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1476 if (captrans.type == CaptureStart) {
1477 // by definition, the first data we got above represents the given capture pos
1479 (*chan).write_source->mark_capture_start (captrans.capture_val);
1480 (*chan).curr_capture_cnt = 0;
1484 else if (captrans.type == CaptureEnd) {
1486 // capture end, the capture_val represents total frames in capture
1488 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1490 // shorten to make the write a perfect fit
1491 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1493 if (nto_write < to_write) {
1494 ret = 1; // should we?
1496 to_write = nto_write;
1498 (*chan).write_source->mark_capture_end ();
1500 // increment past this transition, but go no further
1505 // actually ends just beyond this chunk, so force more work
1513 (*chan).capture_transition_buf->increment_read_ptr(ti);
1517 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write, workbuf) != to_write) {
1518 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1522 (*chan).capture_buf->increment_read_ptr (to_write);
1523 (*chan).curr_capture_cnt += to_write;
1525 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1527 /* we wrote all of vector.len[0] but it wasn't an entire
1528 disk_io_chunk_frames of data, so arrange for some part
1529 of vector.len[1] to be flushed to disk as well.
1532 to_write = min ((jack_nframes_t)(disk_io_chunk_frames - to_write), (jack_nframes_t) vector.len[1]);
1534 if ((*chan).write_source->write (vector.buf[1], to_write, workbuf) != to_write) {
1535 error << string_compose(_("AudioDiskstream %1: cannot write to disk"), _id) << endmsg;
1539 _write_data_count += (*chan).write_source->write_data_count();
1541 (*chan).capture_buf->increment_read_ptr (to_write);
1542 (*chan).curr_capture_cnt += to_write;
1551 AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1553 uint32_t buffer_position;
1554 bool more_work = true;
1556 AudioRegion* region = 0;
1557 jack_nframes_t total_capture;
1558 AudioRegion::SourceList srcs;
1559 AudioRegion::SourceList::iterator src;
1560 ChannelList::iterator chan;
1561 vector<CaptureInfo*>::iterator ci;
1563 list<AudioFileSource*>* deletion_list;
1564 bool mark_write_completed = false;
1566 finish_capture (true);
1568 /* butler is already stopped, but there may be work to do
1569 to flush remaining data to disk.
1572 while (more_work && !err) {
1573 switch (do_flush ( _session.conversion_buffer(Session::TransportContext), true)) {
1580 error << string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1585 /* XXX is there anything we can do if err != 0 ? */
1586 Glib::Mutex::Lock lm (capture_info_lock);
1588 if (capture_info.empty()) {
1592 if (abort_capture) {
1594 ChannelList::iterator chan;
1596 deletion_list = new list<AudioFileSource*>;
1598 for ( chan = channels.begin(); chan != channels.end(); ++chan) {
1600 if ((*chan).write_source) {
1602 (*chan).write_source->mark_for_remove ();
1603 (*chan).write_source->release ();
1605 deletion_list->push_back ((*chan).write_source);
1607 (*chan).write_source = 0;
1610 /* new source set up in "out" below */
1613 if (!deletion_list->empty()) {
1614 DeleteSources (deletion_list);
1616 delete deletion_list;
1622 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1623 total_capture += (*ci)->frames;
1626 /* figure out the name for this take */
1628 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1630 AudioFileSource* s = (*chan).write_source;
1634 AudioFileSource* fsrc;
1638 if ((fsrc = dynamic_cast<AudioFileSource *>(s)) != 0) {
1639 cerr << "updating source after capture\n";
1640 fsrc->update_header (capture_info.front()->start, when, twhen);
1643 s->set_captured_for (_name);
1648 /* destructive tracks have a single, never changing region */
1650 if (destructive()) {
1652 /* send a signal that any UI can pick up to do the right thing. there is
1653 a small problem here in that a UI may need the peak data to be ready
1654 for the data that was recorded and this isn't interlocked with that
1655 process. this problem is deferred to the UI.
1658 _playlist->Modified();
1662 /* Register a new region with the Session that
1663 describes the entire source. Do this first
1664 so that any sub-regions will obviously be
1665 children of this one (later!)
1669 region = new AudioRegion (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1670 region_name_from_path (channels[0].write_source->name()),
1671 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
1673 region->special_set_position (capture_info.front()->start);
1677 catch (failed_constructor& err) {
1678 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1682 _last_capture_regions.push_back (region);
1684 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1686 _session.add_undo (_playlist->get_memento());
1687 _playlist->freeze ();
1689 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1692 _session.region_name (region_name, channels[0].write_source->name(), false);
1694 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1697 region = new AudioRegion (srcs, buffer_position, (*ci)->frames, region_name);
1700 catch (failed_constructor& err) {
1701 error << _("AudioDiskstream: could not create region for captured audio!") << endmsg;
1702 continue; /* XXX is this OK? */
1705 _last_capture_regions.push_back (region);
1707 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1709 i_am_the_modifier++;
1710 _playlist->add_region (*region, (*ci)->start);
1711 i_am_the_modifier--;
1713 buffer_position += (*ci)->frames;
1717 _session.add_redo_no_execute (_playlist->get_memento());
1720 mark_write_completed = true;
1722 reset_write_sources (mark_write_completed);
1725 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1729 capture_info.clear ();
1730 capture_start_frame = 0;
1734 AudioDiskstream::finish_capture (bool rec_monitors_input)
1736 was_recording = false;
1738 if (capture_captured == 0) {
1742 if (recordable() && destructive()) {
1743 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1745 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1746 (*chan).capture_transition_buf->get_write_vector(&transvec);
1749 if (transvec.len[0] > 0) {
1750 transvec.buf[0]->type = CaptureEnd;
1751 transvec.buf[0]->capture_val = capture_captured;
1752 (*chan).capture_transition_buf->increment_write_ptr(1);
1756 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1762 CaptureInfo* ci = new CaptureInfo;
1764 ci->start = capture_start_frame;
1765 ci->frames = capture_captured;
1767 /* XXX theoretical race condition here. Need atomic exchange ?
1768 However, the circumstances when this is called right
1769 now (either on record-disable or transport_stopped)
1770 mean that no actual race exists. I think ...
1771 We now have a capture_info_lock, but it is only to be used
1772 to synchronize in the transport_stop and the capture info
1773 accessors, so that invalidation will not occur (both non-realtime).
1776 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1778 capture_info.push_back (ci);
1779 capture_captured = 0;
1783 AudioDiskstream::set_record_enabled (bool yn, void* src)
1785 if (!recordable() || !_session.record_enabling_legal()) {
1789 /* can't rec-enable in destructive mode if transport is before start */
1791 if (destructive() && yn && _session.transport_frame() < _session.current_start_frame()) {
1795 if (yn && channels[0].source == 0) {
1797 /* pick up connections not initiated *from* the IO object
1798 we're associated with.
1801 get_input_sources ();
1804 /* yes, i know that this not proof against race conditions, but its
1805 good enough. i think.
1808 if (record_enabled() != yn) {
1810 engage_record_enable (src);
1812 disengage_record_enable (src);
1818 AudioDiskstream::engage_record_enable (void* src)
1820 bool rolling = _session.transport_speed() != 0.0f;
1822 g_atomic_int_set (&_record_enabled, 1);
1823 capturing_sources.clear ();
1824 if (Config->get_use_hardware_monitoring()) {
1825 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1826 if ((*chan).source) {
1827 (*chan).source->request_monitor_input (!(_session.get_auto_input() && rolling));
1829 capturing_sources.push_back ((*chan).write_source);
1832 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1833 capturing_sources.push_back ((*chan).write_source);
1837 RecordEnableChanged (src); /* EMIT SIGNAL */
1841 AudioDiskstream::disengage_record_enable (void* src)
1843 g_atomic_int_set (&_record_enabled, 0);
1844 if (Config->get_use_hardware_monitoring()) {
1845 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1846 if ((*chan).source) {
1847 (*chan).source->request_monitor_input (false);
1851 capturing_sources.clear ();
1852 RecordEnableChanged (src); /* EMIT SIGNAL */
1857 AudioDiskstream::get_state ()
1859 XMLNode* node = new XMLNode ("AudioDiskstream");
1861 LocaleGuard lg (X_("POSIX"));
1863 snprintf (buf, sizeof(buf), "0x%x", _flags);
1864 node->add_property ("flags", buf);
1866 snprintf (buf, sizeof(buf), "%zd", channels.size());
1867 node->add_property ("channels", buf);
1869 node->add_property ("playlist", _playlist->name());
1871 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1872 node->add_property ("speed", buf);
1874 node->add_property("name", _name);
1876 node->add_property("id", buf);
1878 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1880 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1881 XMLNode* cs_grandchild;
1883 for (vector<AudioFileSource*>::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1884 cs_grandchild = new XMLNode (X_("file"));
1885 cs_grandchild->add_property (X_("path"), (*i)->path());
1886 cs_child->add_child_nocopy (*cs_grandchild);
1889 /* store the location where capture will start */
1893 if (_session.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1894 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
1896 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
1899 cs_child->add_property (X_("at"), buf);
1900 node->add_child_nocopy (*cs_child);
1904 node->add_child_copy (*_extra_xml);
1911 AudioDiskstream::set_state (const XMLNode& node)
1913 const XMLProperty* prop;
1914 XMLNodeList nlist = node.children();
1915 XMLNodeIterator niter;
1916 uint32_t nchans = 1;
1917 XMLNode* capture_pending_node = 0;
1918 LocaleGuard lg (X_("POSIX"));
1920 in_set_state = true;
1922 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1923 if ((*niter)->name() == IO::state_node_name) {
1924 deprecated_io_node = new XMLNode (**niter);
1927 if ((*niter)->name() == X_("CapturingSources")) {
1928 capture_pending_node = *niter;
1932 /* prevent write sources from being created */
1934 in_set_state = true;
1936 if ((prop = node.property ("name")) != 0) {
1937 _name = prop->value();
1940 if (deprecated_io_node) {
1941 if ((prop = deprecated_io_node->property ("id")) != 0) {
1942 _id = prop->value ();
1945 if ((prop = node.property ("id")) != 0) {
1946 _id = prop->value ();
1950 if ((prop = node.property ("flags")) != 0) {
1951 _flags = strtol (prop->value().c_str(), 0, 0);
1954 if ((prop = node.property ("channels")) != 0) {
1955 nchans = atoi (prop->value().c_str());
1958 // create necessary extra channels
1959 // we are always constructed with one
1960 // and we always need one
1962 if (nchans > _n_channels) {
1964 // we need to add new channel infos
1965 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1967 int diff = nchans - channels.size();
1969 for (int i=0; i < diff; ++i) {
1973 } else if (nchans < _n_channels) {
1975 // we need to get rid of channels
1976 //LockMonitor lm (state_lock, __LINE__, __FILE__);
1978 int diff = channels.size() - nchans;
1980 for (int i = 0; i < diff; ++i) {
1985 if ((prop = node.property ("playlist")) == 0) {
1990 bool had_playlist = (_playlist != 0);
1992 if (find_and_use_playlist (prop->value())) {
1996 if (!had_playlist) {
1997 _playlist->set_orig_diskstream_id (_id);
2000 if (!destructive() && capture_pending_node) {
2001 /* destructive streams have one and only one source per channel,
2002 and so they never end up in pending capture in any useful
2005 use_pending_capture_data (*capture_pending_node);
2010 if ((prop = node.property ("speed")) != 0) {
2011 double sp = atof (prop->value().c_str());
2013 if (realtime_set_speed (sp, false)) {
2014 non_realtime_set_speed ();
2018 _n_channels = channels.size();
2020 in_set_state = false;
2022 /* make sure this is clear before we do anything else */
2024 capturing_sources.clear ();
2026 /* write sources are handled when we handle the input set
2027 up of the IO that owns this DS (::non_realtime_input_change())
2030 in_set_state = false;
2036 AudioDiskstream::use_new_write_source (uint32_t n)
2038 if (!recordable()) {
2042 if (n >= channels.size()) {
2043 error << string_compose (_("AudioDiskstream: channel %1 out of range"), n) << endmsg;
2047 ChannelInfo &chan = channels[n];
2049 if (chan.write_source) {
2051 if (AudioFileSource::is_empty (chan.write_source->path())) {
2052 chan.write_source->mark_for_remove ();
2053 chan.write_source->release();
2054 delete chan.write_source;
2056 chan.write_source->release();
2057 chan.write_source = 0;
2062 if ((chan.write_source = _session.create_audio_source_for_session (*this, n, destructive())) == 0) {
2063 throw failed_constructor();
2067 catch (failed_constructor &err) {
2068 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
2069 chan.write_source = 0;
2073 chan.write_source->use ();
2075 /* do not remove destructive files even if they are empty */
2077 chan.write_source->set_allow_remove_if_empty (!destructive());
2083 AudioDiskstream::reset_write_sources (bool mark_write_complete, bool force)
2085 ChannelList::iterator chan;
2088 if (!recordable()) {
2092 capturing_sources.clear ();
2094 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2095 if (!destructive()) {
2097 if ((*chan).write_source && mark_write_complete) {
2098 (*chan).write_source->mark_streaming_write_completed ();
2100 use_new_write_source (n);
2102 if (record_enabled()) {
2103 capturing_sources.push_back ((*chan).write_source);
2107 if ((*chan).write_source == 0) {
2108 use_new_write_source (n);
2113 if (destructive()) {
2115 /* we now have all our write sources set up, so create the
2116 playlist's single region.
2119 if (_playlist->empty()) {
2120 setup_destructive_playlist ();
2126 AudioDiskstream::rename_write_sources ()
2128 ChannelList::iterator chan;
2131 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2132 if ((*chan).write_source != 0) {
2133 (*chan).write_source->set_name (_name, destructive());
2134 /* XXX what to do if one of them fails ? */
2142 AudioDiskstream::set_block_size (jack_nframes_t nframes)
2144 if (_session.get_block_size() > speed_buffer_size) {
2145 speed_buffer_size = _session.get_block_size();
2147 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2148 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2149 (*chan).speed_buffer = new Sample[speed_buffer_size];
2152 allocate_temporary_buffers ();
2156 AudioDiskstream::allocate_temporary_buffers ()
2158 /* make sure the wrap buffer is at least large enough to deal
2159 with the speeds up to 1.2, to allow for micro-variation
2160 when slaving to MTC, SMPTE etc.
2163 double sp = max (fabsf (_actual_speed), 1.2f);
2164 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * sp) + 1;
2166 if (required_wrap_size > wrap_buffer_size) {
2168 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2169 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2170 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2171 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2172 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2175 wrap_buffer_size = required_wrap_size;
2180 AudioDiskstream::monitor_input (bool yn)
2182 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2184 if ((*chan).source) {
2185 (*chan).source->request_monitor_input (yn);
2191 AudioDiskstream::set_align_style_from_io ()
2193 bool have_physical = false;
2199 get_input_sources ();
2201 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2202 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2203 have_physical = true;
2208 if (have_physical) {
2209 set_align_style (ExistingMaterial);
2211 set_align_style (CaptureTime);
2216 AudioDiskstream::add_channel ()
2218 /* XXX need to take lock??? */
2222 init_channel (chan);
2224 chan.speed_buffer = new Sample[speed_buffer_size];
2225 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2226 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2228 channels.push_back (chan);
2230 _n_channels = channels.size();
2236 AudioDiskstream::remove_channel ()
2238 if (channels.size() > 1) {
2239 /* XXX need to take lock??? */
2240 ChannelInfo & chan = channels.back();
2241 destroy_channel (chan);
2242 channels.pop_back();
2244 _n_channels = channels.size();
2252 AudioDiskstream::playback_buffer_load () const
2254 return (float) ((double) channels.front().playback_buf->read_space()/
2255 (double) channels.front().playback_buf->bufsize());
2259 AudioDiskstream::capture_buffer_load () const
2261 return (float) ((double) channels.front().capture_buf->write_space()/
2262 (double) channels.front().capture_buf->bufsize());
2266 AudioDiskstream::set_loop (Location *location)
2269 if (location->start() >= location->end()) {
2270 error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
2275 loop_location = location;
2277 LoopSet (location); /* EMIT SIGNAL */
2282 AudioDiskstream::use_pending_capture_data (XMLNode& node)
2284 const XMLProperty* prop;
2285 XMLNodeList nlist = node.children();
2286 XMLNodeIterator niter;
2287 AudioFileSource* fs;
2288 AudioFileSource* first_fs = 0;
2289 AudioRegion::SourceList pending_sources;
2290 jack_nframes_t position;
2292 if ((prop = node.property (X_("at"))) == 0) {
2296 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2300 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2301 if ((*niter)->name() == X_("file")) {
2303 if ((prop = (*niter)->property (X_("path"))) == 0) {
2308 fs = new SndFileSource (prop->value(),
2309 Config->get_native_file_data_format(),
2310 Config->get_native_file_header_format(),
2311 _session.frame_rate());
2314 catch (failed_constructor& err) {
2315 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2316 _name, prop->value())
2321 pending_sources.push_back (fs);
2323 if (first_fs == 0) {
2327 fs->set_captured_for (_name);
2331 if (pending_sources.size() == 0) {
2332 /* nothing can be done */
2336 if (pending_sources.size() != _n_channels) {
2337 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2342 AudioRegion* region;
2345 region = new AudioRegion (pending_sources, 0, first_fs->length(),
2346 region_name_from_path (first_fs->name()),
2347 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
2349 region->special_set_position (0);
2352 catch (failed_constructor& err) {
2353 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2361 region = new AudioRegion (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name()));
2364 catch (failed_constructor& err) {
2365 error << string_compose (_("%1: cannot create region from pending capture sources"),
2372 _playlist->add_region (*region, position);