Skip to content

Commit fb985f9

Browse files
b-maorottier
authored andcommitted
refactor: align message passing strategy with AudioBufferSourceNode
1 parent 51d50c7 commit fb985f9

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

src/node/oscillator.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::any::Any;
22
use std::fmt::Debug;
33
use std::sync::atomic::{AtomicU32, Ordering};
4-
use std::sync::Arc;
54

65
use crate::context::{AudioContextRegistration, AudioParamId, BaseAudioContext};
76
use crate::param::{AudioParam, AudioParamDescriptor, AutomationRate};
@@ -132,7 +131,7 @@ pub struct OscillatorNode {
132131
/// A detuning value (in cents) which will offset the frequency by the given amount.
133132
detune: AudioParam,
134133
/// Waveform of an oscillator
135-
shared_type: Arc<AtomicU32>,
134+
type_: AtomicU32,
136135
}
137136

138137
impl AudioNode for OscillatorNode {
@@ -215,11 +214,8 @@ impl OscillatorNode {
215214
let (det_param, det_proc) = context.create_audio_param(det_param_opts, &registration);
216215
det_param.set_value(detune);
217216

218-
let shared_type = Arc::new(AtomicU32::new(type_ as u32));
219-
220217
let renderer = OscillatorRenderer {
221218
type_,
222-
shared_type: Arc::clone(&shared_type),
223219
frequency: f_proc,
224220
detune: det_proc,
225221
phase: 0.,
@@ -236,7 +232,7 @@ impl OscillatorNode {
236232
channel_config: channel_config.into(),
237233
frequency: f_param,
238234
detune: det_param,
239-
shared_type,
235+
type_: AtomicU32::new(type_ as u32),
240236
};
241237

242238
// if periodic wave has been given, init it
@@ -271,7 +267,7 @@ impl OscillatorNode {
271267
/// Returns the oscillator type
272268
#[must_use]
273269
pub fn type_(&self) -> OscillatorType {
274-
self.shared_type.load(Ordering::Acquire).into()
270+
self.type_.load(Ordering::Acquire).into()
275271
}
276272

277273
/// Set the oscillator type
@@ -291,10 +287,11 @@ impl OscillatorNode {
291287
);
292288

293289
// if periodic wave has been set specified, type_ changes are ignored
294-
if self.shared_type.load(Ordering::Acquire) == OscillatorType::Custom as u32 {
290+
if self.type_.load(Ordering::Acquire) == OscillatorType::Custom as u32 {
295291
return;
296292
}
297293

294+
self.type_.store(type_ as u32, Ordering::Release);
298295
self.registration.post_message(type_);
299296
}
300297

@@ -303,11 +300,8 @@ impl OscillatorNode {
303300
/// Calling this sets the oscillator type to `custom`, once set to `custom`
304301
/// the oscillator cannot be reverted back to a standard waveform.
305302
pub fn set_periodic_wave(&self, periodic_wave: PeriodicWave) {
306-
// Already store the oscillator type, so you can't call set_type with conflicting settings
307-
// immediately after this call.
308-
self.shared_type
303+
self.type_
309304
.store(OscillatorType::Custom as u32, Ordering::Release);
310-
311305
self.registration.post_message(periodic_wave);
312306
}
313307
}
@@ -316,8 +310,6 @@ impl OscillatorNode {
316310
struct OscillatorRenderer {
317311
/// The shape of the periodic waveform
318312
type_: OscillatorType,
319-
/// The shape of the periodic waveform (shared with control thread)
320-
shared_type: Arc<AtomicU32>,
321313
/// The frequency of the fundamental frequency.
322314
frequency: AudioParamId,
323315
/// A detuning value (in cents) which will offset the frequency by the given amount.
@@ -439,7 +431,6 @@ impl AudioProcessor for OscillatorRenderer {
439431

440432
fn onmessage(&mut self, msg: &mut dyn Any) {
441433
if let Some(&type_) = msg.downcast_ref::<OscillatorType>() {
442-
self.shared_type.store(type_ as u32, Ordering::Release);
443434
self.type_ = type_;
444435
return;
445436
}

0 commit comments

Comments
 (0)