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