Skip to content

Commit 3209aa5

Browse files
committed
AudioBufferSourceNode: Keep inner state in a single RefCell
1 parent 61ba7c0 commit 3209aa5

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/node/audio_buffer_source.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::any::Any;
2-
use std::cell::{Cell, OnceCell, RefCell};
2+
use std::cell::{OnceCell, RefCell};
33

44
use crate::buffer::AudioBuffer;
55
use crate::context::{AudioContextRegistration, AudioParamId, BaseAudioContext};
@@ -101,9 +101,14 @@ pub struct AudioBufferSourceNode {
101101
channel_config: ChannelConfig,
102102
detune: AudioParam, // has constraints, no a-rate
103103
playback_rate: AudioParam, // has constraints, no a-rate
104-
loop_state: RefCell<LoopState>,
105104
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,
107112
}
108113

109114
impl AudioNode for AudioBufferSourceNode {
@@ -141,7 +146,7 @@ impl AudioScheduledSourceNode for AudioBufferSourceNode {
141146

142147
fn stop_at(&self, when: f64) {
143148
assert!(
144-
self.source_started.get(),
149+
self.inner_state.borrow().source_started,
145150
"InvalidStateError cannot stop before start"
146151
);
147152

@@ -205,14 +210,17 @@ impl AudioBufferSourceNode {
205210
ended_triggered: false,
206211
};
207212

213+
let inner_state = InnerState {
214+
loop_state,
215+
source_started: false,
216+
};
208217
let node = Self {
209218
registration,
210219
channel_config: ChannelConfig::default(),
211220
detune: d_param,
212221
playback_rate: pr_param,
213-
loop_state: RefCell::new(loop_state),
214222
buffer: OnceCell::new(),
215-
source_started: Cell::new(false),
223+
inner_state: RefCell::new(inner_state),
216224
};
217225

218226
if let Some(buf) = buffer {
@@ -238,11 +246,15 @@ impl AudioBufferSourceNode {
238246
///
239247
/// Panics if the source was already started
240248
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+
}
246258

247259
let control = ControlMessage::StartWithOffsetAndDuration(start, offset, duration);
248260
self.registration.post_message(control);
@@ -289,32 +301,32 @@ impl AudioBufferSourceNode {
289301

290302
/// Defines if the playback the [`AudioBuffer`] should be looped
291303
pub fn loop_(&self) -> bool {
292-
self.loop_state.borrow().is_looping
304+
self.inner_state.borrow().loop_state.is_looping
293305
}
294306

295307
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;
297309
self.registration.post_message(ControlMessage::Loop(value));
298310
}
299311

300312
/// Defines the loop start point, in the time reference of the [`AudioBuffer`]
301313
pub fn loop_start(&self) -> f64 {
302-
self.loop_state.borrow().start
314+
self.inner_state.borrow().loop_state.start
303315
}
304316

305317
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;
307319
self.registration
308320
.post_message(ControlMessage::LoopStart(value));
309321
}
310322

311323
/// Defines the loop end point, in the time reference of the [`AudioBuffer`]
312324
pub fn loop_end(&self) -> f64 {
313-
self.loop_state.borrow().end
325+
self.inner_state.borrow().loop_state.end
314326
}
315327

316328
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;
318330
self.registration
319331
.post_message(ControlMessage::LoopEnd(value));
320332
}

0 commit comments

Comments
 (0)