2 Copyright (C) 2000-2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include "pbd/error.h"
34 #include <glibmm/thread.h>
35 #include "pbd/xml++.h"
36 #include "pbd/memento_command.h"
37 #include "pbd/enumwriter.h"
38 #include "pbd/stateful_diff_command.h"
40 #include "ardour/analyser.h"
41 #include "ardour/ardour.h"
42 #include "ardour/audio_buffer.h"
43 #include "ardour/audio_diskstream.h"
44 #include "ardour/audio_port.h"
45 #include "ardour/audioengine.h"
46 #include "ardour/audiofilesource.h"
48 #include "ardour/audioplaylist.h"
49 #include "ardour/audioregion.h"
50 #include "ardour/butler.h"
51 #include "ardour/configuration.h"
52 #include "ardour/cycle_timer.h"
53 #include "ardour/debug.h"
54 #include "ardour/io.h"
55 #include "ardour/playlist_factory.h"
56 #include "ardour/region_factory.h"
57 #include "ardour/send.h"
58 #include "ardour/session.h"
59 #include "ardour/source_factory.h"
60 #include "ardour/utils.h"
61 #include "ardour/session_playlists.h"
62 #include "ardour/route.h"
68 using namespace ARDOUR
;
71 size_t AudioDiskstream::_working_buffers_size
= 0;
72 Sample
* AudioDiskstream::_mixdown_buffer
= 0;
73 gain_t
* AudioDiskstream::_gain_buffer
= 0;
75 AudioDiskstream::AudioDiskstream (Session
&sess
, const string
&name
, Diskstream::Flag flag
)
76 : Diskstream(sess
, name
, flag
)
77 , deprecated_io_node(NULL
)
78 , channels (new ChannelList
)
80 /* prevent any write sources from being created */
87 AudioDiskstream::AudioDiskstream (Session
& sess
, const XMLNode
& node
)
88 : Diskstream(sess
, node
)
89 , deprecated_io_node(NULL
)
90 , channels (new ChannelList
)
95 if (set_state (node
, Stateful::loading_state_version
)) {
97 throw failed_constructor();
100 in_set_state
= false;
103 use_destructive_playlist ();
108 AudioDiskstream::init ()
110 /* there are no channels at this point, so these
111 two calls just get speed_buffer_size and wrap_buffer
112 size setup without duplicating their code.
115 set_block_size (_session
.get_block_size());
116 allocate_temporary_buffers ();
119 assert(_n_channels
== ChanCount(DataType::AUDIO
, 1));
122 AudioDiskstream::~AudioDiskstream ()
124 DEBUG_TRACE (DEBUG::Destruction
, string_compose ("Audio Diskstream %1 destructor\n", _name
));
127 RCUWriter
<ChannelList
> writer (channels
);
128 boost::shared_ptr
<ChannelList
> c
= writer
.get_copy();
130 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
139 delete deprecated_io_node
;
143 AudioDiskstream::allocate_working_buffers()
145 assert(disk_io_frames() > 0);
147 _working_buffers_size
= disk_io_frames();
148 _mixdown_buffer
= new Sample
[_working_buffers_size
];
149 _gain_buffer
= new gain_t
[_working_buffers_size
];
153 AudioDiskstream::free_working_buffers()
155 delete [] _mixdown_buffer
;
156 delete [] _gain_buffer
;
157 _working_buffers_size
= 0;
163 AudioDiskstream::non_realtime_input_change ()
166 Glib::Mutex::Lock
lm (state_lock
);
168 if (input_change_pending
== NoChange
) {
173 RCUWriter
<ChannelList
> writer (channels
);
174 boost::shared_ptr
<ChannelList
> c
= writer
.get_copy();
176 _n_channels
.set(DataType::AUDIO
, c
->size());
178 if (_io
->n_ports().n_audio() > _n_channels
.n_audio()) {
179 add_channel_to (c
, _io
->n_ports().n_audio() - _n_channels
.n_audio());
180 } else if (_io
->n_ports().n_audio() < _n_channels
.n_audio()) {
181 remove_channel_from (c
, _n_channels
.n_audio() - _io
->n_ports().n_audio());
185 get_input_sources ();
186 set_capture_offset ();
188 if (first_input_change
) {
189 set_align_style (_persistent_alignment_style
);
190 first_input_change
= false;
192 set_align_style_from_io ();
195 input_change_pending
= NoChange
;
197 /* implicit unlock */
200 /* reset capture files */
202 reset_write_sources (false);
204 /* now refill channel buffers */
206 if (speed() != 1.0f
|| speed() != -1.0f
) {
207 seek ((nframes_t
) (_session
.transport_frame() * (double) speed()));
209 seek (_session
.transport_frame());
214 AudioDiskstream::non_realtime_locate (nframes_t location
)
216 /* now refill channel buffers */
218 if (speed() != 1.0f
|| speed() != -1.0f
) {
219 seek ((nframes_t
) (location
* (double) speed()));
226 AudioDiskstream::get_input_sources ()
228 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
231 ChannelList::iterator chan
;
232 uint32_t ni
= _io
->n_ports().n_audio();
233 vector
<string
> connections
;
235 for (n
= 0, chan
= c
->begin(); chan
!= c
->end() && n
< ni
; ++chan
, ++n
) {
237 connections
.clear ();
239 if (_io
->nth (n
)->get_connections (connections
) == 0) {
241 if ((*chan
)->source
) {
242 // _source->disable_metering ();
248 (*chan
)->source
= dynamic_cast<AudioPort
*>(_session
.engine().get_port_by_name (connections
[0]) );
254 AudioDiskstream::find_and_use_playlist (const string
& name
)
256 boost::shared_ptr
<AudioPlaylist
> playlist
;
258 if ((playlist
= boost::dynamic_pointer_cast
<AudioPlaylist
> (_session
.playlists
->by_name (name
))) == 0) {
259 playlist
= boost::dynamic_pointer_cast
<AudioPlaylist
> (PlaylistFactory::create (DataType::AUDIO
, _session
, name
));
263 error
<< string_compose(_("AudioDiskstream: Playlist \"%1\" isn't an audio playlist"), name
) << endmsg
;
267 return use_playlist (playlist
);
271 AudioDiskstream::use_playlist (boost::shared_ptr
<Playlist
> playlist
)
273 assert(boost::dynamic_pointer_cast
<AudioPlaylist
>(playlist
));
275 Diskstream::use_playlist(playlist
);
281 AudioDiskstream::use_new_playlist ()
284 boost::shared_ptr
<AudioPlaylist
> playlist
;
286 if (!in_set_state
&& destructive()) {
291 newname
= Playlist::bump_name (_playlist
->name(), _session
);
293 newname
= Playlist::bump_name (_name
, _session
);
296 if ((playlist
= boost::dynamic_pointer_cast
<AudioPlaylist
> (PlaylistFactory::create (DataType::AUDIO
, _session
, newname
, hidden()))) != 0) {
298 playlist
->set_orig_diskstream_id (id());
299 return use_playlist (playlist
);
307 AudioDiskstream::use_copy_playlist ()
309 assert(audio_playlist());
315 if (_playlist
== 0) {
316 error
<< string_compose(_("AudioDiskstream %1: there is no existing playlist to make a copy of!"), _name
) << endmsg
;
321 boost::shared_ptr
<AudioPlaylist
> playlist
;
323 newname
= Playlist::bump_name (_playlist
->name(), _session
);
325 if ((playlist
= boost::dynamic_pointer_cast
<AudioPlaylist
>(PlaylistFactory::create (audio_playlist(), newname
))) != 0) {
326 playlist
->set_orig_diskstream_id (id());
327 return use_playlist (playlist
);
334 AudioDiskstream::setup_destructive_playlist ()
337 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
339 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
340 srcs
.push_back ((*chan
)->write_source
);
343 /* a single full-sized region */
345 assert (!srcs
.empty ());
348 plist
.add (Properties::name
, _name
.val());
349 plist
.add (Properties::start
, 0);
350 plist
.add (Properties::length
, max_frames
- max_frames
- srcs
.front()->natural_position());
352 boost::shared_ptr
<Region
> region (RegionFactory::create (srcs
, plist
));
353 _playlist
->add_region (region
, srcs
.front()->natural_position());
357 AudioDiskstream::use_destructive_playlist ()
359 /* this is called from the XML-based constructor or ::set_destructive. when called,
360 we already have a playlist and a region, but we need to
361 set up our sources for write. we use the sources associated
362 with the (presumed single, full-extent) region.
365 boost::shared_ptr
<Region
> rp
= _playlist
->find_next_region (_session
.current_start_frame(), Start
, 1);
368 reset_write_sources (false, true);
372 boost::shared_ptr
<AudioRegion
> region
= boost::dynamic_pointer_cast
<AudioRegion
> (rp
);
375 throw failed_constructor();
378 /* be sure to stretch the region out to the maximum length */
380 region
->set_length (max_frames
- region
->position(), this);
383 ChannelList::iterator chan
;
384 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
386 for (n
= 0, chan
= c
->begin(); chan
!= c
->end(); ++chan
, ++n
) {
387 (*chan
)->write_source
= boost::dynamic_pointer_cast
<AudioFileSource
>(region
->source (n
));
388 assert((*chan
)->write_source
);
389 (*chan
)->write_source
->set_allow_remove_if_empty (false);
391 /* this might be false if we switched modes, so force it */
393 (*chan
)->write_source
->set_destructive (true);
396 /* the source list will never be reset for a destructive track */
400 AudioDiskstream::prepare_record_status(nframes_t capture_start_frame
)
402 if (recordable() && destructive()) {
403 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
404 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
406 RingBufferNPT
<CaptureTransition
>::rw_vector transvec
;
407 (*chan
)->capture_transition_buf
->get_write_vector(&transvec
);
409 if (transvec
.len
[0] > 0) {
410 transvec
.buf
[0]->type
= CaptureStart
;
411 transvec
.buf
[0]->capture_val
= capture_start_frame
;
412 (*chan
)->capture_transition_buf
->increment_write_ptr(1);
416 fatal
<< X_("programming error: capture_transition_buf is full on rec start! inconceivable!")
424 AudioDiskstream::process (nframes_t transport_frame
, nframes_t nframes
, bool can_record
, bool rec_monitors_input
, bool& need_butler
)
427 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
428 ChannelList::iterator chan
;
430 nframes_t rec_offset
= 0;
431 nframes_t rec_nframes
= 0;
432 bool collect_playback
= false;
434 playback_distance
= 0;
436 if (!_io
|| !_io
->active()) {
440 check_record_status (transport_frame
, nframes
, can_record
);
446 Glib::Mutex::Lock
sm (state_lock
, Glib::TRY_LOCK
);
452 adjust_capture_position
= 0;
454 for (chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
455 (*chan
)->current_capture_buffer
= 0;
456 (*chan
)->current_playback_buffer
= 0;
459 /* two conditions to test for here:
461 A: this track is rec-enabled, and the session has confirmed that we can record
462 B: this track is rec-enabled, has been recording, and we are set up for auto-punch-in
464 The second test is necessary to capture the extra material that arrives AFTER the transport
465 frame has left the punch range (which will cause the "can_record" argument to be false).
469 // Safeguard against situations where process() goes haywire when autopunching and last_recordable_frame < first_recordable_frame
470 if (last_recordable_frame
< first_recordable_frame
) {
471 last_recordable_frame
= max_frames
;
474 OverlapType ot
= coverage (first_recordable_frame
, last_recordable_frame
, transport_frame
, transport_frame
+ nframes
);
476 calculate_record_range (ot
, transport_frame
, nframes
, rec_nframes
, rec_offset
);
478 if (rec_nframes
&& !was_recording
) {
479 capture_captured
= 0;
480 was_recording
= true;
483 if (can_record
&& !_last_capture_sources
.empty()) {
484 _last_capture_sources
.clear ();
489 uint32_t limit
= _io
->n_ports ().n_audio();
491 /* one or more ports could already have been removed from _io, but our
492 channel setup hasn't yet been updated. prevent us from trying to
493 use channels that correspond to missing ports. note that the
494 process callback (from which this is called) is always atomic
495 with respect to port removal/addition.
498 for (n
= 0, chan
= c
->begin(); chan
!= c
->end() && n
< limit
; ++chan
, ++n
) {
500 ChannelInfo
* chaninfo (*chan
);
502 chaninfo
->capture_buf
->get_write_vector (&chaninfo
->capture_vector
);
504 if (rec_nframes
<= chaninfo
->capture_vector
.len
[0]) {
506 chaninfo
->current_capture_buffer
= chaninfo
->capture_vector
.buf
[0];
508 /* note: grab the entire port buffer, but only copy what we were supposed to
509 for recording, and use rec_offset
512 AudioPort
* const ap
= _io
->audio (n
);
514 assert(rec_nframes
<= ap
->get_audio_buffer(nframes
).capacity());
515 memcpy (chaninfo
->current_capture_buffer
, ap
->get_audio_buffer (nframes
).data(rec_offset
), sizeof (Sample
) * rec_nframes
);
520 nframes_t total
= chaninfo
->capture_vector
.len
[0] + chaninfo
->capture_vector
.len
[1];
522 if (rec_nframes
> total
) {
527 AudioPort
* const ap
= _io
->audio (n
);
530 Sample
* buf
= ap
->get_audio_buffer(nframes
).data();
531 nframes_t first
= chaninfo
->capture_vector
.len
[0];
533 memcpy (chaninfo
->capture_wrap_buffer
, buf
, sizeof (Sample
) * first
);
534 memcpy (chaninfo
->capture_vector
.buf
[0], buf
, sizeof (Sample
) * first
);
535 memcpy (chaninfo
->capture_wrap_buffer
+first
, buf
+ first
, sizeof (Sample
) * (rec_nframes
- first
));
536 memcpy (chaninfo
->capture_vector
.buf
[1], buf
+ first
, sizeof (Sample
) * (rec_nframes
- first
));
538 chaninfo
->current_capture_buffer
= chaninfo
->capture_wrap_buffer
;
545 finish_capture (rec_monitors_input
, c
);
552 /* data will be written to disk */
554 if (rec_nframes
== nframes
&& rec_offset
== 0) {
556 for (chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
557 (*chan
)->current_playback_buffer
= (*chan
)->current_capture_buffer
;
560 playback_distance
= nframes
;
565 /* we can't use the capture buffer as the playback buffer, because
566 we recorded only a part of the current process' cycle data
570 collect_playback
= true;
573 adjust_capture_position
= rec_nframes
;
575 } else if (can_record
&& record_enabled()) {
577 /* can't do actual capture yet - waiting for latency effects to finish before we start*/
579 for (chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
580 (*chan
)->current_playback_buffer
= (*chan
)->current_capture_buffer
;
583 playback_distance
= nframes
;
587 collect_playback
= true;
590 if (collect_playback
) {
592 /* we're doing playback */
594 nframes_t necessary_samples
;
596 /* no varispeed playback if we're recording, because the output .... TBD */
598 if (rec_nframes
== 0 && _actual_speed
!= 1.0f
) {
599 necessary_samples
= (nframes_t
) floor ((nframes
* fabs (_actual_speed
))) + 1;
601 necessary_samples
= nframes
;
604 for (chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
605 (*chan
)->playback_buf
->get_read_vector (&(*chan
)->playback_vector
);
610 for (chan
= c
->begin(); chan
!= c
->end(); ++chan
, ++n
) {
612 ChannelInfo
* chaninfo (*chan
);
614 if (necessary_samples
<= chaninfo
->playback_vector
.len
[0]) {
616 chaninfo
->current_playback_buffer
= chaninfo
->playback_vector
.buf
[0];
619 nframes_t total
= chaninfo
->playback_vector
.len
[0] + chaninfo
->playback_vector
.len
[1];
621 if (necessary_samples
> total
) {
622 cerr
<< _name
<< " Need " << necessary_samples
<< " total = " << total
<< endl
;
623 cerr
<< "underrun for " << _name
<< endl
;
629 memcpy ((char *) chaninfo
->playback_wrap_buffer
,
630 chaninfo
->playback_vector
.buf
[0],
631 chaninfo
->playback_vector
.len
[0] * sizeof (Sample
));
632 memcpy (chaninfo
->playback_wrap_buffer
+ chaninfo
->playback_vector
.len
[0],
633 chaninfo
->playback_vector
.buf
[1],
634 (necessary_samples
- chaninfo
->playback_vector
.len
[0])
637 chaninfo
->current_playback_buffer
= chaninfo
->playback_wrap_buffer
;
642 if (rec_nframes
== 0 && _actual_speed
!= 1.0f
&& _actual_speed
!= -1.0f
) {
643 process_varispeed_playback(nframes
, c
);
645 playback_distance
= nframes
;
648 _speed
= _target_speed
;
654 if (commit (nframes
)) {
663 AudioDiskstream::process_varispeed_playback(nframes_t nframes
, boost::shared_ptr
<ChannelList
> c
)
665 ChannelList::iterator chan
;
667 interpolation
.set_speed (_target_speed
);
670 for (chan
= c
->begin(); chan
!= c
->end(); ++chan
, ++channel
) {
671 ChannelInfo
* chaninfo (*chan
);
673 playback_distance
= interpolation
.interpolate (
674 channel
, nframes
, chaninfo
->current_playback_buffer
, chaninfo
->speed_buffer
);
676 chaninfo
->current_playback_buffer
= chaninfo
->speed_buffer
;
681 AudioDiskstream::commit (nframes_t
/* nframes */)
683 bool need_butler
= false;
685 if (!_io
|| !_io
->active()) {
689 if (_actual_speed
< 0.0) {
690 playback_sample
-= playback_distance
;
692 playback_sample
+= playback_distance
;
695 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
696 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
698 (*chan
)->playback_buf
->increment_read_ptr (playback_distance
);
700 if (adjust_capture_position
) {
701 (*chan
)->capture_buf
->increment_write_ptr (adjust_capture_position
);
705 if (adjust_capture_position
!= 0) {
706 capture_captured
+= adjust_capture_position
;
707 adjust_capture_position
= 0;
711 if (_io
&& _io
->active()) {
712 need_butler
= c
->front()->playback_buf
->write_space() >= c
->front()->playback_buf
->bufsize() / 2;
717 if (_io
&& _io
->active()) {
718 need_butler
= c
->front()->playback_buf
->write_space() >= disk_io_chunk_frames
719 || c
->front()->capture_buf
->read_space() >= disk_io_chunk_frames
;
721 need_butler
= c
->front()->capture_buf
->read_space() >= disk_io_chunk_frames
;
729 AudioDiskstream::set_pending_overwrite (bool yn
)
731 /* called from audio thread, so we can use the read ptr and playback sample as we wish */
733 _pending_overwrite
= yn
;
735 overwrite_frame
= playback_sample
;
736 overwrite_offset
= channels
.reader()->front()->playback_buf
->get_read_ptr();
740 AudioDiskstream::overwrite_existing_buffers ()
742 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
743 Sample
* mixdown_buffer
;
746 bool reversed
= (_visible_speed
* _session
.transport_speed()) < 0.0f
;
748 overwrite_queued
= false;
750 /* assume all are the same size */
751 nframes_t size
= c
->front()->playback_buf
->bufsize();
753 mixdown_buffer
= new Sample
[size
];
754 gain_buffer
= new float[size
];
756 /* reduce size so that we can fill the buffer correctly. */
762 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
, ++n
) {
764 start
= overwrite_frame
;
765 nframes_t cnt
= size
;
767 /* to fill the buffer without resetting the playback sample, we need to
768 do it one or two chunks (normally two).
770 |----------------------------------------------------------------------|
774 |<- second chunk->||<----------------- first chunk ------------------>|
778 nframes_t to_read
= size
- overwrite_offset
;
780 if (read ((*chan
)->playback_buf
->buffer() + overwrite_offset
, mixdown_buffer
, gain_buffer
, start
, to_read
, *chan
, n
, reversed
)) {
781 error
<< string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
782 _id
, size
, playback_sample
) << endmsg
;
790 if (read ((*chan
)->playback_buf
->buffer(), mixdown_buffer
, gain_buffer
,
791 start
, cnt
, *chan
, n
, reversed
)) {
792 error
<< string_compose(_("AudioDiskstream %1: when refilling, cannot read %2 from playlist at frame %3"),
793 _id
, size
, playback_sample
) << endmsg
;
802 _pending_overwrite
= false;
803 delete [] gain_buffer
;
804 delete [] mixdown_buffer
;
809 AudioDiskstream::seek (nframes_t frame
, bool complete_refill
)
813 ChannelList::iterator chan
;
814 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
816 Glib::Mutex::Lock
lm (state_lock
);
818 for (n
= 0, chan
= c
->begin(); chan
!= c
->end(); ++chan
, ++n
) {
819 (*chan
)->playback_buf
->reset ();
820 (*chan
)->capture_buf
->reset ();
823 /* can't rec-enable in destructive mode if transport is before start */
825 if (destructive() && record_enabled() && frame
< _session
.current_start_frame()) {
826 disengage_record_enable ();
829 playback_sample
= frame
;
832 if (complete_refill
) {
833 while ((ret
= do_refill_with_alloc ()) > 0) ;
835 ret
= do_refill_with_alloc ();
842 AudioDiskstream::can_internal_playback_seek (nframes_t distance
)
844 ChannelList::iterator chan
;
845 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
847 for (chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
848 if ((*chan
)->playback_buf
->read_space() < distance
) {
856 AudioDiskstream::internal_playback_seek (nframes_t distance
)
858 ChannelList::iterator chan
;
859 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
861 for (chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
862 (*chan
)->playback_buf
->increment_read_ptr (distance
);
865 first_recordable_frame
+= distance
;
866 playback_sample
+= distance
;
872 AudioDiskstream::read (Sample
* buf
, Sample
* mixdown_buffer
, float* gain_buffer
, nframes_t
& start
, nframes_t cnt
,
873 ChannelInfo
* /*channel_info*/, int channel
, bool reversed
)
875 nframes_t this_read
= 0;
877 nframes_t loop_end
= 0;
878 nframes_t loop_start
= 0;
879 nframes_t offset
= 0;
882 /* XXX we don't currently play loops in reverse. not sure why */
886 nframes_t loop_length
= 0;
888 /* Make the use of a Location atomic for this read operation.
890 Note: Locations don't get deleted, so all we care about
891 when I say "atomic" is that we are always pointing to
892 the same one and using a start/length values obtained
896 if ((loc
= loop_location
) != 0) {
897 loop_start
= loc
->start();
898 loop_end
= loc
->end();
899 loop_length
= loop_end
- loop_start
;
902 /* if we are looping, ensure that the first frame we read is at the correct
903 position within the loop.
906 if (loc
&& start
>= loop_end
) {
907 //cerr << "start adjusted from " << start;
908 start
= loop_start
+ ((start
- loop_start
) % loop_length
);
909 //cerr << "to " << start << endl;
912 //cerr << "start is " << start << " loopstart: " << loop_start << " loopend: " << loop_end << endl;
921 /* take any loop into account. we can't read past the end of the loop. */
923 if (loc
&& (loop_end
- start
< cnt
)) {
924 this_read
= loop_end
- start
;
925 //cerr << "reloop true: thisread: " << this_read << " cnt: " << cnt << endl;
932 if (this_read
== 0) {
936 this_read
= min(cnt
,this_read
);
938 if (audio_playlist()->read (buf
+offset
, mixdown_buffer
, gain_buffer
, start
, this_read
, channel
) != this_read
) {
939 error
<< string_compose(_("AudioDiskstream %1: cannot read %2 from playlist at frame %3"), _id
, this_read
,
944 _read_data_count
= _playlist
->read_data_count();
948 swap_by_ptr (buf
, buf
+ this_read
- 1);
952 /* if we read to the end of the loop, go back to the beginning */
969 AudioDiskstream::do_refill_with_alloc ()
971 Sample
* mix_buf
= new Sample
[disk_io_chunk_frames
];
972 float* gain_buf
= new float[disk_io_chunk_frames
];
974 int ret
= _do_refill(mix_buf
, gain_buf
);
983 AudioDiskstream::_do_refill (Sample
* mixdown_buffer
, float* gain_buffer
)
987 RingBufferNPT
<Sample
>::rw_vector vector
;
988 bool reversed
= (_visible_speed
* _session
.transport_speed()) < 0.0f
;
989 nframes_t total_space
;
992 ChannelList::iterator i
;
993 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
1000 assert(mixdown_buffer
);
1001 assert(gain_buffer
);
1008 c
->front()->playback_buf
->get_write_vector (&vector
);
1010 if ((total_space
= vector
.len
[0] + vector
.len
[1]) == 0) {
1014 /* if there are 2+ chunks of disk i/o possible for
1015 this track, let the caller know so that it can arrange
1016 for us to be called again, ASAP.
1019 if (total_space
>= (_slaved
?3:2) * disk_io_chunk_frames
) {
1023 /* if we're running close to normal speed and there isn't enough
1024 space to do disk_io_chunk_frames of I/O, then don't bother.
1026 at higher speeds, just do it because the sync between butler
1027 and audio thread may not be good enough.
1030 if ((total_space
< disk_io_chunk_frames
) && fabs (_actual_speed
) < 2.0f
) {
1034 /* when slaved, don't try to get too close to the read pointer. this
1035 leaves space for the buffer reversal to have something useful to
1039 if (_slaved
&& total_space
< (c
->front()->playback_buf
->bufsize() / 2)) {
1043 /* never do more than disk_io_chunk_frames worth of disk input per call (limit doesn't apply for memset) */
1045 total_space
= min (disk_io_chunk_frames
, total_space
);
1049 if (file_frame
== 0) {
1051 /* at start: nothing to do but fill with silence */
1053 for (chan_n
= 0, i
= c
->begin(); i
!= c
->end(); ++i
, ++chan_n
) {
1055 ChannelInfo
* chan (*i
);
1056 chan
->playback_buf
->get_write_vector (&vector
);
1057 memset (vector
.buf
[0], 0, sizeof(Sample
) * vector
.len
[0]);
1058 if (vector
.len
[1]) {
1059 memset (vector
.buf
[1], 0, sizeof(Sample
) * vector
.len
[1]);
1061 chan
->playback_buf
->increment_write_ptr (vector
.len
[0] + vector
.len
[1]);
1066 if (file_frame
< total_space
) {
1068 /* too close to the start: read what we can,
1069 and then zero fill the rest
1072 zero_fill
= total_space
- file_frame
;
1073 total_space
= file_frame
;
1083 if (file_frame
== max_frames
) {
1085 /* at end: nothing to do but fill with silence */
1087 for (chan_n
= 0, i
= c
->begin(); i
!= c
->end(); ++i
, ++chan_n
) {
1089 ChannelInfo
* chan (*i
);
1090 chan
->playback_buf
->get_write_vector (&vector
);
1091 memset (vector
.buf
[0], 0, sizeof(Sample
) * vector
.len
[0]);
1092 if (vector
.len
[1]) {
1093 memset (vector
.buf
[1], 0, sizeof(Sample
) * vector
.len
[1]);
1095 chan
->playback_buf
->increment_write_ptr (vector
.len
[0] + vector
.len
[1]);
1100 if (file_frame
> max_frames
- total_space
) {
1102 /* to close to the end: read what we can, and zero fill the rest */
1104 zero_fill
= total_space
- (max_frames
- file_frame
);
1105 total_space
= max_frames
- file_frame
;
1112 nframes_t file_frame_tmp
= 0;
1114 for (chan_n
= 0, i
= c
->begin(); i
!= c
->end(); ++i
, ++chan_n
) {
1116 ChannelInfo
* chan (*i
);
1119 nframes_t len1
, len2
;
1121 chan
->playback_buf
->get_write_vector (&vector
);
1123 if (vector
.len
[0] > disk_io_chunk_frames
) {
1125 /* we're not going to fill the first chunk, so certainly do not bother with the
1126 other part. it won't be connected with the part we do fill, as in:
1128 .... => writable space
1129 ++++ => readable space
1130 ^^^^ => 1 x disk_io_chunk_frames that would be filled
1132 |......|+++++++++++++|...............................|
1137 So, just pretend that the buf1 part isn't there.
1147 file_frame_tmp
= file_frame
;
1149 buf1
= vector
.buf
[0];
1150 len1
= vector
.len
[0];
1151 buf2
= vector
.buf
[1];
1152 len2
= vector
.len
[1];
1154 to_read
= min (ts
, len1
);
1155 to_read
= min (to_read
, disk_io_chunk_frames
);
1159 if (read (buf1
, mixdown_buffer
, gain_buffer
, file_frame_tmp
, to_read
, chan
, chan_n
, reversed
)) {
1164 chan
->playback_buf
->increment_write_ptr (to_read
);
1168 to_read
= min (ts
, len2
);
1172 /* we read all of vector.len[0], but it wasn't an entire disk_io_chunk_frames of data,
1173 so read some or all of vector.len[1] as well.
1176 if (read (buf2
, mixdown_buffer
, gain_buffer
, file_frame_tmp
, to_read
, chan
, chan_n
, reversed
)) {
1181 chan
->playback_buf
->increment_write_ptr (to_read
);
1190 file_frame
= file_frame_tmp
;
1197 /** Flush pending data to disk.
1199 * Important note: this function will write *AT MOST* disk_io_chunk_frames
1200 * of data to disk. it will never write more than that. If it writes that
1201 * much and there is more than that waiting to be written, it will return 1,
1202 * otherwise 0 on success or -1 on failure.
1204 * If there is less than disk_io_chunk_frames to be written, no data will be
1205 * written at all unless @a force_flush is true.
1208 AudioDiskstream::do_flush (RunContext
/*context*/, bool force_flush
)
1212 RingBufferNPT
<Sample
>::rw_vector vector
;
1213 RingBufferNPT
<CaptureTransition
>::rw_vector transvec
;
1216 _write_data_count
= 0;
1218 transvec
.buf
[0] = 0;
1219 transvec
.buf
[1] = 0;
1223 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
1224 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
1226 (*chan
)->capture_buf
->get_read_vector (&vector
);
1228 total
= vector
.len
[0] + vector
.len
[1];
1230 if (total
== 0 || (total
< disk_io_chunk_frames
&& !force_flush
&& was_recording
)) {
1234 /* if there are 2+ chunks of disk i/o possible for
1235 this track, let the caller know so that it can arrange
1236 for us to be called again, ASAP.
1238 if we are forcing a flush, then if there is* any* extra
1239 work, let the caller know.
1241 if we are no longer recording and there is any extra work,
1242 let the caller know too.
1245 if (total
>= 2 * disk_io_chunk_frames
|| ((force_flush
|| !was_recording
) && total
> disk_io_chunk_frames
)) {
1249 to_write
= min (disk_io_chunk_frames
, (nframes_t
) vector
.len
[0]);
1251 // check the transition buffer when recording destructive
1252 // important that we get this after the capture buf
1254 if (destructive()) {
1255 (*chan
)->capture_transition_buf
->get_read_vector(&transvec
);
1256 size_t transcount
= transvec
.len
[0] + transvec
.len
[1];
1257 bool have_start
= false;
1260 for (ti
=0; ti
< transcount
; ++ti
) {
1261 CaptureTransition
& captrans
= (ti
< transvec
.len
[0]) ? transvec
.buf
[0][ti
] : transvec
.buf
[1][ti
-transvec
.len
[0]];
1263 if (captrans
.type
== CaptureStart
) {
1264 // by definition, the first data we got above represents the given capture pos
1266 (*chan
)->write_source
->mark_capture_start (captrans
.capture_val
);
1267 (*chan
)->curr_capture_cnt
= 0;
1271 else if (captrans
.type
== CaptureEnd
) {
1273 // capture end, the capture_val represents total frames in capture
1275 if (captrans
.capture_val
<= (*chan
)->curr_capture_cnt
+ to_write
) {
1277 // shorten to make the write a perfect fit
1278 uint32_t nto_write
= (captrans
.capture_val
- (*chan
)->curr_capture_cnt
);
1280 if (nto_write
< to_write
) {
1281 ret
= 1; // should we?
1283 to_write
= nto_write
;
1285 (*chan
)->write_source
->mark_capture_end ();
1287 // increment past this transition, but go no further
1292 // actually ends just beyond this chunk, so force more work
1300 (*chan
)->capture_transition_buf
->increment_read_ptr(ti
);
1304 if ((!(*chan
)->write_source
) || (*chan
)->write_source
->write (vector
.buf
[0], to_write
) != to_write
) {
1305 error
<< string_compose(_("AudioDiskstream %1: cannot write to disk"), _id
) << endmsg
;
1309 (*chan
)->capture_buf
->increment_read_ptr (to_write
);
1310 (*chan
)->curr_capture_cnt
+= to_write
;
1312 if ((to_write
== vector
.len
[0]) && (total
> to_write
) && (to_write
< disk_io_chunk_frames
) && !destructive()) {
1314 /* we wrote all of vector.len[0] but it wasn't an entire
1315 disk_io_chunk_frames of data, so arrange for some part
1316 of vector.len[1] to be flushed to disk as well.
1319 to_write
= min ((nframes_t
)(disk_io_chunk_frames
- to_write
), (nframes_t
) vector
.len
[1]);
1321 if ((*chan
)->write_source
->write (vector
.buf
[1], to_write
) != to_write
) {
1322 error
<< string_compose(_("AudioDiskstream %1: cannot write to disk"), _id
) << endmsg
;
1326 _write_data_count
+= (*chan
)->write_source
->write_data_count();
1328 (*chan
)->capture_buf
->increment_read_ptr (to_write
);
1329 (*chan
)->curr_capture_cnt
+= to_write
;
1338 AudioDiskstream::transport_stopped_wallclock (struct tm
& when
, time_t twhen
, bool abort_capture
)
1340 uint32_t buffer_position
;
1341 bool more_work
= true;
1343 boost::shared_ptr
<AudioRegion
> region
;
1344 nframes_t total_capture
;
1346 SourceList::iterator src
;
1347 ChannelList::iterator chan
;
1348 vector
<CaptureInfo
*>::iterator ci
;
1349 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
1351 bool mark_write_completed
= false;
1353 finish_capture (true, c
);
1355 /* butler is already stopped, but there may be work to do
1356 to flush remaining data to disk.
1359 while (more_work
&& !err
) {
1360 switch (do_flush (TransportContext
, true)) {
1367 error
<< string_compose(_("AudioDiskstream \"%1\": cannot flush captured data to disk!"), _name
) << endmsg
;
1372 /* XXX is there anything we can do if err != 0 ? */
1373 Glib::Mutex::Lock
lm (capture_info_lock
);
1375 if (capture_info
.empty()) {
1379 if (abort_capture
) {
1381 if (destructive()) {
1385 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
1387 if ((*chan
)->write_source
) {
1389 (*chan
)->write_source
->mark_for_remove ();
1390 (*chan
)->write_source
->drop_references ();
1391 _session
.remove_source ((*chan
)->write_source
);
1392 (*chan
)->write_source
.reset ();
1395 /* new source set up in "out" below */
1401 for (total_capture
= 0, ci
= capture_info
.begin(); ci
!= capture_info
.end(); ++ci
) {
1402 total_capture
+= (*ci
)->frames
;
1405 /* figure out the name for this take */
1407 for (n
= 0, chan
= c
->begin(); chan
!= c
->end(); ++chan
, ++n
) {
1409 boost::shared_ptr
<AudioFileSource
> s
= (*chan
)->write_source
;
1413 if (s
->unstubify ()) {
1414 error
<< string_compose (_("Could not move capture file from %1"), s
->path()) << endmsg
;
1416 s
->update_header (capture_info
.front()->start
, when
, twhen
);
1417 s
->set_captured_for (_name
.val());
1418 s
->mark_immutable ();
1420 if (Config
->get_auto_analyse_audio()) {
1421 Analyser::queue_source_for_analysis (s
, true);
1426 /* destructive tracks have a single, never changing region */
1428 if (destructive()) {
1430 /* send a signal that any UI can pick up to do the right thing. there is
1431 a small problem here in that a UI may need the peak data to be ready
1432 for the data that was recorded and this isn't interlocked with that
1433 process. this problem is deferred to the UI.
1436 _playlist
->LayeringChanged(); // XXX this may not get the UI to do the right thing
1440 string whole_file_region_name
;
1441 whole_file_region_name
= region_name_from_path (c
->front()->write_source
->name(), true);
1443 /* Register a new region with the Session that
1444 describes the entire source. Do this first
1445 so that any sub-regions will obviously be
1446 children of this one (later!)
1452 plist
.add (Properties::start
, c
->front()->write_source
->last_capture_start_frame());
1453 plist
.add (Properties::length
, total_capture
);
1454 plist
.add (Properties::name
, whole_file_region_name
);
1455 boost::shared_ptr
<Region
> rx (RegionFactory::create (srcs
, plist
));
1456 rx
->set_automatic (true);
1457 rx
->set_whole_file (true);
1459 region
= boost::dynamic_pointer_cast
<AudioRegion
> (rx
);
1460 region
->special_set_position (capture_info
.front()->start
);
1464 catch (failed_constructor
& err
) {
1465 error
<< string_compose(_("%1: could not create region for complete audio file"), _name
) << endmsg
;
1469 _last_capture_sources
.insert (_last_capture_sources
.end(), srcs
.begin(), srcs
.end());
1471 // cerr << _name << ": there are " << capture_info.size() << " capture_info records\n";
1473 _playlist
->clear_history ();
1474 _playlist
->freeze ();
1476 for (buffer_position
= c
->front()->write_source
->last_capture_start_frame(), ci
= capture_info
.begin(); ci
!= capture_info
.end(); ++ci
) {
1480 RegionFactory::region_name (region_name
, whole_file_region_name
, false);
1482 // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl;
1488 plist
.add (Properties::start
, buffer_position
);
1489 plist
.add (Properties::length
, (*ci
)->frames
);
1490 plist
.add (Properties::name
, region_name
);
1492 boost::shared_ptr
<Region
> rx (RegionFactory::create (srcs
, plist
));
1493 region
= boost::dynamic_pointer_cast
<AudioRegion
> (rx
);
1496 catch (failed_constructor
& err
) {
1497 error
<< _("AudioDiskstream: could not create region for captured audio!") << endmsg
;
1498 continue; /* XXX is this OK? */
1501 i_am_the_modifier
++;
1502 _playlist
->add_region (region
, (*ci
)->start
, 1, non_layered());
1503 i_am_the_modifier
--;
1505 buffer_position
+= (*ci
)->frames
;
1509 _session
.add_command (new StatefulDiffCommand (_playlist
));
1512 mark_write_completed
= true;
1515 reset_write_sources (mark_write_completed
);
1519 for (ci
= capture_info
.begin(); ci
!= capture_info
.end(); ++ci
) {
1523 capture_info
.clear ();
1524 capture_start_frame
= 0;
1528 AudioDiskstream::transport_looped (nframes_t transport_frame
)
1530 if (was_recording
) {
1531 // all we need to do is finish this capture, with modified capture length
1532 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
1534 // adjust the capture length knowing that the data will be recorded to disk
1535 // only necessary after the first loop where we're recording
1536 if (capture_info
.size() == 0) {
1537 capture_captured
+= _capture_offset
;
1539 if (_alignment_style
== ExistingMaterial
) {
1540 capture_captured
+= _session
.worst_output_latency();
1542 capture_captured
+= _roll_delay
;
1546 finish_capture (true, c
);
1548 // the next region will start recording via the normal mechanism
1549 // we'll set the start position to the current transport pos
1550 // no latency adjustment or capture offset needs to be made, as that already happened the first time
1551 capture_start_frame
= transport_frame
;
1552 first_recordable_frame
= transport_frame
; // mild lie
1553 last_recordable_frame
= max_frames
;
1554 was_recording
= true;
1556 if (recordable() && destructive()) {
1557 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
1559 RingBufferNPT
<CaptureTransition
>::rw_vector transvec
;
1560 (*chan
)->capture_transition_buf
->get_write_vector(&transvec
);
1562 if (transvec
.len
[0] > 0) {
1563 transvec
.buf
[0]->type
= CaptureStart
;
1564 transvec
.buf
[0]->capture_val
= capture_start_frame
;
1565 (*chan
)->capture_transition_buf
->increment_write_ptr(1);
1569 fatal
<< X_("programming error: capture_transition_buf is full on rec loop! inconceivable!")
1579 AudioDiskstream::finish_capture (bool /*rec_monitors_input*/, boost::shared_ptr
<ChannelList
> c
)
1581 was_recording
= false;
1582 first_recordable_frame
= max_frames
;
1583 last_recordable_frame
= max_frames
;
1585 if (capture_captured
== 0) {
1589 if (recordable() && destructive()) {
1590 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
1592 RingBufferNPT
<CaptureTransition
>::rw_vector transvec
;
1593 (*chan
)->capture_transition_buf
->get_write_vector(&transvec
);
1595 if (transvec
.len
[0] > 0) {
1596 transvec
.buf
[0]->type
= CaptureEnd
;
1597 transvec
.buf
[0]->capture_val
= capture_captured
;
1598 (*chan
)->capture_transition_buf
->increment_write_ptr(1);
1602 fatal
<< string_compose (_("programmer error: %1"), X_("capture_transition_buf is full when stopping record! inconceivable!")) << endmsg
;
1608 CaptureInfo
* ci
= new CaptureInfo
;
1610 ci
->start
= capture_start_frame
;
1611 ci
->frames
= capture_captured
;
1613 /* XXX theoretical race condition here. Need atomic exchange ?
1614 However, the circumstances when this is called right
1615 now (either on record-disable or transport_stopped)
1616 mean that no actual race exists. I think ...
1617 We now have a capture_info_lock, but it is only to be used
1618 to synchronize in the transport_stop and the capture info
1619 accessors, so that invalidation will not occur (both non-realtime).
1622 // cerr << "Finish capture, add new CI, " << ci->start << '+' << ci->frames << endl;
1624 capture_info
.push_back (ci
);
1625 capture_captured
= 0;
1627 /* now we've finished a capture, reset first_recordable_frame for next time */
1628 first_recordable_frame
= max_frames
;
1632 AudioDiskstream::set_record_enabled (bool yn
)
1634 if (!recordable() || !_session
.record_enabling_legal() || _io
->n_ports().n_audio() == 0) {
1638 /* can't rec-enable in destructive mode if transport is before start */
1640 if (destructive() && yn
&& _session
.transport_frame() < _session
.current_start_frame()) {
1644 /* yes, i know that this not proof against race conditions, but its
1645 good enough. i think.
1648 if (record_enabled() != yn
) {
1650 engage_record_enable ();
1652 disengage_record_enable ();
1658 AudioDiskstream::engage_record_enable ()
1660 bool rolling
= _session
.transport_speed() != 0.0f
;
1661 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
1663 g_atomic_int_set (&_record_enabled
, 1);
1664 capturing_sources
.clear ();
1666 if (Config
->get_monitoring_model() == HardwareMonitoring
) {
1668 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
1669 if ((*chan
)->source
) {
1670 (*chan
)->source
->ensure_monitor_input (!(_session
.config
.get_auto_input() && rolling
));
1672 capturing_sources
.push_back ((*chan
)->write_source
);
1673 (*chan
)->write_source
->mark_streaming_write_started ();
1677 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
1678 capturing_sources
.push_back ((*chan
)->write_source
);
1679 (*chan
)->write_source
->mark_streaming_write_started ();
1683 RecordEnableChanged (); /* EMIT SIGNAL */
1687 AudioDiskstream::disengage_record_enable ()
1689 g_atomic_int_set (&_record_enabled
, 0);
1690 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
1691 if (Config
->get_monitoring_model() == HardwareMonitoring
) {
1692 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
1693 if ((*chan
)->source
) {
1694 (*chan
)->source
->ensure_monitor_input (false);
1698 capturing_sources
.clear ();
1699 RecordEnableChanged (); /* EMIT SIGNAL */
1703 AudioDiskstream::get_state ()
1705 XMLNode
* node
= new XMLNode ("Diskstream");
1707 LocaleGuard
lg (X_("POSIX"));
1708 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
1710 node
->add_property ("flags", enum_2_string (_flags
));
1712 snprintf (buf
, sizeof(buf
), "%zd", c
->size());
1713 node
->add_property ("channels", buf
);
1715 node
->add_property ("playlist", _playlist
->name());
1717 snprintf (buf
, sizeof(buf
), "%.12g", _visible_speed
);
1718 node
->add_property ("speed", buf
);
1720 node
->add_property("name", _name
);
1721 id().print (buf
, sizeof (buf
));
1722 node
->add_property("id", buf
);
1724 if (!capturing_sources
.empty() && _session
.get_record_enabled()) {
1726 XMLNode
* cs_child
= new XMLNode (X_("CapturingSources"));
1727 XMLNode
* cs_grandchild
;
1729 for (vector
<boost::shared_ptr
<AudioFileSource
> >::iterator i
= capturing_sources
.begin(); i
!= capturing_sources
.end(); ++i
) {
1730 cs_grandchild
= new XMLNode (X_("file"));
1731 cs_grandchild
->add_property (X_("path"), (*i
)->path());
1732 cs_child
->add_child_nocopy (*cs_grandchild
);
1735 /* store the location where capture will start */
1739 if (_session
.config
.get_punch_in() && ((pi
= _session
.locations()->auto_punch_location()) != 0)) {
1740 snprintf (buf
, sizeof (buf
), "%" PRId64
, pi
->start());
1742 snprintf (buf
, sizeof (buf
), "%" PRId64
, _session
.transport_frame());
1745 cs_child
->add_property (X_("at"), buf
);
1746 node
->add_child_nocopy (*cs_child
);
1750 node
->add_child_copy (*_extra_xml
);
1757 AudioDiskstream::set_state (const XMLNode
& node
, int /*version*/)
1759 const XMLProperty
* prop
;
1760 XMLNodeList nlist
= node
.children();
1761 XMLNodeIterator niter
;
1762 uint32_t nchans
= 1;
1763 XMLNode
* capture_pending_node
= 0;
1764 LocaleGuard
lg (X_("POSIX"));
1766 in_set_state
= true;
1768 for (niter
= nlist
.begin(); niter
!= nlist
.end(); ++niter
) {
1769 if ((*niter
)->name() == IO::state_node_name
) {
1770 deprecated_io_node
= new XMLNode (**niter
);
1773 if ((*niter
)->name() == X_("CapturingSources")) {
1774 capture_pending_node
= *niter
;
1778 /* prevent write sources from being created */
1780 in_set_state
= true;
1782 if ((prop
= node
.property ("name")) != 0) {
1783 _name
= prop
->value();
1786 if (deprecated_io_node
) {
1787 if ((prop
= deprecated_io_node
->property ("id")) != 0) {
1788 _id
= prop
->value ();
1791 if ((prop
= node
.property ("id")) != 0) {
1792 _id
= prop
->value ();
1796 if ((prop
= node
.property ("flags")) != 0) {
1797 _flags
= Flag (string_2_enum (prop
->value(), _flags
));
1800 if ((prop
= node
.property ("channels")) != 0) {
1801 nchans
= atoi (prop
->value().c_str());
1804 // create necessary extra channels
1805 // we are always constructed with one and we always need one
1807 _n_channels
.set(DataType::AUDIO
, channels
.reader()->size());
1809 if (nchans
> _n_channels
.n_audio()) {
1811 add_channel (nchans
- _n_channels
.n_audio());
1812 IO::PortCountChanged(_n_channels
);
1814 } else if (nchans
< _n_channels
.n_audio()) {
1816 remove_channel (_n_channels
.n_audio() - nchans
);
1819 if ((prop
= node
.property ("playlist")) == 0) {
1824 bool had_playlist
= (_playlist
!= 0);
1826 if (find_and_use_playlist (prop
->value())) {
1830 if (!had_playlist
) {
1831 _playlist
->set_orig_diskstream_id (id());
1834 if (!destructive() && capture_pending_node
) {
1835 /* destructive streams have one and only one source per channel,
1836 and so they never end up in pending capture in any useful
1839 use_pending_capture_data (*capture_pending_node
);
1844 if ((prop
= node
.property ("speed")) != 0) {
1845 double sp
= atof (prop
->value().c_str());
1847 if (realtime_set_speed (sp
, false)) {
1848 non_realtime_set_speed ();
1852 in_set_state
= false;
1854 /* make sure this is clear before we do anything else */
1856 capturing_sources
.clear ();
1858 /* write sources are handled when we handle the input set
1859 up of the IO that owns this DS (::non_realtime_input_change())
1866 AudioDiskstream::use_new_write_source (uint32_t n
)
1868 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
1870 if (!recordable()) {
1874 if (n
>= c
->size()) {
1875 error
<< string_compose (_("AudioDiskstream: channel %1 out of range"), n
) << endmsg
;
1879 ChannelInfo
* chan
= (*c
)[n
];
1881 if (chan
->write_source
) {
1882 chan
->write_source
->done_with_peakfile_writes ();
1883 chan
->write_source
->set_allow_remove_if_empty (true);
1884 chan
->write_source
.reset ();
1888 /* file starts off as a stub file, it will be converted
1889 when we're done with a capture pass.
1892 if ((chan
->write_source
= _session
.create_audio_source_for_session (n_channels().n_audio(),
1893 name(), n
, destructive(),
1895 throw failed_constructor();
1899 catch (failed_constructor
&err
) {
1900 error
<< string_compose (_("%1:%2 new capture file not initialized correctly"), _name
, n
) << endmsg
;
1901 chan
->write_source
.reset ();
1905 /* do not remove destructive files even if they are empty */
1907 chan
->write_source
->set_allow_remove_if_empty (!destructive());
1912 list
<boost::shared_ptr
<Source
> >
1913 AudioDiskstream::steal_write_sources()
1915 /* not possible to steal audio write sources */
1916 list
<boost::shared_ptr
<Source
> > ret
;
1921 AudioDiskstream::reset_write_sources (bool mark_write_complete
, bool /*force*/)
1923 ChannelList::iterator chan
;
1924 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
1927 if (!_session
.writable() || !recordable()) {
1931 capturing_sources
.clear ();
1933 for (chan
= c
->begin(), n
= 0; chan
!= c
->end(); ++chan
, ++n
) {
1935 if (!destructive()) {
1937 if ((*chan
)->write_source
&& mark_write_complete
) {
1938 (*chan
)->write_source
->mark_streaming_write_completed ();
1941 if ((*chan
)->write_source
) {
1942 if ((*chan
)->write_source
->removable()) {
1943 (*chan
)->write_source
->mark_for_remove ();
1944 (*chan
)->write_source
->drop_references ();
1945 _session
.remove_source ((*chan
)->write_source
);
1947 (*chan
)->write_source
.reset ();
1950 use_new_write_source (n
);
1952 if (record_enabled()) {
1953 capturing_sources
.push_back ((*chan
)->write_source
);
1958 if ((*chan
)->write_source
== 0) {
1959 use_new_write_source (n
);
1964 if (destructive() && !c
->empty ()) {
1966 /* we now have all our write sources set up, so create the
1967 playlist's single region.
1970 if (_playlist
->empty()) {
1971 setup_destructive_playlist ();
1977 AudioDiskstream::rename_write_sources ()
1979 ChannelList::iterator chan
;
1980 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
1983 for (chan
= c
->begin(), n
= 0; chan
!= c
->end(); ++chan
, ++n
) {
1984 if ((*chan
)->write_source
!= 0) {
1985 (*chan
)->write_source
->set_source_name (_name
.val(), destructive());
1986 /* XXX what to do if one of them fails ? */
1994 AudioDiskstream::set_block_size (nframes_t
/*nframes*/)
1996 if (_session
.get_block_size() > speed_buffer_size
) {
1997 speed_buffer_size
= _session
.get_block_size();
1998 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
2000 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
2001 if ((*chan
)->speed_buffer
)
2002 delete [] (*chan
)->speed_buffer
;
2003 (*chan
)->speed_buffer
= new Sample
[speed_buffer_size
];
2006 allocate_temporary_buffers ();
2010 AudioDiskstream::allocate_temporary_buffers ()
2012 /* make sure the wrap buffer is at least large enough to deal
2013 with the speeds up to 1.2, to allow for micro-variation
2014 when slaving to MTC, Timecode etc.
2017 double const sp
= max (fabsf (_actual_speed
), 1.2f
);
2018 nframes_t required_wrap_size
= (nframes_t
) floor (_session
.get_block_size() * sp
) + 1;
2020 if (required_wrap_size
> wrap_buffer_size
) {
2022 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
2024 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
2025 if ((*chan
)->playback_wrap_buffer
)
2026 delete [] (*chan
)->playback_wrap_buffer
;
2027 (*chan
)->playback_wrap_buffer
= new Sample
[required_wrap_size
];
2028 if ((*chan
)->capture_wrap_buffer
)
2029 delete [] (*chan
)->capture_wrap_buffer
;
2030 (*chan
)->capture_wrap_buffer
= new Sample
[required_wrap_size
];
2033 wrap_buffer_size
= required_wrap_size
;
2038 AudioDiskstream::monitor_input (bool yn
)
2040 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
2042 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
2044 if ((*chan
)->source
) {
2045 (*chan
)->source
->ensure_monitor_input (yn
);
2051 AudioDiskstream::set_align_style_from_io ()
2053 bool have_physical
= false;
2059 get_input_sources ();
2061 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
2063 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
2064 if ((*chan
)->source
&& (*chan
)->source
->flags() & JackPortIsPhysical
) {
2065 have_physical
= true;
2070 if (have_physical
) {
2071 set_align_style (ExistingMaterial
);
2073 set_align_style (CaptureTime
);
2078 AudioDiskstream::add_channel_to (boost::shared_ptr
<ChannelList
> c
, uint32_t how_many
)
2080 while (how_many
--) {
2081 c
->push_back (new ChannelInfo(_session
.butler()->audio_diskstream_playback_buffer_size(),
2082 _session
.butler()->audio_diskstream_capture_buffer_size(),
2083 speed_buffer_size
, wrap_buffer_size
));
2084 interpolation
.add_channel_to (_session
.butler()->audio_diskstream_playback_buffer_size(), speed_buffer_size
);
2087 _n_channels
.set(DataType::AUDIO
, c
->size());
2093 AudioDiskstream::add_channel (uint32_t how_many
)
2095 RCUWriter
<ChannelList
> writer (channels
);
2096 boost::shared_ptr
<ChannelList
> c
= writer
.get_copy();
2098 return add_channel_to (c
, how_many
);
2102 AudioDiskstream::remove_channel_from (boost::shared_ptr
<ChannelList
> c
, uint32_t how_many
)
2104 while (how_many
-- && !c
->empty()) {
2107 interpolation
.remove_channel_from ();
2110 _n_channels
.set(DataType::AUDIO
, c
->size());
2116 AudioDiskstream::remove_channel (uint32_t how_many
)
2118 RCUWriter
<ChannelList
> writer (channels
);
2119 boost::shared_ptr
<ChannelList
> c
= writer
.get_copy();
2121 return remove_channel_from (c
, how_many
);
2125 AudioDiskstream::playback_buffer_load () const
2127 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
2129 return (float) ((double) c
->front()->playback_buf
->read_space()/
2130 (double) c
->front()->playback_buf
->bufsize());
2134 AudioDiskstream::capture_buffer_load () const
2136 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
2138 return (float) ((double) c
->front()->capture_buf
->write_space()/
2139 (double) c
->front()->capture_buf
->bufsize());
2143 AudioDiskstream::use_pending_capture_data (XMLNode
& node
)
2145 const XMLProperty
* prop
;
2146 XMLNodeList nlist
= node
.children();
2147 XMLNodeIterator niter
;
2148 boost::shared_ptr
<AudioFileSource
> fs
;
2149 boost::shared_ptr
<AudioFileSource
> first_fs
;
2150 SourceList pending_sources
;
2153 if ((prop
= node
.property (X_("at"))) == 0) {
2157 if (sscanf (prop
->value().c_str(), "%" PRIu32
, &position
) != 1) {
2161 for (niter
= nlist
.begin(); niter
!= nlist
.end(); ++niter
) {
2162 if ((*niter
)->name() == X_("file")) {
2164 if ((prop
= (*niter
)->property (X_("path"))) == 0) {
2168 // This protects sessions from errant CapturingSources in stored sessions
2170 if (stat (prop
->value().c_str(), &sbuf
)) {
2175 fs
= boost::dynamic_pointer_cast
<AudioFileSource
> (
2176 SourceFactory::createWritable (DataType::AUDIO
, _session
,
2177 prop
->value(), false, _session
.frame_rate()));
2180 catch (failed_constructor
& err
) {
2181 error
<< string_compose (_("%1: cannot restore pending capture source file %2"),
2182 _name
, prop
->value())
2187 pending_sources
.push_back (fs
);
2189 if (first_fs
== 0) {
2193 fs
->set_captured_for (_name
.val());
2197 if (pending_sources
.size() == 0) {
2198 /* nothing can be done */
2202 if (pending_sources
.size() != _n_channels
.n_audio()) {
2203 error
<< string_compose (_("%1: incorrect number of pending sources listed - ignoring them all"), _name
)
2208 boost::shared_ptr
<AudioRegion
> region
;
2214 plist
.add (Properties::start
, 0);
2215 plist
.add (Properties::length
, first_fs
->length (first_fs
->timeline_position()));
2216 plist
.add (Properties::name
, region_name_from_path (first_fs
->name(), true));
2218 region
= boost::dynamic_pointer_cast
<AudioRegion
> (RegionFactory::create (pending_sources
, plist
));
2220 region
->set_automatic (true);
2221 region
->set_whole_file (true);
2222 region
->special_set_position (0);
2225 catch (failed_constructor
& err
) {
2226 error
<< string_compose (
2227 _("%1: cannot create whole-file region from pending capture sources"),
2233 _playlist
->add_region (region
, position
);
2239 AudioDiskstream::set_non_layered (bool yn
)
2241 if (yn
!= non_layered()) {
2244 _flags
= Flag (_flags
| NonLayered
);
2246 _flags
= Flag (_flags
& ~NonLayered
);
2254 AudioDiskstream::set_destructive (bool yn
)
2256 if (yn
!= destructive()) {
2259 bool bounce_ignored
;
2260 /* requestor should already have checked this and
2261 bounced if necessary and desired
2263 if (!can_become_destructive (bounce_ignored
)) {
2266 _flags
= Flag (_flags
| Destructive
);
2267 use_destructive_playlist ();
2269 _flags
= Flag (_flags
& ~Destructive
);
2270 reset_write_sources (true, true);
2278 AudioDiskstream::can_become_destructive (bool& requires_bounce
) const
2281 requires_bounce
= false;
2285 /* is there only one region ? */
2287 if (_playlist
->n_regions() != 1) {
2288 requires_bounce
= true;
2292 boost::shared_ptr
<Region
> first
= _playlist
->find_next_region (_session
.current_start_frame(), Start
, 1);
2294 requires_bounce
= false;
2298 /* do the source(s) for the region cover the session start position ? */
2300 if (first
->position() != _session
.current_start_frame()) {
2301 if (first
->start() > _session
.current_start_frame()) {
2302 requires_bounce
= true;
2307 /* is the source used by only 1 playlist ? */
2309 boost::shared_ptr
<AudioRegion
> afirst
= boost::dynamic_pointer_cast
<AudioRegion
> (first
);
2313 if (_session
.playlists
->source_use_count (afirst
->source()) > 1) {
2314 requires_bounce
= true;
2318 requires_bounce
= false;
2323 AudioDiskstream::adjust_playback_buffering ()
2325 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
2327 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
2328 (*chan
)->resize_playback (_session
.butler()->audio_diskstream_playback_buffer_size());
2333 AudioDiskstream::adjust_capture_buffering ()
2335 boost::shared_ptr
<ChannelList
> c
= channels
.reader();
2337 for (ChannelList::iterator chan
= c
->begin(); chan
!= c
->end(); ++chan
) {
2338 (*chan
)->resize_capture (_session
.butler()->audio_diskstream_capture_buffer_size());
2342 AudioDiskstream::ChannelInfo::ChannelInfo (nframes_t playback_bufsize
, nframes_t capture_bufsize
, nframes_t speed_size
, nframes_t wrap_size
)
2346 current_capture_buffer
= 0;
2347 current_playback_buffer
= 0;
2348 curr_capture_cnt
= 0;
2350 speed_buffer
= new Sample
[speed_size
];
2351 playback_wrap_buffer
= new Sample
[wrap_size
];
2352 capture_wrap_buffer
= new Sample
[wrap_size
];
2354 playback_buf
= new RingBufferNPT
<Sample
> (playback_bufsize
);
2355 capture_buf
= new RingBufferNPT
<Sample
> (capture_bufsize
);
2356 capture_transition_buf
= new RingBufferNPT
<CaptureTransition
> (256);
2358 /* touch the ringbuffer buffers, which will cause
2359 them to be mapped into locked physical RAM if
2360 we're running with mlockall(). this doesn't do
2364 memset (playback_buf
->buffer(), 0, sizeof (Sample
) * playback_buf
->bufsize());
2365 memset (capture_buf
->buffer(), 0, sizeof (Sample
) * capture_buf
->bufsize());
2366 memset (capture_transition_buf
->buffer(), 0, sizeof (CaptureTransition
) * capture_transition_buf
->bufsize());
2370 AudioDiskstream::ChannelInfo::resize_playback (nframes_t playback_bufsize
)
2372 delete playback_buf
;
2373 playback_buf
= new RingBufferNPT
<Sample
> (playback_bufsize
);
2374 memset (playback_buf
->buffer(), 0, sizeof (Sample
) * playback_buf
->bufsize());
2378 AudioDiskstream::ChannelInfo::resize_capture (nframes_t capture_bufsize
)
2382 capture_buf
= new RingBufferNPT
<Sample
> (capture_bufsize
);
2383 memset (capture_buf
->buffer(), 0, sizeof (Sample
) * capture_buf
->bufsize());
2386 AudioDiskstream::ChannelInfo::~ChannelInfo ()
2388 write_source
.reset ();
2390 delete [] speed_buffer
;
2393 delete [] playback_wrap_buffer
;
2394 playback_wrap_buffer
= 0;
2396 delete [] capture_wrap_buffer
;
2397 capture_wrap_buffer
= 0;
2399 delete playback_buf
;
2405 delete capture_transition_buf
;
2406 capture_transition_buf
= 0;