1
1
use std:: any:: Any ;
2
- use std:: cell:: { Cell , OnceCell , RefCell } ;
2
+ use std:: cell:: { OnceCell , RefCell } ;
3
3
4
4
use crate :: buffer:: AudioBuffer ;
5
5
use crate :: context:: { AudioContextRegistration , AudioParamId , BaseAudioContext } ;
@@ -101,9 +101,14 @@ 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
- loop_state : RefCell < LoopState > ,
105
104
buffer : OnceCell < AudioBuffer > ,
106
- source_started : Cell < bool > ,
105
+ inner_state : RefCell < InnerState > ,
106
+ }
107
+
108
+ #[ derive( Debug , Clone ) ]
109
+ struct InnerState {
110
+ loop_state : LoopState ,
111
+ source_started : bool ,
107
112
}
108
113
109
114
impl AudioNode for AudioBufferSourceNode {
@@ -141,7 +146,7 @@ impl AudioScheduledSourceNode for AudioBufferSourceNode {
141
146
142
147
fn stop_at ( & self , when : f64 ) {
143
148
assert ! (
144
- self . source_started . get ( ) ,
149
+ self . inner_state . borrow ( ) . source_started ,
145
150
"InvalidStateError cannot stop before start"
146
151
) ;
147
152
@@ -205,14 +210,17 @@ impl AudioBufferSourceNode {
205
210
ended_triggered : false ,
206
211
} ;
207
212
213
+ let inner_state = InnerState {
214
+ loop_state,
215
+ source_started : false ,
216
+ } ;
208
217
let node = Self {
209
218
registration,
210
219
channel_config : ChannelConfig :: default ( ) ,
211
220
detune : d_param,
212
221
playback_rate : pr_param,
213
- loop_state : RefCell :: new ( loop_state) ,
214
222
buffer : OnceCell :: new ( ) ,
215
- source_started : Cell :: new ( false ) ,
223
+ inner_state : RefCell :: new ( inner_state ) ,
216
224
} ;
217
225
218
226
if let Some ( buf) = buffer {
@@ -238,11 +246,15 @@ impl AudioBufferSourceNode {
238
246
///
239
247
/// Panics if the source was already started
240
248
pub fn start_at_with_offset_and_duration ( & self , start : f64 , offset : f64 , duration : f64 ) {
241
- assert ! (
242
- !self . source_started. get( ) ,
243
- "InvalidStateError: Cannot call `start` twice"
244
- ) ;
245
- self . source_started . set ( true ) ;
249
+ {
250
+ let source_started = & mut self . inner_state . borrow_mut ( ) . source_started ;
251
+ assert ! (
252
+ !* source_started,
253
+ "InvalidStateError: Cannot call `start` twice"
254
+ ) ;
255
+ * source_started = true ;
256
+ // Drop the mutable borrow
257
+ }
246
258
247
259
let control = ControlMessage :: StartWithOffsetAndDuration ( start, offset, duration) ;
248
260
self . registration . post_message ( control) ;
@@ -289,32 +301,32 @@ impl AudioBufferSourceNode {
289
301
290
302
/// Defines if the playback the [`AudioBuffer`] should be looped
291
303
pub fn loop_ ( & self ) -> bool {
292
- self . loop_state . borrow ( ) . is_looping
304
+ self . inner_state . borrow ( ) . loop_state . is_looping
293
305
}
294
306
295
307
pub fn set_loop ( & self , value : bool ) {
296
- self . loop_state . borrow_mut ( ) . is_looping = value;
308
+ self . inner_state . borrow_mut ( ) . loop_state . is_looping = value;
297
309
self . registration . post_message ( ControlMessage :: Loop ( value) ) ;
298
310
}
299
311
300
312
/// Defines the loop start point, in the time reference of the [`AudioBuffer`]
301
313
pub fn loop_start ( & self ) -> f64 {
302
- self . loop_state . borrow ( ) . start
314
+ self . inner_state . borrow ( ) . loop_state . start
303
315
}
304
316
305
317
pub fn set_loop_start ( & self , value : f64 ) {
306
- self . loop_state . borrow_mut ( ) . start = value;
318
+ self . inner_state . borrow_mut ( ) . loop_state . start = value;
307
319
self . registration
308
320
. post_message ( ControlMessage :: LoopStart ( value) ) ;
309
321
}
310
322
311
323
/// Defines the loop end point, in the time reference of the [`AudioBuffer`]
312
324
pub fn loop_end ( & self ) -> f64 {
313
- self . loop_state . borrow ( ) . end
325
+ self . inner_state . borrow ( ) . loop_state . end
314
326
}
315
327
316
328
pub fn set_loop_end ( & self , value : f64 ) {
317
- self . loop_state . borrow_mut ( ) . end = value;
329
+ self . inner_state . borrow_mut ( ) . loop_state . end = value;
318
330
self . registration
319
331
. post_message ( ControlMessage :: LoopEnd ( value) ) ;
320
332
}
0 commit comments