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