Skip to content

Commit 66ca300

Browse files
b-maorottier
authored andcommitted
refactor: simplify initialization
1 parent c48da0a commit 66ca300

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

src/node/waveshaper.rs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,18 @@ impl WaveShaperNode {
157157
/// * `context` - audio context in which the audio node will live.
158158
/// * `options` - waveshaper options
159159
pub fn new<C: BaseAudioContext>(context: &C, options: WaveShaperOptions) -> Self {
160-
context.register(move |registration| {
161-
let WaveShaperOptions {
162-
oversample,
163-
curve,
164-
channel_config,
165-
} = options;
160+
let WaveShaperOptions {
161+
oversample,
162+
curve,
163+
channel_config,
164+
} = options;
166165

166+
let node = context.register(move |registration| {
167167
let sample_rate = context.sample_rate() as usize;
168168
let channel_config = channel_config.into();
169169

170170
let config = RendererConfig {
171171
oversample,
172-
curve: curve.clone(),
173172
sample_rate,
174173
};
175174

@@ -181,15 +180,15 @@ impl WaveShaperNode {
181180
oversample: AtomicU32::new(oversample as u32),
182181
};
183182

184-
if let Some(c) = curve {
185-
// we are sure the OnceCell is empty, cannot fail
186-
let _ = node.curve.set(c);
187-
}
188-
189183
(node, Box::new(renderer))
190-
})
184+
});
185+
186+
// renderer has been sent to render thread, we can sent it messages
187+
if let Some(curve) = curve {
188+
node.set_curve(curve);
189+
}
191190

192-
// @todo - use node.set_curve(curve) here
191+
node
193192
}
194193

195194
/// Returns the distortion curve
@@ -235,17 +234,6 @@ impl WaveShaperNode {
235234
}
236235
}
237236

238-
/// Helper struct which regroups all parameters
239-
/// required to build `WaveShaperRenderer`
240-
struct RendererConfig {
241-
/// oversample factor
242-
oversample: OverSampleType,
243-
/// oversample factor
244-
curve: Option<Vec<f32>>,
245-
/// Sample rate (equals to audio context sample rate)
246-
sample_rate: usize,
247-
}
248-
249237
#[derive(Debug, Clone, PartialEq, Eq)]
250238
struct ResamplerConfig {
251239
channels: usize,
@@ -363,6 +351,15 @@ impl Resampler {
363351
}
364352
}
365353

354+
/// Helper struct which regroups all parameters
355+
/// required to build `WaveShaperRenderer`
356+
struct RendererConfig {
357+
/// oversample factor
358+
oversample: OverSampleType,
359+
/// Sample rate (equals to audio context sample rate)
360+
sample_rate: usize,
361+
}
362+
366363
/// `WaveShaperRenderer` represents the rendering part of `WaveShaperNode`
367364
struct WaveShaperRenderer {
368365
/// oversample factor
@@ -519,12 +516,8 @@ impl WaveShaperRenderer {
519516
let RendererConfig {
520517
sample_rate,
521518
oversample,
522-
curve,
523519
} = config;
524520

525-
let curve_set = curve.is_some();
526-
let curve = curve.unwrap_or(Vec::with_capacity(0));
527-
528521
let channels_x2 = 1;
529522
let channels_x4 = 1;
530523

@@ -540,8 +533,8 @@ impl WaveShaperRenderer {
540533

541534
Self {
542535
oversample,
543-
curve,
544-
curve_set,
536+
curve: Vec::with_capacity(0),
537+
curve_set: false,
545538
sample_rate,
546539
channels_x2,
547540
channels_x4,

0 commit comments

Comments
 (0)