Skip to content

Commit 45b7afc

Browse files
committed
OscillatorNode: don't use atomics for interior mutability
1 parent 0f5250c commit 45b7afc

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

examples/benchmarks.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ fn main() {
400400
let env = context.create_gain();
401401
env.connect(&context.destination());
402402

403-
let osc = context.create_oscillator();
403+
let mut osc = context.create_oscillator();
404404
osc.connect(&env);
405405
osc.set_type(OscillatorType::Sawtooth);
406406
osc.frequency().set_value(110.);
@@ -430,7 +430,7 @@ fn main() {
430430
let env = context.create_gain();
431431
env.connect(&context.destination());
432432

433-
let osc = context.create_oscillator();
433+
let mut osc = context.create_oscillator();
434434
osc.connect(&env);
435435
osc.set_type(OscillatorType::Sawtooth);
436436
osc.frequency().set_value(110.);
@@ -453,7 +453,7 @@ fn main() {
453453
let duration = DURATION as f64;
454454

455455
while offset < duration {
456-
let osc = context.create_oscillator();
456+
let mut osc = context.create_oscillator();
457457
osc.connect(&context.destination());
458458
osc.set_type(OscillatorType::Sawtooth);
459459
osc.frequency().set_value(110.);
@@ -482,7 +482,7 @@ fn main() {
482482
env.connect(&filter);
483483
env.gain().set_value_at_time(0., 0.);
484484

485-
let osc = context.create_oscillator();
485+
let mut osc = context.create_oscillator();
486486
osc.connect(&env);
487487
osc.set_type(OscillatorType::Sawtooth);
488488
osc.frequency().set_value(110.);
@@ -547,7 +547,7 @@ fn main() {
547547

548548
let context = OfflineAudioContext::new(2, DURATION * sample_rate as usize, sample_rate);
549549

550-
let osc = context.create_oscillator();
550+
let mut osc = context.create_oscillator();
551551
osc.connect(&context.destination());
552552
osc.set_type(OscillatorType::Sawtooth);
553553
osc.frequency().set_value(2000.);

examples/oscillators.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn main() {
2828
});
2929

3030
// Create an oscillator node with sine (default) type
31-
let osc = context.create_oscillator();
31+
let mut osc = context.create_oscillator();
3232

3333
// Connect osc to the destination node which is the default output device
3434
osc.connect(&context.destination());

src/node/oscillator.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::any::Any;
22
use std::fmt::Debug;
3-
use std::sync::atomic::{AtomicU32, Ordering};
43

54
use crate::context::{AudioContextRegistration, AudioParamId, BaseAudioContext};
65
use crate::param::{AudioParam, AudioParamDescriptor, AutomationRate};
@@ -109,7 +108,7 @@ enum Schedule {
109108
///
110109
/// let context = AudioContext::default();
111110
///
112-
/// let osc = context.create_oscillator();
111+
/// let mut osc = context.create_oscillator();
113112
/// osc.frequency().set_value(200.);
114113
/// osc.connect(&context.destination());
115114
/// osc.start();
@@ -131,7 +130,7 @@ pub struct OscillatorNode {
131130
/// A detuning value (in cents) which will offset the frequency by the given amount.
132131
detune: AudioParam,
133132
/// Waveform of an oscillator
134-
type_: AtomicU32,
133+
type_: OscillatorType,
135134
}
136135

137136
impl AudioNode for OscillatorNode {
@@ -228,12 +227,12 @@ impl OscillatorNode {
228227
sine_table: precomputed_sine_table(),
229228
};
230229

231-
let node = Self {
230+
let mut node = Self {
232231
registration,
233232
channel_config: channel_config.into(),
234233
frequency: f_param,
235234
detune: det_param,
236-
type_: AtomicU32::new(type_ as u32),
235+
type_,
237236
};
238237

239238
// if periodic wave has been given, init it
@@ -268,7 +267,7 @@ impl OscillatorNode {
268267
/// Returns the oscillator type
269268
#[must_use]
270269
pub fn type_(&self) -> OscillatorType {
271-
self.type_.load(Ordering::Acquire).into()
270+
self.type_
272271
}
273272

274273
/// Set the oscillator type
@@ -280,29 +279,28 @@ impl OscillatorNode {
280279
/// # Panics
281280
///
282281
/// if `type_` is `OscillatorType::Custom`
283-
pub fn set_type(&self, type_: OscillatorType) {
282+
pub fn set_type(&mut self, type_: OscillatorType) {
284283
assert_ne!(
285284
type_,
286285
OscillatorType::Custom,
287286
"InvalidStateError: Custom type cannot be set manually"
288287
);
289288

290289
// if periodic wave has been set specified, type_ changes are ignored
291-
if self.type_.load(Ordering::Acquire) == OscillatorType::Custom as u32 {
290+
if self.type_ == OscillatorType::Custom {
292291
return;
293292
}
294293

295-
self.type_.store(type_ as u32, Ordering::Release);
294+
self.type_ = type_;
296295
self.registration.post_message(type_);
297296
}
298297

299298
/// Sets a `PeriodicWave` which describes a waveform to be used by the oscillator.
300299
///
301300
/// Calling this sets the oscillator type to `custom`, once set to `custom`
302301
/// the oscillator cannot be reverted back to a standard waveform.
303-
pub fn set_periodic_wave(&self, periodic_wave: PeriodicWave) {
304-
self.type_
305-
.store(OscillatorType::Custom as u32, Ordering::Release);
302+
pub fn set_periodic_wave(&mut self, periodic_wave: PeriodicWave) {
303+
self.type_ = OscillatorType::Custom;
306304
self.registration.post_message(periodic_wave);
307305
}
308306
}
@@ -613,7 +611,7 @@ mod tests {
613611
#[should_panic]
614612
fn set_type_to_custom_should_panic() {
615613
let context = OfflineAudioContext::new(2, 1, 44_100.);
616-
let osc = OscillatorNode::new(&context, OscillatorOptions::default());
614+
let mut osc = OscillatorNode::new(&context, OscillatorOptions::default());
617615
osc.set_type(OscillatorType::Custom);
618616
}
619617

@@ -648,7 +646,7 @@ mod tests {
648646
..OscillatorOptions::default()
649647
};
650648

651-
let osc = OscillatorNode::new(&context, options);
649+
let mut osc = OscillatorNode::new(&context, options);
652650

653651
osc.set_type(OscillatorType::Sine);
654652
assert_eq!(osc.type_(), expected_type);
@@ -741,7 +739,7 @@ mod tests {
741739

742740
let context = OfflineAudioContext::new(1, sample_rate, sample_rate as f32);
743741

744-
let osc = context.create_oscillator();
742+
let mut osc = context.create_oscillator();
745743
osc.connect(&context.destination());
746744
osc.frequency().set_value(freq);
747745
osc.set_type(OscillatorType::Square);
@@ -779,7 +777,7 @@ mod tests {
779777

780778
let context = OfflineAudioContext::new(1, sample_rate, sample_rate as f32);
781779

782-
let osc = context.create_oscillator();
780+
let mut osc = context.create_oscillator();
783781
osc.connect(&context.destination());
784782
osc.frequency().set_value(freq);
785783
osc.set_type(OscillatorType::Triangle);
@@ -826,7 +824,7 @@ mod tests {
826824

827825
let context = OfflineAudioContext::new(1, sample_rate, sample_rate as f32);
828826

829-
let osc = context.create_oscillator();
827+
let mut osc = context.create_oscillator();
830828
osc.connect(&context.destination());
831829
osc.frequency().set_value(freq);
832830
osc.set_type(OscillatorType::Sawtooth);
@@ -879,7 +877,7 @@ mod tests {
879877

880878
let periodic_wave = context.create_periodic_wave(options);
881879

882-
let osc = context.create_oscillator();
880+
let mut osc = context.create_oscillator();
883881
osc.connect(&context.destination());
884882
osc.set_periodic_wave(periodic_wave);
885883
osc.frequency().set_value(freq);
@@ -926,7 +924,7 @@ mod tests {
926924

927925
let periodic_wave = context.create_periodic_wave(options);
928926

929-
let osc = context.create_oscillator();
927+
let mut osc = context.create_oscillator();
930928
osc.connect(&context.destination());
931929
osc.set_periodic_wave(periodic_wave);
932930
osc.frequency().set_value(freq);

src/periodic_wave.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub struct PeriodicWaveOptions {
5959
///
6060
/// let periodic_wave = PeriodicWave::new(&context, options);
6161
///
62-
/// let osc = context.create_oscillator();
62+
/// let mut osc = context.create_oscillator();
6363
/// osc.set_periodic_wave(periodic_wave);
6464
/// osc.connect(&context.destination());
6565
/// osc.start();

0 commit comments

Comments
 (0)