Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/src/nodes/audio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod fir;
pub mod hadamard;
pub mod householder;
pub mod mixer;
pub mod noise;
pub mod onepole;
pub mod ops;
pub mod oversample;
Expand Down
69 changes: 69 additions & 0 deletions crates/src/nodes/audio/noise.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::{
builder::{ResourceBuilderView, ValidationError},
dsl::ir::DSLParams,
node::{DynNode, Node},
ports::{PortBuilder, Ports},
spec::NodeDefinition,
};

#[derive(Clone)]
pub struct Noise {
state: u32,
ports: Ports,
}

impl Noise {
pub fn new() -> Self {
Self {
state: 0xBAADF00D,
ports: PortBuilder::default().audio_out(1).build(),
}
}

// Yields the next pseudo-random u32 val
#[inline(always)]
fn next_val(&mut self) -> u32 {
self.state ^= self.state << 13;
self.state ^= self.state >> 17;
self.state ^= self.state << 5;
self.state
}

#[inline(always)]
pub fn white(&mut self) -> f32 {
// Map u32 to -1,1
// TODO: Is there something with less ops?
// TODO: Could I get subnormals here?
(self.next_val() as i32 as f32) * (1.0 / i32::MAX as f32)
}
}

impl Node for Noise {
fn ports(&self) -> &Ports {
&self.ports
}
fn process(
&mut self,
_ctx: &mut crate::context::AudioContext,
_inputs: &crate::node::Inputs,
outputs: &mut [&mut [f32]],
) {
if let Some(out) = outputs.get_mut(0) {
out.iter_mut().for_each(|x| *x = self.white())
}
}
}

impl NodeDefinition for Noise {
const NAME: &'static str = "noise";
const DESCRIPTION: &'static str = "A basic noise generator";
const REQUIRED_PARAMS: &'static [&'static str] = &[];
const OPTIONAL_PARAMS: &'static [&'static str] = &[];

fn create(
_rb: &mut ResourceBuilderView,
_p: &DSLParams,
) -> Result<Box<dyn DynNode>, ValidationError> {
Ok(Box::new(Self::new()))
}
}
2 changes: 2 additions & 0 deletions crates/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
hadamard::HadamardMixer,
householder::HouseholderMixer,
mixer::{MonoFanOut, TrackMixer},
noise::Noise,
onepole::OnePole,
ops::{AddDef, DivDef, GainDef, MultDef, SubDef},
pan::Pan,
Expand Down Expand Up @@ -105,6 +106,7 @@ pub fn audio_registry_factory() -> NodeRegistry {
registry.register_node::<Pan>();
registry.register_node::<MonoFanOut>();
registry.register_node::<Sweep>();
registry.register_node::<Noise>();
registry.register_node::<OnePole>();
registry.register_node::<Allpass>();
registry.register_node::<HadamardMixer>();
Expand Down
Loading