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