Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MorsGames
GitHub Repository: MorsGames/sm64plus
Path: blob/master/tools/sdk-tools/adpcm/util.c
7861 views
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include "vadpcm.h"
4
5
static s32 input_word = 0;
6
static s32 in_bit_pos = -1;
7
8
static u32 getshort(FILE *ifile)
9
{
10
u32 c1;
11
u32 c2;
12
13
if ((c1 = getc(ifile)) == -1)
14
{
15
return 0;
16
}
17
18
if ((c2 = getc(ifile)) == -1)
19
{
20
return 0;
21
}
22
23
return (c1 << 8) | c2;
24
}
25
26
u32 readbits(u32 nbits, FILE *ifile)
27
{
28
u32 c;
29
u32 b;
30
u32 left;
31
u32 mask;
32
33
if (nbits <= in_bit_pos + 1)
34
{
35
mask = (1U << nbits) - 1;
36
b = ((u32) input_word >> (in_bit_pos - nbits + 1)) & mask;
37
in_bit_pos -= nbits;
38
if (in_bit_pos < 0)
39
{
40
c = getshort(ifile);
41
input_word = c;
42
in_bit_pos = 15;
43
}
44
return b;
45
}
46
else
47
{
48
b = input_word & ((1U << (in_bit_pos + 1)) - 1);
49
left = nbits - in_bit_pos - 1;
50
c = getshort(ifile);
51
input_word = c;
52
in_bit_pos = 15;
53
b = readbits(left, ifile) | (b << left);
54
return b;
55
}
56
}
57
58
char *ReadPString(FILE *ifile)
59
{
60
u8 c;
61
char *st;
62
63
fread(&c, 1, 1, ifile);
64
st = malloc(c + 1);
65
fread(st, c, 1, ifile);
66
st[c] = '\0';
67
if ((c & 1) == 0)
68
{
69
fread(&c, 1, 1, ifile);
70
}
71
return st;
72
}
73
74
s32 lookupMarker(u32 *sample, s16 loopPoint, Marker *markers, s32 nmarkers)
75
{
76
s32 i;
77
78
for (i = 0; i < nmarkers; i++)
79
{
80
if (markers[i].MarkerID == loopPoint)
81
{
82
*sample = (markers[i].positionH << 16) + markers[i].positionL;
83
return 0;
84
}
85
}
86
return 1;
87
}
88
89
ALADPCMloop *readlooppoints(FILE *ifile, s16 *nloops)
90
{
91
s32 i;
92
ALADPCMloop *al;
93
94
fread(nloops, sizeof(s16), 1, ifile);
95
BSWAP16(*nloops)
96
al = malloc(*nloops * sizeof(ALADPCMloop));
97
for (i = 0; i < *nloops; i++)
98
{
99
fread(&al[i], sizeof(ALADPCMloop), 1, ifile);
100
BSWAP32(al[i].start)
101
BSWAP32(al[i].end)
102
BSWAP32(al[i].count)
103
BSWAP16_MANY(al[i].state, 16)
104
}
105
return al;
106
}
107
108