Debian Codegraph @

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

raw
1 // Part of SAASound copyright 1998-2018 Dave Hooper <[email protected]>
2 //
3 // SAAImpl.cpp: implementation of the CSAASound class.
4 // the bones of the 'virtual SAA-1099' emulation
5 //
6 // the actual sound generation is carried out in the other classes;
7 // this class provides the output stage and the external interface only
8 //
9 //////////////////////////////////////////////////////////////////////
10
11 #include "SAASound.h"
12
13 #include "types.h"
14 #include "SAAImpl.h"
15 #include "defns.h"
16
17 //////////////////////////////////////////////////////////////////////
18 // Construction/Destruction
19 //////////////////////////////////////////////////////////////////////
20
21 CSAASoundInternal::CSAASoundInternal()
22 :
23 m_chip(),
24 m_uParam(0),
25 m_uParamRate(0),
26 m_nClockRate(EXTERNAL_CLK_HZ),
27 m_nSampleRate(SAMPLE_RATE_HZ),
28 m_nOversample(DEFAULT_OVERSAMPLE),
29 m_bHighpass(false),
30 filterout_z1_left_mixed(0),
31 filterout_z1_right_mixed(0)
32 {
33 #ifdef USE_CONFIG_FILE
34 m_Config.ReadConfig();
35 #endif
36
37 #if defined(DEBUGSAA)
38 m_dbgfile.open(_T(DEBUG_SAA_REGISTER_LOG), std::ios_base::out);
39 m_pcmfile.open(_T(DEBUG_SAA_PCM_LOG), std::ios_base::out | std::ios_base::binary);
40 #elif defined(USE_CONFIG_FILE)
41 if (m_Config.m_bGenerateRegisterLogs)
42 m_dbgfile.open(m_Config.m_strRegisterLogPath, std::ios_base::out);
43 if (m_Config.m_bGeneratePcmLogs)
44 m_pcmfile.open(m_Config.m_strPcmOutputPath, std::ios_base::out | std::ios_base::binary);
45
46 if (m_Config.m_bGeneratePcmLogs && m_Config.m_bGeneratePcmSeparateChannels)
47 {
48 for (int i = 0; i < 6; i++)
49 {
50 m_channel_pcmfile[i].open(m_Config.getChannelPcmOutputPath(i), std::ios_base::out | std::ios_base::binary);
51 }
52 }
53
54
55 #endif
56 // set parameters
57 // TODO support defaults and overrides from config file
58 // m_chip.SetSoundParameters(SAAP_FILTER | SAAP_11025 | SAAP_8BIT | SAAP_MONO);
59 // reset the virtual SAA
60 // m_chip.Clear();
61
62 m_chip._SetClockRate(m_nClockRate);
63 m_chip._SetOversample(m_nOversample);
64 }
65
66 CSAASoundInternal::~CSAASoundInternal()
67 {
68 //
69 }
70
71 //////////////////////////////////////////////////////////////////////
72 // CSAASound members
73 //////////////////////////////////////////////////////////////////////
74
75 void CSAASoundInternal::SetClockRate(unsigned int nClockRate)
76 {
77 m_nClockRate = nClockRate;
78 m_chip._SetClockRate(m_nClockRate);
79 }
80
81 void CSAASoundInternal::Clear(void)
82 {
83 // reinitialises virtual SAA:
84 // sets reg 28 to 0x02; - sync and disabled
85 // sets regs 00-31 (except 28) to 0x00;
86 // sets reg 28 to 0x00;
87 // sets current reg to 0
88 WriteAddressData(28,2);
89 for (int i=31; i>=0; i--)
90 {
91 if (i!=28) WriteAddressData(i,0);
92 }
93 WriteAddressData(28,0);
94 WriteAddress(0);
95 }
96
97 void CSAASoundInternal::WriteData(BYTE nData)
98 {
99 // originated from an OUT 255,d call
100 m_chip._WriteData(nData);
101 #if defined(DEBUGSAA) || defined(USE_CONFIG_FILE)
102 #ifdef USE_CONFIG_FILE
103 if (m_Config.m_bGenerateRegisterLogs)
104 {
105 #endif
106 m_dbgfile << m_nDebugSample << " " << (int)m_chip._ReadAddress() << ":" << (int)nData << std::endl;
107 #ifdef USE_CONFIG_FILE
108 }
109 #endif
110 #endif
111 }
112
113 void CSAASoundInternal::WriteAddress(BYTE nReg)
114 {
115 // originated from an OUT 511,r call
116 m_chip._WriteAddress(nReg);
117 #if defined(DEBUGSAA) || defined(USE_CONFIG_FILE)
118 #ifdef USE_CONFIG_FILE
119 if (m_Config.m_bGenerateRegisterLogs)
120 {
121 #endif
122 m_dbgfile << m_nDebugSample << " " << (int)nReg << ":";
123 if (nReg==24)
124 {
125 m_dbgfile << "<!ENVO!>";
126 }
127 else if (nReg==25)
128 {
129 m_dbgfile << "<!ENV1!>";
130 }
131 m_dbgfile << std::endl;
132 #ifdef USE_CONFIG_FILE
133 }
134 #endif
135 #endif
136 }
137
138 void CSAASoundInternal::WriteAddressData(BYTE nReg, BYTE nData)
139 {
140 // performs WriteAddress(nReg) followed by WriteData(nData)
141 m_chip._WriteAddress(nReg);
142 m_chip._WriteData(nData);
143 }
144
145 #ifdef DEBUG
146 BYTE CSAASoundInternal::ReadAddress(void)
147 {
148 // Not a real hardware function of the SAA-1099, which is write-only
149 return(m_chip._ReadAddress());
150 }
151 #else
152 BYTE CSAASoundInternal::ReadAddress(void)
153 {
154 // Not a real hardware function of the SAA-1099, which is write-only
155 return(0);
156 }
157 #endif
158
159 void CSAASoundInternal::SetSoundParameters(SAAPARAM uParam)
160 {
161 // set samplerate properties from uParam (deprecated but still supported)
162 unsigned int nSampleRate = m_nSampleRate;
163 switch (uParam & SAAP_MASK_SAMPLERATE)
164 {
165 case SAAP_44100:
166 nSampleRate = 44100;
167 m_uParamRate = (m_uParamRate & ~SAAP_MASK_SAMPLERATE) | SAAP_44100;
168 break;
169 case SAAP_22050:
170 nSampleRate = 22050;
171 m_uParamRate = (m_uParamRate & ~SAAP_MASK_SAMPLERATE) | SAAP_22050;
172 break;
173 case SAAP_11025:
174 nSampleRate = 11025;
175 m_uParamRate = (m_uParamRate & ~SAAP_MASK_SAMPLERATE) | SAAP_11025;
176 break;
177 case 0:// change nothing!
178 default:
179 break;
180 }
181
182 if (nSampleRate != m_nSampleRate)
183 {
184 m_nSampleRate = nSampleRate;
185 m_chip._SetSampleRate(m_nSampleRate);
186 }
187
188 // set filter properties from uParam
189 m_uParam = (m_uParam & ~SAAP_MASK_FILTER) | (uParam & SAAP_MASK_FILTER);
190 }
191
192 void CSAASoundInternal::SetSampleRate(unsigned int nSampleRate)
193 {
194 if (nSampleRate != m_nSampleRate)
195 {
196 m_nSampleRate = nSampleRate;
197 m_chip._SetSampleRate(m_nSampleRate);
198 }
199 }
200
201 void CSAASoundInternal::SetOversample(unsigned int nOversample)
202 {
203 if (nOversample != m_nOversample)
204 {
205 m_nOversample = nOversample;
206 m_chip._SetOversample(m_nOversample);
207 }
208 }
209
210 SAAPARAM CSAASoundInternal::GetCurrentSoundParameters(void)
211 {
212 return m_uParam | m_uParamRate;
213 }
214
215 unsigned short CSAASoundInternal::GetCurrentBytesPerSample(void)
216 {
217 // 16 bit stereo => 4 bytes per sample
218 return 4;
219 }
220
221 /*static*/ unsigned short CSAASound::GetBytesPerSample(SAAPARAM uParam)
222 {
223 // 16 bit stereo => 4 bytes per sample
224 switch (uParam & (SAAP_MASK_CHANNELS | SAAP_MASK_BITDEPTH))
225 {
226 case SAAP_STEREO | SAAP_16BIT:
227 return 4;
228 default:
229 return 0;
230 }
231 }
232
233 unsigned int CSAASoundInternal::GetCurrentSampleRate(void)
234 {
235 return CSAASound::GetSampleRate(m_uParamRate);
236 }
237
238 /*static*/ unsigned int CSAASound::GetSampleRate(SAAPARAM uParam) // static member function
239 {
240 switch (uParam & SAAP_MASK_SAMPLERATE)
241 {
242 case SAAP_11025:
243 return 11025;
244 case SAAP_22050:
245 return 22050;
246 case SAAP_44100:
247 return 44100;
248 default:
249 return 0;
250 }
251 }
252
253 #if defined(USE_CONFIG_FILE) || (defined(DEFAULT_BOOST) && DEFAULT_BOOST>1)
254 #define DO_BOOST
255 #endif
256
257 void scale_for_output(unsigned int left_input, unsigned int right_input,
258 double oversample_scalar, bool highpass, double boost,
259 double& filterout_z1_left, double& filterout_z1_right,
260 BYTE* &pBuffer)
261 {
262 double float_left = (double)left_input;
263 double float_right = (double)right_input;
264 float_left /= oversample_scalar;
265 float_right /= oversample_scalar;
266
267 // scale output into good range
268 float_left *= DEFAULT_UNBOOSTED_MULTIPLIER;
269 float_right *= DEFAULT_UNBOOSTED_MULTIPLIER;
270
271 if (highpass)
272 {
273 /* cutoff = 5 Hz (say)
274 const double b1 = exp(-2.0 * M_PI * (Fc/Fs))
275 const double a0 = 1.0 - b1;
276 */
277 const double b1 = 0.99928787;
278 const double a0 = 1.0 - b1;
279
280 filterout_z1_left = float_left * a0 + filterout_z1_left * b1;
281 filterout_z1_right = float_right * a0 + filterout_z1_right * b1;
282 float_left -= filterout_z1_left;
283 float_right -= filterout_z1_right;
284 }
285
286 // multiply by boost, if defined
287 #if defined(DO_BOOST)
288 float_left *= boost;
289 float_right *= boost;
290 #endif
291 // convert to 16-bit signed range with hard clipping
292 signed short left_output = (signed short)(float_left > 32767 ? 32767 : float_left < -32768 ? -32768 : float_left);
293 signed short right_output = (signed short)(float_right > 32767 ? 32767 : float_right < -32768 ? -32768 : float_right);
294
295 *pBuffer++ = left_output & 0x00ff;
296 *pBuffer++ = (left_output >> 8) & 0x00ff;
297 *pBuffer++ = right_output & 0x00ff;
298 *pBuffer++ = (right_output >> 8) & 0x00ff;
299 }
300
301 void CSAASoundInternal::GenerateMany(BYTE* pBuffer, unsigned int nSamples, DivDispatchOscBuffer** oscBuf)
302 {
303 unsigned int left_mixed, right_mixed;
304
305 #if defined(DEBUGSAA) || defined(USE_CONFIG_FILE)
306 BYTE* pBufferStart = pBuffer;
307 unsigned int nTotalSamples = nSamples;
308 #endif
309
310 #if defined(DO_BOOST)
311 #if defined(USE_CONFIG_FILE)
312 double nBoost = m_Config.m_nBoost;
313 #else
314 double nBoost = DEFAULT_BOOST;
315 #endif
316 #else
317 double nBoost = DEFAULT_BOOST;
318 #endif
319
320 double oversample = double(1 << m_nOversample);
321
322 #if defined(USE_CONFIG_FILE)
323 static double filterout_z1_left_0 = 0, filterout_z1_right_0 = 0;
324 static double filterout_z1_left_1 = 0, filterout_z1_right_1 = 0;
325 static double filterout_z1_left_2 = 0, filterout_z1_right_2 = 0;
326 static double filterout_z1_left_3 = 0, filterout_z1_right_3 = 0;
327 static double filterout_z1_left_4 = 0, filterout_z1_right_4 = 0;
328 static double filterout_z1_left_5 = 0, filterout_z1_right_5 = 0;
329
330 if (m_Config.m_bGeneratePcmLogs && m_Config.m_bGeneratePcmSeparateChannels)
331 {
332 unsigned int left0, right0, left1, right1, left2, right2, left3, right3, left4, right4, left5, right5;
333 BYTE* pChannelBufferPtr[6] = { m_pChannelBuffer[0], m_pChannelBuffer[1], m_pChannelBuffer[2], m_pChannelBuffer[3], m_pChannelBuffer[4], m_pChannelBuffer[5] };
334
335 while (nSamples--)
336 {
337 m_chip._TickAndOutputSeparate(left_mixed, right_mixed,
338 left0, right0,
339 left1, right1,
340 left2, right2,
341 left3, right3,
342 left4, right4,
343 left5, right5);
344 scale_for_output(left_mixed, right_mixed, oversample, m_bHighpass, nBoost, filterout_z1_left_mixed, filterout_z1_right_mixed, pBuffer);
345
346 // and the separate channels
347 scale_for_output(left0, right0, oversample, m_bHighpass, nBoost, filterout_z1_left_0, filterout_z1_right_0, pChannelBufferPtr[0]);
348 scale_for_output(left1, right1, oversample, m_bHighpass, nBoost, filterout_z1_left_1, filterout_z1_right_1, pChannelBufferPtr[1]);
349 scale_for_output(left2, right2, oversample, m_bHighpass, nBoost, filterout_z1_left_2, filterout_z1_right_2, pChannelBufferPtr[2]);
350 scale_for_output(left3, right3, oversample, m_bHighpass, nBoost, filterout_z1_left_3, filterout_z1_right_3, pChannelBufferPtr[3]);
351 scale_for_output(left4, right4, oversample, m_bHighpass, nBoost, filterout_z1_left_4, filterout_z1_right_4, pChannelBufferPtr[4]);
352 scale_for_output(left5, right5, oversample, m_bHighpass, nBoost, filterout_z1_left_5, filterout_z1_right_5, pChannelBufferPtr[5]);
353
354 // flush channel output PCM buffers when full
355 if (pChannelBufferPtr[0] >= m_pChannelBuffer[0] + CHANNEL_BUFFER_SIZE)
356 {
357 for (int i = 0; i < 6; i++)
358 {
359 m_channel_pcmfile[i].write((const char*)m_pChannelBuffer[i], CHANNEL_BUFFER_SIZE);
360 pChannelBufferPtr[i] = m_pChannelBuffer[i];
361 }
362 }
363 }
364 // flush remaining channel PCM output data
365 if (pChannelBufferPtr[0] >= m_pChannelBuffer[0])
366 {
367 for (int i = 0; i < 6; i++)
368 {
369 m_channel_pcmfile[i].write((const char*)m_pChannelBuffer[i], pChannelBufferPtr[i]-m_pChannelBuffer[i]);
370 }
371 }
372 }
373 else
374 {
375 #endif
376 unsigned int where=0;
377 while (nSamples--)
378 {
379 m_chip._TickAndOutputStereo(left_mixed, right_mixed, oscBuf, where++);
380 scale_for_output(left_mixed, right_mixed, oversample, m_bHighpass, nBoost, filterout_z1_left_mixed, filterout_z1_right_mixed, pBuffer);
381 }
382
383 #if defined(USE_CONFIG_FILE)
384 }
385 #endif
386
387 #if defined(DEBUGSAA) || defined(USE_CONFIG_FILE)
388 #ifdef USE_CONFIG_FILE
389 if (m_Config.m_bGeneratePcmLogs)
390 {
391 #endif
392 m_pcmfile.write((const char *)pBufferStart, nTotalSamples * (unsigned int)GetCurrentBytesPerSample());
393 m_nDebugSample += nTotalSamples;
394 #ifdef USE_CONFIG_FILE
395 }
396 #endif
397
398 #endif
399 }
400
401 ///////////////////////////////////////////////////////
402
403 LPCSAASOUND SAAAPI CreateCSAASound(void)
404 {
405 return (new CSAASoundInternal);
406 }
407
408 void SAAAPI DestroyCSAASound(LPCSAASOUND object)
409 {
410 delete (object);
411 }
412
413
414 /* thoughts on lowpass filtering as part of oversampling.
415 I tried this and really it didn't seem to make a lot of (audible) difference.
416
417 // lowpass oversample filter adds complexity and not particularly audibly better than simple averaging.
418 // use_lowpass_oversample_filter_average_output adds an additional averaging step to the output of the oversample
419 // filter. this seems critical, because without this, the raw output of the lowpass filter is full of aliases
420 // If use_lowpass_oversample_filter is False, then the _average_output flag is ignored.
421 // Default, use_lowpass_oversample_filter is False, it sounds just fine really.
422
423 //#define USE_LOWPASS_OVERSAMPLE_FILTER
424 #undef USE_LOWPASS_OVERSAMPLE_FILTER
425 //#define USE_LOWPASS_OVERSAMPLE_FILTER_AVERAGE_OUTPUT
426 #undef USE_LOWPASS_OVERSAMPLE_FILTER_AVERAGE_OUTPUT
427
428 #ifdef USE_LOWPASS_OVERSAMPLE_FILTER
429 static double oversample_lp_filterout_z1_left_stages[10] = { 0,0,0,0,0,0,0,0,0,0 };
430 static double oversample_lp_filterout_z1_right_stages[10] = { 0,0,0,0,0,0,0,0,0,0 };
431 double averaged_filterout_left = 0.0, averaged_filterout_right = 0.0;
432 const int nStages = 10;
433 for (int i = 0; i < 1 << m_nOversample; i++)
434 {
435 Noise[0]->Tick();
436 Noise[1]->Tick();
437 f_left = f_right = 0;
438 for (int c = 0; c < 6; c++)
439 {
440 Amp[c]->TickAndOutputStereo(temp_left, temp_right);
441 f_left += (double)temp_left;
442 f_right += (double)temp_right;
443 }
444 // apply lowpass here.
445 // HACK: ASSUME m_nOversample is 64 (I was experimenting only using the 64x oversample anyway)
446 // therefore Fs = 44100*64
447 // let's set Fc = 10kHz
448 // so Fc/Fs = 0.00354308390022675736961451247166
449 // const double b1 = exp(-2.0 * M_PI * (Fc/Fs))
450 // const double a0 = 1.0 - b1;
451 // const double b1 = 0.9779841137335348363722276130195;
452 const double b1 = 0.977;
453 const double a0 = 1.0 - b1;
454
455 oversample_lp_filterout_z1_left_stages[0] = f_left * a0 + oversample_lp_filterout_z1_left_stages[0] * b1;
456 for (int stage = 1; stage < nStages; stage++)
457 oversample_lp_filterout_z1_left_stages[stage] = oversample_lp_filterout_z1_left_stages[stage - 1] * a0 + oversample_lp_filterout_z1_left_stages[stage] * b1;
458 oversample_lp_filterout_z1_right_stages[0] = f_right * a0 + oversample_lp_filterout_z1_right_stages[0] * b1;
459 for (int stage = 1; stage < nStages; stage++)
460 oversample_lp_filterout_z1_right_stages[stage] = oversample_lp_filterout_z1_right_stages[stage - 1] * a0 + oversample_lp_filterout_z1_right_stages[stage] * b1;
461
462 #ifdef USE_LOWPASS_OVERSAMPLE_FILTER_AVERAGE_OUTPUT
463 averaged_filterout_left += oversample_lp_filterout_4z1_left;
464 averaged_filterout_right += oversample_lp_filterout_4z1_right;
465 #endif
466 }
467
468 // by the end of this loop we will have computed the oversample lowpass filter m_nOversample times
469 // and yielded exactly ONE sample output.
470 #ifdef USE_LOWPASS_OVERSAMPLE_FILTER_AVERAGE_OUTPUT
471 f_left = averaged_filterout_left / (1 << m_nOversample);
472 f_right = averaged_filterout_right / (1 << m_nOversample);
473 #else
474 f_left = oversample_lp_filterout_z1_left_stages[nStages - 1];
475 f_right = oversample_lp_filterout_z1_right_stages[nStages - 1];
476 #endif
477
478 #else
479 // do the simple 1/N averaging which is easier and sounds good enough
480
481 #endif
482
483 */
484