libresidfp 1.3.0
Noise.h
1/*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2026 Leandro Nini <drfiemost@users.sourceforge.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#ifndef NOISE_H
22#define NOISE_H
23
24namespace reSIDfp
25{
26
27class Noise
28{
29private:
30 mutable uint32_t m_wnoise = 34653463u;
31 mutable int32_t m_hpnoise = 0;
32
33 uint32_t m_mask;
34 int32_t m_middle;
35
36 static constexpr int defLevel = 5;
37public:
38 Noise()
39 {
40 setLevel(defLevel);
41 }
42
43 void setLevel(int level)
44 {
45 m_mask = (1u << level) - 1;
46 m_middle = 1 << (level - 1);
47 }
48
49 inline int32_t output() const
50 {
51 // white noise
52 m_wnoise = m_wnoise * 1664525u + 1013904223u;
53
54 // Reduce to 8bit signed
55 int32_t n = (int32_t)((m_wnoise >> 20) & m_mask) - m_middle;
56
57 // high-passed noise
58 m_hpnoise = m_hpnoise + ((n - m_hpnoise) / 8);
59
60 return n - m_hpnoise;
61 }
62};
63
64} // namespace reSIDfp
65
66#endif
Definition Noise.h:28