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