8 #include "wave_ms_adpcm.h"
16 #elif defined(__VBCC__)
25 struct MS_ADPCM_Format
{
30 WORD blockAlign
; /* amount to read for each block */
31 WORD bitsPerSample
; /* 4 */
33 WORD extraSize
; /* 4+4*numCoefs */
35 WORD numCoefs
; // number of coef sets in file
36 struct coefset aCoef
[]; // numCoef coef sets
43 #elif defined(__VBCC__)
47 // Note: Coefs are fixed point 8.8 signed numbers.
51 * struct MS_ADPCM_Block {
52 * BYTE bpredictor[nchannels];
53 * WORD newdelta[nchannels];
54 * WORD isamp1[nchannels];
55 * WORD isamp2[nchannels];
59 DEC_SETUPPROTO(SetupMS_ADPCM
) {
60 struct MS_ADPCM_Format
* fmt
;
62 fmt
= (struct MS_ADPCM_Format
*)data
->fmt
;
64 if (fmt
->bitsPerSample
!= 4)
65 return DTERROR_UNKNOWN_COMPRESSION
;
66 fmt
->numCoefs
= read_le16(&fmt
->numCoefs
);
67 if (4 + (fmt
->numCoefs
<< 2) == data
->chunk
.size
)
69 /* change endianness of coefsets */
70 for (i
= 0; i
< fmt
->numCoefs
; i
++) {
71 fmt
->aCoef
[i
].coef1
= (WORD
)read_le16(&fmt
->aCoef
[i
].coef1
);
72 fmt
->aCoef
[i
].coef2
= (WORD
)read_le16(&fmt
->aCoef
[i
].coef2
);
74 data
->blockFrames
= fmt
->samplesPerBlock
= read_le16(&fmt
->samplesPerBlock
);
78 static const WORD adaptiontable
[16] = {
79 230, 230, 230, 230, 307, 409, 512, 614,
80 768, 614, 512, 409, 307, 230, 230, 230
83 struct ms_adpcm_stat
{
89 static LONG
DecodeAdpcmSample (struct ms_adpcm_stat
* stat
, LONG err
) {
92 stat
->nstep
= (adaptiontable
[err
] * step
) >> 8;
93 if (stat
->nstep
< 16) stat
->nstep
= 16;
94 if (err
& 8) err
-= 16;
95 nsamp
= ((stat
->samp1
* stat
->coef1
) + (stat
->samp2
* stat
->coef2
)) >> 8;
96 nsamp
+= (err
* step
);
97 if (nsamp
> 0x7FFF) nsamp
= 0x7FFF;
98 if (nsamp
< -0x8000) nsamp
= -0x8000;
99 stat
->samp2
= stat
->samp1
;
104 DECODERPROTO(DecodeMS_ADPCM
) {
105 struct ms_adpcm_stat stat
[MAX_CHANNELS
];
106 struct MS_ADPCM_Format
*fmt_ext
;
111 fmt_ext
=(struct MS_ADPCM_Format
*)fmt
;
112 numChan
= fmt_ext
->numChannels
;
114 for (ch
= 0; ch
< numChan
; ch
++) {
116 bpred
= *(UBYTE
*)Src
; Src
++;
117 if (bpred
>= fmt_ext
->numCoefs
) bpred
= 0;
118 stat
[ch
].coef1
= fmt_ext
->aCoef
[bpred
].coef1
;
119 stat
[ch
].coef2
= fmt_ext
->aCoef
[bpred
].coef2
;
121 for (ch
= 0; ch
< numChan
; ch
++) {
122 stat
[ch
].nstep
= (WORD
)read_le16(Src
); Src
+=2;
124 for (ch
= 0; ch
< numChan
; ch
++) {
125 stat
[ch
].samp1
= (WORD
)read_le16(Src
); Src
+=2;
127 for (ch
= 0; ch
< numChan
; ch
++) {
128 stat
[ch
].samp2
= (WORD
)read_le16(Src
); Src
+=2;
131 Dst
[ch
][1] = stat
[ch
].samp1
>> 8;
133 Dst
[ch
][0] = stat
[ch
].samp2
>> 8;
140 if (numFrames
<= 2) return numFrames
;
141 numSamp
= (numFrames
-2) * numChan
;
144 LONG tmp
= numSamp
>> 1;
145 for (s
= 0; s
< tmp
; s
++) {
146 *Dst
[ch
]++ = DecodeAdpcmSample(&stat
[ch
], *Src
>> 4) >> 8;
147 if (++ch
== numChan
) ch
= 0;
148 *Dst
[ch
]++ = DecodeAdpcmSample(&stat
[ch
], *Src
& 0xF) >> 8;
149 if (++ch
== numChan
) ch
= 0;
154 *Dst
[ch
]++ = DecodeAdpcmSample(&stat
[ch
], *Src
>> 4) >> 8;