author | koda |
Mon, 06 Jul 2009 22:46:33 +0000 | |
changeset 2230 | d6963f72d21a |
parent 2220 | 110266ba2ef7 |
child 2257 | 7eb31efcfb9b |
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 |
|
6 |
* it under the terms of the GNU 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 General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU 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 |
||
21 |
#ifdef __CPLUSPLUS |
|
22 |
extern "C" { |
|
23 |
#endif |
|
24 |
||
2213 | 25 |
int load_WavPcm (const char *filename, ALenum *format, char ** data, ALsizei *bitsize, ALsizei *freq) { |
2191 | 26 |
WAV_header_t WAVHeader; |
27 |
FILE *wavfile; |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
28 |
int32_t t; |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
29 |
uint32_t n = 0; |
2191 | 30 |
|
31 |
wavfile = Fopen(filename, "rb"); |
|
32 |
||
2215 | 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); |
|
2191 | 36 |
|
37 |
#ifdef DEBUG |
|
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)); |
|
41 |
#endif |
|
42 |
||
2215 | 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); |
|
2191 | 51 |
|
52 |
#ifdef DEBUG |
|
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); |
|
61 |
#endif |
|
62 |
||
2212 | 63 |
do { /*remove useless header chunks (plenty room for improvements)*/ |
2215 | 64 |
t = fread(&WAVHeader.Subchunk2ID, sizeof(uint32_t), 1, wavfile); |
2191 | 65 |
if (invert_endianness(WAVHeader.Subchunk2ID) == 0x64617461) |
66 |
break; |
|
2212 | 67 |
if (t <= 0) { /*eof*/ |
2191 | 68 |
fprintf(stderr, "ERROR: wrong WAV header\n"); |
69 |
return AL_FALSE; |
|
70 |
} |
|
71 |
} while (1); |
|
2215 | 72 |
fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile); |
2191 | 73 |
|
74 |
#ifdef DEBUG |
|
75 |
fprintf(stderr, "Subchunk2ID: %X\n", invert_endianness(WAVHeader.Subchunk2ID)); |
|
76 |
fprintf(stderr, "Subchunk2Size: %d\n", WAVHeader.Subchunk2Size); |
|
77 |
#endif |
|
78 |
||
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
79 |
*data = (char*) Malloc (sizeof(char) * WAVHeader.Subchunk2Size); |
2191 | 80 |
|
2212 | 81 |
/*this could be improved*/ |
2191 | 82 |
do { |
2215 | 83 |
n += fread(&((*data)[n]), sizeof(uint8_t), 1, wavfile); |
2191 | 84 |
} while (n < WAVHeader.Subchunk2Size); |
85 |
||
86 |
fclose(wavfile); |
|
87 |
||
88 |
#ifdef DEBUG |
|
89 |
fprintf(stderr, "Last two bytes of data: %X%X\n", (*data)[n-2], (*data)[n-1]); |
|
90 |
#endif |
|
91 |
||
92 |
/*remaining parameters*/ |
|
2212 | 93 |
/*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/ |
2191 | 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 |
} |
|
127 |
||
2213 | 128 |
int load_OggVorbis (const char *filename, ALenum *format, char **data, ALsizei *bitsize, ALsizei *freq) { |
2212 | 129 |
/*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */ |
130 |
FILE *oggFile; /*ogg handle*/ |
|
131 |
OggVorbis_File oggStream; /*stream handle*/ |
|
132 |
vorbis_info *vorbisInfo; /*some formatting data*/ |
|
133 |
int64_t pcm_length; /*length of the decoded data*/ |
|
2191 | 134 |
int size = 0; |
2210 | 135 |
int section, result; |
136 |
#ifdef DEBUG |
|
137 |
int i; |
|
2212 | 138 |
vorbis_comment *vorbisComment; /*other less useful data*/ |
2210 | 139 |
#endif |
2191 | 140 |
|
141 |
oggFile = Fopen(filename, "rb"); |
|
142 |
result = ov_open(oggFile, &oggStream, NULL, 0); |
|
2212 | 143 |
/*TODO: check returning value of result*/ |
2191 | 144 |
|
145 |
vorbisInfo = ov_info(&oggStream, -1); |
|
146 |
pcm_length = ov_pcm_total(&oggStream,-1) << vorbisInfo->channels; |
|
147 |
||
148 |
#ifdef DEBUG |
|
149 |
vorbisComment = ov_comment(&oggStream, -1); |
|
150 |
fprintf(stderr, "Version: %d\n", vorbisInfo->version); |
|
151 |
fprintf(stderr, "Channels: %d\n", vorbisInfo->channels); |
|
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
152 |
fprintf(stderr, "Rate (Hz): %ld\n", vorbisInfo->rate); |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
153 |
fprintf(stderr, "Bitrate Upper: %ld\n", vorbisInfo->bitrate_upper); |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
154 |
fprintf(stderr, "Bitrate Nominal: %ld\n", vorbisInfo->bitrate_nominal); |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
155 |
fprintf(stderr, "Bitrate Lower: %ld\n", vorbisInfo->bitrate_lower); |
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
156 |
fprintf(stderr, "Bitrate Windows: %ld\n", vorbisInfo->bitrate_window); |
2191 | 157 |
fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor); |
2220 | 158 |
fprintf(stderr, "PCM data size: %lld\n", pcm_length); |
2191 | 159 |
fprintf(stderr, "# comment: %d\n", vorbisComment->comments); |
160 |
for (i = 0; i < vorbisComment->comments; i++) |
|
161 |
fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]); |
|
162 |
#endif |
|
163 |
||
2212 | 164 |
/*allocates enough room for the decoded data*/ |
2216
82e7da49c26a
-Smaxx' patch: checks for initialized openal + disables sound options if openal init fails
koda
parents:
2215
diff
changeset
|
165 |
*data = (char*) Malloc (sizeof(char) * pcm_length); |
2191 | 166 |
|
2212 | 167 |
/*there *should* not be ogg at 8 bits*/ |
2191 | 168 |
if (vorbisInfo->channels == 1) |
169 |
*format = AL_FORMAT_MONO16; |
|
170 |
else { |
|
171 |
if (vorbisInfo->channels == 2) |
|
172 |
*format = AL_FORMAT_STEREO16; |
|
173 |
else { |
|
174 |
fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels); |
|
175 |
return AL_FALSE; |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
while(size < pcm_length) { |
|
2212 | 180 |
/*ov_read decodes the ogg stream and storse the pcm in data*/ |
2191 | 181 |
result = ov_read (&oggStream, *data + size, pcm_length - size, 0, 2, 1, §ion); |
182 |
if(result > 0) { |
|
183 |
size += result; |
|
184 |
} else { |
|
185 |
if (result == 0) |
|
186 |
break; |
|
187 |
else { |
|
188 |
fprintf(stderr, "ERROR: end of file from OGG stream\n"); |
|
189 |
return AL_FALSE; |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
||
2212 | 194 |
/*records the last fields*/ |
2191 | 195 |
*bitsize = size; |
196 |
*freq = vorbisInfo->rate; |
|
197 |
return AL_TRUE; |
|
198 |
} |
|
199 |
||
200 |
#ifdef __CPLUSPLUS |
|
201 |
} |
|
202 |
#endif |