Debian Codegraph @

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

raw
1 // Part of SAASound copyright 1998-2018 Dave Hooper <[email protected]>
2 //
3 // SAAFreq.cpp: implementation of the CSAAFreq class.
4 // only 7-bit fractional accuracy on oscillator periods. I may consider fixing that.
5 //
6 //////////////////////////////////////////////////////////////////////
7
8 #include "SAASound.h"
9 #include "types.h"
10 #include "SAANoise.h"
11 #include "SAAEnv.h"
12 #include "SAAFreq.h"
13 #include "defns.h"
14
15 const int INITIAL_LEVEL = 1;
16
17 //////////////////////////////////////////////////////////////////////
18 // Construction/Destruction
19 //////////////////////////////////////////////////////////////////////
20
21 CSAAFreq::CSAAFreq(CSAANoise * const NoiseGenerator, CSAAEnv * const EnvGenerator)
22 :
23 m_nClockRate(0), m_nCounter(0), m_nAdd(0), m_nCounter_low(0),
24 m_nOversample(0),
25 m_nCounterLimit_low(1), m_nLevel(INITIAL_LEVEL),
26 m_nCurrentOffset(0), m_nCurrentOctave(0), m_nNextOffset(0), m_nNextOctave(0),
27 m_bIgnoreOffsetData(false), m_bNewData(false),
28 m_bSync(false),
29 m_nSampleRate(SAMPLE_RATE_HZ),
30 m_pcConnectedNoiseGenerator(NoiseGenerator),
31 m_pcConnectedEnvGenerator(EnvGenerator),
32 m_nConnectedMode((NoiseGenerator == NULL) ? ((EnvGenerator == NULL) ? 0 : 1) : 2)
33 {
34 _SetClockRate(EXTERNAL_CLK_HZ);
35 SetAdd(); // current octave, current offset
36 }
37
38 CSAAFreq::~CSAAFreq()
39 {
40 // Nothing to do
41 }
42
43 void CSAAFreq::SetFreqOffset(BYTE nOffset)
44 {
45 // nOffset between 0 and 255
46
47 if (!m_bSync)
48 {
49 m_nNextOffset = nOffset;
50 m_bNewData=true;
51 if (m_nNextOctave==m_nCurrentOctave)
52 {
53 // According to Philips, if you send the SAA-1099
54 // new Octave data and then new Offset data in that
55 // order, on the next half-cycle of the current frequency
56 // generator, ONLY the octave data is acted upon.
57 // The offset data will be acted upon next time.
58
59 // ?? TEST CASE : if you set the octave and then the offset
60 // but the octave you set it to is the same one it already was.
61 // Will this ignore the offset data?
62 // Do you get the same behaviour if you set offset THEN octave
63 // even if you set octave to the same value it was before?
64
65 m_bIgnoreOffsetData=true;
66 }
67 }
68 else
69 {
70 // updates straightaway if m_bSync
71 m_bNewData=false;
72 m_bIgnoreOffsetData = false;
73 m_nCurrentOffset = nOffset;
74 m_nNextOffset = nOffset;
75 m_nCurrentOctave = m_nNextOctave;
76 SetAdd();
77 }
78
79 }
80
81 void CSAAFreq::SetFreqOctave(BYTE nOctave)
82 {
83 // nOctave between 0 and 7
84
85 if (!m_bSync)
86 {
87 m_nNextOctave = nOctave;
88 m_bNewData=true;
89 m_bIgnoreOffsetData = false;
90 }
91 else
92 {
93 // updates straightaway if m_bSync
94 m_bNewData=false;
95 m_bIgnoreOffsetData = false;
96 m_nCurrentOctave = nOctave;
97 m_nNextOctave = nOctave;
98 m_nCurrentOffset = m_nNextOffset;
99 SetAdd();
100 }
101 }
102
103 void CSAAFreq::UpdateOctaveOffsetData(void)
104 {
105 // loads the buffered new octave and new offset data into the current registers
106 // and sets up the new frequency for this frequency generator (i.e. sets up m_nAdd)
107 // - called during Sync, and called when waveform half-cycle completes
108
109 // How the SAA-1099 really treats new data:
110 // if only new octave data is present,
111 // then set new period based on just the octave data
112 // Otherwise, if only new offset data is present,
113 // then set new period based on just the offset data
114 // Otherwise, if new octave data is present, and new offset data is present,
115 // and the offset data was set BEFORE the octave data,
116 // then set new period based on both the octave and offset data
117 // Else, if the offset data came AFTER the new octave data
118 // then set new period based on JUST THE OCTAVE DATA, and continue
119 // signalling the offset data as 'new', so it will be acted upon
120 // next half-cycle
121 //
122 // Weird, I know. But that's how it works. Philips even documented as much.
123
124 if (!m_bNewData)
125 {
126 // optimise for the most common case! No new data!
127 return;
128 }
129
130 m_nCurrentOctave=m_nNextOctave;
131 if (!m_bIgnoreOffsetData)
132 {
133 m_nCurrentOffset=m_nNextOffset;
134 m_bNewData=false;
135 }
136 m_bIgnoreOffsetData=false;
137
138 SetAdd();
139 }
140
141 void CSAAFreq::_SetSampleRate(unsigned int nSampleRate)
142 {
143 m_nSampleRate = nSampleRate;
144 }
145
146 void CSAAFreq::_SetOversample(unsigned int oversample)
147 {
148 // oversample is a power of 2 i.e.
149 // if oversample == 2 then 4x oversample
150 // if oversample == 6 then 64x oversample
151 if (oversample < m_nOversample)
152 {
153 m_nCounter_low <<= (m_nOversample - oversample);
154 }
155 else
156 {
157 m_nCounter_low >>= (oversample - m_nOversample);
158 }
159
160 m_nCounterLimit_low = 1<<oversample;
161 m_nOversample = oversample;
162 }
163
164 #ifdef SAAFREQ_FIXED_CLOCKRATE
165 void CSAAFreq::_SetClockRate(int nClockRate)
166 {
167 // if SAAFREQ clock rate is hardcoded, then we don't support dynamically
168 // adjusting the SAA clock rate, so this is a no-op
169 }
170 #else
171 void CSAAFreq::_SetClockRate(int nClockRate)
172 {
173 // initialise the frequency table based on the SAA clockrate
174 // Each item in m_FreqTable corresponds to the frequency calculated by
175 // the standard formula (15625 << octave) / (511 - offset)
176 // then multiplied by 8192 (and represented as a int integer value).
177 // We are therefore using 12 bits (i.e. 2^12 = 4096) as fractional part.
178 // The reason we multiply by 8192, not 4096, is that we use this as a counter
179 // to toggle the oscillator state, so we need to count half-waves (i.e. twice
180 // the frequency)
181 //
182 // Finally, note that the standard formula corresponds to a 8MHz base clock
183 // so we rescale the final result by the ratio nClockRate/8000000
184
185 if (nClockRate != (int)m_nClockRate)
186 {
187 m_nClockRate = nClockRate;
188 int ix = 0;
189 for (int nOctave = 0; nOctave < 8; nOctave++)
190 for (int nOffset = 0; nOffset < 256; nOffset++)
191 m_FreqTable[ix++] = (unsigned int)((8192.0 * 15625.0 * double(1 << nOctave) * (double(nClockRate) / 8000000.0)) / (511.0 - double(nOffset)));
192 }
193 }
194 #endif
195
196 int CSAAFreq::Tick(void)
197 {
198 // set to the absolute level (0 or 1)
199 if (m_bSync)
200 return 1;
201
202 m_nCounter += m_nAdd;
203 while (m_nCounter >= (m_nSampleRate<<12))
204 {
205 m_nCounter -= (m_nSampleRate<<12);
206 m_nCounter_low++;
207 if (m_nCounter_low >= m_nCounterLimit_low)
208 {
209 // period elapsed for (at least) one half-cycle of
210 // current frequency
211 m_nCounter_low = 0;
212 // flip state - from 0 to 1 or vice versa
213 m_nLevel = 1 - m_nLevel;
214
215 // trigger any connected devices
216 switch (m_nConnectedMode)
217 {
218 case 1:
219 // env trigger
220 m_pcConnectedEnvGenerator->InternalClock();
221 break;
222
223 case 2:
224 // noise trigger
225 m_pcConnectedNoiseGenerator->Trigger();
226 break;
227
228 default:
229 // do nothing
230 break;
231 }
232
233 // get new frequency (set period length m_nAdd) if new data is waiting:
234 UpdateOctaveOffsetData();
235 }
236 }
237
238 return m_nLevel;
239 }
240
241 void CSAAFreq::SetAdd(void)
242 {
243 // nOctave between 0 and 7; nOffset between 0 and 255
244
245 // Used to be:
246 // m_nAdd = (15625 << nOctave) / (511 - nOffset);
247 // Now just table lookup:
248 m_nAdd = m_FreqTable[m_nCurrentOctave<<8 | m_nCurrentOffset];
249 }
250
251 void CSAAFreq::Sync(bool bSync)
252 {
253 m_bSync = bSync;
254
255 // update straightaway if m_bSync
256 if (m_bSync)
257 {
258 m_nCounter = 0;
259 m_nCounter_low = 0;
260
261 // this seems to need to be required to make the Fred59 SPACE DEMO audio work correctly
262 m_nLevel = INITIAL_LEVEL;
263
264 m_nCurrentOctave=m_nNextOctave;
265 m_nCurrentOffset=m_nNextOffset;
266 SetAdd();
267 }
268 }