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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2021 Weird Constructor <weirdconstructor@gmail.com>
// This file is a part of HexoDSP. Released under GPL-3.0-or-later.
// See README.md and COPYING for details.

use crate::dsp::{
    DspNode, GraphFun, LedPhaseVals, NodeContext, NodeGlobalRef, NodeId, ProcBuf, SAtom,
};
use crate::nodes::{NodeAudioContext, NodeExecContext};
use synfx_dsp::Rng;

#[macro_export]
macro_rules! fa_noise_mode {
    ($formatter: expr, $v: expr, $denorm_v: expr) => {{
        let s = match ($v.round() as usize) {
            0 => "Bipolar",
            1 => "Unipolar",
            _ => "?",
        };
        write!($formatter, "{}", s)
    }};
}

/// A simple noise generator
#[derive(Debug, Clone)]
pub struct Noise {
    seed: u64,
    rng: Rng,
}

impl Noise {
    pub fn new(nid: &NodeId, _node_global: &NodeGlobalRef) -> Self {
        let mut rng = Rng::new();
        rng.seed((0x193a67f4a8a6d769_u64).wrapping_add(0x131415 * (nid.instance() as u64 + 1)));

        Self { seed: nid.instance() as u64, rng }
    }

    pub const atv: &'static str = "Attenuverter input, to attenuate or invert \
        the noise";
    pub const offs: &'static str = "Offset input, that is added to the output \
        signal after attenuvertig it.";
    pub const mode: &'static str = "You can switch between **Bipolar** noise, which \
         uses the full range from **-1** to **1**, or **Unipolar** noise that \
         only uses the range from **0** to **1**.";
    pub const sig: &'static str = "The noise output.";

    pub const DESC: &'static str = r#"Noise Oscillator

This is a very simple noise oscillator, which can be used for any kind of audio rate noise.
And as a source for sample & hold like nodes to generate low frequency modulation. The white
noise is uniformly distributed and not normal distributed (which could be a bit more natural
in some contexts). See also the `XNoise` node for more noise alternatives.
"#;
    pub const HELP: &'static str = r#"A Simple Noise Oscillator

This is a very simple noise oscillator, which can be used for
any kind of audio rate noise. And as a source for sample & hold
like nodes to generate low frequency modulation.

The noise follows a uniform distribution. That means all amplitudes are equally likely to occur.
While it might sound similar, white noise is usually following a normal distribution, which makes
some amplitudes more likely to occur than others.
See also the `XNoise` node for more noise alternatives.

The ~~atv~~ attenuverter and ~~offs~~ parameters control the value range
of the noise, and the ~~mode~~ allows to switch the oscillator between
unipolar and bipolar output.
"#;

    pub fn graph_fun() -> Option<GraphFun> {
        None
    }
}

impl DspNode for Noise {
    fn set_sample_rate(&mut self, _srate: f32) {}

    fn reset(&mut self) {
        self.rng.seed((0x193a67f4a8a6d769_u64).wrapping_add(0x131415 * (self.seed + 1)));
    }

    #[inline]
    fn process(
        &mut self,
        ctx: &mut dyn NodeAudioContext,
        _ectx: &mut NodeExecContext,
        _nctx: &NodeContext,
        atoms: &[SAtom],
        inputs: &[ProcBuf],
        outputs: &mut [ProcBuf],
        ctx_vals: LedPhaseVals,
    ) {
        use crate::dsp::{at, denorm, inp, out};

        let mode = at::Noise::mode(atoms);
        let atv = inp::Noise::atv(inputs);
        let offs = inp::Noise::offs(inputs);
        let out = out::Noise::sig(outputs);

        let rng = &mut self.rng;

        if mode.i() == 0 {
            for frame in 0..ctx.nframes() {
                let s = (rng.next() * 2.0) - 1.0;
                let s = s * denorm::Noise::atv(atv, frame) + denorm::Noise::offs(offs, frame);
                out.write(frame, s);
            }
        } else {
            for frame in 0..ctx.nframes() {
                let s =
                    rng.next() * denorm::Noise::atv(atv, frame) + denorm::Noise::offs(offs, frame);
                out.write(frame, s);
            }
        }

        let last_frame = ctx.nframes() - 1;
        ctx_vals[0].set(out.read(last_frame));
    }
}