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 |
|
|
25 |
int load_WavPcm (const char *filename, ALenum *format, uint8_t** data, ALsizei *bitsize, ALsizei *freq) {
|
|
26 |
WAV_header_t WAVHeader;
|
|
27 |
FILE *wavfile;
|
|
28 |
int t, n = 0;
|
|
29 |
|
|
30 |
wavfile = Fopen(filename, "rb");
|
|
31 |
|
|
32 |
fread(&WAVHeader.ChunkID, sizeof(uint32_t), 1, wavfile);
|
|
33 |
fread(&WAVHeader.ChunkSize, sizeof(uint32_t), 1, wavfile);
|
|
34 |
fread(&WAVHeader.Format, sizeof(uint32_t), 1, wavfile);
|
|
35 |
|
|
36 |
#ifdef DEBUG
|
|
37 |
fprintf(stderr, "ChunkID: %X\n", invert_endianness(WAVHeader.ChunkID));
|
|
38 |
fprintf(stderr, "ChunkSize: %d\n", WAVHeader.ChunkSize);
|
|
39 |
fprintf(stderr, "Format: %X\n", invert_endianness(WAVHeader.Format));
|
|
40 |
#endif
|
|
41 |
|
|
42 |
fread(&WAVHeader.Subchunk1ID, sizeof(uint32_t), 1, wavfile);
|
|
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", invert_endianness(WAVHeader.Subchunk1ID));
|
|
53 |
fprintf(stderr, "Subchunk1Size: %d\n", WAVHeader.Subchunk1Size);
|
|
54 |
fprintf(stderr, "AudioFormat: %d\n", WAVHeader.AudioFormat);
|
|
55 |
fprintf(stderr, "NumChannels: %d\n", WAVHeader.NumChannels);
|
|
56 |
fprintf(stderr, "SampleRate: %d\n", WAVHeader.SampleRate);
|
|
57 |
fprintf(stderr, "ByteRate: %d\n", WAVHeader.ByteRate);
|
|
58 |
fprintf(stderr, "BlockAlign: %d\n", WAVHeader.BlockAlign);
|
|
59 |
fprintf(stderr, "BitsPerSample: %d\n", WAVHeader.BitsPerSample);
|
|
60 |
#endif
|
|
61 |
|
2212
|
62 |
do { /*remove useless header chunks (plenty room for improvements)*/
|
2191
|
63 |
t = fread(&WAVHeader.Subchunk2ID, sizeof(uint32_t), 1, wavfile);
|
|
64 |
if (invert_endianness(WAVHeader.Subchunk2ID) == 0x64617461)
|
|
65 |
break;
|
2212
|
66 |
if (t <= 0) { /*eof*/
|
2191
|
67 |
fprintf(stderr, "ERROR: wrong WAV header\n");
|
|
68 |
return AL_FALSE;
|
|
69 |
}
|
|
70 |
} while (1);
|
|
71 |
fread(&WAVHeader.Subchunk2Size, sizeof(uint32_t), 1, wavfile);
|
|
72 |
|
|
73 |
#ifdef DEBUG
|
|
74 |
fprintf(stderr, "Subchunk2ID: %X\n", invert_endianness(WAVHeader.Subchunk2ID));
|
|
75 |
fprintf(stderr, "Subchunk2Size: %d\n", WAVHeader.Subchunk2Size);
|
|
76 |
#endif
|
|
77 |
|
|
78 |
*data = (uint8_t*) malloc (sizeof(uint8_t) * WAVHeader.Subchunk2Size);
|
|
79 |
|
2212
|
80 |
/*this could be improved*/
|
2191
|
81 |
do {
|
|
82 |
n += fread(&((*data)[n]), sizeof(uint8_t), 1, wavfile);
|
|
83 |
} while (n < WAVHeader.Subchunk2Size);
|
|
84 |
|
|
85 |
fclose(wavfile);
|
|
86 |
|
|
87 |
#ifdef DEBUG
|
|
88 |
fprintf(stderr, "Last two bytes of data: %X%X\n", (*data)[n-2], (*data)[n-1]);
|
|
89 |
#endif
|
|
90 |
|
|
91 |
/*remaining parameters*/
|
2212
|
92 |
/*Valid formats are AL_FORMAT_MONO8, AL_FORMAT_MONO16, AL_FORMAT_STEREO8, and AL_FORMAT_STEREO16*/
|
2191
|
93 |
if (WAVHeader.NumChannels == 1) {
|
|
94 |
if (WAVHeader.BitsPerSample == 8)
|
|
95 |
*format = AL_FORMAT_MONO8;
|
|
96 |
else {
|
|
97 |
if (WAVHeader.BitsPerSample == 16)
|
|
98 |
*format = AL_FORMAT_MONO16;
|
|
99 |
else {
|
|
100 |
fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n");
|
|
101 |
return AL_FALSE;
|
|
102 |
}
|
|
103 |
}
|
|
104 |
} else {
|
|
105 |
if (WAVHeader.NumChannels == 2) {
|
|
106 |
if (WAVHeader.BitsPerSample == 8)
|
|
107 |
*format = AL_FORMAT_STEREO8;
|
|
108 |
else {
|
|
109 |
if (WAVHeader.BitsPerSample == 16)
|
|
110 |
*format = AL_FORMAT_STEREO16;
|
|
111 |
else {
|
|
112 |
fprintf(stderr, "ERROR: wrong WAV header - bitsample value\n");
|
|
113 |
return AL_FALSE;
|
|
114 |
}
|
|
115 |
}
|
|
116 |
} else {
|
|
117 |
fprintf(stderr, "ERROR: wrong WAV header - format value\n");
|
|
118 |
return AL_FALSE;
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
*bitsize = WAVHeader.Subchunk2Size;
|
|
123 |
*freq = WAVHeader.SampleRate;
|
|
124 |
return AL_TRUE;
|
|
125 |
}
|
|
126 |
|
|
127 |
int load_OggVorbis (const char *filename, ALenum *format, uint8_t**data, ALsizei *bitsize, ALsizei *freq) {
|
2212
|
128 |
/*implementation inspired from http://www.devmaster.net/forums/showthread.php?t=1153 */
|
|
129 |
FILE *oggFile; /*ogg handle*/
|
|
130 |
OggVorbis_File oggStream; /*stream handle*/
|
|
131 |
vorbis_info *vorbisInfo; /*some formatting data*/
|
|
132 |
int64_t pcm_length; /*length of the decoded data*/
|
2191
|
133 |
int size = 0;
|
2210
|
134 |
int section, result;
|
|
135 |
#ifdef DEBUG
|
|
136 |
int i;
|
2212
|
137 |
vorbis_comment *vorbisComment; /*other less useful data*/
|
2210
|
138 |
#endif
|
2191
|
139 |
|
|
140 |
oggFile = Fopen(filename, "rb");
|
|
141 |
result = ov_open(oggFile, &oggStream, NULL, 0);
|
2212
|
142 |
/*TODO: check returning value of result*/
|
2191
|
143 |
|
|
144 |
vorbisInfo = ov_info(&oggStream, -1);
|
|
145 |
pcm_length = ov_pcm_total(&oggStream,-1) << vorbisInfo->channels;
|
|
146 |
|
|
147 |
#ifdef DEBUG
|
|
148 |
vorbisComment = ov_comment(&oggStream, -1);
|
|
149 |
fprintf(stderr, "Version: %d\n", vorbisInfo->version);
|
|
150 |
fprintf(stderr, "Channels: %d\n", vorbisInfo->channels);
|
|
151 |
fprintf(stderr, "Rate (Hz): %d\n", vorbisInfo->rate);
|
|
152 |
fprintf(stderr, "Bitrate Upper: %d\n", vorbisInfo->bitrate_upper);
|
|
153 |
fprintf(stderr, "Bitrate Nominal: %d\n", vorbisInfo->bitrate_nominal);
|
|
154 |
fprintf(stderr, "Bitrate Lower: %d\n", vorbisInfo->bitrate_lower);
|
|
155 |
fprintf(stderr, "Bitrate Windows: %d\n", vorbisInfo->bitrate_window);
|
|
156 |
fprintf(stderr, "Vendor: %s\n", vorbisComment->vendor);
|
|
157 |
fprintf(stderr, "PCM data size: %d\n", pcm_length);
|
|
158 |
fprintf(stderr, "# comment: %d\n", vorbisComment->comments);
|
|
159 |
for (i = 0; i < vorbisComment->comments; i++)
|
|
160 |
fprintf(stderr, "\tComment %d: %s\n", i, vorbisComment->user_comments[i]);
|
|
161 |
#endif
|
|
162 |
|
2212
|
163 |
/*allocates enough room for the decoded data*/
|
2191
|
164 |
*data = (uint8_t*) malloc (sizeof(uint8_t) * pcm_length);
|
|
165 |
|
2212
|
166 |
/*there *should* not be ogg at 8 bits*/
|
2191
|
167 |
if (vorbisInfo->channels == 1)
|
|
168 |
*format = AL_FORMAT_MONO16;
|
|
169 |
else {
|
|
170 |
if (vorbisInfo->channels == 2)
|
|
171 |
*format = AL_FORMAT_STEREO16;
|
|
172 |
else {
|
|
173 |
fprintf(stderr, "ERROR: wrong OGG header - channel value (%d)\n", vorbisInfo->channels);
|
|
174 |
return AL_FALSE;
|
|
175 |
}
|
|
176 |
}
|
|
177 |
|
|
178 |
while(size < pcm_length) {
|
2212
|
179 |
/*ov_read decodes the ogg stream and storse the pcm in data*/
|
2191
|
180 |
result = ov_read (&oggStream, *data + size, pcm_length - size, 0, 2, 1, §ion);
|
|
181 |
if(result > 0) {
|
|
182 |
size += result;
|
|
183 |
} else {
|
|
184 |
if (result == 0)
|
|
185 |
break;
|
|
186 |
else {
|
|
187 |
fprintf(stderr, "ERROR: end of file from OGG stream\n");
|
|
188 |
return AL_FALSE;
|
|
189 |
}
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
2212
|
193 |
/*records the last fields*/
|
2191
|
194 |
*bitsize = size;
|
|
195 |
*freq = vorbisInfo->rate;
|
|
196 |
return AL_TRUE;
|
|
197 |
}
|
|
198 |
|
|
199 |
#ifdef __CPLUSPLUS
|
|
200 |
}
|
|
201 |
#endif
|