util.c
2.01 KB
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
#include "vadpcm.h"
#define LSBMASK 1
#define WORDSIZE 16
static uint input_word = 0; /* Where to keep the current input bits */
static int in_bit_pos = -1; /* Pointers to bit position */
static uint
getshort(FILE *ifile)
{
int c1, c2;
if ((c1 = getc(ifile)) == EOF){
return 0;
}
if ((c2 = getc(ifile)) == EOF){
return 0;
}
return (c1<<8) | c2;
}
uint
readbits(uint nbits, FILE *ifile)
{
int c;
uint b, left;
/* Maximum of WORDSIZE bits can be read - This is in MSB first
style - ie first bit is MSB in returned value */
if (nbits <= in_bit_pos+1){
uint mask = (uint) (1L<<nbits) - 1;
b = mask & (input_word>>(in_bit_pos-nbits+1));
in_bit_pos -= nbits;
if (in_bit_pos < 0){
c = getshort(ifile);
input_word = (uint) c;
in_bit_pos = WORDSIZE-1;
}
return b;
}
else{
b = input_word & ((1L<<(in_bit_pos+1)) - 1);
left = nbits - in_bit_pos - 1;
c = getshort(ifile);
input_word = (uint) c;
in_bit_pos = WORDSIZE-1;
b = (b<<left) | readbits(left, ifile);
return b;
}
}
char *ReadPString(FILE *ifile)
{
unsigned char c;
char *st;
fread(&c, 1, 1, ifile);
st = (char *) malloc(c+1);
fread(st, c, 1, ifile);
*(st+c) = 0;
/*
* Read the pad byte if necessary
*/
if (!(c & 1))
fread(&c, 1, 1, ifile);
return st;
}
int
lookupMarker(unsigned long *sample, MarkerID loopPoint, Marker *markers, int nmarkers)
{
int i;
for (i=0; i<nmarkers; i++)
if (loopPoint == markers[i].id){
*sample = (markers[i].positionH<<16) + markers[i].positionL;
return 0;
}
return 1;
}
ALADPCMloop *
readlooppoints(FILE *ifile, short *nloops)
{
int
i;
ALADPCMloop
*al;
fread(nloops, sizeof(short), 1, ifile);
*nloops = htons(*nloops);
al = (ALADPCMloop *) malloc(*nloops * sizeof(ALADPCMloop));
for (i=0; i<*nloops; i++)
fread(&al[i], sizeof(ALADPCMloop), 1, ifile);
return al;
}