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
|
|
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;
|
|
33 |
|
|
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 |
|
|
40 |
#ifdef DEBUG
|
|
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));
|
|
44 |
#endif
|
|
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 |
|
|
55 |
#ifdef DEBUG
|
|
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));
|
|
64 |
#endif
|
|
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 |
|
|
93 |
#ifdef DEBUG
|
|
94 |
fprintf(stderr, "Subchunk2ID: %X\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2ID));
|
|
95 |
fprintf(stderr, "Subchunk2Size: %d\n", ENDIAN_LITTLE_32(WAVHeader.Subchunk2Size));
|
|
96 |
#endif
|
|
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 |
|
|
107 |
#ifdef DEBUG
|
|
108 |
err_msg("(%s) INFO - WAV data loaded", prog);
|
|
109 |
#endif
|
|
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;
|
|
123 |
}
|
|
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 |
}
|
|
143 |
}
|
|
144 |
|
|
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 */
|
|
153 |
|
|
154 |
/*stream handle*/
|
|
155 |
OggVorbis_File oggStream;
|
|
156 |
/*some formatting data*/
|
|
157 |
vorbis_info *vorbisInfo;
|
|
158 |
/*length of the decoded data*/
|
|
159 |
int64_t pcm_length;
|
|
160 |
/*other vars*/
|
|
161 |
int section, result, size, endianness;
|
|
162 |
#ifdef DEBUG
|
|
163 |
int i;
|
|
164 |
/*other less useful data*/
|
|
165 |
vorbis_comment *vorbisComment;
|
|
166 |
#endif
|
|
167 |
|
|
168 |
result = ov_fopen((char*) filename, &oggStream);
|
|
169 |
if (result < 0) {
|
|
170 |
errno = EINVAL;
|
|
171 |
err_ret("(%s) ERROR - ov_fopen() failed with %X", prog, result);
|
|
172 |
ov_clear(&oggStream);
|
|
173 |
return AL_FALSE;
|
|
174 |
}
|
|
175 |
|
|
176 |
/*load OGG header and determine the decoded data size*/
|
|
177 |
vorbisInfo = ov_info(&oggStream, -1);
|
|
178 |
pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels;
|
|
179 |
|
|
180 |
#ifdef DEBUG
|
|
181 |
vorbisComment = ov_comment(&oggStream, -1);
|
|
182 |
fprintf(stderr, "Version: %d\n", vorbisInfo->version);
|
|
183 |
fprintf(stderr, "Channels: %d\n", vorbisInfo->channels);
|
|
184 |
fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate);
|
|
185 |
fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper);
|
|
186 |
fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal);
|
|
187 |
fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower);
|
|
188 |
fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window);
|
|
189 |
fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor);
|
|
190 |
fprintf(stderr, "PCM data size: %lld\n", pcm_length);
|
|
191 |
fprintf(stderr, "# comment: %d\n", vorbisComment->comments);
|
|
192 |
for (i = 0; i < vorbisComment->comments; i++)
|
|
193 |
fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]);
|
|
194 |
#endif
|
|
195 |
|
|
196 |
/*allocates enough room for the decoded data*/
|
|
197 |
*data = (char*) Malloc (sizeof(char) * pcm_length);
|
|
198 |
|
|
199 |
/*there *should* not be ogg at 8 bits*/
|
|
200 |
if (vorbisInfo->channels == 1)
|
|
201 |
*format = AL_FORMAT_MONO16;
|
|
202 |
else {
|
|
203 |
if (vorbisInfo->channels == 2)
|
|
204 |
*format = AL_FORMAT_STEREO16;
|
|
205 |
else {
|
|
206 |
errno = EILSEQ;
|
|
207 |
err_ret("(%s) ERROR - wrong OGG header [channel %d]", prog, vorbisInfo->channels);
|
|
208 |
ov_clear(&oggStream);
|
|
209 |
return AL_FALSE;
|
|
210 |
}
|
|
211 |
}
|
|
212 |
|
|
213 |
size = 0;
|
|
214 |
#ifdef __LITTLE_ENDIAN__
|
|
215 |
endianness = 0;
|
|
216 |
#elif __BIG_ENDIAN__
|
|
217 |
endianness = 1;
|
|
218 |
#endif
|
|
219 |
while (size < pcm_length) {
|
|
220 |
/*ov_read decodes the ogg stream and storse the pcm in data*/
|
|
221 |
result = ov_read (&oggStream, *data + size, pcm_length - size, endianness, 2, 1, §ion);
|
|
222 |
if (result > 0) {
|
|
223 |
size += result;
|
|
224 |
} else {
|
|
225 |
if (result == 0)
|
|
226 |
break;
|
|
227 |
else {
|
|
228 |
errno = EILSEQ;
|
|
229 |
err_ret("(%s) ERROR - End of file from OGG stream", prog);
|
|
230 |
ov_clear(&oggStream);
|
|
231 |
return AL_FALSE;
|
|
232 |
}
|
|
233 |
}
|
|
234 |
}
|
|
235 |
|
|
236 |
/*set the last fields*/
|
|
237 |
*bitsize = size;
|
|
238 |
*freq = vorbisInfo->rate;
|
|
239 |
|
|
240 |
/*cleaning time*/
|
|
241 |
ov_clear(&oggStream);
|
|
242 |
|
|
243 |
return AL_TRUE;
|
|
244 |
}
|
|
245 |
|
|
246 |
#ifdef __CPLUSPLUS
|
|
247 |
}
|
|
248 |
#endif
|