author | unc0rr |
Wed, 02 Sep 2009 08:53:46 +0000 | |
changeset 2344 | 63b3da03ce46 |
parent 2266 | 289dc8e51210 |
child 2415 | 35d09cbf819a |
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; |
|
30 |
||
31 |
wavfile = Fopen(filename, "rb"); |
|
32 |
||
33 |
fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile); |
|
34 |
fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile); |
|
35 |
fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile); |
|
36 |
||
2191 | 37 |
#ifdef DEBUG |
2259 | 38 |
fprintf(stderr, "ChunkID: %X\n", invert_endianness(WAVHeader.ChunkID)); |
39 |
fprintf(stderr, "ChunkSize: %d\n", WAVHeader.ChunkSize); |
|
40 |
fprintf(stderr, "Format: %X\n", invert_endianness(WAVHeader.Format)); |
|
2191 | 41 |
#endif |
2259 | 42 |
|
43 |
fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile); |
|
44 |
fread(&WAVHeader.Subchunk1Size, sizeof(uint32_t), 1, wavfile); |
|
45 |
fread(&WAVHeader.AudioFormat, sizeof(uint16_t), 1, wavfile); |
|
46 |
fread(&WAVHeader.NumChannels, sizeof(uint16_t), 1, wavfile); |
|
47 |
fread(&WAVHeader.SampleRate, sizeof(uint32_t), 1, wavfile); |
|
48 |
fread(&WAVHeader.ByteRate, sizeof(uint32_t), 1, wavfile); |
|
49 |
fread(&WAVHeader.BlockAlign, sizeof(uint16_t), 1, wavfile); |
|
50 |
fread(&WAVHeader.BitsPerSample, sizeof(uint16_t), 1, wavfile); |
|
51 |
||
2191 | 52 |
#ifdef DEBUG |
2259 | 53 |
fprintf(stderr, "Subchunk1ID: %X\n", invert_endianness(WAVHeader.Subchunk1ID)); |
54 |
fprintf(stderr, "Subchunk1Size: %d\n", WAVHeader.Subchunk1Size); |
|
55 |
fprintf(stderr, "AudioFormat: %d\n", WAVHeader.AudioFormat); |
|
56 |
fprintf(stderr, "NumChannels: %d\n", WAVHeader.NumChannels); |
|
57 |
fprintf(stderr, "SampleRate: %d\n", WAVHeader.SampleRate); |
|
58 |
fprintf(stderr, "ByteRate: %d\n", WAVHeader.ByteRate); |
|
59 |
fprintf(stderr, "BlockAlign: %d\n", WAVHeader.BlockAlign); |
|
60 |
fprintf(stderr, "BitsPerSample: %d\n", WAVHeader.BitsPerSample); |
|
2191 | 61 |
#endif |
2259 | 62 |
|
63 |
do { /*remove useless header chunks (plenty room for improvements)*/ |
|
64 |
t = fread(&WAVHeader.Subchunk2ID, sizeof(uint32_t), 1, wavfile); |
|
65 |
if (invert_endianness(WAVHeader.Subchunk2ID) == 0x64617461) |
|
66 |
break; |
|
67 |
if (t <= 0) { /*eof*/ |
|
68 |
fprintf(stderr, "ERROR: wrong WAV header\n"); |
|
69 |
return AL_FALSE; |
|
70 |
} |
|
71 |
} while (1); |
|
72 |
fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile); |
|
73 |
||
2191 | 74 |
#ifdef DEBUG |
2259 | 75 |
fprintf(stderr, "Subchunk2ID: %X\n", invert_endianness(WAVHeader.Subchunk2ID)); |
76 |
fprintf(stderr, "Subchunk2Size: %d\n", WAVHeader.Subchunk2Size); |
|
2191 | 77 |
#endif |
2259 | 78 |
|
79 |
*data = (char*) Malloc (sizeof(char) * WAVHeader.Subchunk2Size); |
|
80 |
||
81 |
/*this could be improved*/ |
|
82 |
do { |
|
83 |
n += fread(&((*data)[n]), sizeof(uint8_t), 1, wavfile); |
|
84 |
} while (n < WAVHeader.Subchunk2Size); |
|
85 |
||
86 |
fclose(wavfile); |
|
87 |
||
2191 | 88 |
#ifdef DEBUG |
2259 | 89 |
fprintf(stderr, "Last two bytes of data: %X%X\n", (*data)[n-2], (*data)[n-1]); |
2191 | 90 |
#endif |
2259 | 91 |
|
92 |
/*remaining parameters*/ |
|
93 |
/*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/ |
|
94 |
if (WAVHeader.NumChannels == 1) { |
|
95 |
if (WAVHeader.BitsPerSample == 8) |
|
96 |
*format = AL_FORMAT_MONO8; |
|
97 |
else { |
|
98 |
if (WAVHeader.BitsPerSample == 16) |
|
99 |
*format = AL_FORMAT_MONO16; |
|
100 |
else { |
|
101 |
fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n"); |
|
102 |
return AL_FALSE; |
|
103 |
} |
|
104 |
} |
|
105 |
} else { |
|
106 |
if (WAVHeader.NumChannels == 2) { |
|
107 |
if (WAVHeader.BitsPerSample == 8) |
|
108 |
*format = AL_FORMAT_STEREO8; |
|
109 |
else { |
|
110 |
if (WAVHeader.BitsPerSample == 16) |
|
111 |
*format = AL_FORMAT_STEREO16; |
|
112 |
else { |
|
113 |
fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n"); |
|
114 |
return AL_FALSE; |
|
115 |
} |
|
116 |
} |
|
117 |
} else { |
|
118 |
fprintf(stderr, "ERROR: wrong WAV header - format value\n"); |
|
119 |
return AL_FALSE; |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
*bitsize = WAVHeader.Subchunk2Size; |
|
124 |
*freq = WAVHeader.SampleRate; |
|
125 |
return AL_TRUE; |
|
126 |
} |
|
2257
7eb31efcfb9b
updates licence and fix a memory leak (which was consuming iphone memory)
koda
parents:
2220
diff
changeset
|
127 |
|
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
128 |
|
2259 | 129 |
int load_oggvorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) { |
130 |
/*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */ |
|
2266 | 131 |
OggVorbis_File oggStream; /*stream handle*/ |
2259 | 132 |
vorbis_info *vorbisInfo; /*some formatting data*/ |
2266 | 133 |
int64_t pcm_length; /*length of the decoded data*/ |
2260
31756e21c436
other indentation, binding and miscellaneous fixes to openalbridge
koda
parents:
2259
diff
changeset
|
134 |
int section, result, size = 0; |
2210 | 135 |
#ifdef DEBUG |
2259 | 136 |
int i; |
137 |
vorbis_comment *vorbisComment; /*other less useful data*/ |
|
2210 | 138 |
#endif |
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
139 |
|
2266 | 140 |
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
|
141 |
if (result < 0) { |
2266 | 142 |
fprintf (stderr, "ERROR: ov_open_callbacks failed with %X", result); |
143 |
ov_clear(&oggStream); |
|
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
144 |
return -1; |
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
145 |
} |
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
146 |
|
2259 | 147 |
vorbisInfo = ov_info(&oggStream, -1); |
148 |
pcm_length = ov_pcm_total(&oggStream, -1) << vorbisInfo->channels; |
|
149 |
||
2191 | 150 |
#ifdef DEBUG |
2259 | 151 |
vorbisComment = ov_comment(&oggStream, -1); |
152 |
fprintf(stderr, "Version: %d\n", vorbisInfo->version); |
|
153 |
fprintf(stderr, "Channels: %d\n", vorbisInfo->channels); |
|
154 |
fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate); |
|
155 |
fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper); |
|
156 |
fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal); |
|
157 |
fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower); |
|
158 |
fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window); |
|
159 |
fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor); |
|
160 |
fprintf(stderr, "PCM data size: %lld\n", pcm_length); |
|
161 |
fprintf(stderr, "# comment: %d\n", vorbisComment->comments); |
|
162 |
for (i = 0; i < vorbisComment->comments; i++) |
|
163 |
fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]); |
|
2191 | 164 |
#endif |
2259 | 165 |
|
166 |
/*allocates enough room for the decoded data*/ |
|
167 |
*data = (char*) Malloc (sizeof(char) * pcm_length); |
|
168 |
||
169 |
/*there *should* not be ogg at 8 bits*/ |
|
170 |
if (vorbisInfo->channels == 1) |
|
171 |
*format = AL_FORMAT_MONO16; |
|
172 |
else { |
|
173 |
if (vorbisInfo->channels == 2) |
|
174 |
*format = AL_FORMAT_STEREO16; |
|
175 |
else { |
|
176 |
fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels); |
|
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
177 |
ov_clear(&oggStream); |
2259 | 178 |
return AL_FALSE; |
179 |
} |
|
180 |
} |
|
181 |
||
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
182 |
while (size < pcm_length) { |
2259 | 183 |
/*ov_read decodes the ogg stream and storse the pcm in data*/ |
184 |
result = ov_read (&oggStream, *data + size, pcm_length - size, 0, 2, 1, §ion); |
|
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
185 |
if (result > 0) { |
2259 | 186 |
size += result; |
187 |
} else { |
|
188 |
if (result == 0) |
|
189 |
break; |
|
190 |
else { |
|
191 |
fprintf(stderr, "ERROR: 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
|
192 |
ov_clear(&oggStream); |
2259 | 193 |
return AL_FALSE; |
194 |
} |
|
195 |
} |
|
196 |
} |
|
197 |
||
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
198 |
/*set the last fields*/ |
2259 | 199 |
*bitsize = size; |
200 |
*freq = vorbisInfo->rate; |
|
201 |
||
2265
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
202 |
/*cleaning time*/ |
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
203 |
ov_clear(&oggStream); |
eae64600fb69
fix a bug where a fclose() was called after an ov_clear()
koda
parents:
2260
diff
changeset
|
204 |
|
2259 | 205 |
return AL_TRUE; |
206 |
} |
|
207 |
||
2191 | 208 |
#ifdef __CPLUSPLUS |
209 |
} |
|
210 |
#endif |