-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
pdx.c
44 lines (40 loc) · 1.22 KB
/
pdx.c
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
#include <stdlib.h>
#include "pdx.h"
#include "adpcm.h"
// TODO: check out of bounds conditions
int pdx_file_load(struct pdx_file *f, uint8_t *data, int len) {
if(data == 0 || len == 0) return -1;
int total_samples = 0;
for(int i = 0; i < 96; i++) {
int ofs = (data[i * 8] << 24) | (data[i * 8 + 1] << 16) | (data[i * 8 + 2] << 8) | data[i * 8 + 3];
int l = (data[i * 8 + 4] << 24) | (data[i * 8 + 5] << 16) | (data[i * 8 + 6] << 8) | data[i * 8 + 7];
if(ofs < len && ofs + l <= len) {
f->samples[i].data = data + ofs;
if(ofs + l > len)
l = len - ofs;
f->samples[i].len = l;
total_samples += l * 2;
} else {
f->samples[i].data = 0;
f->samples[i].len = 0;
}
}
int16_t *sample_data = malloc(total_samples * 2);
int16_t *cur_sample = sample_data;
for(int i = 0; i < 96; i++) {
if(f->samples[i].len > 0) {
f->samples[i].decoded_data = cur_sample;
f->samples[i].num_samples = f->samples[i].len * 2;
struct adpcm_status st;
adpcm_init(&st);
for(int j = 0; j < f->samples[i].len; j++) {
uint8_t c = f->samples[i].data[j];
int16_t d1 = adpcm_decode(c & 0x0f, &st);
int16_t d2 = adpcm_decode(c >> 4, &st);
*(cur_sample++) = d1;
*(cur_sample++) = d2;
}
}
}
return 0;
}