@@ -135,7 +135,11 @@ impl OfflineAudioContext {
135
135
..
136
136
} = renderer;
137
137
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
139
143
}
140
144
141
145
/// Given the current connections and scheduled changes, starts rendering audio.
@@ -163,9 +167,15 @@ impl OfflineAudioContext {
163
167
..
164
168
} = renderer;
165
169
166
- renderer
170
+ self . base . set_state ( AudioContextState :: Running ) ;
171
+
172
+ let result = renderer
167
173
. render_audiobuffer ( self . length , suspend_promises, resume_receiver)
168
- . await
174
+ . await ;
175
+
176
+ self . base . set_state ( AudioContextState :: Closed ) ;
177
+
178
+ result
169
179
}
170
180
171
181
/// get the length of rendering audio buffer
@@ -257,7 +267,8 @@ impl OfflineAudioContext {
257
267
. insert ( insert_pos, ( quantum, sender) ) ;
258
268
} // lock is dropped
259
269
260
- receiver. await . unwrap ( )
270
+ receiver. await . unwrap ( ) ;
271
+ self . base ( ) . set_state ( AudioContextState :: Suspended ) ;
261
272
}
262
273
263
274
/// Schedules a suspension of the time progression in the audio context at the specified time
@@ -317,9 +328,15 @@ impl OfflineAudioContext {
317
328
"InvalidStateError: cannot suspend multiple times at the same render quantum" ,
318
329
) ;
319
330
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
+
320
337
renderer
321
338
. suspend_callbacks
322
- . insert ( insert_pos, ( quantum, Box :: new ( callback ) ) ) ;
339
+ . insert ( insert_pos, ( quantum, boxed_callback ) ) ;
323
340
}
324
341
325
342
/// Resumes the progression of the OfflineAudioContext's currentTime when it has been suspended
@@ -328,6 +345,7 @@ impl OfflineAudioContext {
328
345
///
329
346
/// Panics when the context is closed or rendering has not started
330
347
pub async fn resume ( & self ) {
348
+ self . base ( ) . set_state ( AudioContextState :: Running ) ;
331
349
self . resume_sender . clone ( ) . send ( ( ) ) . await . unwrap ( )
332
350
}
333
351
}
@@ -343,6 +361,7 @@ mod tests {
343
361
#[ test]
344
362
fn render_empty_graph ( ) {
345
363
let mut context = OfflineAudioContext :: new ( 2 , 555 , 44_100. ) ;
364
+ assert_eq ! ( context. state( ) , AudioContextState :: Suspended ) ;
346
365
let buffer = context. start_rendering_sync ( ) ;
347
366
348
367
assert_eq ! ( context. length( ) , 555 ) ;
@@ -351,6 +370,8 @@ mod tests {
351
370
assert_eq ! ( buffer. length( ) , 555 ) ;
352
371
assert_float_eq ! ( buffer. get_channel_data( 0 ) , & [ 0. ; 555 ] [ ..] , abs_all <= 0. ) ;
353
372
assert_float_eq ! ( buffer. get_channel_data( 1 ) , & [ 0. ; 555 ] [ ..] , abs_all <= 0. ) ;
373
+
374
+ assert_eq ! ( context. state( ) , AudioContextState :: Closed ) ;
354
375
}
355
376
356
377
#[ test]
@@ -369,12 +390,14 @@ mod tests {
369
390
let mut context = OfflineAudioContext :: new ( 1 , len, sample_rate as f32 ) ;
370
391
371
392
context. suspend_sync ( RENDER_QUANTUM_SIZE as f64 / sample_rate, |context| {
393
+ assert_eq ! ( context. state( ) , AudioContextState :: Suspended ) ;
372
394
let mut src = context. create_constant_source ( ) ;
373
395
src. connect ( & context. destination ( ) ) ;
374
396
src. start ( ) ;
375
397
} ) ;
376
398
377
399
context. suspend_sync ( ( 3 * RENDER_QUANTUM_SIZE ) as f64 / sample_rate, |context| {
400
+ assert_eq ! ( context. state( ) , AudioContextState :: Suspended ) ;
378
401
context. destination ( ) . disconnect ( ) ;
379
402
} ) ;
380
403
0 commit comments