Skip to content

Commit

Permalink
Improve AudioBufferSource test readability by defering Mutex wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
orottier committed Sep 5, 2024
1 parent 685cfdf commit be88f46
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions src/node/audio_buffer_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,22 +1493,18 @@ mod tests {
let data = vec![1.; 1];
buffer.copy_to_channel(&data, 0);

let src = Arc::new(Mutex::new(context.create_buffer_source()));
let mut guard = src.lock().unwrap();
guard.connect(&context.destination());
guard.set_buffer(buffer);
let mut src = context.create_buffer_source();
src.connect(&context.destination());
src.set_buffer(buffer);
// play in fast track
guard.start_at(0.);
src.start_at(0.);

let src = Arc::new(Mutex::new(src));
let clone = Arc::clone(&src);

guard.set_onended(move |_| {
let mut guard = clone.lock().unwrap();
guard.set_loop(true);
src.lock().unwrap().set_onended(move |_| {
clone.lock().unwrap().set_loop(true);
});

drop(guard);

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

Expand All @@ -1529,22 +1525,18 @@ mod tests {
let data = vec![1.; 1];
buffer.copy_to_channel(&data, 0);

let src = Arc::new(Mutex::new(context.create_buffer_source()));
let mut guard = src.lock().unwrap();
guard.connect(&context.destination());
guard.set_buffer(buffer);
let mut src = context.create_buffer_source();
src.connect(&context.destination());
src.set_buffer(buffer);
// play in slow track
guard.start_at(1. / sample_rate as f64);
src.start_at(1. / sample_rate as f64);

let src = Arc::new(Mutex::new(src));
let clone = Arc::clone(&src);

guard.set_onended(move |_| {
let mut guard = clone.lock().unwrap();
guard.set_loop(true);
src.lock().unwrap().set_onended(move |_| {
clone.lock().unwrap().set_loop(true);
});

drop(guard);

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

Expand Down

0 comments on commit be88f46

Please sign in to comment.