2 * The following ADPCM algorithm was stolen from MAME aica driver.
3 * I'm quite sure it's not the right one, but it's the
4 * best sounding of the ones that I tried.
7 #include "../PicoInt.h"
10 #define ADFIX(f) (int) ((double)f * (double)(1<<ADPCMSHIFT))
13 #define Limit(val, max, min) { \
14 if ( val > max ) val = max; \
15 else if ( val < min ) val = min; \
18 const int TableQuant[8] =
30 // changed using trial and error..
31 //const int quant_mul[16] = { 1, 3, 5, 7, 9, 11, 13, 15, -1, -3, -5, -7, -9, -11, -13, -15 };
32 const int quant_mul[16] = { 1, 3, 5, 7, 9, 11, 13, -1, -1, -3, -5, -7, -9, -11, -13, -15 };
34 static int sample = 0, quant = 0;
36 PICO_INTERNAL void PicoPicoPCMReset(void)
40 memset(PicoPicohw.xpcm_buffer, 0, sizeof(PicoPicohw.xpcm_buffer));
47 sample += quant * quant_mul[srcval] >> XSHIFT; \
48 quant = (quant * TableQuant[srcval&7]) >> ADPCMSHIFT; \
49 Limit(quant, 0x6000, 0x7f); \
50 Limit(sample, 32767, -32768); \
53 PICO_INTERNAL void PicoPicoPCMUpdate(short *buffer, int length, int stereo)
55 unsigned char *src = PicoPicohw.xpcm_buffer;
56 unsigned char *lim = PicoPicohw.xpcm_ptr;
57 int srcval, stepsamples = (44100<<10)/16000, needsamples = 0; // TODO: stepsamples
62 // still must expand SN76496 to stereo
63 for (; length > 0; buffer+=2, length--)
64 buffer[1] = buffer[0];
69 for (; length > 0 && src < lim; src++)
74 for (needsamples += stepsamples; needsamples > (1<<10) && length > 0; needsamples -= (1<<10), length--) {
76 if (stereo) { buffer[0] = buffer[-1]; buffer++; }
82 for (needsamples += stepsamples; needsamples > (1<<10) && length > 0; needsamples -= (1<<10), length--) {
84 if (stereo) { buffer[0] = buffer[-1]; buffer++; }
90 memmove(PicoPicohw.xpcm_buffer, src, di);
91 PicoPicohw.xpcm_ptr = PicoPicohw.xpcm_buffer + di;
92 elprintf(EL_STATUS, "xpcm update: over %i", di);
96 elprintf(EL_STATUS, "xpcm update: under %i", length);
97 PicoPicohw.xpcm_ptr = PicoPicohw.xpcm_buffer;