@@ -172,6 +172,19 @@ impl OfflineAudioContext {
172
172
self . length
173
173
}
174
174
175
+ #[ track_caller]
176
+ fn calculate_suspend_frame ( & self , suspend_time : f64 ) -> usize {
177
+ assert ! (
178
+ suspend_time >= 0. ,
179
+ "InvalidStateError: suspendTime cannot be negative"
180
+ ) ;
181
+ assert ! (
182
+ suspend_time < self . length as f64 / self . sample_rate( ) as f64 ,
183
+ "InvalidStateError: suspendTime cannot be greater than or equal to the total render duration"
184
+ ) ;
185
+ ( suspend_time * self . base . sample_rate ( ) as f64 / RENDER_QUANTUM_SIZE as f64 ) . ceil ( ) as usize
186
+ }
187
+
175
188
/// Schedules a suspension of the time progression in the audio context at the specified time
176
189
/// and returns a promise
177
190
///
@@ -214,8 +227,7 @@ impl OfflineAudioContext {
214
227
/// assert_eq!(buffer.length(), 512);
215
228
/// ```
216
229
pub async fn suspend ( & self , suspend_time : f64 ) {
217
- let quantum = ( suspend_time * self . base . sample_rate ( ) as f64 / RENDER_QUANTUM_SIZE as f64 )
218
- . ceil ( ) as usize ;
230
+ let quantum = self . calculate_suspend_frame ( suspend_time) ;
219
231
220
232
let ( sender, receiver) = oneshot:: channel ( ) ;
221
233
@@ -284,8 +296,7 @@ impl OfflineAudioContext {
284
296
suspend_time : f64 ,
285
297
callback : F ,
286
298
) {
287
- let quantum = ( suspend_time * self . base . sample_rate ( ) as f64 / RENDER_QUANTUM_SIZE as f64 )
288
- . ceil ( ) as usize ;
299
+ let quantum = self . calculate_suspend_frame ( suspend_time) ;
289
300
290
301
let mut lock = self . renderer . lock ( ) . unwrap ( ) ;
291
302
let renderer = match lock. as_mut ( ) {
@@ -411,4 +422,26 @@ mod tests {
411
422
abs_all <= 0.
412
423
) ;
413
424
}
425
+
426
+ #[ test]
427
+ #[ should_panic]
428
+ fn test_suspend_negative_panics ( ) {
429
+ let mut context = OfflineAudioContext :: new ( 2 , 128 , 44_100. ) ;
430
+ context. suspend_sync ( -1.0 , |_| ( ) ) ;
431
+ }
432
+
433
+ #[ test]
434
+ #[ should_panic]
435
+ fn test_suspend_after_duration_panics ( ) {
436
+ let mut context = OfflineAudioContext :: new ( 2 , 128 , 44_100. ) ;
437
+ context. suspend_sync ( 1.0 , |_| ( ) ) ;
438
+ }
439
+
440
+ #[ test]
441
+ #[ should_panic]
442
+ fn test_suspend_after_render_panics ( ) {
443
+ let mut context = OfflineAudioContext :: new ( 2 , 128 , 44_100. ) ;
444
+ let _ = context. start_rendering_sync ( ) ;
445
+ context. suspend_sync ( 0.0 , |_| ( ) ) ;
446
+ }
414
447
}
0 commit comments