Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/track/clip_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,41 @@ fn clip_sample_window(
}

impl TrackData {
/// Offline renders of seekable (engine-rate PCM) clips only: fill
/// `clip_render_stream_scratch` with exactly `len` frames from
/// `source_from` by a blocking random-access read (`clip_sample_window`),
/// instead of popping the real-time rings, which silence-fill whenever the
/// producer thread falls behind.
fn read_clip_window_offline(
&mut self,
buffer: &AudioClipBuffer,
source_from: usize,
len: usize,
channels: usize,
) {
let mut window = std::mem::take(&mut self.rt.clip_window_scratch);
let mut bytes = std::mem::take(&mut self.rt.clip_window_bytes);
let mut interleaved = std::mem::take(&mut self.rt.clip_window_interleaved);
let read = clip_sample_window(
buffer,
source_from,
len,
&mut window,
&mut bytes,
&mut interleaved,
);
self.rt.clip_window_bytes = bytes;
self.rt.clip_window_interleaved = interleaved;
let blocks = &mut self.rt.clip_render_stream_scratch;
reset_channel_buffers(blocks, channels, len);
if read.is_some() {
for (dst, src) in blocks.iter_mut().zip(&window) {
dst.copy_from_slice(&src[..len]);
}
}
self.rt.clip_window_scratch = window;
}

/// Decoding strategy for an audio clip file.
#[cfg(feature = "audio")]
pub(crate) fn choose_clip_strategy(
Expand Down Expand Up @@ -1088,6 +1123,9 @@ impl TrackData {
let input_count = self.audio.ins.len().max(1);
let local_start = absolute_from.saturating_sub(clip_start);
let source_from = clip.offset.saturating_add(local_start);
// Offline renders keep the rings here: a random-access read of
// a compressed file re-decodes it from the start on every call
// and returns frames at the file's own rate, not the engine's.
let stream_blocks = &mut self.rt.clip_render_stream_scratch;
reset_channel_buffers(stream_blocks, clip_channels, request_len);
stream.read_frames(source_from, request_len, stream_blocks);
Expand All @@ -1108,9 +1146,17 @@ impl TrackData {
let input_count = self.audio.ins.len().max(1);
let local_start = absolute_from.saturating_sub(clip_start);
let source_from = clip.offset.saturating_add(local_start);
if self
.offline_random_access
.load(std::sync::atomic::Ordering::Relaxed)
{
self.read_clip_window_offline(&buffer, source_from, request_len, clip_channels);
} else {
let stream_blocks = &mut self.rt.clip_render_stream_scratch;
reset_channel_buffers(stream_blocks, clip_channels, request_len);
stream.read_frames(source_from, request_len, stream_blocks);
}
let stream_blocks = &mut self.rt.clip_render_stream_scratch;
reset_channel_buffers(stream_blocks, clip_channels, request_len);
stream.read_frames(source_from, request_len, stream_blocks);
reset_channel_buffers(out, input_count, request_len);
for (in_channel, block) in out.iter_mut().enumerate() {
let source_channel = if clip_channels == 1 {
Expand Down
4 changes: 4 additions & 0 deletions src/track/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,10 @@ pub struct TrackData {
pub phase_inverted: AtomicBool,
pub soloed: AtomicBool,
pub is_master: AtomicBool,
/// Set by the offline bounce worker: read seekable (engine-rate PCM) clips
/// by random access (blocking disk read) instead of the real-time rings,
/// so a bounce never silence-fills on a producer underrun.
pub offline_random_access: AtomicBool,
input_monitor: ArcSwap<Vec<bool>>,
disk_monitor: ArcSwap<Vec<bool>>,
midi_input_monitor: ArcSwap<Vec<bool>>,
Expand Down
1 change: 1 addition & 0 deletions src/track/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl TrackData {
phase_inverted: AtomicBool::new(false),
soloed: AtomicBool::new(false),
is_master: AtomicBool::new(false),
offline_random_access: AtomicBool::new(false),
input_monitor: ArcSwap::from_pointee(vec![false; io.audio_ins]),
disk_monitor: ArcSwap::from_pointee(vec![true; io.audio_ins]),
midi_input_monitor: ArcSwap::from_pointee(vec![false; io.midi_ins]),
Expand Down
40 changes: 40 additions & 0 deletions src/track/seekable_streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,46 @@ mod tests {
);
}

#[test]
#[cfg_attr(miri, ignore = "Miri isolation does not support filesystem open")]
fn offline_random_access_is_exact_from_the_first_block_at_a_clip_offset() {
use crate::track::Track;

let (path, source) = tone_wav("maolan_seekstream_offline", 48_000, 1.0, 440.0);
let mut track = Track::new("t".to_string(), 2, 2, 0, 0, 256, 48_000.0);
track.set_input_monitor(vec![false; 2]);
track.set_disk_monitor(vec![true; 2]);
let mut clip = crate::audio::clip::AudioClip::new(
path.to_string_lossy().into_owned(),
0,
48_000 * 240,
);
clip.id = "clip-1".to_string();
clip.fade_enabled = false;
clip.offset = 1_000;
track.audio.push_clip(clip);
track
.offline_random_access
.store(true, std::sync::atomic::Ordering::Relaxed);

// No waiting for the producer thread: an offline render must read
// exactly the requested frames, starting at the clip offset, on the
// very first block, where the real-time rings would still be priming.
for cycle in 0..4 {
track.rt.transport_sample = cycle * 256;
track.process();
let out = track.last_audio_outputs();
let expected = &source[1_000 + cycle * 256..1_000 + (cycle + 1) * 256];
for (&actual, &want) in out[0].iter().zip(expected) {
assert!(
(actual - want).abs() < 1.0e-6,
"block {cycle} is not frame-exact at the clip offset"
);
}
}
std::fs::remove_file(&path).expect("remove temporary test file");
}

#[test]
#[cfg_attr(miri, ignore = "Miri isolation does not support filesystem open")]
fn seekable_stream_wakes_after_full_ring_is_consumed() {
Expand Down
Loading