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.
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/diskstream.h>
42 #include <ardour/utils.h>
43 #include <ardour/configuration.h>
44 #include <ardour/filesource.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;
57 jack_nframes_t DiskStream::disk_io_chunk_frames;
59 sigc::signal<void,DiskStream*> DiskStream::DiskStreamCreated;
60 sigc::signal<void,list<Source*>*> DiskStream::DeleteSources;
61 sigc::signal<void> DiskStream::DiskOverrun;
62 sigc::signal<void> DiskStream::DiskUnderrun;
64 DiskStream::DiskStream (Session &sess, const string &name, Flag flag)
68 /* prevent any write sources from being created */
77 DiskStreamCreated (this); /* EMIT SIGNAL */
80 DiskStream::DiskStream (Session& sess, const XMLNode& node)
87 if (set_state (node)) {
89 throw failed_constructor();
95 use_destructive_playlist ();
98 DiskStreamCreated (this); /* EMIT SIGNAL */
102 DiskStream::init_channel (ChannelInfo &chan)
104 chan.playback_wrap_buffer = 0;
105 chan.capture_wrap_buffer = 0;
106 chan.speed_buffer = 0;
107 chan.peak_power = 0.0f;
108 chan.write_source = 0;
110 chan.current_capture_buffer = 0;
111 chan.current_playback_buffer = 0;
112 chan.curr_capture_cnt = 0;
114 chan.playback_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
115 chan.capture_buf = new RingBufferNPT<Sample> (_session.diskstream_buffer_size());
116 chan.capture_transition_buf = new RingBufferNPT<CaptureTransition> (128);
119 /* touch the ringbuffer buffers, which will cause
120 them to be mapped into locked physical RAM if
121 we're running with mlockall(). this doesn't do
124 memset (chan.playback_buf->buffer(), 0, sizeof (Sample) * chan.playback_buf->bufsize());
125 memset (chan.capture_buf->buffer(), 0, sizeof (Sample) * chan.capture_buf->bufsize());
126 memset (chan.capture_transition_buf->buffer(), 0, sizeof (CaptureTransition) * chan.capture_transition_buf->bufsize());
131 DiskStream::init (Flag f)
137 _alignment_style = ExistingMaterial;
138 _persistent_alignment_style = ExistingMaterial;
139 first_input_change = true;
141 i_am_the_modifier = 0;
142 g_atomic_int_set (&_record_enabled, 0);
143 was_recording = false;
144 capture_start_frame = 0;
145 capture_captured = 0;
146 _visible_speed = 1.0f;
147 _actual_speed = 1.0f;
148 _buffer_reallocation_required = false;
149 _seek_required = false;
150 first_recordable_frame = max_frames;
151 last_recordable_frame = max_frames;
156 adjust_capture_position = 0;
157 last_possibly_recording = 0;
159 wrap_buffer_size = 0;
160 speed_buffer_size = 0;
162 phi = (uint64_t) (0x1000000);
165 playback_distance = 0;
166 _read_data_count = 0;
167 _write_data_count = 0;
168 deprecated_io_node = 0;
170 /* there are no channels at this point, so these
171 two calls just get speed_buffer_size and wrap_buffer
172 size setup without duplicating their code.
175 set_block_size (_session.get_block_size());
176 allocate_temporary_buffers ();
178 pending_overwrite = false;
180 overwrite_queued = false;
181 input_change_pending = NoChange;
188 DiskStream::destroy_channel (ChannelInfo &chan)
190 if (chan.write_source) {
191 chan.write_source->release ();
192 chan.write_source = 0;
195 if (chan.speed_buffer) {
196 delete [] chan.speed_buffer;
199 if (chan.playback_wrap_buffer) {
200 delete [] chan.playback_wrap_buffer;
202 if (chan.capture_wrap_buffer) {
203 delete [] chan.capture_wrap_buffer;
206 delete chan.playback_buf;
207 delete chan.capture_buf;
208 delete chan.capture_transition_buf;
210 chan.playback_buf = 0;
211 chan.capture_buf = 0;
214 DiskStream::~DiskStream ()
216 Glib::Mutex::Lock lm (state_lock);
222 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
223 destroy_channel((*chan));
230 DiskStream::handle_input_change (IOChange change, void *src)
232 Glib::Mutex::Lock lm (state_lock);
234 if (!(input_change_pending & change)) {
235 input_change_pending = IOChange (input_change_pending|change);
236 _session.request_input_change_handling ();
241 DiskStream::non_realtime_input_change ()
244 Glib::Mutex::Lock lm (state_lock);
246 if (input_change_pending == NoChange) {
250 if (input_change_pending & ConfigurationChanged) {
252 if (_io->n_inputs() > _n_channels) {
254 // we need to add new channel infos
256 int diff = _io->n_inputs() - channels.size();
258 for (int i = 0; i < diff; ++i) {
262 } else if (_io->n_inputs() < _n_channels) {
264 // we need to get rid of channels
266 int diff = channels.size() - _io->n_inputs();
268 for (int i = 0; i < diff; ++i) {
274 get_input_sources ();
275 set_capture_offset ();
277 if (first_input_change) {
278 set_align_style (_persistent_alignment_style);
279 first_input_change = false;
281 set_align_style_from_io ();
284 input_change_pending = NoChange;
287 /* reset capture files */
289 reset_write_sources (false);
291 /* now refill channel buffers */
293 if (speed() != 1.0f || speed() != -1.0f) {
294 seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()));
297 seek (_session.transport_frame());
302 DiskStream::get_input_sources ()
304 uint32_t ni = _io->n_inputs();
306 for (uint32_t n = 0; n < ni; ++n) {
308 const char **connections = _io->input(n)->get_connections ();
309 ChannelInfo& chan = channels[n];
311 if (connections == 0 || connections[0] == 0) {
314 // _source->disable_metering ();
320 chan.source = _session.engine().get_port_by_name (connections[0]);
330 DiskStream::find_and_use_playlist (const string& name)
333 AudioPlaylist* playlist;
335 if ((pl = _session.get_playlist (name)) == 0) {
336 error << string_compose(_("DiskStream: Session doesn't know about a Playlist called \"%1\""), name) << endmsg;
340 if ((playlist = dynamic_cast<AudioPlaylist*> (pl)) == 0) {
341 error << string_compose(_("DiskStream: Playlist \"%1\" isn't an audio playlist"), name) << endmsg;
345 return use_playlist (playlist);
349 DiskStream::use_playlist (AudioPlaylist* playlist)
352 Glib::Mutex::Lock lm (state_lock);
354 if (playlist == _playlist) {
358 plstate_connection.disconnect();
359 plmod_connection.disconnect ();
360 plgone_connection.disconnect ();
366 _playlist = playlist;
369 if (!in_set_state && recordable()) {
370 reset_write_sources (false);
373 plstate_connection = _playlist->StateChanged.connect (mem_fun (*this, &DiskStream::playlist_changed));
374 plmod_connection = _playlist->Modified.connect (mem_fun (*this, &DiskStream::playlist_modified));
375 plgone_connection = _playlist->GoingAway.connect (mem_fun (*this, &DiskStream::playlist_deleted));
378 if (!overwrite_queued) {
379 _session.request_overwrite_buffer (this);
380 overwrite_queued = true;
383 PlaylistChanged (); /* EMIT SIGNAL */
384 _session.set_dirty ();
390 DiskStream::playlist_deleted (Playlist* pl)
392 /* this catches an ordering issue with session destruction. playlists
393 are destroyed before diskstreams. we have to invalidate any handles
394 we have to the playlist.
401 DiskStream::use_new_playlist ()
404 AudioPlaylist* playlist;
406 if (!in_set_state && destructive()) {
411 newname = Playlist::bump_name (_playlist->name(), _session);
413 newname = Playlist::bump_name (_name, _session);
416 if ((playlist = new AudioPlaylist (_session, newname, hidden())) != 0) {
417 playlist->set_orig_diskstream_id (id());
418 return use_playlist (playlist);
425 DiskStream::use_copy_playlist ()
431 if (_playlist == 0) {
432 error << string_compose(_("DiskStream %1: there is no existing playlist to make a copy of!"), _name) << endmsg;
437 AudioPlaylist* playlist;
439 newname = Playlist::bump_name (_playlist->name(), _session);
441 if ((playlist = new AudioPlaylist (*_playlist, newname)) != 0) {
442 playlist->set_orig_diskstream_id (id());
443 return use_playlist (playlist);
450 DiskStream::setup_destructive_playlist ()
452 AudioRegion::SourceList srcs;
454 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
455 srcs.push_back ((*chan).write_source);
458 /* a single full-sized region */
460 AudioRegion* region = new AudioRegion (srcs, 0, max_frames, _name);
461 _playlist->add_region (*region, 0);
465 DiskStream::use_destructive_playlist ()
467 /* use the sources associated with the single full-extent region */
469 Playlist::RegionList* rl = _playlist->regions_at (0);
472 reset_write_sources (false, true);
476 AudioRegion* region = dynamic_cast<AudioRegion*> (rl->front());
479 throw failed_constructor();
485 ChannelList::iterator chan;
487 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
488 (*chan).write_source = dynamic_cast<FileSource*>(®ion->source (n));
489 (*chan).write_source->set_allow_remove_if_empty (false);
492 /* the source list will never be reset for a destructive track */
496 DiskStream::set_io (IO& io)
499 set_align_style_from_io ();
503 DiskStream::set_name (string str, void *src)
506 _playlist->set_name (str);
509 if (!in_set_state && recordable()) {
510 /* rename existing capture files so that they have the correct name */
511 return rename_write_sources ();
521 DiskStream::set_speed (double sp)
523 _session.request_diskstream_speed (*this, sp);
525 /* to force a rebuffering at the right place */
530 DiskStream::realtime_set_speed (double sp, bool global)
532 bool changed = false;
533 double new_speed = sp * _session.transport_speed();
535 if (_visible_speed != sp) {
540 if (new_speed != _actual_speed) {
542 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() *
543 fabs (new_speed)) + 1;
545 if (required_wrap_size > wrap_buffer_size) {
546 _buffer_reallocation_required = true;
549 _actual_speed = new_speed;
550 phi = (uint64_t) (0x1000000 * fabs(_actual_speed));
555 _seek_required = true;
557 speed_changed (); /* EMIT SIGNAL */
560 return _buffer_reallocation_required || _seek_required;
564 DiskStream::non_realtime_set_speed ()
566 if (_buffer_reallocation_required)
568 Glib::Mutex::Lock lm (state_lock);
569 allocate_temporary_buffers ();
571 _buffer_reallocation_required = false;
574 if (_seek_required) {
575 if (speed() != 1.0f || speed() != -1.0f) {
576 seek ((jack_nframes_t) (_session.transport_frame() * (double) speed()), true);
579 seek (_session.transport_frame(), true);
582 _seek_required = false;
587 DiskStream::prepare ()
590 playback_distance = 0;
594 DiskStream::check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record)
596 int possibly_recording;
599 const int transport_rolling = 0x4;
600 const int track_rec_enabled = 0x2;
601 const int global_rec_enabled = 0x1;
603 /* merge together the 3 factors that affect record status, and compute
607 rolling = _session.transport_speed() != 0.0f;
608 possibly_recording = (rolling << 2) | (record_enabled() << 1) | can_record;
609 change = possibly_recording ^ last_possibly_recording;
611 if (possibly_recording == last_possibly_recording) {
617 /* if per-track or global rec-enable turned on while the other was already on, we've started recording */
619 if ((change & track_rec_enabled) && record_enabled() && (!(change & global_rec_enabled) && can_record) ||
620 ((change & global_rec_enabled) && can_record && (!(change & track_rec_enabled) && record_enabled()))) {
622 /* starting to record: compute first+last frames */
624 first_recordable_frame = transport_frame + _capture_offset;
625 last_recordable_frame = max_frames;
626 capture_start_frame = transport_frame;
628 if (!(last_possibly_recording & transport_rolling) && (possibly_recording & transport_rolling)) {
630 /* was stopped, now rolling (and recording) */
632 if (_alignment_style == ExistingMaterial) {
633 first_recordable_frame += _session.worst_output_latency();
635 first_recordable_frame += _roll_delay;
640 /* was rolling, but record state changed */
642 if (_alignment_style == ExistingMaterial) {
645 if (!_session.get_punch_in()) {
647 /* manual punch in happens at the correct transport frame
648 because the user hit a button. but to get alignment correct
649 we have to back up the position of the new region to the
650 appropriate spot given the roll delay.
653 capture_start_frame -= _roll_delay;
655 /* XXX paul notes (august 2005): i don't know why
659 first_recordable_frame += _capture_offset;
663 /* autopunch toggles recording at the precise
664 transport frame, and then the DS waits
665 to start recording for a time that depends
666 on the output latency.
669 first_recordable_frame += _session.worst_output_latency();
674 if (_session.get_punch_in()) {
675 first_recordable_frame += _roll_delay;
677 capture_start_frame -= _roll_delay;
683 if (_flags & Recordable) {
684 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
686 RingBufferNPT<CaptureTransition>::rw_vector transvec;
687 (*chan).capture_transition_buf->get_write_vector(&transvec);
689 if (transvec.len[0] > 0) {
690 transvec.buf[0]->type = CaptureStart;
691 transvec.buf[0]->capture_val = capture_start_frame;
692 (*chan).capture_transition_buf->increment_write_ptr(1);
696 fatal << X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
702 } else if (!record_enabled() || !can_record) {
706 last_recordable_frame = transport_frame + _capture_offset;
708 if (_alignment_style == ExistingMaterial) {
709 last_recordable_frame += _session.worst_output_latency();
711 last_recordable_frame += _roll_delay;
715 last_possibly_recording = possibly_recording;
719 DiskStream::process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input)
722 ChannelList::iterator c;
724 jack_nframes_t rec_offset = 0;
725 jack_nframes_t rec_nframes = 0;
726 bool nominally_recording;
727 bool re = record_enabled ();
728 bool collect_playback = false;
730 /* if we've already processed the frames corresponding to this call,
731 just return. this allows multiple routes that are taking input
732 from this diskstream to call our ::process() method, but have
733 this stuff only happen once. more commonly, it allows both
734 the AudioTrack that is using this DiskStream *and* the Session
735 to call process() without problems.
742 check_record_status (transport_frame, nframes, can_record);
744 nominally_recording = (can_record && re);
751 /* This lock is held until the end of DiskStream::commit, so these two functions
752 must always be called as a pair. The only exception is if this function
753 returns a non-zero value, in which case, ::commit should not be called.
756 // If we can't take the state lock return.
757 if (!state_lock.trylock()) {
761 adjust_capture_position = 0;
763 for (c = channels.begin(); c != channels.end(); ++c) {
764 (*c).current_capture_buffer = 0;
765 (*c).current_playback_buffer = 0;
768 if (nominally_recording || (_session.get_record_enabled() && _session.get_punch_in())) {
771 ot = coverage (first_recordable_frame, last_recordable_frame, transport_frame, transport_frame + nframes);
778 case OverlapInternal:
779 /* ---------- recrange
782 rec_nframes = nframes;
787 /* |--------| recrange
790 rec_nframes = transport_frame + nframes - first_recordable_frame;
792 rec_offset = first_recordable_frame - transport_frame;
797 /* |--------| recrange
800 rec_nframes = last_recordable_frame - transport_frame;
804 case OverlapExternal:
805 /* |--------| recrange
806 -------------- transrange
808 rec_nframes = last_recordable_frame - last_recordable_frame;
809 rec_offset = first_recordable_frame - transport_frame;
813 if (rec_nframes && !was_recording) {
814 capture_captured = 0;
815 was_recording = true;
820 if (can_record && !_last_capture_regions.empty()) {
821 _last_capture_regions.clear ();
824 if (nominally_recording || rec_nframes) {
826 for (n = 0, c = channels.begin(); c != channels.end(); ++c, ++n) {
828 ChannelInfo& chan (*c);
830 chan.capture_buf->get_write_vector (&chan.capture_vector);
832 if (rec_nframes <= chan.capture_vector.len[0]) {
834 chan.current_capture_buffer = chan.capture_vector.buf[0];
836 /* note: grab the entire port buffer, but only copy what we were supposed to for recording, and use
840 memcpy (chan.current_capture_buffer, _io->input(n)->get_buffer (rec_nframes) + offset + rec_offset, sizeof (Sample) * rec_nframes);
844 jack_nframes_t total = chan.capture_vector.len[0] + chan.capture_vector.len[1];
846 if (rec_nframes > total) {
851 Sample* buf = _io->input (n)->get_buffer (nframes) + offset;
852 jack_nframes_t first = chan.capture_vector.len[0];
854 memcpy (chan.capture_wrap_buffer, buf, sizeof (Sample) * first);
855 memcpy (chan.capture_vector.buf[0], buf, sizeof (Sample) * first);
856 memcpy (chan.capture_wrap_buffer+first, buf + first, sizeof (Sample) * (rec_nframes - first));
857 memcpy (chan.capture_vector.buf[1], buf + first, sizeof (Sample) * (rec_nframes - first));
859 chan.current_capture_buffer = chan.capture_wrap_buffer;
866 finish_capture (rec_monitors_input);
873 /* data will be written to disk */
875 if (rec_nframes == nframes && rec_offset == 0) {
877 for (c = channels.begin(); c != channels.end(); ++c) {
878 (*c).current_playback_buffer = (*c).current_capture_buffer;
881 playback_distance = nframes;
886 /* we can't use the capture buffer as the playback buffer, because
887 we recorded only a part of the current process' cycle data
891 collect_playback = true;
894 adjust_capture_position = rec_nframes;
896 } else if (nominally_recording) {
898 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
900 for (c = channels.begin(); c != channels.end(); ++c) {
901 (*c).current_playback_buffer = (*c).current_capture_buffer;
904 playback_distance = nframes;
908 collect_playback = true;
911 if (collect_playback) {
913 /* we're doing playback */
915 jack_nframes_t necessary_samples;
917 /* no varispeed playback if we're recording, because the output .... TBD */
919 if (rec_nframes == 0 && _actual_speed != 1.0f) {
920 necessary_samples = (jack_nframes_t) floor ((nframes * fabs (_actual_speed))) + 1;
922 necessary_samples = nframes;
925 for (c = channels.begin(); c != channels.end(); ++c) {
926 (*c).playback_buf->get_read_vector (&(*c).playback_vector);
931 for (c = channels.begin(); c != channels.end(); ++c, ++n) {
933 ChannelInfo& chan (*c);
935 if (necessary_samples <= chan.playback_vector.len[0]) {
937 chan.current_playback_buffer = chan.playback_vector.buf[0];
940 jack_nframes_t total = chan.playback_vector.len[0] + chan.playback_vector.len[1];
942 if (necessary_samples > total) {
948 memcpy ((char *) chan.playback_wrap_buffer, chan.playback_vector.buf[0],
949 chan.playback_vector.len[0] * sizeof (Sample));
950 memcpy (chan.playback_wrap_buffer + chan.playback_vector.len[0], chan.playback_vector.buf[1],
951 (necessary_samples - chan.playback_vector.len[0]) * sizeof (Sample));
953 chan.current_playback_buffer = chan.playback_wrap_buffer;
958 if (rec_nframes == 0 && _actual_speed != 1.0f && _actual_speed != -1.0f) {
960 uint64_t phase = last_phase;
961 jack_nframes_t i = 0;
963 // Linearly interpolate into the alt buffer
964 // using 40.24 fixp maths (swh)
966 for (c = channels.begin(); c != channels.end(); ++c) {
969 ChannelInfo& chan (*c);
974 for (jack_nframes_t outsample = 0; outsample < nframes; ++outsample) {
976 fr = (phase & 0xFFFFFF) / 16777216.0f;
977 chan.speed_buffer[outsample] =
978 chan.current_playback_buffer[i] * (1.0f - fr) +
979 chan.current_playback_buffer[i+1] * fr;
983 chan.current_playback_buffer = chan.speed_buffer;
986 playback_distance = i + 1;
987 last_phase = (phase & 0xFFFFFF);
990 playback_distance = nframes;
1002 /* we're exiting with failure, so ::commit will not
1003 be called. unlock the state lock.
1006 state_lock.unlock();
1013 DiskStream::recover ()
1015 state_lock.unlock();
1020 DiskStream::commit (jack_nframes_t nframes)
1022 bool need_butler = false;
1024 if (_actual_speed < 0.0) {
1025 playback_sample -= playback_distance;
1027 playback_sample += playback_distance;
1030 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1032 (*chan).playback_buf->increment_read_ptr (playback_distance);
1034 if (adjust_capture_position) {
1035 (*chan).capture_buf->increment_write_ptr (adjust_capture_position);
1039 if (adjust_capture_position != 0) {
1040 capture_captured += adjust_capture_position;
1041 adjust_capture_position = 0;
1045 need_butler = channels[0].playback_buf->write_space() >= channels[0].playback_buf->bufsize() / 2;
1047 need_butler = channels[0].playback_buf->write_space() >= disk_io_chunk_frames
1048 || channels[0].capture_buf->read_space() >= disk_io_chunk_frames;
1051 state_lock.unlock();
1059 DiskStream::set_pending_overwrite (bool yn)
1061 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
1063 pending_overwrite = yn;
1065 overwrite_frame = playback_sample;
1066 overwrite_offset = channels.front().playback_buf->get_read_ptr();
1070 DiskStream::overwrite_existing_buffers ()
1072 Sample* mixdown_buffer;
1076 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1078 overwrite_queued = false;
1080 /* assume all are the same size */
1081 jack_nframes_t size = channels[0].playback_buf->bufsize();
1083 mixdown_buffer = new Sample[size];
1084 gain_buffer = new float[size];
1085 workbuf = new char[size*4];
1087 /* reduce size so that we can fill the buffer correctly. */
1091 jack_nframes_t start;
1093 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1095 start = overwrite_frame;
1096 jack_nframes_t cnt = size;
1098 /* to fill the buffer without resetting the playback sample, we need to
1099 do it one or two chunks (normally two).
1101 |----------------------------------------------------------------------|
1105 |<- second chunk->||<----------------- first chunk ------------------>|
1109 jack_nframes_t to_read = size - overwrite_offset;
1111 if (read ((*chan).playback_buf->buffer() + overwrite_offset, mixdown_buffer, gain_buffer, workbuf,
1112 start, to_read, *chan, n, reversed)) {
1113 error << string_compose(_("DiskStream %1: when refilling, cannot read %2 from playlist at frame %3"),
1114 _id, size, playback_sample) << endmsg;
1118 if (cnt > to_read) {
1122 if (read ((*chan).playback_buf->buffer(), mixdown_buffer, gain_buffer, workbuf,
1123 start, cnt, *chan, n, reversed)) {
1124 error << string_compose(_("DiskStream %1: when refilling, cannot read %2 from playlist at frame %3"),
1125 _id, size, playback_sample) << endmsg;
1134 pending_overwrite = false;
1135 delete [] gain_buffer;
1136 delete [] mixdown_buffer;
1142 DiskStream::seek (jack_nframes_t frame, bool complete_refill)
1144 Glib::Mutex::Lock lm (state_lock);
1147 ChannelList::iterator chan;
1149 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1150 (*chan).playback_buf->reset ();
1151 (*chan).capture_buf->reset ();
1152 if ((*chan).write_source) {
1153 (*chan).write_source->seek (frame);
1157 playback_sample = frame;
1160 if (complete_refill) {
1161 while ((ret = do_refill (0, 0, 0)) > 0);
1163 ret = do_refill (0, 0, 0);
1170 DiskStream::can_internal_playback_seek (jack_nframes_t distance)
1172 ChannelList::iterator chan;
1174 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1175 if ((*chan).playback_buf->read_space() < distance) {
1183 DiskStream::internal_playback_seek (jack_nframes_t distance)
1185 ChannelList::iterator chan;
1187 for (chan = channels.begin(); chan != channels.end(); ++chan) {
1188 (*chan).playback_buf->increment_read_ptr (distance);
1191 first_recordable_frame += distance;
1192 playback_sample += distance;
1198 DiskStream::read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, char * workbuf, jack_nframes_t& start, jack_nframes_t cnt,
1199 ChannelInfo& channel_info, int channel, bool reversed)
1201 jack_nframes_t this_read = 0;
1202 bool reloop = false;
1203 jack_nframes_t loop_end = 0;
1204 jack_nframes_t loop_start = 0;
1205 jack_nframes_t loop_length = 0;
1206 jack_nframes_t offset = 0;
1210 /* Make the use of a Location atomic for this read operation.
1212 Note: Locations don't get deleted, so all we care about
1213 when I say "atomic" is that we are always pointing to
1214 the same one and using a start/length values obtained
1218 if ((loc = loop_location) != 0) {
1219 loop_start = loc->start();
1220 loop_end = loc->end();
1221 loop_length = loop_end - loop_start;
1224 /* if we are looping, ensure that the first frame we read is at the correct
1225 position within the loop.
1228 if (loc && start >= loop_end) {
1229 //cerr << "start adjusted from " << start;
1230 start = loop_start + ((start - loop_start) % loop_length);
1231 //cerr << "to " << start << endl;
1233 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
1238 /* take any loop into account. we can't read past the end of the loop. */
1240 if (loc && (loop_end - start < cnt)) {
1241 this_read = loop_end - start;
1242 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
1249 if (this_read == 0) {
1253 this_read = min(cnt,this_read);
1255 if (_playlist->read (buf+offset, mixdown_buffer, gain_buffer, workbuf, start, this_read, channel) != this_read) {
1256 error << string_compose(_("DiskStream %1: cannot read %2 from playlist at frame %3"), _id, this_read,
1261 _read_data_count = _playlist->read_data_count();
1265 /* don't adjust start, since caller has already done that
1268 swap_by_ptr (buf, buf + this_read - 1);
1272 /* if we read to the end of the loop, go back to the beginning */
1282 offset += this_read;
1289 DiskStream::do_refill (Sample* mixdown_buffer, float* gain_buffer, char * workbuf)
1292 jack_nframes_t to_read;
1293 RingBufferNPT<Sample>::rw_vector vector;
1297 bool reversed = (_visible_speed * _session.transport_speed()) < 0.0f;
1298 jack_nframes_t total_space;
1299 jack_nframes_t zero_fill;
1301 ChannelList::iterator i;
1304 channels.front().playback_buf->get_write_vector (&vector);
1306 if ((total_space = vector.len[0] + vector.len[1]) == 0) {
1310 /* if there are 2+ chunks of disk i/o possible for
1311 this track, let the caller know so that it can arrange
1312 for us to be called again, ASAP.
1315 if (total_space >= (_slaved?3:2) * disk_io_chunk_frames) {
1319 /* if we're running close to normal speed and there isn't enough
1320 space to do disk_io_chunk_frames of I/O, then don't bother.
1322 at higher speeds, just do it because the sync between butler
1323 and audio thread may not be good enough.
1326 if ((total_space < disk_io_chunk_frames) && fabs (_actual_speed) < 2.0f) {
1330 /* when slaved, don't try to get too close to the read pointer. this
1331 leaves space for the buffer reversal to have something useful to
1335 if (_slaved && total_space < (channels.front().playback_buf->bufsize() / 2)) {
1339 total_space = min (disk_io_chunk_frames, total_space);
1343 if (file_frame == 0) {
1345 /* at start: nothing to do but fill with silence */
1347 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1349 ChannelInfo& chan (*i);
1350 chan.playback_buf->get_write_vector (&vector);
1351 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1352 if (vector.len[1]) {
1353 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1355 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1360 if (file_frame < total_space) {
1362 /* too close to the start: read what we can,
1363 and then zero fill the rest
1366 zero_fill = total_space - file_frame;
1367 total_space = file_frame;
1372 /* move read position backwards because we are going
1373 to reverse the data.
1376 file_frame -= total_space;
1382 if (file_frame == max_frames) {
1384 /* at end: nothing to do but fill with silence */
1386 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1388 ChannelInfo& chan (*i);
1389 chan.playback_buf->get_write_vector (&vector);
1390 memset (vector.buf[0], 0, sizeof(Sample) * vector.len[0]);
1391 if (vector.len[1]) {
1392 memset (vector.buf[1], 0, sizeof(Sample) * vector.len[1]);
1394 chan.playback_buf->increment_write_ptr (vector.len[0] + vector.len[1]);
1399 if (file_frame > max_frames - total_space) {
1401 /* to close to the end: read what we can, and zero fill the rest */
1403 zero_fill = total_space - (max_frames - file_frame);
1404 total_space = max_frames - file_frame;
1411 /* Please note: the code to allocate buffers isn't run
1412 during normal butler thread operation. Its there
1413 for other times when we need to call do_refill()
1414 from somewhere other than the butler thread.
1417 if (mixdown_buffer == 0) {
1418 mixdown_buffer = new Sample[disk_io_chunk_frames];
1419 free_mixdown = true;
1421 free_mixdown = false;
1424 if (gain_buffer == 0) {
1425 gain_buffer = new float[disk_io_chunk_frames];
1432 workbuf = new char[disk_io_chunk_frames * 4];
1433 free_workbuf = true;
1435 free_workbuf = false;
1438 jack_nframes_t file_frame_tmp = 0;
1440 for (chan_n = 0, i = channels.begin(); i != channels.end(); ++i, ++chan_n) {
1442 ChannelInfo& chan (*i);
1445 jack_nframes_t len1, len2;
1447 chan.playback_buf->get_write_vector (&vector);
1450 file_frame_tmp = file_frame;
1453 buf1 = vector.buf[1];
1454 len1 = vector.len[1];
1455 buf2 = vector.buf[0];
1456 len2 = vector.len[0];
1458 buf1 = vector.buf[0];
1459 len1 = vector.len[0];
1460 buf2 = vector.buf[1];
1461 len2 = vector.len[1];
1465 to_read = min (ts, len1);
1466 to_read = min (to_read, disk_io_chunk_frames);
1470 if (read (buf1, mixdown_buffer, gain_buffer, workbuf, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1475 chan.playback_buf->increment_write_ptr (to_read);
1479 to_read = min (ts, len2);
1484 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1485 so read some or all of vector.len[1] as well.
1488 if (read (buf2, mixdown_buffer, gain_buffer, workbuf, file_frame_tmp, to_read, chan, chan_n, reversed)) {
1493 chan.playback_buf->increment_write_ptr (to_read);
1502 file_frame = file_frame_tmp;
1506 delete [] mixdown_buffer;
1509 delete [] gain_buffer;
1519 DiskStream::do_flush (char * workbuf, bool force_flush)
1523 RingBufferNPT<Sample>::rw_vector vector;
1524 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1525 jack_nframes_t total;
1527 /* important note: this function will write *AT MOST*
1528 disk_io_chunk_frames of data to disk. it will never
1529 write more than that. if its writes that much and there
1530 is more than that waiting to be written, it will return 1,
1531 otherwise 0 on success or -1 on failure.
1533 if there is less than disk_io_chunk_frames to be written,
1534 no data will be written at all unless `force_flush' is true.
1537 _write_data_count = 0;
1539 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1541 (*chan).capture_buf->get_read_vector (&vector);
1543 total = vector.len[0] + vector.len[1];
1546 if (total == 0 || (total < disk_io_chunk_frames && !force_flush && was_recording)) {
1551 /* if there are 2+ chunks of disk i/o possible for
1552 this track, let the caller know so that it can arrange
1553 for us to be called again, ASAP.
1555 if we are forcing a flush, then if there is* any* extra
1556 work, let the caller know.
1558 if we are no longer recording and there is any extra work,
1559 let the caller know too.
1562 if (total >= 2 * disk_io_chunk_frames || ((force_flush || !was_recording) && total > disk_io_chunk_frames)) {
1566 to_write = min (disk_io_chunk_frames, (jack_nframes_t) vector.len[0]);
1569 // check the transition buffer when recording destructive
1570 // important that we get this after the capture buf
1572 if (destructive()) {
1573 (*chan).capture_transition_buf->get_read_vector(&transvec);
1574 size_t transcount = transvec.len[0] + transvec.len[1];
1575 bool have_start = false;
1578 for (ti=0; ti < transcount; ++ti) {
1579 CaptureTransition & captrans = (ti < transvec.len[0]) ? transvec.buf[0][ti] : transvec.buf[1][ti-transvec.len[0]];
1581 if (captrans.type == CaptureStart) {
1582 // by definition, the first data we got above represents the given capture pos
1584 (*chan).write_source->mark_capture_start (captrans.capture_val);
1585 (*chan).curr_capture_cnt = 0;
1589 else if (captrans.type == CaptureEnd) {
1591 // capture end, the capture_val represents total frames in capture
1593 if (captrans.capture_val <= (*chan).curr_capture_cnt + to_write) {
1595 // shorten to make the write a perfect fit
1596 uint32_t nto_write = (captrans.capture_val - (*chan).curr_capture_cnt);
1598 if (nto_write < to_write) {
1599 ret = 1; // should we?
1601 to_write = nto_write;
1603 (*chan).write_source->mark_capture_end ();
1605 // increment past this transition, but go no further
1610 // actually ends just beyond this chunk, so force more work
1618 (*chan).capture_transition_buf->increment_read_ptr(ti);
1622 if ((!(*chan).write_source) || (*chan).write_source->write (vector.buf[0], to_write, workbuf) != to_write) {
1623 error << string_compose(_("DiskStream %1: cannot write to disk"), _id) << endmsg;
1627 (*chan).capture_buf->increment_read_ptr (to_write);
1628 (*chan).curr_capture_cnt += to_write;
1630 if ((to_write == vector.len[0]) && (total > to_write) && (to_write < disk_io_chunk_frames) && !destructive()) {
1632 /* we wrote all of vector.len[0] but it wasn't an entire
1633 disk_io_chunk_frames of data, so arrange for some part
1634 of vector.len[1] to be flushed to disk as well.
1637 to_write = min ((jack_nframes_t)(disk_io_chunk_frames - to_write), (jack_nframes_t) vector.len[1]);
1639 if ((*chan).write_source->write (vector.buf[1], to_write, workbuf) != to_write) {
1640 error << string_compose(_("DiskStream %1: cannot write to disk"), _id) << endmsg;
1644 _write_data_count += (*chan).write_source->write_data_count();
1646 (*chan).capture_buf->increment_read_ptr (to_write);
1647 (*chan).curr_capture_cnt += to_write;
1656 DiskStream::playlist_changed (Change ignored)
1658 playlist_modified ();
1662 DiskStream::playlist_modified ()
1664 if (!i_am_the_modifier && !overwrite_queued) {
1665 _session.request_overwrite_buffer (this);
1666 overwrite_queued = true;
1671 DiskStream::transport_stopped (struct tm& when, time_t twhen, bool abort_capture)
1673 uint32_t buffer_position;
1674 bool more_work = true;
1676 AudioRegion* region = 0;
1677 jack_nframes_t total_capture;
1678 AudioRegion::SourceList srcs;
1679 AudioRegion::SourceList::iterator src;
1680 ChannelList::iterator chan;
1681 vector<CaptureInfo*>::iterator ci;
1683 list<Source*>* deletion_list;
1684 bool mark_write_completed = false;
1686 finish_capture (true);
1688 /* butler is already stopped, but there may be work to do
1689 to flush remaining data to disk.
1692 while (more_work && !err) {
1693 switch (do_flush ( _session.conversion_buffer(Session::TransportContext), true)) {
1700 error << string_compose(_("DiskStream \"%1\": cannot flush captured data to disk!"), _name) << endmsg;
1705 /* XXX is there anything we can do if err != 0 ? */
1706 Glib::Mutex::Lock lm (capture_info_lock);
1708 if (capture_info.empty()) {
1712 if (abort_capture) {
1714 ChannelList::iterator chan;
1716 deletion_list = new list<Source*>;
1718 for ( chan = channels.begin(); chan != channels.end(); ++chan) {
1720 if ((*chan).write_source) {
1722 (*chan).write_source->mark_for_remove ();
1723 (*chan).write_source->release ();
1725 deletion_list->push_back ((*chan).write_source);
1727 (*chan).write_source = 0;
1730 /* new source set up in "out" below */
1733 if (!deletion_list->empty()) {
1734 DeleteSources (deletion_list);
1736 delete deletion_list;
1742 for (total_capture = 0, ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1743 total_capture += (*ci)->frames;
1746 /* figure out the name for this take */
1748 for (n = 0, chan = channels.begin(); chan != channels.end(); ++chan, ++n) {
1750 Source* s = (*chan).write_source;
1758 if ((fsrc = dynamic_cast<FileSource *>(s)) != 0) {
1759 fsrc->update_header (capture_info.front()->start, when, twhen);
1762 s->set_captured_for (_name);
1767 /* destructive tracks have a single, never changing region */
1769 if (destructive()) {
1771 /* send a signal that any UI can pick up to do the right thing. there is
1772 a small problem here in that a UI may need the peak data to be ready
1773 for the data that was recorded and this isn't interlocked with that
1774 process. this problem is deferred to the UI.
1777 _playlist->Modified();
1781 /* Register a new region with the Session that
1782 describes the entire source. Do this first
1783 so that any sub-regions will obviously be
1784 children of this one (later!)
1788 region = new AudioRegion (srcs, channels[0].write_source->last_capture_start_frame(), total_capture,
1789 region_name_from_path (channels[0].write_source->name()),
1790 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
1792 region->special_set_position (capture_info.front()->start);
1796 catch (failed_constructor& err) {
1797 error << string_compose(_("%1: could not create region for complete audio file"), _name) << endmsg;
1801 _last_capture_regions.push_back (region);
1803 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1805 _session.add_undo (_playlist->get_memento());
1806 _playlist->freeze ();
1808 for (buffer_position = channels[0].write_source->last_capture_start_frame(), ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1811 _session.region_name (region_name, channels[0].write_source->name(), false);
1813 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n";
1816 region = new AudioRegion (srcs, buffer_position, (*ci)->frames, region_name);
1819 catch (failed_constructor& err) {
1820 error << _("DiskStream: could not create region for captured audio!") << endmsg;
1821 continue; /* XXX is this OK? */
1824 _last_capture_regions.push_back (region);
1826 // cerr << "add new region, buffer position = " << buffer_position << " @ " << (*ci)->start << endl;
1828 i_am_the_modifier++;
1829 _playlist->add_region (*region, (*ci)->start);
1830 i_am_the_modifier--;
1832 buffer_position += (*ci)->frames;
1836 _session.add_redo_no_execute (_playlist->get_memento());
1839 mark_write_completed = true;
1841 reset_write_sources (mark_write_completed);
1844 for (ci = capture_info.begin(); ci != capture_info.end(); ++ci) {
1848 capture_info.clear ();
1849 capture_start_frame = 0;
1853 DiskStream::finish_capture (bool rec_monitors_input)
1855 was_recording = false;
1857 if (capture_captured == 0) {
1861 if (recordable() && destructive()) {
1862 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1864 RingBufferNPT<CaptureTransition>::rw_vector transvec;
1865 (*chan).capture_transition_buf->get_write_vector(&transvec);
1868 if (transvec.len[0] > 0) {
1869 transvec.buf[0]->type = CaptureEnd;
1870 transvec.buf[0]->capture_val = capture_captured;
1871 (*chan).capture_transition_buf->increment_write_ptr(1);
1875 fatal << string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg;
1881 CaptureInfo* ci = new CaptureInfo;
1883 ci->start = capture_start_frame;
1884 ci->frames = capture_captured;
1886 /* XXX theoretical race condition here. Need atomic exchange ?
1887 However, the circumstances when this is called right
1888 now (either on record-disable or transport_stopped)
1889 mean that no actual race exists. I think ...
1890 We now have a capture_info_lock, but it is only to be used
1891 to synchronize in the transport_stop and the capture info
1892 accessors, so that invalidation will not occur (both non-realtime).
1895 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1897 capture_info.push_back (ci);
1898 capture_captured = 0;
1902 DiskStream::set_record_enabled (bool yn, void* src)
1904 bool rolling = _session.transport_speed() != 0.0f;
1906 if (!recordable() || !_session.record_enabling_legal()) {
1910 /* if we're turning on rec-enable, there needs to be an
1914 if (yn && channels[0].source == 0) {
1916 /* pick up connections not initiated *from* the IO object
1917 we're associated with.
1920 get_input_sources ();
1923 /* yes, i know that this not proof against race conditions, but its
1924 good enough. i think.
1927 if (record_enabled() != yn) {
1929 g_atomic_int_set (&_record_enabled, 1);
1930 capturing_sources.clear ();
1931 if (Config->get_use_hardware_monitoring()) {
1932 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1933 if ((*chan).source) {
1934 (*chan).source->request_monitor_input (!(_session.get_auto_input() && rolling));
1936 capturing_sources.push_back ((*chan).write_source);
1939 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1940 capturing_sources.push_back ((*chan).write_source);
1945 g_atomic_int_set (&_record_enabled, 0);
1946 if (Config->get_use_hardware_monitoring()) {
1947 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
1948 if ((*chan).source) {
1949 (*chan).source->request_monitor_input (false);
1953 capturing_sources.clear ();
1956 record_enable_changed (src); /* EMIT SIGNAL */
1961 DiskStream::get_state ()
1963 XMLNode* node = new XMLNode ("DiskStream");
1965 LocaleGuard lg (X_("POSIX"));
1967 snprintf (buf, sizeof(buf), "0x%x", _flags);
1968 node->add_property ("flags", buf);
1970 snprintf (buf, sizeof(buf), "%zd", channels.size());
1971 node->add_property ("channels", buf);
1973 node->add_property ("playlist", _playlist->name());
1975 snprintf (buf, sizeof(buf), "%f", _visible_speed);
1976 node->add_property ("speed", buf);
1978 node->add_property("name", _name);
1979 snprintf (buf, sizeof(buf), "%" PRIu64, id());
1980 node->add_property("id", buf);
1982 if (!capturing_sources.empty() && _session.get_record_enabled()) {
1984 XMLNode* cs_child = new XMLNode (X_("CapturingSources"));
1985 XMLNode* cs_grandchild;
1987 for (vector<FileSource*>::iterator i = capturing_sources.begin(); i != capturing_sources.end(); ++i) {
1988 cs_grandchild = new XMLNode (X_("file"));
1989 cs_grandchild->add_property (X_("path"), (*i)->path());
1990 cs_child->add_child_nocopy (*cs_grandchild);
1993 /* store the location where capture will start */
1997 if (_session.get_punch_in() && ((pi = _session.locations()->auto_punch_location()) != 0)) {
1998 snprintf (buf, sizeof (buf), "%" PRIu32, pi->start());
2000 snprintf (buf, sizeof (buf), "%" PRIu32, _session.transport_frame());
2003 cs_child->add_property (X_("at"), buf);
2004 node->add_child_nocopy (*cs_child);
2008 node->add_child_copy (*_extra_xml);
2015 DiskStream::set_state (const XMLNode& node)
2017 const XMLProperty* prop;
2018 XMLNodeList nlist = node.children();
2019 XMLNodeIterator niter;
2020 uint32_t nchans = 1;
2021 XMLNode* capture_pending_node = 0;
2022 LocaleGuard lg (X_("POSIX"));
2024 in_set_state = true;
2026 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2027 if ((*niter)->name() == IO::state_node_name) {
2028 deprecated_io_node = new XMLNode (**niter);
2031 if ((*niter)->name() == X_("CapturingSources")) {
2032 capture_pending_node = *niter;
2036 /* prevent write sources from being created */
2038 in_set_state = true;
2040 if ((prop = node.property ("name")) != 0) {
2041 _name = prop->value();
2044 if (deprecated_io_node) {
2045 if ((prop = deprecated_io_node->property ("id")) != 0) {
2046 sscanf (prop->value().c_str(), "%" PRIu64, &_id);
2049 if ((prop = node.property ("id")) != 0) {
2050 sscanf (prop->value().c_str(), "%" PRIu64, &_id);
2054 if ((prop = node.property ("flags")) != 0) {
2055 _flags = strtol (prop->value().c_str(), 0, 0);
2058 if ((prop = node.property ("channels")) != 0) {
2059 nchans = atoi (prop->value().c_str());
2062 // create necessary extra channels
2063 // we are always constructed with one
2064 // and we always need one
2066 if (nchans > _n_channels) {
2068 // we need to add new channel infos
2069 //LockMonitor lm (state_lock, __LINE__, __FILE__);
2071 int diff = nchans - channels.size();
2073 for (int i=0; i < diff; ++i) {
2077 } else if (nchans < _n_channels) {
2079 // we need to get rid of channels
2080 //LockMonitor lm (state_lock, __LINE__, __FILE__);
2082 int diff = channels.size() - nchans;
2084 for (int i = 0; i < diff; ++i) {
2089 if ((prop = node.property ("playlist")) == 0) {
2094 bool had_playlist = (_playlist != 0);
2096 if (find_and_use_playlist (prop->value())) {
2100 if (!had_playlist) {
2101 _playlist->set_orig_diskstream_id (_id);
2104 if (!destructive() && capture_pending_node) {
2105 /* destructive streams have one and only one source per channel,
2106 and so they never end up in pending capture in any useful
2109 use_pending_capture_data (*capture_pending_node);
2114 if ((prop = node.property ("speed")) != 0) {
2115 double sp = atof (prop->value().c_str());
2117 if (realtime_set_speed (sp, false)) {
2118 non_realtime_set_speed ();
2122 _n_channels = channels.size();
2124 in_set_state = false;
2126 /* make sure this is clear before we do anything else */
2128 capturing_sources.clear ();
2130 /* write sources are handled when we handle the input set
2131 up of the IO that owns this DS (::non_realtime_input_change())
2134 in_set_state = false;
2140 DiskStream::use_new_write_source (uint32_t n)
2142 if (!recordable()) {
2146 if (n >= channels.size()) {
2147 error << string_compose (_("DiskStream: channel %1 out of range"), n) << endmsg;
2151 ChannelInfo &chan = channels[n];
2153 if (chan.write_source) {
2155 if (FileSource::is_empty (chan.write_source->path())) {
2156 chan.write_source->mark_for_remove ();
2157 chan.write_source->release();
2158 delete chan.write_source;
2160 chan.write_source->release();
2161 chan.write_source = 0;
2166 if ((chan.write_source = _session.create_file_source (*this, n, destructive())) == 0) {
2167 throw failed_constructor();
2171 catch (failed_constructor &err) {
2172 error << string_compose (_("%1:%2 new capture file not initialized correctly"), _name, n) << endmsg;
2173 chan.write_source = 0;
2177 chan.write_source->use ();
2179 /* do not remove destructive files even if they are empty */
2181 chan.write_source->set_allow_remove_if_empty (!destructive());
2187 DiskStream::reset_write_sources (bool mark_write_complete, bool force)
2189 ChannelList::iterator chan;
2192 if (!recordable()) {
2196 capturing_sources.clear ();
2198 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2199 if (!destructive()) {
2201 if ((*chan).write_source && mark_write_complete) {
2202 (*chan).write_source->mark_streaming_write_completed ();
2204 use_new_write_source (n);
2206 if (record_enabled()) {
2207 capturing_sources.push_back ((*chan).write_source);
2211 if ((*chan).write_source == 0) {
2212 use_new_write_source (n);
2217 if (destructive()) {
2219 /* we now have all our write sources set up, so create the
2220 playlist's single region.
2223 if (_playlist->empty()) {
2224 setup_destructive_playlist ();
2230 DiskStream::rename_write_sources ()
2232 ChannelList::iterator chan;
2235 for (chan = channels.begin(), n = 0; chan != channels.end(); ++chan, ++n) {
2236 if ((*chan).write_source != 0) {
2237 (*chan).write_source->set_name (_name, destructive());
2238 /* XXX what to do if one of them fails ? */
2246 DiskStream::set_block_size (jack_nframes_t nframes)
2248 if (_session.get_block_size() > speed_buffer_size) {
2249 speed_buffer_size = _session.get_block_size();
2251 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2252 if ((*chan).speed_buffer) delete [] (*chan).speed_buffer;
2253 (*chan).speed_buffer = new Sample[speed_buffer_size];
2256 allocate_temporary_buffers ();
2260 DiskStream::allocate_temporary_buffers ()
2262 /* make sure the wrap buffer is at least large enough to deal
2263 with the speeds up to 1.2, to allow for micro-variation
2264 when slaving to MTC, SMPTE etc.
2267 double sp = max (fabsf (_actual_speed), 1.2f);
2268 jack_nframes_t required_wrap_size = (jack_nframes_t) floor (_session.get_block_size() * sp) + 1;
2270 if (required_wrap_size > wrap_buffer_size) {
2272 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2273 if ((*chan).playback_wrap_buffer) delete [] (*chan).playback_wrap_buffer;
2274 (*chan).playback_wrap_buffer = new Sample[required_wrap_size];
2275 if ((*chan).capture_wrap_buffer) delete [] (*chan).capture_wrap_buffer;
2276 (*chan).capture_wrap_buffer = new Sample[required_wrap_size];
2279 wrap_buffer_size = required_wrap_size;
2284 DiskStream::monitor_input (bool yn)
2286 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2288 if ((*chan).source) {
2289 (*chan).source->request_monitor_input (yn);
2295 DiskStream::set_capture_offset ()
2298 /* can't capture, so forget it */
2302 _capture_offset = _io->input_latency();
2306 DiskStream::set_persistent_align_style (AlignStyle a)
2308 _persistent_alignment_style = a;
2312 DiskStream::set_align_style_from_io ()
2314 bool have_physical = false;
2320 get_input_sources ();
2322 for (ChannelList::iterator chan = channels.begin(); chan != channels.end(); ++chan) {
2323 if ((*chan).source && (*chan).source->flags() & JackPortIsPhysical) {
2324 have_physical = true;
2329 if (have_physical) {
2330 set_align_style (ExistingMaterial);
2332 set_align_style (CaptureTime);
2337 DiskStream::set_align_style (AlignStyle a)
2339 if (record_enabled() && _session.actively_recording()) {
2344 if (a != _alignment_style) {
2345 _alignment_style = a;
2346 AlignmentStyleChanged ();
2351 DiskStream::add_channel ()
2353 /* XXX need to take lock??? */
2357 init_channel (chan);
2359 chan.speed_buffer = new Sample[speed_buffer_size];
2360 chan.playback_wrap_buffer = new Sample[wrap_buffer_size];
2361 chan.capture_wrap_buffer = new Sample[wrap_buffer_size];
2363 channels.push_back (chan);
2365 _n_channels = channels.size();
2371 DiskStream::remove_channel ()
2373 if (channels.size() > 1) {
2374 /* XXX need to take lock??? */
2375 ChannelInfo & chan = channels.back();
2376 destroy_channel (chan);
2377 channels.pop_back();
2379 _n_channels = channels.size();
2387 DiskStream::playback_buffer_load () const
2389 return (float) ((double) channels.front().playback_buf->read_space()/
2390 (double) channels.front().playback_buf->bufsize());
2394 DiskStream::capture_buffer_load () const
2396 return (float) ((double) channels.front().capture_buf->write_space()/
2397 (double) channels.front().capture_buf->bufsize());
2401 DiskStream::set_loop (Location *location)
2404 if (location->start() >= location->end()) {
2405 error << string_compose(_("Location \"%1\" not valid for track loop (start >= end)"), location->name()) << endl;
2410 loop_location = location;
2412 LoopSet (location); /* EMIT SIGNAL */
2417 DiskStream::get_capture_start_frame (uint32_t n)
2419 Glib::Mutex::Lock lm (capture_info_lock);
2421 if (capture_info.size() > n) {
2422 return capture_info[n]->start;
2425 return capture_start_frame;
2430 DiskStream::get_captured_frames (uint32_t n)
2432 Glib::Mutex::Lock lm (capture_info_lock);
2434 if (capture_info.size() > n) {
2435 return capture_info[n]->frames;
2438 return capture_captured;
2443 DiskStream::punch_in ()
2448 DiskStream::punch_out ()
2453 DiskStream::use_pending_capture_data (XMLNode& node)
2455 const XMLProperty* prop;
2456 XMLNodeList nlist = node.children();
2457 XMLNodeIterator niter;
2459 FileSource* first_fs = 0;
2460 AudioRegion::SourceList pending_sources;
2461 jack_nframes_t position;
2463 if ((prop = node.property (X_("at"))) == 0) {
2467 if (sscanf (prop->value().c_str(), "%" PRIu32, &position) != 1) {
2471 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2472 if ((*niter)->name() == X_("file")) {
2474 if ((prop = (*niter)->property (X_("path"))) == 0) {
2479 fs = new FileSource (prop->value(), _session.frame_rate(), true, Config->get_native_file_data_format());
2482 catch (failed_constructor& err) {
2483 error << string_compose (_("%1: cannot restore pending capture source file %2"),
2484 _name, prop->value())
2489 pending_sources.push_back (fs);
2491 if (first_fs == 0) {
2495 fs->set_captured_for (_name);
2499 if (pending_sources.size() == 0) {
2500 /* nothing can be done */
2504 if (pending_sources.size() != _n_channels) {
2505 error << string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name)
2510 AudioRegion* region;
2513 region = new AudioRegion (pending_sources, 0, first_fs->length(),
2514 region_name_from_path (first_fs->name()),
2515 0, AudioRegion::Flag (AudioRegion::DefaultFlags|AudioRegion::Automatic|AudioRegion::WholeFile));
2517 region->special_set_position (0);
2520 catch (failed_constructor& err) {
2521 error << string_compose (_("%1: cannot create whole-file region from pending capture sources"),
2529 region = new AudioRegion (pending_sources, 0, first_fs->length(), region_name_from_path (first_fs->name()));
2532 catch (failed_constructor& err) {
2533 error << string_compose (_("%1: cannot create region from pending capture sources"),
2540 _playlist->add_region (*region, position);
2546 DiskStream::set_roll_delay (jack_nframes_t nframes)
2548 _roll_delay = nframes;
2552 DiskStream::set_destructive (bool yn)
2554 if (yn != destructive()) {
2555 reset_write_sources (true, true);
2557 _flags |= Destructive;
2559 _flags &= ~Destructive;