-
Notifications
You must be signed in to change notification settings - Fork 0
/
sounds.c
307 lines (224 loc) · 6.72 KB
/
sounds.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
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
#include <stdio.h>
#define bool int
#define true 1
#define false 0
typedef unsigned char byte;
void* malloc(size_t size);
void free(void* ptr);
void ParseSoundFile(char* sndFileName, FILE* soundHeader);
int lengthContaining(const char* str, const char* key);
int lengthNotContaining(const char* str, const char* key);
int _memcmp(const void* p1, const void* p2, int length);
void _memcpy(void* dest, const void* src, int length);
byte* ReadEntireFile(char* fileName, int* outFileLength, bool writeZero){
FILE* soundsFile = fopen(fileName, "rb");
if(soundsFile == NULL){
printf("Error: Could not open file '%s'.\n", fileName);
*outFileLength = 0;
return NULL;
}
fseek(soundsFile, 0, SEEK_END);
*outFileLength = ftell(soundsFile);
fseek(soundsFile, 0, SEEK_SET);
byte* fileBuffer = (byte*)malloc(*outFileLength + (writeZero ? 1 : 0));
fread(fileBuffer, 1, *outFileLength, soundsFile);
if(writeZero){
fileBuffer[*outFileLength] = '\0';
}
fclose(soundsFile);
return fileBuffer;
}
int main(int argc, char** argv){
if(argc < 2){
printf("Usage: sounds.exe [folder name].\n");
return 0;
}
char* dirName = argv[1];
char soundsFileName[256] = {};
sprintf(soundsFileName, "%s/sounds.txt", dirName);
int fileLength = 0;
char* fileBuffer = (char*)ReadEntireFile(soundsFileName, &fileLength, true);
if(fileBuffer == NULL){
return 0;
}
printf("Read in assets descr file.\n");
int length = lengthNotContaining(fileBuffer, "\n\t\r ");
printf("About to parse sound file.\n");
char wavFileName[256] = {};
sprintf(wavFileName, "%s/%.*s", dirName, length, fileBuffer);
char soundHeaderFileName[256] = {};
sprintf(soundHeaderFileName, "%s/sounds.h", dirName);
FILE* soundHeader = fopen(soundHeaderFileName, "wb");
fprintf(soundHeader, "typedef struct{ const unsigned char* data; int dataLength; } SoundClip;\n");
ParseSoundFile(wavFileName, soundHeader);
fclose(soundHeader);
printf("Parsed sound file.\n");
free(fileBuffer);
return 0;
}
int lengthContaining(const char* str, const char* key){
const char* cursor = str;
while(*cursor){
bool found = false;
for(int i = 0; key[i]; i++){
if(key[i] == *cursor){
found = true;
break;
}
}
if(!found){
break;
}
cursor++;
}
return cursor - str;
}
int lengthNotContaining(const char* str, const char* key){
const char* cursor = str;
while(*cursor){
bool found = false;
for(int i = 0; key[i]; i++){
if(key[i] == *cursor){
found = true;
break;
}
}
if(found){
break;
}
cursor++;
}
return cursor - str;
}
typedef struct{
short AudioFormat;
short NumChannels;
int SampleRate;
int ByteRate;
short BlockAlign;
short BitsPerSample;
} WaveFormatHeader;
void WriteSoundHeader(byte* data, int dataLen, WaveFormatHeader hdr, FILE* soundHeader, int newSampleRate){
double audioLengthInSeconds = ((double)dataLen)/hdr.ByteRate;
int newSampleCount = (int)(audioLengthInSeconds*newSampleRate);
printf("Resampled sound to %d samples.\n", newSampleCount);
byte* newSamples = (byte*)malloc(newSampleCount);
int dataIdx = 0;
double deltaErr = ((double)hdr.SampleRate)/newSampleRate;
double error = 0;
for(int i = 0; i < newSampleCount; i++){
double newSample = 0;
error += deltaErr;
int resampleCount = 0;
while(error >= 1){
double currDataSample = (data[dataIdx]);
if(hdr.BitsPerSample == 16){
currDataSample = *(short*)(&data[dataIdx]);
}
currDataSample /= (1 << (hdr.BitsPerSample-8));
newSample += currDataSample;
dataIdx += (hdr.BitsPerSample/8);
error -= 1.0;
resampleCount++;
}
newSamples[i] = (byte)(newSample/resampleCount);
}
fprintf(soundHeader, "const unsigned char snd_data[] __attribute__((section(\".rodata\"))) = {\n");
for(int i = 0; i < newSampleCount; i++){
fprintf(soundHeader, "%d, ", newSamples[i]);
if(i % 20 == 19){
fprintf(soundHeader, "\n");
}
}
fprintf(soundHeader, "};\n");
fprintf(soundHeader, "SoundClip snd = {snd_data, %d};\n", newSampleCount);
free(newSamples);
}
void ParseSoundFile(char* sndFileName, FILE* soundHeader){
int fileLength = 0;
byte* fileBuffer = ReadEntireFile(sndFileName, &fileLength, false);
if(fileBuffer == NULL){
return;
}
printf("Parsing sound file: '%s'\n", sndFileName);
byte* fileCursor = fileBuffer;
WaveFormatHeader format = {};
byte* data = NULL;
int dataSize = 0;
if(_memcmp(fileCursor, "RIFF", 4) == 0){
fileCursor += 8;
if(_memcmp(fileCursor, "WAVE", 4) == 0){
fileCursor += 4;
while(fileCursor - fileBuffer < fileLength){
if(_memcmp(fileCursor, "fmt ", 4) == 0){
fileCursor += 4;
int chunkSize = *(int*)fileCursor;
fileCursor += 4;
format = *(WaveFormatHeader*)fileCursor;
printf("Found format: fmt: %d, numChannels: %d, SampleRate: %d,\n ByteRate: %d, blockAlgn: %d, bitsPerSample: %d\n",
format.AudioFormat, format.NumChannels, format.SampleRate, format.ByteRate, format.BlockAlign, format.BitsPerSample);
fileCursor += chunkSize;
}
else if(_memcmp(fileCursor, "data", 4) == 0){
fileCursor += 4;
int chunkSize = *(int*)fileCursor;
fileCursor += 4;
byte* oldData = data;
data = malloc(dataSize+chunkSize);
_memcpy(data, oldData, dataSize);
_memcpy(data+dataSize, fileCursor, chunkSize);
dataSize += chunkSize;
free(oldData);
printf("Found data of %d bytes, starts with: |%0X, %0X, %0X|.\n", chunkSize, data[0], data[1], data[2]);
fileCursor += chunkSize;
}
else if(_memcmp(fileCursor, "LIST", 4) == 0){
fileCursor += 4;
int chunkSize = *(int*)fileCursor;
fileCursor += 4;
fileCursor += chunkSize;
}
else if(_memcmp(fileCursor, "id3 ", 4) == 0){
fileCursor += 4;
int chunkSize = *(int*)fileCursor;
fileCursor += 4;
fileCursor += chunkSize;
}
else{
printf("Error in file '%s', unexpected chunk ID: '%.*s'\n", sndFileName, 4, fileCursor);
break;
}
}
}
else{
printf("Error, '%s' may not be a proper WAVE file, header unexpected format.\n", sndFileName);
}
}
else{
printf("Error, '%s' may not be a proper WAVE file, missing header.\n", sndFileName);
}
const int newSampleRate = 5734;
WriteSoundHeader(data, dataSize, format, soundHeader, newSampleRate);
free(data);
free(fileBuffer);
}
int _memcmp(const void* p1, const void* p2, int length){
byte* s1 = (byte*)p1;
byte* s2 = (byte*)p2;
for(int i = 0; i < length; i++){
if(s1[i] < s2[i]){
return -1;
}
else if(s1[i] > s2[i]){
return 1;
}
}
return 0;
}
void _memcpy(void* dest, const void* src, int length){
byte* _dest = (byte*)dest;
byte* _src = (byte*)src;
for(int i = 0; i < length; i++){
_dest[i] = _src[i];
}
}