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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
#include "meta.h"
#include "../layout/layout.h"
#include "../util.h"
/* Resource Interchange File Format */
/* only the bare minimum needed to read PCM wavs */
/* return milliseconds */
long parse_marker(unsigned char * marker) {
long hh,mm,ss,ms;
if (memcmp("Marker ",marker,7)) return -1;
if (4 != sscanf((char*)marker+7,"%ld:%ld:%ld.%ld",&hh,&mm,&ss,&ms))
return -1;
return ((hh*60+mm)*60+ss)*1000+ms;
}
/* loop points have been found hiding here */
void parse_adtl(off_t adtl_offset, off_t adtl_length, STREAMFILE *streamFile,
long *loop_start, long *loop_end, int *loop_flag) {
int loop_start_found = 0;
int loop_end_found = 0;
off_t current_chunk = adtl_offset+4;
while (current_chunk < adtl_offset+adtl_length) {
uint32_t chunk_type = read_32bitBE(current_chunk,streamFile);
off_t chunk_size = read_32bitLE(current_chunk+4,streamFile);
if (current_chunk+8+chunk_size > adtl_offset+adtl_length) return;
switch(chunk_type) {
case 0x6c61626c: /* labl */
{
unsigned char *labelcontent;
labelcontent = malloc(chunk_size-4);
if (!labelcontent) return;
if (read_streamfile(labelcontent,current_chunk+0xc,
chunk_size-4,streamFile)!=chunk_size-4) {
free(labelcontent);
return;
}
switch (read_32bitLE(current_chunk+8,streamFile)) {
case 1:
if (!loop_start_found &&
(*loop_start = parse_marker(labelcontent))>=0)
{
loop_start_found = 1;
}
break;
case 2:
if (!loop_end_found &&
(*loop_end = parse_marker(labelcontent))>=0)
{
loop_end_found = 1;
}
break;
default:
break;
}
free(labelcontent);
}
break;
default:
break;
}
current_chunk += 8 + chunk_size;
}
if (loop_start_found && loop_end_found) *loop_flag = 1;
/* labels don't seem to be consistently ordered */
if (*loop_start > *loop_end) {
long temp = *loop_start;
*loop_start = *loop_end;
*loop_end = temp;
}
}
VGMSTREAM * init_vgmstream_riff(STREAMFILE *streamFile) {
VGMSTREAM * vgmstream = NULL;
char filename[260];
off_t file_size = -1;
int channel_count = 0;
int sample_count = 0;
int sample_rate = 0;
int coding_type = -1;
off_t start_offset = -1;
int interleave = -1;
int loop_flag = 0;
long loop_start_ms = -1;
long loop_end_ms = -1;
off_t loop_start_offset = -1;
off_t loop_end_offset = -1;
uint32_t riff_size;
uint32_t data_size = 0;
int FormatChunkFound = 0;
int DataChunkFound = 0;
/* Level-5 mwv */
int mwv = 0;
off_t mwv_pflt_offset = -1;
off_t mwv_ctrl_offset = -1;
/* check extension, case insensitive */
streamFile->get_name(streamFile,filename,sizeof(filename));
if (strcasecmp("wav",filename_extension(filename)) &&
strcasecmp("lwav",filename_extension(filename)))
{
if (strcasecmp("mwv",filename_extension(filename)))
goto fail;
else
mwv = 1;
}
/* check header */
if ((uint32_t)read_32bitBE(0,streamFile)!=0x52494646) /* "RIFF" */
goto fail;
/* check for WAVE form */
if ((uint32_t)read_32bitBE(8,streamFile)!=0x57415645) /* "WAVE" */
goto fail;
riff_size = read_32bitLE(4,streamFile);
file_size = get_streamfile_size(streamFile);
/* check for tructated RIFF */
if (file_size < riff_size+8) goto fail;
/* read through chunks to verify format and find metadata */
{
off_t current_chunk = 0xc; /* start with first chunk */
while (current_chunk < file_size && current_chunk < riff_size+8) {
uint32_t chunk_type = read_32bitBE(current_chunk,streamFile);
off_t chunk_size = read_32bitLE(current_chunk+4,streamFile);
if (current_chunk+8+chunk_size > file_size) goto fail;
switch(chunk_type) {
case 0x666d7420: /* "fmt " */
/* only one per file */
if (FormatChunkFound) goto fail;
FormatChunkFound = 1;
sample_rate = read_32bitLE(current_chunk+0x0c,streamFile);
channel_count = read_16bitLE(current_chunk+0x0a,streamFile);
switch (read_16bitLE(current_chunk+0x8,streamFile)) {
case 1: /* PCM */
switch (read_16bitLE(current_chunk+0x16,streamFile)) {
case 16:
coding_type = coding_PCM16LE;
interleave = 2;
break;
case 8:
coding_type = coding_PCM8_U_int;
interleave = 1;
break;
default:
goto fail;
}
break;
case 0x555: /* Level-5 0x555 ADPCM */
if (!mwv) goto fail;
coding_type = coding_L5_555;
interleave = 0x12;
break;
default:
goto fail;
}
break;
case 0x64617461: /* data */
/* at most one per file */
if (DataChunkFound) goto fail;
DataChunkFound = 1;
start_offset = current_chunk + 8;
data_size = chunk_size;
break;
case 0x4C495354: /* LIST */
/* what lurks within?? */
switch (read_32bitBE(current_chunk + 8, streamFile)) {
case 0x6164746C: /* adtl */
/* yay, atdl is its own little world */
parse_adtl(current_chunk + 8, chunk_size,
streamFile,
&loop_start_ms,&loop_end_ms,&loop_flag);
break;
default:
break;
}
break;
case 0x736D706C: /* smpl */
/* check loop count */
if (read_32bitLE(current_chunk+0x24, streamFile)==1)
{
/* check loop info */
if (read_32bitLE(current_chunk+0x2c+4, streamFile)==0)
{
loop_flag = 1;
loop_start_offset =
read_32bitLE(current_chunk+0x2c+8, streamFile);
loop_end_offset =
read_32bitLE(current_chunk+0x2c+0xc,streamFile);
}
}
break;
case 0x70666c74: /* pflt */
if (!mwv) break; /* ignore if not in an mwv */
/* predictor filters */
mwv_pflt_offset = current_chunk;
break;
case 0x6374726c: /* ctrl */
if (!mwv) break; /* ignore if not in an mwv */
/* loops! */
if (read_32bitLE(current_chunk+8, streamFile))
{
loop_flag = 1;
}
mwv_ctrl_offset = current_chunk;
break;
default:
/* ignorance is bliss */
break;
}
current_chunk += 8+chunk_size;
}
}
if (!FormatChunkFound || !DataChunkFound) goto fail;
switch (coding_type) {
case coding_PCM16LE:
sample_count = data_size/2/channel_count;
break;
case coding_PCM8_U_int:
sample_count = data_size/channel_count;
break;
case coding_L5_555:
sample_count = data_size/0x12/channel_count*32;
break;
}
/* build the VGMSTREAM */
vgmstream = allocate_vgmstream(channel_count,loop_flag);
if (!vgmstream) goto fail;
/* fill in the vital statistics */
vgmstream->num_samples = sample_count;
vgmstream->sample_rate = sample_rate;
vgmstream->coding_type = coding_type;
if (channel_count > 1 && coding_type != coding_PCM8_U_int)
vgmstream->layout_type = layout_interleave;
else
vgmstream->layout_type = layout_none;
vgmstream->interleave_block_size = interleave;
if (loop_flag) {
if (loop_start_ms >= 0)
{
vgmstream->loop_start_sample =
(long long)loop_start_ms*sample_rate/1000;
vgmstream->loop_end_sample =
(long long)loop_end_ms*sample_rate/1000;
vgmstream->meta_type = meta_RIFF_WAVE_labl_Marker;
}
else if (loop_start_offset >= 0)
{
vgmstream->loop_start_sample = loop_start_offset;
vgmstream->loop_end_sample = loop_end_offset;
vgmstream->meta_type = meta_RIFF_WAVE_smpl;
}
else if (mwv && mwv_ctrl_offset != -1)
{
vgmstream->loop_start_sample = read_32bitLE(mwv_ctrl_offset+12,
streamFile);
vgmstream->loop_end_sample = sample_count;
}
}
else
{
vgmstream->meta_type = meta_RIFF_WAVE;
}
if (mwv)
{
int i, c;
if (coding_type == coding_L5_555)
{
const int filter_order = 3;
int filter_count = read_32bitLE(mwv_pflt_offset+12, streamFile);
if (mwv_pflt_offset == -1 ||
read_32bitLE(mwv_pflt_offset+8, streamFile) != filter_order ||
read_32bitLE(mwv_pflt_offset+4, streamFile) < 8 + filter_count * 4 * filter_order)
goto fail;
if (filter_count > 0x20) goto fail;
for (c = 0; c < channel_count; c++)
{
for (i = 0; i < filter_count * filter_order; i++)
{
vgmstream->ch[c].adpcm_coef_3by32[i] = read_32bitLE(
mwv_pflt_offset+16+i*4, streamFile
);
}
}
}
vgmstream->meta_type = meta_RIFF_WAVE_MWV;
}
/* open the file, set up each channel */
{
int i;
vgmstream->ch[0].streamfile = streamFile->open(streamFile,filename,
STREAMFILE_DEFAULT_BUFFER_SIZE);
if (!vgmstream->ch[0].streamfile) goto fail;
for (i=0;i<channel_count;i++) {
vgmstream->ch[i].streamfile = vgmstream->ch[0].streamfile;
vgmstream->ch[i].offset = vgmstream->ch[i].channel_start_offset =
start_offset+i*interleave;
}
}
return vgmstream;
/* clean up anything we may have opened */
fail:
if (vgmstream) close_vgmstream(vgmstream);
return NULL;
}
|