1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! White noise from a small deterministic PRNG—useful for percussion, air, or modulation sources.
use crate::graph::{ProcessContext, Processor, ProcessorInfo, Sig};
/// White noise generator using a linear congruential generator.
/// No external dependencies — deterministic PRNG seeded at construction.
pub struct Noise {
state: u32,
}
impl Noise {
/// Default seed; same instance always produces the same sequence after construction or [`Processor::reset`].
pub fn new() -> Self {
Self { state: 0x12345678 }
}
/// Chooses a starting LCG state (forced odd) so parallel noise nodes can be uncorrelated.
pub fn with_seed(seed: u32) -> Self {
Self { state: seed | 1 }
}
fn next_sample(&mut self) -> f32 {
// LCG: state = state * 1664525 + 1013904223 (Numerical Recipes)
self.state = self.state.wrapping_mul(1664525).wrapping_add(1013904223);
// Map u32 to [-1, 1]
(self.state as f32 / u32::MAX as f32) * 2.0 - 1.0
}
}
impl Default for Noise {
fn default() -> Self {
Self::new()
}
}
impl Processor for Noise {
fn info(&self) -> ProcessorInfo {
ProcessorInfo {
name: "noise",
sig: Sig::SOURCE1,
description: "White noise generator",
}
}
fn process(&mut self, ctx: &mut ProcessContext) {
for i in 0..ctx.frames {
ctx.outputs[0][i] = self.next_sample();
}
}
fn reset(&mut self) {
self.state = 0x12345678;
}
}