1
1
use std:: any:: Any ;
2
- use std:: cell :: { OnceCell , RefCell } ;
2
+ use std:: sync :: { Mutex , OnceLock } ;
3
3
4
4
use crate :: buffer:: AudioBuffer ;
5
5
use crate :: context:: { AudioContextRegistration , AudioParamId , BaseAudioContext } ;
@@ -101,8 +101,8 @@ pub struct AudioBufferSourceNode {
101
101
channel_config : ChannelConfig ,
102
102
detune : AudioParam , // has constraints, no a-rate
103
103
playback_rate : AudioParam , // has constraints, no a-rate
104
- buffer : OnceCell < AudioBuffer > ,
105
- inner_state : RefCell < InnerState > ,
104
+ buffer : OnceLock < AudioBuffer > ,
105
+ inner_state : Mutex < InnerState > ,
106
106
}
107
107
108
108
#[ derive( Debug , Clone ) ]
@@ -146,7 +146,7 @@ impl AudioScheduledSourceNode for AudioBufferSourceNode {
146
146
147
147
fn stop_at ( & self , when : f64 ) {
148
148
assert ! (
149
- self . inner_state. borrow ( ) . source_started,
149
+ self . inner_state. lock ( ) . unwrap ( ) . source_started,
150
150
"InvalidStateError cannot stop before start"
151
151
) ;
152
152
@@ -219,8 +219,8 @@ impl AudioBufferSourceNode {
219
219
channel_config : ChannelConfig :: default ( ) ,
220
220
detune : d_param,
221
221
playback_rate : pr_param,
222
- buffer : OnceCell :: new ( ) ,
223
- inner_state : RefCell :: new ( inner_state) ,
222
+ buffer : OnceLock :: new ( ) ,
223
+ inner_state : Mutex :: new ( inner_state) ,
224
224
} ;
225
225
226
226
if let Some ( buf) = buffer {
@@ -246,12 +246,12 @@ impl AudioBufferSourceNode {
246
246
///
247
247
/// Panics if the source was already started
248
248
pub fn start_at_with_offset_and_duration ( & self , start : f64 , offset : f64 , duration : f64 ) {
249
- let source_started = & mut self . inner_state . borrow_mut ( ) . source_started ;
249
+ let inner_state = & mut self . inner_state . lock ( ) . unwrap ( ) ;
250
250
assert ! (
251
- !* source_started,
251
+ !inner_state . source_started,
252
252
"InvalidStateError: Cannot call `start` twice"
253
253
) ;
254
- * source_started = true ;
254
+ inner_state . source_started = true ;
255
255
256
256
let control = ControlMessage :: StartWithOffsetAndDuration ( start, offset, duration) ;
257
257
self . registration . post_message ( control) ;
@@ -297,33 +297,39 @@ impl AudioBufferSourceNode {
297
297
}
298
298
299
299
/// Defines if the playback the [`AudioBuffer`] should be looped
300
+ #[ allow( clippy:: missing_panics_doc) ]
300
301
pub fn loop_ ( & self ) -> bool {
301
- self . inner_state . borrow ( ) . loop_state . is_looping
302
+ self . inner_state . lock ( ) . unwrap ( ) . loop_state . is_looping
302
303
}
303
304
305
+ #[ allow( clippy:: missing_panics_doc) ]
304
306
pub fn set_loop ( & self , value : bool ) {
305
- self . inner_state . borrow_mut ( ) . loop_state . is_looping = value;
307
+ self . inner_state . lock ( ) . unwrap ( ) . loop_state . is_looping = value;
306
308
self . registration . post_message ( ControlMessage :: Loop ( value) ) ;
307
309
}
308
310
309
311
/// Defines the loop start point, in the time reference of the [`AudioBuffer`]
312
+ #[ allow( clippy:: missing_panics_doc) ]
310
313
pub fn loop_start ( & self ) -> f64 {
311
- self . inner_state . borrow ( ) . loop_state . start
314
+ self . inner_state . lock ( ) . unwrap ( ) . loop_state . start
312
315
}
313
316
317
+ #[ allow( clippy:: missing_panics_doc) ]
314
318
pub fn set_loop_start ( & self , value : f64 ) {
315
- self . inner_state . borrow_mut ( ) . loop_state . start = value;
319
+ self . inner_state . lock ( ) . unwrap ( ) . loop_state . start = value;
316
320
self . registration
317
321
. post_message ( ControlMessage :: LoopStart ( value) ) ;
318
322
}
319
323
320
324
/// Defines the loop end point, in the time reference of the [`AudioBuffer`]
325
+ #[ allow( clippy:: missing_panics_doc) ]
321
326
pub fn loop_end ( & self ) -> f64 {
322
- self . inner_state . borrow ( ) . loop_state . end
327
+ self . inner_state . lock ( ) . unwrap ( ) . loop_state . end
323
328
}
324
329
330
+ #[ allow( clippy:: missing_panics_doc) ]
325
331
pub fn set_loop_end ( & self , value : f64 ) {
326
- self . inner_state . borrow_mut ( ) . loop_state . end = value;
332
+ self . inner_state . lock ( ) . unwrap ( ) . loop_state . end = value;
327
333
self . registration
328
334
. post_message ( ControlMessage :: LoopEnd ( value) ) ;
329
335
}
0 commit comments