Skip to content

Commit

Permalink
Merge branch 'main' into feature/rethrow-unit-test-processor-panics
Browse files Browse the repository at this point in the history
  • Loading branch information
orottier authored Sep 10, 2024
2 parents 8e8a5df + 04da9b6 commit 6adf77e
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/node/audio_buffer_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,25 @@ impl AudioBufferSourceRenderer {
ControlMessage::LoopStart(loop_start) => self.loop_state.start = *loop_start,
ControlMessage::LoopEnd(loop_end) => self.loop_state.end = *loop_end,
}

self.clamp_loop_boundaries();
}

fn clamp_loop_boundaries(&mut self) {
if let Some(buffer) = &self.buffer {
let duration = buffer.duration();

if self.loop_state.start < 0. {
self.loop_state.start = 0.;
}
if self.loop_state.start > duration {
self.loop_state.start = duration;
}

if self.loop_state.end <= 0. || self.loop_state.end > duration {
self.loop_state.end = 0.;
}
}
}
}

Expand Down Expand Up @@ -760,6 +779,7 @@ impl AudioProcessor for AudioBufferSourceRenderer {
sample_rate: Default::default(),
};
self.buffer = Some(std::mem::replace(buffer, tombstone_buffer));
self.clamp_loop_boundaries();
}
return;
};
Expand Down Expand Up @@ -1424,6 +1444,33 @@ mod tests {
}
}

#[test]
fn test_loop_hangs() {
let sample_rate = 48_000.;
let length = sample_rate as usize;
let mut context = OfflineAudioContext::new(1, length, sample_rate);

let mut buffer = context.create_buffer(1, 500, sample_rate);
let data = vec![1.; 1];
buffer.copy_to_channel(&data, 0);

let mut src = context.create_buffer_source();
src.connect(&context.destination());
src.set_buffer(buffer);

src.set_loop(true);
src.set_loop_start(0.5); // outside of buffer duration
src.set_loop_end(1.5); // outside of buffer duration

src.start();

let result = context.start_rendering_sync(); // should terminate
let channel = result.get_channel_data(0);

assert_float_eq!(channel[0], 1.0, abs_all <= 0.);
assert_float_eq!(channel[1..], [0.; 48_000 - 1][..], abs_all <= 0.);
}

#[test]
// regression test for #452
// - fast track
Expand Down

0 comments on commit 6adf77e

Please sign in to comment.