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