1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/*
author: unknown, probably bitmaster?
slightly modified by dbalster
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "common.h"
#include "xadecode.h"
#if USE_FXD
static FXD K0[4] = {
0x00000000,
0x0000F000,
0x0001CC00,
0x00018800
};
static FXD K1[4] = {
0x00000000,
0x00000000,
0xFFFF3000,
0xFFFF2400
};
FXD t1, t2, at1[256], at2[256];
FXD t1_x, t2_x, at1_x[256], at2_x[256];
#else
static double K0[4] = {
0.0,
0.9375,
1.796875,
1.53125
};
static double K1[4] = {
0.0,
0.0,
-0.8125,
-0.859375
};
double t1, t2, at1[256], at2[256];
double t1_x, t2_x, at1_x[256], at2_x[256];
#endif
void initXaDecode(void)
{
int i;
for (i=0; i<256; ++i)
{
at1[i] = at2[i] = at1_x[i] = at2_x[i] = 0;
}
}
void reinitXaDecode(int i)
{
at1[i] = at2[i] = at1_x[i] = at2_x[i] = 0;
}
void switchXaDecode(int i)
{
t1 = at1[i];
t2 = at2[i];
t1_x = at1_x[i];
t2_x = at2_x[i];
}
void saveXaDecode(int i)
{
at1[i] = t1;
at2[i] = t2;
at1_x[i]= t1_x;
at2_x[i]= t2_x;
}
char xachannel(SoundSector *ss)
{
return(ss->sectorFiller[XAChannel]);
}
unsigned char xatype(SoundSector *ss)
{
return(unsigned char) (ss->sectorFiller[XAType]);
}
char xafileno(SoundSector *ss)
{
return(ss->sectorFiller[XAFile]);
}
char xastereo(SoundSector *ss)
{
return(ss->sectorFiller[XAFlags]&XAFStereo);
}
char xahalfhz(SoundSector *ss)
{
return(ss->sectorFiller[XAFlags]&XAFHalfHz);
}
long convXaToWave(char *adp, char *wav, int cn, int fn_s, int fn_e)
{
SoundSector ssct;
int i;
memcpy(ssct.sectorFiller,adp,sizeof(ssct.sectorFiller));
for(i=0;i<18;i++)
memcpy(ssct.SoundGroups[i],adp+sizeof(ssct.sectorFiller)+(128*i),128);
if ((xachannel(&ssct) == cn) && (xatype(&ssct) == XAAUDIO))
{
if (xafileno(&ssct) >= fn_s
&& xafileno(&ssct) <= fn_e)
{
if (xastereo(&ssct))
return(decodeSoundSect1(&ssct, wav));
else
return(decodeSoundSect(&ssct, wav));
}
}
return(0);
}
long decodeSoundSect(SoundSector *ssct, char *wav)
{
long count, outputBytes;
signed char snddat, filt, range;
short decoded;
long unit, sample;
long sndgrp;
#if USE_FXD
FXD tmp2, tmp3, tmp4, tmp5;
#else
double tmp2, tmp3, tmp4, tmp5;
#endif
outputBytes = 0;
for (sndgrp = 0; sndgrp < kNumOfSGs; sndgrp++)
{
count = 0;
for (unit = 0; unit < 8; unit++)
{
range = getRange(ssct->SoundGroups[sndgrp], unit);
filt = getFilter(ssct->SoundGroups[sndgrp], unit);
for (sample = 0; sample < 28; sample++)
{
snddat = getSoundData(ssct->SoundGroups[sndgrp], unit, sample);
#if USE_FXD
tmp2 = (long)(snddat) << (12 - range);
tmp3 = FXD_Pcm16ToFxd(tmp2);
tmp4 = FXD_FixMul(K0[filt], t1);
tmp5 = FXD_FixMul(K1[filt], t2);
t2 = t1;
t1 = tmp3 + tmp4 + tmp5;
decoded = FXD_FxdToPcm16(t1);
#else
tmp2 = (double)(1 << (12 - range));
tmp3 = (double)snddat * tmp2;
tmp4 = t1 * K0[filt];
tmp5 = t2 * K1[filt];
t2 = t1;
t1 = tmp3 + tmp4 + tmp5;
decoded = DblToPCM(t1);
#endif
wav[outputBytes+count++] = (char)(decoded & 0x0000ffff);
wav[outputBytes+count++] = (char)(decoded >> 8);
}
}
outputBytes += count;
}
return outputBytes;
}
long decodeSoundSect1(SoundSector *ssct, char *wav)
{
long count, outputBytes;
signed char snddat, filt, range;
signed char filt1, range1;
short decoded;
long unit, sample;
long sndgrp;
#if USE_FXD
FXD tmp2, tmp3, tmp4, tmp5;
#else
double tmp2, tmp3, tmp4, tmp5;
#endif
outputBytes = 0;
for (sndgrp = 0; sndgrp < kNumOfSGs; sndgrp++)
{
count = 0;
for (unit = 0; unit < 8; unit+= 2)
{
range = getRange(ssct->SoundGroups[sndgrp], unit);
filt = getFilter(ssct->SoundGroups[sndgrp], unit);
range1 = getRange(ssct->SoundGroups[sndgrp], unit+1);
filt1 = getFilter(ssct->SoundGroups[sndgrp], unit+1);
for (sample = 0; sample < 28; sample++)
{
/* Channel 1 */
snddat = getSoundData(ssct->SoundGroups[sndgrp], unit, sample);
#if USE_FXD
tmp2 = (long)(snddat) << (12 - range);
tmp3 = FXD_Pcm16ToFxd(tmp2);
tmp4 = FXD_FixMul(K0[filt], t1);
tmp5 = FXD_FixMul(K1[filt], t2);
t2 = t1;
t1 = tmp3 + tmp4 + tmp5;
decoded = FXD_FxdToPcm16(t1);
#else
tmp2 = (double)(1 << (12 - range));
tmp3 = (double)snddat * tmp2;
tmp4 = t1 * K0[filt];
tmp5 = t2 * K1[filt];
t2 = t1;
t1 = tmp3 + tmp4 + tmp5;
decoded = DblToPCM(t1);
#endif
wav[outputBytes + count++] = (char)(decoded & 0x0000ffff);
wav[outputBytes + count++] = (char)(decoded >> 8);
/* Channel 2 */
snddat = getSoundData(ssct->SoundGroups[sndgrp], unit+1, sample);
#if USE_FXD
tmp2 = (long)(snddat) << (12 - range1);
tmp3 = FXD_Pcm16ToFxd(tmp2);
tmp4 = FXD_FixMul(K0[filt1], t1_x);
tmp5 = FXD_FixMul(K1[filt1], t2_x);
t2_x = t1_x;
t1_x = tmp3 + tmp4 + tmp5;
decoded = FXD_FxdToPcm16(t1_x);
#else
tmp2 = (double)(1 << (12 - range1));
tmp3 = (double)snddat * tmp2;
tmp4 = t1_x * K0[filt1];
tmp5 = t2_x * K1[filt1];
t2_x = t1_x;
t1_x = tmp3 + tmp4 + tmp5;
decoded = DblToPCM(t1_x);
#endif
wav[outputBytes + count++] = (char)(decoded & 0x0000ffff);
wav[outputBytes + count++] = (char)(decoded >> 8);
}
}
outputBytes += count;
}
return outputBytes;
}
signed char getSoundData(char *buf, long unit, long sample)
{
signed char ret;
char *p;
long offset, shift;
p = buf;
shift = (unit%2) * 4;
offset = 16 + (unit / 2) + (sample * 4);
p += offset;
ret = (*p >> shift) & 0x0F;
if (ret > 7) {
ret -= 16;
}
return ret;
}
signed char getFilter(char *buf, long unit)
{
return (*(buf + 4 + unit) >> 4) & 0x03;
}
signed char getRange(char *buf, long unit)
{
return *(buf + 4 + unit) & 0x0F;
}
#if USE_FXD
FXD FXD_FixMul(FXD a, FXD b)
{
long high_a, low_a, high_b, low_b;
long hahb, halb, lahb;
unsigned long lalb;
FXD ret;
high_a = a >> 16;
low_a = a & 0x0000FFFF;
high_b = b >> 16;
low_b = b & 0x0000FFFF;
hahb = (high_a * high_b) << 16;
halb = high_a * low_b;
lahb = low_a * high_b;
lalb = (unsigned long)(low_a * low_b) >> 16;
ret = hahb + halb + lahb + lalb;
return ret;
}
#endif
|