X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=Pico%2FPico%2Fxpcm.c;h=5fe98306bd4a083bf76dd1ef2c34c8b0f0abe714;hb=07abbab17a9baab5eeabe30767b0336326049994;hp=a92f4d678dbaf4268611180fcedbd62e77d7f726;hpb=ef4eb506de324df73bcda06f027a20349c69d05f;p=picodrive.git diff --git a/Pico/Pico/xpcm.c b/Pico/Pico/xpcm.c index a92f4d6..5fe9830 100644 --- a/Pico/Pico/xpcm.c +++ b/Pico/Pico/xpcm.c @@ -15,7 +15,7 @@ else if ( val < min ) val = min; \ } -const int TableQuant[8] = +static const int TableQuant[8] = { ADFIX(0.8984375), ADFIX(0.8984375), @@ -28,43 +28,43 @@ const int TableQuant[8] = }; // changed using trial and error.. -//const int quant_mul[16] = { 1, 3, 5, 7, 9, 11, 13, 15, -1, -3, -5, -7, -9, -11, -13, -15 }; -const int quant_mul[16] = { 1, 3, 5, 7, 9, 11, 13, -1, -1, -3, -5, -7, -9, -11, -13, -15 }; +//static const int quant_mul[16] = { 1, 3, 5, 7, 9, 11, 13, 15, -1, -3, -5, -7, -9, -11, -13, -15 }; +static const int quant_mul[16] = { 1, 3, 5, 7, 9, 11, 13, -1, -1, -3, -5, -7, -9, -11, -13, -15 }; + +static int sample = 0, quant = 0, sgn = 0; +static int stepsamples = (44100<<10)/16000; -static int sample = 0, quant = 0; PICO_INTERNAL void PicoPicoPCMReset(void) { - sample = 0; + sample = sgn = 0; quant = 0x7f; memset(PicoPicohw.xpcm_buffer, 0, sizeof(PicoPicohw.xpcm_buffer)); } -#define XSHIFT 7 +PICO_INTERNAL void PicoPicoPCMRerate(int xpcm_rate) +{ + stepsamples = (PsndRate<<10)/xpcm_rate; +} + +#define XSHIFT 6 #define do_sample() \ { \ - sample += quant * quant_mul[srcval] >> XSHIFT; \ + int delta = quant * quant_mul[srcval] >> XSHIFT; \ + sample += delta - (delta >> 2); /* 3/4 */ \ quant = (quant * TableQuant[srcval&7]) >> ADPCMSHIFT; \ Limit(quant, 0x6000, 0x7f); \ - Limit(sample, 32767, -32768); \ + Limit(sample, 32767*3/4, -32768*3/4); \ } PICO_INTERNAL void PicoPicoPCMUpdate(short *buffer, int length, int stereo) { unsigned char *src = PicoPicohw.xpcm_buffer; unsigned char *lim = PicoPicohw.xpcm_ptr; - int srcval, stepsamples = (44100<<10)/16000, needsamples = 0; // TODO: stepsamples + int srcval, needsamples = 0; - if (src == lim) - { - if (stereo) - // still must expand SN76496 to stereo - for (; length > 0; buffer+=2, length--) - buffer[1] = buffer[0]; - sample = quant = 0; - return; - } + if (src == lim) goto end; for (; length > 0 && src < lim; src++) { @@ -72,7 +72,7 @@ PICO_INTERNAL void PicoPicoPCMUpdate(short *buffer, int length, int stereo) do_sample(); for (needsamples += stepsamples; needsamples > (1<<10) && length > 0; needsamples -= (1<<10), length--) { - *buffer++ = sample; + *buffer++ += sample; if (stereo) { buffer[0] = buffer[-1]; buffer++; } } @@ -80,21 +80,35 @@ PICO_INTERNAL void PicoPicoPCMUpdate(short *buffer, int length, int stereo) do_sample(); for (needsamples += stepsamples; needsamples > (1<<10) && length > 0; needsamples -= (1<<10), length--) { - *buffer++ = sample; + *buffer++ += sample; if (stereo) { buffer[0] = buffer[-1]; buffer++; } } + + // lame normalization stuff, needed due to wrong adpcm algo + sgn += (sample < 0) ? -1 : 1; + if (sgn < -16 || sgn > 16) sample -= sample >> 5; } if (src < lim) { int di = lim - src; memmove(PicoPicohw.xpcm_buffer, src, di); PicoPicohw.xpcm_ptr = PicoPicohw.xpcm_buffer + di; - elprintf(EL_STATUS, "xpcm update: over %i", di); - } - else - { - elprintf(EL_STATUS, "xpcm update: under %i", length); - PicoPicohw.xpcm_ptr = PicoPicohw.xpcm_buffer; + elprintf(EL_PICOHW, "xpcm update: over %i", di); + // adjust fifo + PicoPicohw.fifo_bytes = di; + return; } + + elprintf(EL_PICOHW, "xpcm update: under %i", length); + PicoPicohw.xpcm_ptr = PicoPicohw.xpcm_buffer; + +end: + if (stereo) + // still must expand SN76496 to stereo + for (; length > 0; buffer+=2, length--) + buffer[1] = buffer[0]; + + sample = sgn = 0; + quant = 0x7f; }