1
1
use std:: any:: Any ;
2
2
use std:: fmt:: Debug ;
3
- use std:: sync:: atomic:: { AtomicU32 , Ordering } ;
4
3
5
4
use crate :: context:: { AudioContextRegistration , AudioParamId , BaseAudioContext } ;
6
5
use crate :: param:: { AudioParam , AudioParamDescriptor , AutomationRate } ;
@@ -109,7 +108,7 @@ enum Schedule {
109
108
///
110
109
/// let context = AudioContext::default();
111
110
///
112
- /// let osc = context.create_oscillator();
111
+ /// let mut osc = context.create_oscillator();
113
112
/// osc.frequency().set_value(200.);
114
113
/// osc.connect(&context.destination());
115
114
/// osc.start();
@@ -131,7 +130,7 @@ pub struct OscillatorNode {
131
130
/// A detuning value (in cents) which will offset the frequency by the given amount.
132
131
detune : AudioParam ,
133
132
/// Waveform of an oscillator
134
- type_ : AtomicU32 ,
133
+ type_ : OscillatorType ,
135
134
}
136
135
137
136
impl AudioNode for OscillatorNode {
@@ -228,12 +227,12 @@ impl OscillatorNode {
228
227
sine_table : precomputed_sine_table ( ) ,
229
228
} ;
230
229
231
- let node = Self {
230
+ let mut node = Self {
232
231
registration,
233
232
channel_config : channel_config. into ( ) ,
234
233
frequency : f_param,
235
234
detune : det_param,
236
- type_ : AtomicU32 :: new ( type_ as u32 ) ,
235
+ type_,
237
236
} ;
238
237
239
238
// if periodic wave has been given, init it
@@ -268,7 +267,7 @@ impl OscillatorNode {
268
267
/// Returns the oscillator type
269
268
#[ must_use]
270
269
pub fn type_ ( & self ) -> OscillatorType {
271
- self . type_ . load ( Ordering :: Acquire ) . into ( )
270
+ self . type_
272
271
}
273
272
274
273
/// Set the oscillator type
@@ -280,29 +279,28 @@ impl OscillatorNode {
280
279
/// # Panics
281
280
///
282
281
/// if `type_` is `OscillatorType::Custom`
283
- pub fn set_type ( & self , type_ : OscillatorType ) {
282
+ pub fn set_type ( & mut self , type_ : OscillatorType ) {
284
283
assert_ne ! (
285
284
type_,
286
285
OscillatorType :: Custom ,
287
286
"InvalidStateError: Custom type cannot be set manually"
288
287
) ;
289
288
290
289
// 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 {
292
291
return ;
293
292
}
294
293
295
- self . type_ . store ( type_ as u32 , Ordering :: Release ) ;
294
+ self . type_ = type_ ;
296
295
self . registration . post_message ( type_) ;
297
296
}
298
297
299
298
/// Sets a `PeriodicWave` which describes a waveform to be used by the oscillator.
300
299
///
301
300
/// Calling this sets the oscillator type to `custom`, once set to `custom`
302
301
/// 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 ;
306
304
self . registration . post_message ( periodic_wave) ;
307
305
}
308
306
}
@@ -613,7 +611,7 @@ mod tests {
613
611
#[ should_panic]
614
612
fn set_type_to_custom_should_panic ( ) {
615
613
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 ( ) ) ;
617
615
osc. set_type ( OscillatorType :: Custom ) ;
618
616
}
619
617
@@ -648,7 +646,7 @@ mod tests {
648
646
..OscillatorOptions :: default ( )
649
647
} ;
650
648
651
- let osc = OscillatorNode :: new ( & context, options) ;
649
+ let mut osc = OscillatorNode :: new ( & context, options) ;
652
650
653
651
osc. set_type ( OscillatorType :: Sine ) ;
654
652
assert_eq ! ( osc. type_( ) , expected_type) ;
@@ -741,7 +739,7 @@ mod tests {
741
739
742
740
let context = OfflineAudioContext :: new ( 1 , sample_rate, sample_rate as f32 ) ;
743
741
744
- let osc = context. create_oscillator ( ) ;
742
+ let mut osc = context. create_oscillator ( ) ;
745
743
osc. connect ( & context. destination ( ) ) ;
746
744
osc. frequency ( ) . set_value ( freq) ;
747
745
osc. set_type ( OscillatorType :: Square ) ;
@@ -779,7 +777,7 @@ mod tests {
779
777
780
778
let context = OfflineAudioContext :: new ( 1 , sample_rate, sample_rate as f32 ) ;
781
779
782
- let osc = context. create_oscillator ( ) ;
780
+ let mut osc = context. create_oscillator ( ) ;
783
781
osc. connect ( & context. destination ( ) ) ;
784
782
osc. frequency ( ) . set_value ( freq) ;
785
783
osc. set_type ( OscillatorType :: Triangle ) ;
@@ -826,7 +824,7 @@ mod tests {
826
824
827
825
let context = OfflineAudioContext :: new ( 1 , sample_rate, sample_rate as f32 ) ;
828
826
829
- let osc = context. create_oscillator ( ) ;
827
+ let mut osc = context. create_oscillator ( ) ;
830
828
osc. connect ( & context. destination ( ) ) ;
831
829
osc. frequency ( ) . set_value ( freq) ;
832
830
osc. set_type ( OscillatorType :: Sawtooth ) ;
@@ -879,7 +877,7 @@ mod tests {
879
877
880
878
let periodic_wave = context. create_periodic_wave ( options) ;
881
879
882
- let osc = context. create_oscillator ( ) ;
880
+ let mut osc = context. create_oscillator ( ) ;
883
881
osc. connect ( & context. destination ( ) ) ;
884
882
osc. set_periodic_wave ( periodic_wave) ;
885
883
osc. frequency ( ) . set_value ( freq) ;
@@ -926,7 +924,7 @@ mod tests {
926
924
927
925
let periodic_wave = context. create_periodic_wave ( options) ;
928
926
929
- let osc = context. create_oscillator ( ) ;
927
+ let mut osc = context. create_oscillator ( ) ;
930
928
osc. connect ( & context. destination ( ) ) ;
931
929
osc. set_periodic_wave ( periodic_wave) ;
932
930
osc. frequency ( ) . set_value ( freq) ;
0 commit comments