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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/*
* SND_Utils.cpp
*
* Util functions for soundthingies
*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
#include "SND_Utils.h"
#include "SoundDefines.h"
#include "SND_DependKludge.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <math.h>
#include <string.h>
#if defined(WIN32)
#include <io.h>
#else
#include <unistd.h>
#endif
#define BUFFERSIZE 32
/*****************************************************************************
* Begin of temporary Endian stuff.
* I think there should be a central place to handle endian conversion but for
* the time being it suffices. Note that the defines come from the Blender
* source.
*****************************************************************************/
typedef enum
{
SND_endianBig = 0,
SND_endianLittle
} SND_TEndian;
#if defined(__BIG_ENDIAN__) || defined(__sparc) || defined(__sparc__)
const SND_TEndian SND_fEndian = SND_endianBig;
#else
const SND_TEndian SND_fEndian = SND_endianLittle;
#endif
/* This one swaps the bytes in a short */
#define SWITCH_SHORT(a) { \
char s_i, *p_i; \
p_i= (char *)&(a); \
s_i=p_i[0]; \
p_i[0] = p_i[1]; \
p_i[1] = s_i; }
/* This one rotates the bytes in an int */
#define SWITCH_INT(a) { \
char s_i, *p_i; \
p_i= (char *)&(a); \
s_i=p_i[0]; p_i[0]=p_i[3]; p_i[3]=s_i; \
s_i=p_i[1]; p_i[1]=p_i[2]; p_i[2]=s_i; }
/*****************************************************************************
* End of temporary Endian stuff.
*****************************************************************************/
/* loads a file */
void* SND_LoadSample(char *filename)
{
int file, filelen, buffersize = BUFFERSIZE;
void* data = NULL;
#if defined(WIN32)
file = open(filename, O_BINARY|O_RDONLY);
#else
file = open(filename, 0|O_RDONLY);
#endif
if (file == -1)
{
//printf("can't open file.\n");
//printf("press q for quit.\n");
}
else
{
filelen = lseek(file, 0, SEEK_END);
lseek(file, 0, SEEK_SET);
if (filelen != 0)
{
data = malloc(buffersize);
if (read(file, data, buffersize) != buffersize)
{
free(data);
data = NULL;
}
}
close(file);
}
return (data);
}
bool SND_IsSampleValid(const STR_String& name, void* memlocation)
{
bool result = false;
bool loadedsample = false;
char buffer[BUFFERSIZE];
if (!memlocation)
{
STR_String samplename = name;
memlocation = SND_LoadSample(samplename.Ptr());
if (memlocation)
loadedsample = true;
}
if (memlocation)
{
memcpy(&buffer, memlocation, BUFFERSIZE);
if(!(memcmp(buffer, "RIFF", 4) && memcmp(&(buffer[8]), "WAVEfmt ", 8)))
{
/* This was endian unsafe. See top of the file for the define. */
short shortbuf = *((short *) &buffer[20]);
if (SND_fEndian == SND_endianBig) SWITCH_SHORT(shortbuf);
if (shortbuf == SND_WAVE_FORMAT_PCM)
result = true;
/* only fmod supports compressed wav */
#ifdef USE_FMOD
switch (shortbuf)
{
case SND_WAVE_FORMAT_ADPCM:
case SND_WAVE_FORMAT_ALAW:
case SND_WAVE_FORMAT_MULAW:
case SND_WAVE_FORMAT_DIALOGIC_OKI_ADPCM:
case SND_WAVE_FORMAT_CONTROL_RES_VQLPC:
case SND_WAVE_FORMAT_GSM_610:
case SND_WAVE_FORMAT_MPEG3:
result = true;
break;
default:
{
break;
}
}
#endif
}
#ifdef USE_FMOD
/* only valid publishers may use ogg vorbis */
else if (!memcmp(buffer, "OggS", 4))
{
result = true;
}
/* only valid publishers may use mp3 */
else if (((!memcmp(buffer, "ID3", 3)) || (!memcmp(buffer, "ÿû", 2))))
{
result = true;
}
#endif
}
if (loadedsample)
{
free(memlocation);
memlocation = NULL;
}
return result;
}
/* checks if the passed pointer is a valid sample */
static bool CheckSample(void* sample)
{
bool valid = false;
char buffer[32];
memcpy(buffer, sample, 16);
if(!(memcmp(buffer, "RIFF", 4) && memcmp(&(buffer[8]), "WAVEfmt ", 8)))
{
valid = true;
}
return valid;
}
/* gets the type of the sample (0 == unknown, 1 == PCM etc */
unsigned int SND_GetSampleFormat(void* sample)
{
short sampletype = 0;
if (CheckSample(sample))
{
memcpy(&sampletype, ((char*)sample) + 20, 2);
}
/* This was endian unsafe. See top of the file for the define. */
if (SND_fEndian == SND_endianBig) SWITCH_SHORT(sampletype);
return (unsigned int)sampletype;
}
/* gets the number of channels in a sample */
unsigned int SND_GetNumberOfChannels(void* sample)
{
short numberofchannels = 0;
if (CheckSample(sample))
{
memcpy(&numberofchannels, ((char*)sample) + 22, 2);
}
/* This was endian unsafe. See top of the file for the define. */
if (SND_fEndian == SND_endianBig) SWITCH_SHORT(numberofchannels);
return (unsigned int)numberofchannels;
}
/* gets the samplerate of a sample */
unsigned int SND_GetSampleRate(void* sample)
{
unsigned int samplerate = 0;
if (CheckSample(sample))
{
memcpy(&samplerate, ((char*)sample) + 24, 4);
}
/* This was endian unsafe. See top of the file for the define. */
if (SND_fEndian == SND_endianBig) SWITCH_INT(samplerate);
return samplerate;
}
/* gets the bitrate of a sample */
unsigned int SND_GetBitRate(void* sample)
{
short bitrate = 0;
if (CheckSample(sample))
{
memcpy(&bitrate, ((char*)sample) + 34, 2);
}
/* This was endian unsafe. See top of the file for the define. */
if (SND_fEndian == SND_endianBig) SWITCH_SHORT(bitrate);
return (unsigned int)bitrate;
}
/* gets the length of the actual sample data (without the header) */
unsigned int SND_GetNumberOfSamples(void* sample)
{
unsigned int chunklength, length = 0, offset = 16;
char data[4];
if (CheckSample(sample))
{
memcpy(&chunklength, ((char*)sample) + offset, 4);
/* This was endian unsafe. See top of the file for the define. */
if (SND_fEndian == SND_endianBig) SWITCH_INT(chunklength);
offset = offset + chunklength + 4;
memcpy(data, ((char*)sample) + offset, 4);
/* This seems very unsafe, what if data is never found (f.i. corrupt file)... */
// lets find "data"
while (memcmp(data, "data", 4))
{
offset += 4;
memcpy(data, ((char*)sample) + offset, 4);
}
offset += 4;
memcpy(&length, ((char*)sample) + offset, 4);
/* This was endian unsafe. See top of the file for the define. */
if (SND_fEndian == SND_endianBig) SWITCH_INT(length);
}
return length;
}
/* gets the size of the entire header (file - sampledata) */
unsigned int SND_GetHeaderSize(void* sample)
{
unsigned int chunklength, headersize = 0, offset = 16;
char data[4];
if (CheckSample(sample))
{
memcpy(&chunklength, ((char*)sample) + offset, 4);
/* This was endian unsafe. See top of the file for the define. */
if (SND_fEndian == SND_endianBig) SWITCH_INT(chunklength);
offset = offset + chunklength + 4;
memcpy(data, ((char*)sample) + offset, 4);
// lets find "data"
while (memcmp(data, "data", 4))
{
offset += 4;
memcpy(data, ((char*)sample) + offset, 4);
}
headersize = offset + 8;
}
return headersize;
}
unsigned int SND_GetExtraChunk(void* sample)
{
unsigned int extrachunk = 0, chunklength, offset = 16;
char data[4];
if (CheckSample(sample))
{
memcpy(&chunklength, ((char*)sample) + offset, 4);
offset = offset + chunklength + 4;
memcpy(data, ((char*)sample) + offset, 4);
// lets find "cue"
while (memcmp(data, "cue", 3))
{
offset += 4;
memcpy(data, ((char*)sample) + offset, 4);
}
}
return extrachunk;
}
void SND_GetSampleInfo(signed char* sample, SND_WaveSlot* waveslot)
{
WavFileHeader fileheader;
WavFmtHeader fmtheader;
WavFmtExHeader fmtexheader;
WavSampleHeader sampleheader;
WavChunkHeader chunkheader;
if (CheckSample(sample))
{
memcpy(&fileheader, sample, sizeof(WavFileHeader));
fileheader.size = SND_GetHeaderSize(sample);
sample += sizeof(WavFileHeader);
fileheader.size = ((fileheader.size+1) & ~1) - 4;
while ((fileheader.size > 0) && (memcpy(&chunkheader, sample, sizeof(WavChunkHeader))))
{
sample += sizeof(WavChunkHeader);
if (!memcmp(chunkheader.id, "fmt ", 4))
{
memcpy(&fmtheader, sample, sizeof(WavFmtHeader));
waveslot->SetSampleFormat(fmtheader.format);
if (fmtheader.format == 0x0001)
{
waveslot->SetNumberOfChannels(fmtheader.numberofchannels);
waveslot->SetBitRate(fmtheader.bitrate);
waveslot->SetSampleRate(fmtheader.samplerate);
sample += chunkheader.size;
}
else
{
memcpy(&fmtexheader, sample, sizeof(WavFmtExHeader));
sample += chunkheader.size;
}
}
else if (!memcmp(chunkheader.id, "data", 4))
{
if (fmtheader.format == 0x0001)
{
waveslot->SetNumberOfSamples(chunkheader.size);
sample += chunkheader.size;
}
else if (fmtheader.format == 0x0011)
{
//IMA ADPCM
}
else if (fmtheader.format == 0x0055)
{
//MP3 WAVE
}
}
else if (!memcmp(chunkheader.id, "smpl", 4))
{
memcpy(&sampleheader, sample, sizeof(WavSampleHeader));
//loop = sampleheader.loops;
sample += chunkheader.size;
}
else
sample += chunkheader.size;
sample += chunkheader.size & 1;
fileheader.size -= (((chunkheader.size + 1) & ~1) + 8);
}
}
}
|