Debian Codegraph @

android/app/jni/extern/SAASound/src/SAANoise.cpp

raw
1 // Part of SAASound copyright 1998-2018 Dave Hooper <[email protected]>
2 //
3 // SAANoise.cpp: implementation of the CSAANoise class.
4 // One noise generator
5 //
6 // After construction, it's important to SetSampleRate before
7 // trying to use the generator.
8 // (Just because the CSAANoise object has a default samplerate
9 // doesn't mean you should rely on it)
10 //
11 //////////////////////////////////////////////////////////////////////
12
13 #include "SAASound.h"
14
15 #include "types.h"
16 #include "SAANoise.h"
17 #include "defns.h"
18
19
20 //////////////////////////////////////////////////////////////////////
21 // Construction/Destruction
22 //////////////////////////////////////////////////////////////////////
23
24 CSAANoise::CSAANoise()
25 :
26 m_nCounter(0),
27 m_nCounter_low(0),
28 m_nOversample(0),
29 m_nCounterLimit_low(1),
30 m_bSync(false),
31 m_nSampleRate(SAMPLE_RATE_HZ),
32 m_nSourceMode(0),
33 m_nRand(1)
34 {
35 _SetClockRate(EXTERNAL_CLK_HZ);
36 m_nAdd = m_nAddBase;
37 }
38
39 CSAANoise::CSAANoise(unsigned int seed)
40 :
41 m_nCounter(0),
42 m_nCounter_low(0),
43 m_nOversample(0),
44 m_nCounterLimit_low(1),
45 m_bSync(false),
46 m_nSampleRate(SAMPLE_RATE_HZ),
47 m_nSourceMode(0),
48 m_nRand(seed)
49 {
50 _SetClockRate(EXTERNAL_CLK_HZ);
51 m_nAdd = m_nAddBase;
52 }
53
54 CSAANoise::~CSAANoise()
55 {
56 // Nothing to do
57 }
58
59 void CSAANoise::_SetClockRate(int nClockRate)
60 {
61 // at 8MHz the clock rate is 31.250kHZ
62 // This is simply the clock rate divided by 256 i.e. 2^8
63 // We then shift this by 2^12 (like the Freq) for better
64 // period accuracy. So that's the same as shifting by (12-8)
65 m_nAddBase = nClockRate << (12 - 8);
66 }
67
68 void CSAANoise::Seed(unsigned int seed)
69 {
70 m_nRand = seed;
71 }
72
73 void CSAANoise::SetSource(int nSource)
74 {
75 m_nSourceMode = nSource;
76 m_nAdd = m_nAddBase >> m_nSourceMode;
77 }
78
79 void CSAANoise::Trigger(void)
80 {
81 // Trigger only does anything useful when we're
82 // clocking from the frequency generator - i.e
83 // if bUseFreqGen = true (i.e. SourceMode = 3)
84
85 // So if we're clocking from the noise generator
86 // clock (ie, SourceMode = 0, 1 or 2) then do nothing
87
88 // No point actually checking m_bSync here ... because if sync is true,
89 // then frequency generators won't actually be generating Trigger pulses
90 // so we wouldn't even get here!
91 // EXCEPT - cool edge case: if sync is set, then actually the Noise Generator
92 // is triggered on EVERY CLOCK PULSE (i.e. 8MHz noise). So indeed it is correct
93 // to not check for sync here. NEEDS TEST CASE.
94
95 if (m_nSourceMode == 3)
96 {
97 ChangeLevel();
98 }
99 }
100
101 void CSAANoise::Tick(void)
102 {
103 // Tick only does anything useful when we're
104 // clocking from the noise generator clock
105 // (ie, SourceMode = 0, 1 or 2)
106
107 // So, if SourceMode = 3 (ie, we're clocking from a
108 // frequency generator ==> bUseFreqGen = true)
109 // then do nothing
110 if ( (!m_bSync) && (m_nSourceMode!=3) )
111 {
112 m_nCounter += m_nAdd;
113 while (m_nCounter >= (m_nSampleRate<<12))
114 {
115 m_nCounter -= (m_nSampleRate<<12);
116 m_nCounter_low++;
117 if (m_nCounter_low >= m_nCounterLimit_low)
118 {
119 m_nCounter_low = 0;
120 ChangeLevel();
121 }
122 }
123 }
124 }
125
126 void CSAANoise::Sync(bool bSync)
127 {
128 if (bSync)
129 {
130 m_nCounter = 0;
131 m_nCounter_low = 0;
132 }
133 m_bSync = bSync;
134 }
135
136
137 void CSAANoise::_SetSampleRate(int nSampleRate)
138 {
139 m_nSampleRate = nSampleRate;
140 }
141
142
143 void CSAANoise::_SetOversample(unsigned int oversample)
144 {
145 // oversample is a power of 2 i.e.
146 // if oversample == 2 then 4x oversample
147 // if oversample == 6 then 64x oversample
148 if (oversample < m_nOversample)
149 {
150 m_nCounter_low <<= (m_nOversample - oversample);
151 }
152 else
153 {
154 m_nCounter_low >>= (oversample - m_nOversample);
155 }
156
157 m_nCounterLimit_low = 1<<oversample;
158 m_nOversample = oversample;
159 }
160
161 inline void CSAANoise::ChangeLevel(void)
162 {
163 /*
164 https://www.vogons.org/viewtopic.php?f=9&t=51695
165 SAA1099P noise generator as documented by Jepael
166 18-bit Galois LFSR
167 Feedback polynomial = x^18 + x^11 + x^1
168 Period = 2^18-1 = 262143 bits
169 Verified to match recorded noise from my SAA1099P
170 */
171
172 if (m_nRand & 1)
173 {
174 m_nRand = (m_nRand >> 1) ^ 0x20400;
175 }
176 else
177 {
178 m_nRand >>= 1;
179 }
180 }