| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "client.h" |
| 25 |
#include "snd_codec.h" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 91 |
if( !Q_strncmp( name, chunk, 4 ) ) |
| 92 |
return len; |
| 93 |
|
| 94 |
len = PAD( len, 2 ); |
| 95 |
|
| 96 |
|
| 97 |
FS_Seek( f, len, FS_SEEK_CUR ); |
| 98 |
} |
| 99 |
|
| 100 |
return -1; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 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 |
|
| 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 |
|
| 138 |
FS_Read(dump, 12, file); |
| 139 |
|
| 140 |
|
| 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 |
|
| 148 |
FGetLittleShort(file); |
| 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 |
|
| 165 |
if(fmtlen > 16) |
| 166 |
{ |
| 167 |
fmtlen -= 16; |
| 168 |
FS_Seek( file, fmtlen, FS_SEEK_CUR ); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 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 |
|
| 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 |
|
| 196 |
|
| 197 |
|
| 198 |
void *S_WAV_CodecLoad(const char *filename, snd_info_t *info) |
| 199 |
{ |
| 200 |
fileHandle_t file; |
| 201 |
void *buffer; |
| 202 |
|
| 203 |
|
| 204 |
FS_FOpenFileRead(filename, &file, qtrue); |
| 205 |
if(!file) |
| 206 |
{ |
| 207 |
return NULL; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 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 |
|
| 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 |
|
| 230 |
FS_Read(buffer, info->size, file); |
| 231 |
S_ByteSwapRawSamples(info->samples, info->width, info->channels, (byte *)buffer); |
| 232 |
|
| 233 |
|
| 234 |
FS_FCloseFile(file); |
| 235 |
return buffer; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
snd_stream_t *S_WAV_CodecOpenStream(const char *filename) |
| 244 |
{ |
| 245 |
snd_stream_t *rv; |
| 246 |
|
| 247 |
|
| 248 |
rv = S_CodecUtilOpen(filename, &wav_codec); |
| 249 |
if(!rv) |
| 250 |
return NULL; |
| 251 |
|
| 252 |
|
| 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 |
|
| 265 |
|
| 266 |
|
| 267 |
void S_WAV_CodecCloseStream(snd_stream_t *stream) |
| 268 |
{ |
| 269 |
S_CodecUtilClose(&stream); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 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 |
} |