1
1
use std:: any:: Any ;
2
- use std:: sync:: atomic:: { AtomicU32 , Ordering } ;
3
- use std:: sync:: OnceLock ;
4
2
5
3
use rubato:: { FftFixedInOut , Resampler as _} ;
6
4
@@ -102,7 +100,7 @@ impl Default for WaveShaperOptions {
102
100
/// post_gain.connect(&context.destination());
103
101
/// post_gain.gain().set_value(1. / drive);
104
102
///
105
- /// let shaper = context.create_wave_shaper();
103
+ /// let mut shaper = context.create_wave_shaper();
106
104
/// shaper.connect(&post_gain);
107
105
/// shaper.set_curve(curve);
108
106
///
@@ -126,9 +124,9 @@ pub struct WaveShaperNode {
126
124
/// Infos about audio node channel configuration
127
125
channel_config : ChannelConfig ,
128
126
/// distortion curve
129
- curve : OnceLock < Vec < f32 > > ,
127
+ curve : Option < Vec < f32 > > ,
130
128
/// oversample type
131
- oversample : AtomicU32 ,
129
+ oversample : OverSampleType ,
132
130
}
133
131
134
132
impl AudioNode for WaveShaperNode {
@@ -163,7 +161,7 @@ impl WaveShaperNode {
163
161
channel_config,
164
162
} = options;
165
163
166
- let node = context. register ( move |registration| {
164
+ let mut node = context. register ( move |registration| {
167
165
let sample_rate = context. sample_rate ( ) as usize ;
168
166
169
167
let renderer = WaveShaperRenderer :: new ( RendererConfig {
@@ -174,8 +172,8 @@ impl WaveShaperNode {
174
172
let node = Self {
175
173
registration,
176
174
channel_config : channel_config. into ( ) ,
177
- curve : OnceLock :: new ( ) ,
178
- oversample : AtomicU32 :: new ( oversample as u32 ) ,
175
+ curve : None ,
176
+ oversample,
179
177
} ;
180
178
181
179
( node, Box :: new ( renderer) )
@@ -192,7 +190,7 @@ impl WaveShaperNode {
192
190
/// Returns the distortion curve
193
191
#[ must_use]
194
192
pub fn curve ( & self ) -> Option < & [ f32 ] > {
195
- self . curve . get ( ) . map ( Vec :: as_slice )
193
+ self . curve . as_deref ( )
196
194
}
197
195
198
196
/// Set the distortion `curve` of this node
@@ -205,29 +203,30 @@ impl WaveShaperNode {
205
203
///
206
204
/// Panics if a curve has already been given to the source (though `new` or through
207
205
/// `set_curve`)
208
- pub fn set_curve ( & self , curve : Vec < f32 > ) {
209
- let clone = curve. clone ( ) ;
210
-
211
- if self . curve . set ( curve) . is_err ( ) {
206
+ pub fn set_curve ( & mut self , curve : Vec < f32 > ) {
207
+ if self . curve . is_some ( ) {
212
208
panic ! ( "InvalidStateError - cannot assign curve twice" ) ;
213
209
}
214
210
211
+ let clone = curve. clone ( ) ;
212
+
213
+ self . curve = Some ( curve) ;
215
214
self . registration . post_message ( Some ( clone) ) ;
216
215
}
217
216
218
217
/// Returns the `oversample` faactor of this node
219
218
#[ must_use]
220
219
pub fn oversample ( & self ) -> OverSampleType {
221
- self . oversample . load ( Ordering :: Acquire ) . into ( )
220
+ self . oversample
222
221
}
223
222
224
223
/// set the `oversample` factor of this node
225
224
///
226
225
/// # Arguments
227
226
///
228
227
/// * `oversample` - the desired `OversampleType` variant
229
- pub fn set_oversample ( & self , oversample : OverSampleType ) {
230
- self . oversample . store ( oversample as u32 , Ordering :: Release ) ;
228
+ pub fn set_oversample ( & mut self , oversample : OverSampleType ) {
229
+ self . oversample = oversample ;
231
230
self . registration . post_message ( oversample) ;
232
231
}
233
232
}
@@ -618,7 +617,7 @@ mod tests {
618
617
..Default :: default ( )
619
618
} ;
620
619
621
- let shaper = WaveShaperNode :: new ( & context, options) ;
620
+ let mut shaper = WaveShaperNode :: new ( & context, options) ;
622
621
assert_eq ! ( shaper. curve( ) , Some ( & [ 1.0 ] [ ..] ) ) ;
623
622
assert_eq ! ( shaper. oversample( ) , OverSampleType :: X2 ) ;
624
623
@@ -641,7 +640,7 @@ mod tests {
641
640
..Default :: default ( )
642
641
} ;
643
642
644
- let shaper = WaveShaperNode :: new ( & context, options) ;
643
+ let mut shaper = WaveShaperNode :: new ( & context, options) ;
645
644
assert_eq ! ( shaper. curve( ) , None ) ;
646
645
assert_eq ! ( shaper. oversample( ) , OverSampleType :: X2 ) ;
647
646
@@ -659,7 +658,7 @@ mod tests {
659
658
let sample_rate = 44100. ;
660
659
let context = OfflineAudioContext :: new ( 1 , 3 * RENDER_QUANTUM_SIZE , sample_rate) ;
661
660
662
- let shaper = context. create_wave_shaper ( ) ;
661
+ let mut shaper = context. create_wave_shaper ( ) ;
663
662
let curve = vec ! [ -0.5 , 0. , 0.5 ] ;
664
663
shaper. set_curve ( curve) ;
665
664
shaper. connect ( & context. destination ( ) ) ;
@@ -697,7 +696,7 @@ mod tests {
697
696
let sample_rate = 44100. ;
698
697
let context = OfflineAudioContext :: new ( 1 , RENDER_QUANTUM_SIZE , sample_rate) ;
699
698
700
- let shaper = context. create_wave_shaper ( ) ;
699
+ let mut shaper = context. create_wave_shaper ( ) ;
701
700
let curve = vec ! [ -0.5 , 0. , 0.5 ] ;
702
701
shaper. set_curve ( curve) ;
703
702
shaper. connect ( & context. destination ( ) ) ;
0 commit comments