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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright Jeron Aldaron Lau 2018 - 2020.
// Distributed under either the Apache License, Version 2.0
//    (See accompanying file LICENSE_APACHE_2_0.txt or copy at
//          https://apache.org/licenses/LICENSE-2.0),
// or the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_BOOST_1_0.txt or copy at
//          https://www.boost.org/LICENSE_1_0.txt)
// at your option. This file may not be copied, modified, or distributed except
// according to those terms.

use crate::sig::Signal;

fn pnmask(pncnt: u8) -> u8 {
    match pncnt % 16 {
        _x if _x % 2 != 0 => 0x80,
        _x if _x % 4 != 0 => 0x40,
        _x if _x % 8 != 0 => 0x20,
        8 => 0x10, // _x if _x % 16 != 0
        _ => match pncnt / 16 {
            // only 0
            _x if _x % 2 != 0 => 8,
            _x if _x % 4 != 0 => 4,
            _x if _x % 8 != 0 => 2,
            8 => 1, // _x if _x % 16 != 0
            _ => 0, // only 0
        },
    }
}

fn pfir(pfirm: [f64; 6]) -> [i32; 64] {
    let mut pfir = [0; 64];
    for (i, v) in pfir.iter_mut().enumerate() {
        let i = i as i32;
        let a = i / 8;
        let a = a * 8;
        let b = match i % 8 {
            0 => [0, 1, 2, 3, 4, 5],
            b => [b; 6],
        };
        *v = (pfirm[0] * (2 * (a >> b[0] & 1) - 1) as f64
            + pfirm[1] * (2 * (a >> b[1] & 1) - 1) as f64
            + pfirm[2] * (2 * (a >> b[2] & 1) - 1) as f64
            + pfirm[3] * (2 * (a >> b[3] & 1) - 1) as f64
            + pfirm[4] * (2 * (a >> b[4] & 1) - 1) as f64
            + pfirm[5] * (2 * (a >> b[5] & 1) - 1) as f64) as i32
    }
    pfir
}

/// Pink Noise Generator using algorithm described in research paper
/// [A New Shade of Pink](https://github.com/Stenzel/newshadeofpink/blob/master/newshadeofpink.pdf).
#[derive(Clone)]
#[allow(missing_copy_implementations)]
pub struct Pink {
    pfira: [i32; 64],
    pfirb: [i32; 64],
    lfsr: i32,
    inc: i32,
    dec: i32,
    accu: i32,
    pncnt: u8,
    which: u8,
    bit: i32,
}

impl std::fmt::Debug for Pink {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Pink")
    }
}

impl Default for Pink {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

impl Pink {
    /// Create a new Pink Noise Sampler.
    #[inline(always)]
    pub fn new() -> Self {
        Self {
            pfira: pfir([
                2048.0 * 1.190566,
                2048.0 * 0.162580,
                2048.0 * 0.002208,
                2048.0 * 0.025475,
                2048.0 * -0.001522,
                2048.0 * 0.007322,
            ]),
            pfirb: pfir([
                2048.0 * 0.001774,
                2048.0 * 0.004529,
                2048.0 * -0.001561,
                2048.0 * 0.000776,
                2048.0 * -0.000486,
                2048.0 * 0.002017,
            ]),
            lfsr: 0x5eed41f5i32,
            inc: 0xccc,
            dec: 0xccc,
            accu: 0,
            pncnt: 0,
            which: 0,
            bit: 0,
        }
    }

    fn a(&mut self) -> i16 {
        self.bit = self.lfsr >> 31i32;
        self.dec &= !0x800i32;
        self.lfsr <<= 1i32;
        self.dec |= self.inc & 0x800i32;
        self.inc ^= self.bit & 0x800i32;
        self.b()
    }

    fn b(&mut self) -> i16 {
        self.accu += self.inc - self.dec;
        self.lfsr ^= self.bit & 0x46000001i32;
        (self.accu
            + self.pfira[(self.lfsr & 0x3fi32) as usize]
            + self.pfirb[(self.lfsr >> 6i32 & 0x3fi32) as usize]) as i16
    }

    fn c(&mut self) -> i16 {
        self.bit = self.lfsr >> 31i32;
        self.dec &= !0x400i32;
        self.lfsr <<= 1i32;
        self.dec |= self.inc & 0x400i32;
        self.inc ^= self.bit & 0x400i32;
        self.b()
    }

    fn d(&mut self) -> i16 {
        self.bit = self.lfsr >> 31i32;
        self.dec &= !0x200i32;
        self.lfsr <<= 1i32;
        self.dec |= self.inc & 0x200i32;
        self.inc ^= self.bit & 0x200i32;
        self.b()
    }

    fn e(&mut self) -> i16 {
        self.bit = self.lfsr >> 31i32;
        self.dec &= !0x100i32;
        self.lfsr <<= 1i32;
        self.dec |= self.inc & 0x100i32;
        self.inc ^= self.bit & 0x100i32;
        self.b()
    }

    fn f(&mut self, mask: i32) -> i16 {
        self.bit = self.lfsr >> 31i32;
        self.dec &= !mask;
        self.lfsr <<= 1i32;
        self.dec |= self.inc & mask;
        self.inc ^= self.bit & mask;
        self.b()
    }

    /// Get next sample of pink noise.
    pub fn noise(&mut self) -> Signal {
        // Different functions for each sample.
        let r = match self.which {
            _x if _x % 2 != 0 => self.a(), // odd #s
            _x if _x % 4 != 0 => self.c(),
            _x if _x % 8 != 0 => self.d(),
            0 => {
                let mask = pnmask(self.pncnt).into();
                self.pncnt = self.pncnt.wrapping_add(1);
                self.f(mask)
            }
            8 => self.e(),
            _ => unreachable!(),
        } as f64
            / (std::i16::MAX as f64);
        self.which += 1;
        self.which %= 16;
        Signal::from(r)
    }
}