author | koda |
Sun, 11 Oct 2009 16:19:32 +0000 | |
changeset 2416 | 3278cf087339 |
parent 2415 | 35d09cbf819a |
child 2418 | 538a777f90c4 |
permissions | -rw-r--r-- |
2191 | 1 |
/* |
2 |
* OpenAL Bridge - a simple portable library for OpenAL interface |
|
3 |
* Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
6 |
* it under the terms of the GNU Lesser General Public License as published by |
2191 | 7 |
* the Free Software Foundation; version 2 of the License |
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
12 |
* GNU Lesser General Public License for more details. |
2191 | 13 |
* |
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
14 |
* You should have received a copy of the GNU Lesser General Public License |
2191 | 15 |
* along with this program; if not, write to the Free Software |
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
#include "loaders.h" |
|
20 |
||
21 |
#ifdef __CPLUSPLUS |
|
22 |
extern "C" { |
|
23 |
#endif |
|
2259 | 24 |
|
25 |
int load_wavpcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) { |
|
26 |
WAV_header_t WAVHeader; |
|
27 |
FILE *wavfile; |
|
28 |
int32_t t; |
|
29 |
uint32_t n = 0; |
|
2415 | 30 |
uint8_t sub0, sub1, sub2, sub3; |
2259 | 31 |
|
32 |
wavfile = Fopen(filename, "rb"); |
|
33 |
||
2415 | 34 |
fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile); /*RIFF*/ |
2259 | 35 |
fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile); |
2415 | 36 |
fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile); /*WAVE*/ |
2259 | 37 |
|
2191 | 38 |
#ifdef DEBUG |
2415 | 39 |
fprintf(stderr, "ChunkID: %X\n", ENDIAN_BIG_32(WAVHeader.ChunkID)); |
40 |
fprintf(stderr, "ChunkSize: %d\n", ENDIAN_LITTLE_32(WAVHeader.ChunkSize)); |
|
41 |
fprintf(stderr, "Format: %X\n", ENDIAN_BIG_32(WAVHeader.Format)); |
|
2191 | 42 |
#endif |
2259 | 43 |
|
2415 | 44 |
fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile); /*fmt */ |
2259 | 45 |
fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile); |
46 |
fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile); |
|
47 |
fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile); |
|
48 |
fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile); |
|
49 |
fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile); |
|
50 |
fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile); |
|
51 |
fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile); |
|
52 |
||
2191 | 53 |
#ifdef DEBUG |
2415 | 54 |
fprintf(stderr, "Subchunk1ID: %X\n", ENDIAN_BIG_32(WAVHeader.Subchunk1ID)); |
55 |
fprintf(stderr, "Subchunk1Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk1Size)); |
|
56 |
fprintf(stderr, "AudioFormat: %d\n", ENDIAN_LITTLE_16(WAVHeader.AudioFormat)); |
|
57 |
fprintf(stderr, "NumChannels: %d\n", ENDIAN_LITTLE_16(WAVHeader.NumChannels)); |
|
58 |
fprintf(stderr, "SampleRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.SampleRate)); |
|
59 |
fprintf(stderr, "ByteRate: %d\n", ENDIAN_LITTLE_32(WAVHeader.ByteRate)); |
|
60 |
fprintf(stderr, "BlockAlign: %d\n", ENDIAN_LITTLE_16(WAVHeader.BlockAlign)); |
|
61 |
fprintf(stderr, "BitsPerSample: %d\n", ENDIAN_LITTLE_16(WAVHeader.BitsPerSample)); |
|
2191 | 62 |
#endif |
2415 | 63 |
|
64 |
/*remove useless header chunks by looking for the WAV_HEADER_SUBCHUNK2ID integer */ |
|
65 |
do { |
|
66 |
t = fread(&sub0, sizeof(uint8_t), 1, wavfile); |
|
67 |
if(sub0 == 0x64) { |
|
68 |
t = fread(&sub1, sizeof(uint8_t), 1, wavfile); |
|
69 |
if(sub1 == 0x61) { |
|
70 |
t = fread(&sub2, sizeof(uint8_t), 1, wavfile); |
|
71 |
if(sub2 == 0x74) { |
|
72 |
t = fread(&sub3, sizeof(uint8_t), 1, wavfile); |
|
73 |
if(sub3 == 0x61) { |
|
74 |
WAVHeader.Subchunk2ID = WAV_HEADER_SUBCHUNK2ID; |
|
75 |
break; |
|
76 |
} |
|
77 |
} |
|
78 |
} |
|
79 |
} |
|
80 |
||
81 |
if (t <= 0) { |
|
82 |
/*eof*/ |
|
83 |
fprintf(stderr, "ERROR 'load_wavpcm()': wrong WAV header\n"); |
|
84 |
return AL_FALSE; |
|
85 |
} |
|
2259 | 86 |
} while (1); |
2415 | 87 |
|
2259 | 88 |
fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile); |
89 |
||
2191 | 90 |
#ifdef DEBUG |
2415 | 91 |
fprintf(stderr, "Subchunk2ID: %X\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2ID)); |
92 |
fprintf(stderr, "Subchunk2Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); |
|
2191 | 93 |
#endif |
2259 | 94 |
|
2415 | 95 |
*data = (char*) Malloc (sizeof(char) * ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); |
2259 | 96 |
|
2415 | 97 |
/*read the actual sound data*/ |
2259 | 98 |
do { |
2415 | 99 |
n += fread(&((*data)[n]), sizeof(uint8_t), 4, wavfile); |
100 |
} while (n < ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size)); |
|
2259 | 101 |
|
102 |
fclose(wavfile); |
|
2415 | 103 |
|
2191 | 104 |
#ifdef DEBUG |
2415 | 105 |
fprintf(stderr, "WAV data loaded\n"); |
2191 | 106 |
#endif |
2415 | 107 |
|
108 |
/*set parameters for OpenAL*/ |
|
2259 | 109 |
/*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/ |
2415 | 110 |
if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 1) { |
111 |
if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8) |
|
2259 | 112 |
*format = AL_FORMAT_MONO8; |
113 |
else { |
|
2415 | 114 |
if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16) |
2259 | 115 |
*format = AL_FORMAT_MONO16; |
116 |
else { |
|
2415 | 117 |
fprintf(stderr, "ERROR 'load_wavpcm()': wrong WAV header - bitsample value\n"); |
2259 | 118 |
return AL_FALSE; |
119 |
} |
|
120 |
} |
|
121 |
} else { |
|
2415 | 122 |
if (ENDIAN_LITTLE_16(WAVHeader.NumChannels) == 2) { |
123 |
if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 8) |
|
2259 | 124 |
*format = AL_FORMAT_STEREO8; |
125 |
else { |
|
2415 | 126 |
if (ENDIAN_LITTLE_16(WAVHeader.BitsPerSample) == 16) |
2259 | 127 |
*format = AL_FORMAT_STEREO16; |
128 |
else { |
|
2415 | 129 |
fprintf(stderr, "ERROR 'load_wavpcm()': wrong WAV header - bitsample value\n"); |
2259 | 130 |
return AL_FALSE; |
131 |
} |
|
132 |
} |
|
133 |
} else { |
|
2415 | 134 |
fprintf(stderr, "ERROR 'load_wavpcm()': wrong WAV header - format value\n"); |
2259 | 135 |
return AL_FALSE; |
136 |
} |
|
137 |
} |
|
138 |
||
2415 | 139 |
*bitsize = ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size); |
140 |
*freq = ENDIAN_LITTLE_32(WAVHeader.SampleRate); |
|
2259 | 141 |
return AL_TRUE; |
142 |
} |
|
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
143 |
|
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
144 |
|
2259 | 145 |
int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) { |
2416 | 146 |
/*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */ |
147 |
OggVorbis_File oggStream; /*stream handle*/ |
|
148 |
vorbis_info *vorbisInfo; /*some formatting data*/ |
|
149 |
int64_t pcm_length; /*length of the decoded data*/ |
|
150 |
int section, result, size, endianness; |
|
2210 | 151 |
#ifdef DEBUG |
2416 | 152 |
int i; |
153 |
vorbis_comment *vorbisComment; /*other less useful data*/ |
|
2210 | 154 |
#endif |
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
155 |
|
2266 | 156 |
result = ov_fopen((char*) filename, &oggStream); |
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
157 |
if (result < 0) { |
2415 | 158 |
fprintf (stderr, "ERROR 'load_oggvorbis()': ov_fopen failed with %X", result); |
2266 | 159 |
ov_clear(&oggStream); |
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
160 |
return -1; |
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
161 |
} |
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
162 |
|
2416 | 163 |
/*load OGG header and determine the decoded data size*/ |
2259 | 164 |
vorbisInfo = ov_info(&oggStream, -1); |
165 |
pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels; |
|
166 |
||
2191 | 167 |
#ifdef DEBUG |
2259 | 168 |
vorbisComment = ov_comment(&oggStream, -1); |
169 |
fprintf(stderr, "Version: %d\n", vorbisInfo->version); |
|
170 |
fprintf(stderr, "Channels: %d\n", vorbisInfo->channels); |
|
171 |
fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate); |
|
172 |
fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper); |
|
173 |
fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal); |
|
174 |
fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower); |
|
175 |
fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window); |
|
176 |
fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor); |
|
177 |
fprintf(stderr, "PCM data size: %lld\n", pcm_length); |
|
178 |
fprintf(stderr, "# comment: %d\n", vorbisComment->comments); |
|
179 |
for (i = 0; i < vorbisComment->comments; i++) |
|
2415 | 180 |
fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]); |
2191 | 181 |
#endif |
2259 | 182 |
|
2416 | 183 |
/*allocates enough room for the decoded data*/ |
184 |
*data = (char*) Malloc (sizeof(char) * pcm_length); |
|
2259 | 185 |
|
2416 | 186 |
/*there *should* not be ogg at 8 bits*/ |
187 |
if (vorbisInfo->channels == 1) |
|
188 |
*format = AL_FORMAT_MONO16; |
|
2259 | 189 |
else { |
2416 | 190 |
if (vorbisInfo->channels == 2) |
191 |
*format = AL_FORMAT_STEREO16; |
|
192 |
else { |
|
193 |
fprintf(stderr, "ERROR 'load_oggvorbis()': wrong OGG header - channel value (%d)\n", vorbisInfo->channels); |
|
194 |
ov_clear(&oggStream); |
|
195 |
return AL_FALSE; |
|
196 |
} |
|
2259 | 197 |
} |
198 |
||
2416 | 199 |
size = 0; |
200 |
#ifdef __LITTLE_ENDIAN__ |
|
201 |
endianness = 0; |
|
202 |
#elif __BIG_ENDIAN__ |
|
203 |
endianness = 1; |
|
204 |
#endif |
|
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
205 |
while (size < pcm_length) { |
2259 | 206 |
/*ov_read decodes the ogg stream and storse the pcm in data*/ |
2416 | 207 |
result = ov_read (&oggStream, *data + size, pcm_length - size, endianness, 2, 1, §ion); |
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
208 |
if (result > 0) { |
2259 | 209 |
size += result; |
210 |
} else { |
|
211 |
if (result == 0) |
|
212 |
break; |
|
213 |
else { |
|
2415 | 214 |
fprintf(stderr, "ERROR 'load_oggvorbis()': end of file from OGG stream\n"); |
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
215 |
ov_clear(&oggStream); |
2259 | 216 |
return AL_FALSE; |
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
||
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
221 |
/*set the last fields*/ |
2259 | 222 |
*bitsize = size; |
223 |
*freq = vorbisInfo->rate; |
|
224 |
||
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
225 |
/*cleaning time*/ |
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
226 |
ov_clear(&oggStream); |
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
227 |
|
2259 | 228 |
return AL_TRUE; |
229 |
} |
|
230 |
||
2191 | 231 |
#ifdef __CPLUSPLUS |
232 |
} |
|
233 |
#endif |