Debian Codegraph @

code/client/snd_codec_wav.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4 Copyright (C) 2005 Stuart Dalton ([email protected])
5
6 This file is part of Quake III Arena source code.
7
8 Quake III Arena source code is free software; you can redistribute it
9 and/or modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 Quake III Arena source code is distributed in the hope that it will be
14 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Quake III Arena source code; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ===========================================================================
22 */
23
24 #include "client.h"
25 #include "snd_codec.h"
26
27 /*
28 =================
29 FGetLittleLong
30 =================
31 */
32 static int FGetLittleLong( fileHandle_t f ) {
33 int v;
34
35 FS_Read( &v, sizeof(v), f );
36
37 return LittleLong( v);
38 }
39
40 /*
41 =================
42 FGetLittleShort
43 =================
44 */
45 static short FGetLittleShort( fileHandle_t f ) {
46 short v;
47
48 FS_Read( &v, sizeof(v), f );
49
50 return LittleShort( v);
51 }
52
53 /*
54 =================
55 S_ReadChunkInfo
56 =================
57 */
58 static int S_ReadChunkInfo(fileHandle_t f, char *name)
59 {
60 int len, r;
61
62 name[4] = 0;
63
64 r = FS_Read(name, 4, f);
65 if(r != 4)
66 return -1;
67
68 len = FGetLittleLong(f);
69 if( len < 0 ) {
70 Com_Printf( S_COLOR_YELLOW "WARNING: Negative chunk length\n" );
71 return -1;
72 }
73
74 return len;
75 }
76
77 /*
78 =================
79 S_FindRIFFChunk
80
81 Returns the length of the data in the chunk, or -1 if not found
82 =================
83 */
84 static int S_FindRIFFChunk( fileHandle_t f, char *chunk ) {
85 char name[5];
86 int len;
87
88 while( ( len = S_ReadChunkInfo(f, name) ) >= 0 )
89 {
90 // If this is the right chunk, return
91 if( !Q_strncmp( name, chunk, 4 ) )
92 return len;
93
94 len = PAD( len, 2 );
95
96 // Not the right chunk - skip it
97 FS_Seek( f, len, FS_SEEK_CUR );
98 }
99
100 return -1;
101 }
102
103 /*
104 =================
105 S_ByteSwapRawSamples
106 =================
107 */
108 static void S_ByteSwapRawSamples( int samples, int width, int s_channels, const byte *data ) {
109 int i;
110
111 if ( width != 2 ) {
112 return;
113 }
114 if ( LittleShort( 256 ) == 256 ) {
115 return;
116 }
117
118 if ( s_channels == 2 ) {
119 samples <<= 1;
120 }
121 for ( i = 0 ; i < samples ; i++ ) {
122 ((short *)data)[i] = LittleShort( ((short *)data)[i] );
123 }
124 }
125
126 /*
127 =================
128 S_ReadRIFFHeader
129 =================
130 */
131 static qboolean S_ReadRIFFHeader(fileHandle_t file, snd_info_t *info)
132 {
133 char dump[16];
134 int bits;
135 int fmtlen = 0;
136
137 // skip the riff wav header
138 FS_Read(dump, 12, file);
139
140 // Scan for the format chunk
141 if((fmtlen = S_FindRIFFChunk(file, "fmt ")) < 0)
142 {
143 Com_Printf( S_COLOR_RED "ERROR: Couldn't find \"fmt\" chunk\n");
144 return qfalse;
145 }
146
147 // Save the parameters
148 FGetLittleShort(file); // wav_format
149 info->channels = FGetLittleShort(file);
150 info->rate = FGetLittleLong(file);
151 FGetLittleLong(file);
152 FGetLittleShort(file);
153 bits = FGetLittleShort(file);
154
155 if( bits < 8 )
156 {
157 Com_Printf( S_COLOR_RED "ERROR: Less than 8 bit sound is not supported\n");
158 return qfalse;
159 }
160
161 info->width = bits / 8;
162 info->dataofs = 0;
163
164 // Skip the rest of the format chunk if required
165 if(fmtlen > 16)
166 {
167 fmtlen -= 16;
168 FS_Seek( file, fmtlen, FS_SEEK_CUR );
169 }
170
171 // Scan for the data chunk
172 if( (info->size = S_FindRIFFChunk(file, "data")) < 0)
173 {
174 Com_Printf( S_COLOR_RED "ERROR: Couldn't find \"data\" chunk\n");
175 return qfalse;
176 }
177 info->samples = (info->size / info->width) / info->channels;
178
179 return qtrue;
180 }
181
182 // WAV codec
183 snd_codec_t wav_codec =
184 {
185 "wav",
186 S_WAV_CodecLoad,
187 S_WAV_CodecOpenStream,
188 S_WAV_CodecReadStream,
189 S_WAV_CodecCloseStream,
190 NULL
191 };
192
193 /*
194 =================
195 S_WAV_CodecLoad
196 =================
197 */
198 void *S_WAV_CodecLoad(const char *filename, snd_info_t *info)
199 {
200 fileHandle_t file;
201 void *buffer;
202
203 // Try to open the file
204 FS_FOpenFileRead(filename, &file, qtrue);
205 if(!file)
206 {
207 return NULL;
208 }
209
210 // Read the RIFF header
211 if(!S_ReadRIFFHeader(file, info))
212 {
213 FS_FCloseFile(file);
214 Com_Printf( S_COLOR_RED "ERROR: Incorrect/unsupported format in \"%s\"\n",
215 filename);
216 return NULL;
217 }
218
219 // Allocate some memory
220 buffer = Hunk_AllocateTempMemory(info->size);
221 if(!buffer)
222 {
223 FS_FCloseFile(file);
224 Com_Printf( S_COLOR_RED "ERROR: Out of memory reading \"%s\"\n",
225 filename);
226 return NULL;
227 }
228
229 // Read, byteswap
230 FS_Read(buffer, info->size, file);
231 S_ByteSwapRawSamples(info->samples, info->width, info->channels, (byte *)buffer);
232
233 // Close and return
234 FS_FCloseFile(file);
235 return buffer;
236 }
237
238 /*
239 =================
240 S_WAV_CodecOpenStream
241 =================
242 */
243 snd_stream_t *S_WAV_CodecOpenStream(const char *filename)
244 {
245 snd_stream_t *rv;
246
247 // Open
248 rv = S_CodecUtilOpen(filename, &wav_codec);
249 if(!rv)
250 return NULL;
251
252 // Read the RIFF header
253 if(!S_ReadRIFFHeader(rv->file, &rv->info))
254 {
255 S_CodecUtilClose(&rv);
256 return NULL;
257 }
258
259 return rv;
260 }
261
262 /*
263 =================
264 S_WAV_CodecCloseStream
265 =================
266 */
267 void S_WAV_CodecCloseStream(snd_stream_t *stream)
268 {
269 S_CodecUtilClose(&stream);
270 }
271
272 /*
273 =================
274 S_WAV_CodecReadStream
275 =================
276 */
277 int S_WAV_CodecReadStream(snd_stream_t *stream, int bytes, void *buffer)
278 {
279 int remaining = stream->info.size - stream->pos;
280 int samples;
281
282 if(remaining <= 0)
283 return 0;
284 if(bytes > remaining)
285 bytes = remaining;
286 stream->pos += bytes;
287 samples = (bytes / stream->info.width) / stream->info.channels;
288 FS_Read(buffer, bytes, stream->file);
289 S_ByteSwapRawSamples(samples, stream->info.width, stream->info.channels, buffer);
290 return bytes;
291 }