z80/ym2612 reset handling improved
[picodrive.git] / Pico / Pico / xpcm.c
CommitLineData
ef4eb506 1/*
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.
5 */
6
7#include "../PicoInt.h"
8
9#define ADPCMSHIFT 8
10#define ADFIX(f) (int) ((double)f * (double)(1<<ADPCMSHIFT))
11
12/* limitter */
13#define Limit(val, max, min) { \
14 if ( val > max ) val = max; \
15 else if ( val < min ) val = min; \
16}
17
ed367a3f 18static const int TableQuant[8] =
ef4eb506 19{
20 ADFIX(0.8984375),
21 ADFIX(0.8984375),
22 ADFIX(0.8984375),
23 ADFIX(0.8984375),
24 ADFIX(1.19921875),
25 ADFIX(1.59765625),
26 ADFIX(2.0),
27 ADFIX(2.3984375)
28};
29
30// changed using trial and error..
213c16ad 31//static const int quant_mul[16] = { 1, 3, 5, 7, 9, 11, 13, 15, -1, -3, -5, -7, -9, -11, -13, -15 };
ed367a3f 32static const int quant_mul[16] = { 1, 3, 5, 7, 9, 11, 13, -1, -1, -3, -5, -7, -9, -11, -13, -15 };
33
34static int sample = 0, quant = 0, sgn = 0;
35static int stepsamples = (44100<<10)/16000;
ef4eb506 36
ef4eb506 37
38PICO_INTERNAL void PicoPicoPCMReset(void)
39{
ed367a3f 40 sample = sgn = 0;
ef4eb506 41 quant = 0x7f;
42 memset(PicoPicohw.xpcm_buffer, 0, sizeof(PicoPicohw.xpcm_buffer));
43}
44
213c16ad 45PICO_INTERNAL void PicoPicoPCMRerate(int xpcm_rate)
ed367a3f 46{
213c16ad 47 stepsamples = (PsndRate<<10)/xpcm_rate;
ed367a3f 48}
49
213c16ad 50#define XSHIFT 6
ef4eb506 51
52#define do_sample() \
53{ \
213c16ad 54 int delta = quant * quant_mul[srcval] >> XSHIFT; \
55 sample += delta - (delta >> 2); /* 3/4 */ \
ef4eb506 56 quant = (quant * TableQuant[srcval&7]) >> ADPCMSHIFT; \
57 Limit(quant, 0x6000, 0x7f); \
213c16ad 58 Limit(sample, 32767*3/4, -32768*3/4); \
ef4eb506 59}
60
61PICO_INTERNAL void PicoPicoPCMUpdate(short *buffer, int length, int stereo)
62{
63 unsigned char *src = PicoPicohw.xpcm_buffer;
64 unsigned char *lim = PicoPicohw.xpcm_ptr;
ed367a3f 65 int srcval, needsamples = 0;
ef4eb506 66
ed367a3f 67 if (src == lim) goto end;
ef4eb506 68
69 for (; length > 0 && src < lim; src++)
70 {
71 srcval = *src >> 4;
72 do_sample();
73
74 for (needsamples += stepsamples; needsamples > (1<<10) && length > 0; needsamples -= (1<<10), length--) {
ed367a3f 75 *buffer++ += sample;
ef4eb506 76 if (stereo) { buffer[0] = buffer[-1]; buffer++; }
77 }
78
79 srcval = *src & 0xf;
80 do_sample();
81
82 for (needsamples += stepsamples; needsamples > (1<<10) && length > 0; needsamples -= (1<<10), length--) {
ed367a3f 83 *buffer++ += sample;
ef4eb506 84 if (stereo) { buffer[0] = buffer[-1]; buffer++; }
85 }
ed367a3f 86
87 // lame normalization stuff, needed due to wrong adpcm algo
88 sgn += (sample < 0) ? -1 : 1;
89 if (sgn < -16 || sgn > 16) sample -= sample >> 5;
ef4eb506 90 }
91
92 if (src < lim) {
93 int di = lim - src;
94 memmove(PicoPicohw.xpcm_buffer, src, di);
95 PicoPicohw.xpcm_ptr = PicoPicohw.xpcm_buffer + di;
fa22af4c 96 elprintf(EL_PICOHW, "xpcm update: over %i", di);
ed367a3f 97 // adjust fifo
98 PicoPicohw.fifo_bytes = di;
99 return;
ef4eb506 100 }
ed367a3f 101
fa22af4c 102 elprintf(EL_PICOHW, "xpcm update: under %i", length);
ed367a3f 103 PicoPicohw.xpcm_ptr = PicoPicohw.xpcm_buffer;
104
105end:
106 if (stereo)
107 // still must expand SN76496 to stereo
108 for (; length > 0; buffer+=2, length--)
109 buffer[1] = buffer[0];
110
111 sample = sgn = 0;
112 quant = 0x7f;
ef4eb506 113}
114