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