Skip to content

Commit caa6c51

Browse files
committed
OfflineAudioContext should set state to suspended when suspending
Fixes #416
1 parent 217ed1a commit caa6c51

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/context/offline.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ impl OfflineAudioContext {
135135
..
136136
} = renderer;
137137

138-
renderer.render_audiobuffer_sync(self.length, suspend_callbacks, self)
138+
self.base.set_state(AudioContextState::Running);
139+
let result = renderer.render_audiobuffer_sync(self.length, suspend_callbacks, self);
140+
self.base.set_state(AudioContextState::Closed);
141+
142+
result
139143
}
140144

141145
/// Given the current connections and scheduled changes, starts rendering audio.
@@ -163,9 +167,15 @@ impl OfflineAudioContext {
163167
..
164168
} = renderer;
165169

166-
renderer
170+
self.base.set_state(AudioContextState::Running);
171+
172+
let result = renderer
167173
.render_audiobuffer(self.length, suspend_promises, resume_receiver)
168-
.await
174+
.await;
175+
176+
self.base.set_state(AudioContextState::Closed);
177+
178+
result
169179
}
170180

171181
/// get the length of rendering audio buffer
@@ -257,7 +267,8 @@ impl OfflineAudioContext {
257267
.insert(insert_pos, (quantum, sender));
258268
} // lock is dropped
259269

260-
receiver.await.unwrap()
270+
receiver.await.unwrap();
271+
self.base().set_state(AudioContextState::Suspended);
261272
}
262273

263274
/// Schedules a suspension of the time progression in the audio context at the specified time
@@ -317,9 +328,15 @@ impl OfflineAudioContext {
317328
"InvalidStateError: cannot suspend multiple times at the same render quantum",
318329
);
319330

331+
let boxed_callback = Box::new(|ctx: &mut OfflineAudioContext| {
332+
ctx.base().set_state(AudioContextState::Suspended);
333+
(callback)(ctx);
334+
ctx.base().set_state(AudioContextState::Running);
335+
});
336+
320337
renderer
321338
.suspend_callbacks
322-
.insert(insert_pos, (quantum, Box::new(callback)));
339+
.insert(insert_pos, (quantum, boxed_callback));
323340
}
324341

325342
/// Resumes the progression of the OfflineAudioContext's currentTime when it has been suspended
@@ -328,6 +345,7 @@ impl OfflineAudioContext {
328345
///
329346
/// Panics when the context is closed or rendering has not started
330347
pub async fn resume(&self) {
348+
self.base().set_state(AudioContextState::Running);
331349
self.resume_sender.clone().send(()).await.unwrap()
332350
}
333351
}
@@ -343,6 +361,7 @@ mod tests {
343361
#[test]
344362
fn render_empty_graph() {
345363
let mut context = OfflineAudioContext::new(2, 555, 44_100.);
364+
assert_eq!(context.state(), AudioContextState::Suspended);
346365
let buffer = context.start_rendering_sync();
347366

348367
assert_eq!(context.length(), 555);
@@ -351,6 +370,8 @@ mod tests {
351370
assert_eq!(buffer.length(), 555);
352371
assert_float_eq!(buffer.get_channel_data(0), &[0.; 555][..], abs_all <= 0.);
353372
assert_float_eq!(buffer.get_channel_data(1), &[0.; 555][..], abs_all <= 0.);
373+
374+
assert_eq!(context.state(), AudioContextState::Closed);
354375
}
355376

356377
#[test]
@@ -369,12 +390,14 @@ mod tests {
369390
let mut context = OfflineAudioContext::new(1, len, sample_rate as f32);
370391

371392
context.suspend_sync(RENDER_QUANTUM_SIZE as f64 / sample_rate, |context| {
393+
assert_eq!(context.state(), AudioContextState::Suspended);
372394
let mut src = context.create_constant_source();
373395
src.connect(&context.destination());
374396
src.start();
375397
});
376398

377399
context.suspend_sync((3 * RENDER_QUANTUM_SIZE) as f64 / sample_rate, |context| {
400+
assert_eq!(context.state(), AudioContextState::Suspended);
378401
context.destination().disconnect();
379402
});
380403

0 commit comments

Comments
 (0)