-
Notifications
You must be signed in to change notification settings - Fork 0
/
lgwav.c
217 lines (165 loc) · 6.09 KB
/
lgwav.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
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lgwav.h"
typedef unsigned long SAMPLE;
#define BITS_PER_BYTE 8
#define PRINT_DIVISION "---------------------------------\n"
static int DEBUGLEVEL = 0;
#define DPRINTF if(DEBUGLEVEL>5)printf
/*
* Using WAVE specification little-endianness.
* Would make inline, but that's not ANSI.
*/
/*inline*/ int int_from_bytes_little_endian(char* bytes, int numBytes)
{
int i, out=0;
for(i = numBytes-1; i>=0; i--) {
out = (out << BITS_PER_BYTE) + (bytes[i]);
}
return out;
}
/*
* Reads file into memory
* Reports size through passed argument.
* Returns pointer to allocated file in memory.
*/
int open_and_read_file(char* filename, struct wave* w)
{
FILE *file;
file = fopen(filename, "r");
DPRINTF("Opened file: %s\n", filename);
/* Get file size. */
fseek(file, 0, SEEK_END);
w->fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
DPRINTF("FileSize: %d (bytes)\n", (int)(w->fileSize));
/*
* Should probably use mmap, but that's a bit more annoying,
* and malloc+read should be fast enough for a reasonable WAVE file.
*/
w->waveFile = malloc(w->fileSize);
fread(w->waveFile, w->fileSize, 1, file);
assert(ftell(file) == (w->fileSize));
fclose(file);
DPRINTF("waveFile begins at %p\n", (void*)(w->waveFile));
return RETURN_OKAY;
}
/*
* File was closed after the entire read,
* so currently only need to free the file.
*/
void cleanup_wave(struct wave* w)
{
free(w->waveFile);
}
/*
* Check 12-byte RIFF header against specification and file size.
* Checks all 3 header fields for validity.
* Compares the read-in file size to header-specified size.
*
* Returns RETURN_OKAY on success.
*/
int check_header(struct wave* w)
{
struct RIFF_header* header;
header = &(w->waveFile->header);
DPRINTF(PRINT_DIVISION);
DPRINTF("ChunkID: \"%.4s\" (should be \"RIFF\")\n", header->ChunkID);
assert(strncmp("RIFF", header->ChunkID, 4)==0);
DPRINTF("ChunkSize: %d (should == FileSize-%d)\n", header->ChunkSize, 8);
assert(header->ChunkSize == (w->fileSize)-8);
DPRINTF("Format: \"%.4s\" (should be \"WAVE\")\n", header->Format);
assert(strncmp("WAVE", header->Format, 4)==0);
return RETURN_OKAY;
}
/*
* Check Format Chunk (Subchunk 1)
*
* Returns RETURN_OKAY on success.
*/
int check_fmt_chunk(struct wave* w)
{
struct fmt_chunk* fmtChunk;
void* computedDataChunkStartPointer;
fmtChunk = &(w->waveFile->fmtChunk);
DPRINTF(PRINT_DIVISION);
DPRINTF("Subchunk1ID: \"%.4s\" (should be \"fmt \")\n", fmtChunk->Subchunk1ID);
assert(strncmp("fmt ", fmtChunk->Subchunk1ID, 4)==0);
DPRINTF("Subchunk1Size: %d (assuming 16 for PCM)\n", fmtChunk->Subchunk1Size);
assert((fmtChunk->Subchunk1Size) == 16);
DPRINTF("AudioFormat: %d (should be 1 for PCM)\n", int_from_bytes_little_endian(fmtChunk->AudioFormat,2 ));
DPRINTF("NumChannels: %d (should be 1 or 2, preferably)\n", int_from_bytes_little_endian(fmtChunk->NumChannels, 2));
DPRINTF("SampleRate: %d (samples per second)\n", fmtChunk->SampleRate);
DPRINTF("ByteRate: %d (bytes per second)\n", fmtChunk->ByteRate);
DPRINTF("BlockAlign: %d (bytes per block)\n", int_from_bytes_little_endian(fmtChunk->BlockAlign, 2));
DPRINTF("BitsPerSample: %d\n", int_from_bytes_little_endian(fmtChunk->BitsPerSample, 2));
assert(int_from_bytes_little_endian(fmtChunk->BlockAlign, 2) == int_from_bytes_little_endian(fmtChunk->NumChannels, 2) * int_from_bytes_little_endian(fmtChunk->BitsPerSample, 2) / BITS_PER_BYTE);
assert((fmtChunk->ByteRate) == (fmtChunk->SampleRate) * int_from_bytes_little_endian(fmtChunk->BlockAlign, 2));
/* Making sure we have a canonical PCM file. */
computedDataChunkStartPointer = (void*)(((char*)(&(fmtChunk->Subchunk1Size)))
+ sizeof(fmtChunk->Subchunk1Size)
+ (int)(fmtChunk->Subchunk1Size));
DPRINTF("Computed Data Chunk Start Pointer: %p\n", computedDataChunkStartPointer);
assert(computedDataChunkStartPointer == &(w->waveFile->dataChunkStart));
return RETURN_OKAY;
}
/*
* Check Data Chunk Start (Subchunk 2)
*
* Returns RETURN_OKAY on success.
*/
int check_data_chunk_start(struct wave* w)
{
struct data_chunk_start* dataChunkStart;
struct fmt_chunk* fmtChunk;
dataChunkStart = &(w->waveFile->dataChunkStart);
fmtChunk = &(w->waveFile->fmtChunk);
DPRINTF(PRINT_DIVISION);
DPRINTF("Subchunk2ID: \"%.4s\" (should be \"data\")\n", dataChunkStart->Subchunk2ID);
assert(strncmp("data", dataChunkStart->Subchunk2ID, 4)==0);
DPRINTF("Subchunk2Size: %d\n", dataChunkStart->Subchunk2Size);
return RETURN_OKAY;
}
/*
* Loads and sets up a wave struct.
*
* Returns pointer to a new wave file on success,
* NULL on non-aborting error.
*/
struct wave* ini_wave(char* filename)
{
struct wave* w;
w = malloc(sizeof(wave));
assert(open_and_read_file(filename, w) == RETURN_OKAY);
assert(check_header(w) == RETURN_OKAY);
assert(check_fmt_chunk(w) == RETURN_OKAY);
assert(check_data_chunk_start(w) == RETURN_OKAY);
w->numChannels = int_from_bytes_little_endian(w->waveFile->fmtChunk.NumChannels, 2);
w->sampleRate = w->waveFile->fmtChunk.SampleRate;
w->blockAlign = int_from_bytes_little_endian(w->waveFile->fmtChunk.BlockAlign, 2);
w->bytesPerSample = int_from_bytes_little_endian(w->waveFile->fmtChunk.BitsPerSample, 2)/BITS_PER_BYTE;
w->numSamples = (w->waveFile->dataChunkStart.Subchunk2Size)/(w->numChannels)/(w->bytesPerSample);
w->data = (void*)(&((w->waveFile)->data));
DPRINTF(PRINT_DIVISION);
DPRINTF("Sanity Check\n");
DPRINTF("w->numChannels: %d\n", w->numChannels);
DPRINTF("w->sampleRate: %d\n", w->sampleRate);
DPRINTF("w->blockAlign: %d\n", w->blockAlign);
DPRINTF("w->bytesPerSample: %d\n", w->bytesPerSample);
assert(w->bytesPerSample < sizeof(SAMPLE)); /* Otherwise, can't do computation safely. */
DPRINTF("w->numSamples: %d\n", w->numSamples);
DPRINTF("w->data: %p\n", w->data);
return w;
}
/*
* Channels are 0-indexed.
*
*/
int sample_at(struct wave* w, int channel, int sample_num) {
return int_from_bytes_little_endian((char*)(w->data)
+(w->blockAlign)*sample_num
+(w->bytesPerSample)*(MAX(channel, w->numChannels-1)),
w->bytesPerSample);
}