2011-01-17 01:35:22 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
Copyright (C) 1999-2005 Id Software, Inc.
|
|
|
|
Copyright (C) 2005 Stuart Dalton (badcdev@gmail.com)
|
|
|
|
|
|
|
|
This file is part of Quake III Arena source code.
|
|
|
|
|
|
|
|
Quake III Arena source code is free software; you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
Quake III Arena source code is distributed in the hope that it will be
|
|
|
|
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Quake III Arena source code; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
===========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "quakedef.h"
|
|
|
|
#include "snd_codec.h"
|
|
|
|
|
2011-01-17 02:24:18 +00:00
|
|
|
#define PAD(x,y) (((x)+(y)-1) & ~((y)-1))
|
|
|
|
|
2011-01-17 01:35:22 +00:00
|
|
|
/*
|
|
|
|
=================
|
|
|
|
FGetLittleLong
|
|
|
|
=================
|
|
|
|
*/
|
2011-01-17 02:24:18 +00:00
|
|
|
static int FGetLittleLong( int handle ) {
|
2011-01-17 01:35:22 +00:00
|
|
|
int v;
|
|
|
|
|
2011-01-17 02:24:18 +00:00
|
|
|
Sys_FileRead( handle, &v, sizeof(v) );
|
2011-01-17 01:35:22 +00:00
|
|
|
|
|
|
|
return LittleLong( v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
FGetLittleShort
|
|
|
|
=================
|
|
|
|
*/
|
2011-01-17 02:24:18 +00:00
|
|
|
static short FGetLittleShort( int handle ) {
|
2011-01-17 01:35:22 +00:00
|
|
|
short v;
|
|
|
|
|
2011-01-17 02:24:18 +00:00
|
|
|
Sys_FileRead( handle, &v, sizeof(v) );
|
2011-01-17 01:35:22 +00:00
|
|
|
|
|
|
|
return LittleShort( v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_ReadChunkInfo
|
|
|
|
=================
|
|
|
|
*/
|
2011-01-17 02:24:18 +00:00
|
|
|
static int S_ReadChunkInfo(int handle, char *name)
|
2011-01-17 01:35:22 +00:00
|
|
|
{
|
|
|
|
int len, r;
|
|
|
|
|
|
|
|
name[4] = 0;
|
|
|
|
|
2011-01-17 02:24:18 +00:00
|
|
|
r = Sys_FileRead(handle, name, 4);
|
2011-01-17 01:35:22 +00:00
|
|
|
if(r != 4)
|
|
|
|
return -1;
|
|
|
|
|
2011-01-17 02:24:18 +00:00
|
|
|
len = FGetLittleLong(handle);
|
2011-01-17 01:35:22 +00:00
|
|
|
if( len < 0 ) {
|
2011-01-17 20:27:38 +00:00
|
|
|
//Con_Printf( "WARNING: Negative chunk length\n" );
|
2011-01-17 01:35:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-01-17 20:27:38 +00:00
|
|
|
|
2011-01-17 01:35:22 +00:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_FindRIFFChunk
|
|
|
|
|
|
|
|
Returns the length of the data in the chunk, or -1 if not found
|
|
|
|
=================
|
|
|
|
*/
|
2011-01-17 02:24:18 +00:00
|
|
|
static int S_FindRIFFChunk( int handle, char *chunk ) {
|
2011-01-17 01:35:22 +00:00
|
|
|
char name[5];
|
|
|
|
int len;
|
|
|
|
|
2011-01-17 02:24:18 +00:00
|
|
|
while( ( len = S_ReadChunkInfo(handle, name) ) >= 0 )
|
2011-01-17 01:35:22 +00:00
|
|
|
{
|
|
|
|
// If this is the right chunk, return
|
|
|
|
if( !Q_strncmp( name, chunk, 4 ) )
|
|
|
|
return len;
|
|
|
|
|
|
|
|
len = PAD( len, 2 );
|
|
|
|
|
|
|
|
// Not the right chunk - skip it
|
2011-01-17 02:24:18 +00:00
|
|
|
Sys_FileSeekRelative( handle, len );
|
2011-01-17 01:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_ByteSwapRawSamples
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
static void S_ByteSwapRawSamples( int samples, int width, int s_channels, const byte *data ) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ( width != 2 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( LittleShort( 256 ) == 256 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( s_channels == 2 ) {
|
|
|
|
samples <<= 1;
|
|
|
|
}
|
|
|
|
for ( i = 0 ; i < samples ; i++ ) {
|
|
|
|
((short *)data)[i] = LittleShort( ((short *)data)[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_ReadRIFFHeader
|
|
|
|
=================
|
|
|
|
*/
|
2011-01-17 20:27:38 +00:00
|
|
|
static qboolean S_ReadRIFFHeader( int file, const char *filename, snd_info_t *info)
|
2011-01-17 01:35:22 +00:00
|
|
|
{
|
|
|
|
char dump[16];
|
|
|
|
int wav_format;
|
|
|
|
int bits;
|
|
|
|
int fmtlen = 0;
|
2011-01-17 20:27:38 +00:00
|
|
|
|
2011-01-17 01:35:22 +00:00
|
|
|
// skip the riff wav header
|
2011-01-17 02:24:18 +00:00
|
|
|
Sys_FileRead(file, dump, 12);
|
2011-01-17 01:35:22 +00:00
|
|
|
|
2011-01-17 20:27:38 +00:00
|
|
|
int wavstart = Sys_FileTell(file);
|
|
|
|
|
2011-01-17 01:35:22 +00:00
|
|
|
// Scan for the format chunk
|
|
|
|
if((fmtlen = S_FindRIFFChunk(file, "fmt ")) < 0)
|
|
|
|
{
|
2011-01-17 02:24:18 +00:00
|
|
|
Con_Printf( "ERROR: Couldn't find \"fmt\" chunk\n");
|
|
|
|
return false;
|
2011-01-17 01:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the parameters
|
|
|
|
wav_format = FGetLittleShort(file);
|
|
|
|
info->channels = FGetLittleShort(file);
|
|
|
|
info->rate = FGetLittleLong(file);
|
|
|
|
FGetLittleLong(file);
|
|
|
|
FGetLittleShort(file);
|
|
|
|
bits = FGetLittleShort(file);
|
|
|
|
|
|
|
|
if( bits < 8 )
|
|
|
|
{
|
2011-01-17 02:24:18 +00:00
|
|
|
Con_Printf( "ERROR: Less than 8 bit sound is not supported\n");
|
|
|
|
return false;
|
2011-01-17 01:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info->width = bits / 8;
|
|
|
|
info->dataofs = 0;
|
2011-01-17 20:27:38 +00:00
|
|
|
info->samples = 0;
|
|
|
|
|
2011-01-17 18:18:31 +00:00
|
|
|
// get cue chunk
|
2011-01-17 20:27:38 +00:00
|
|
|
Sys_FileSeek(file, wavstart);
|
|
|
|
if( S_FindRIFFChunk(file, "cue ") > 0)
|
2011-01-17 18:18:31 +00:00
|
|
|
{
|
2011-01-17 20:27:38 +00:00
|
|
|
Sys_FileSeekRelative(file, 24);
|
|
|
|
info->loopstart = FGetLittleLong(file);
|
2011-01-17 18:18:31 +00:00
|
|
|
|
|
|
|
// if the next chunk is a LIST chunk, look for a cue length marker
|
2011-01-17 20:27:38 +00:00
|
|
|
if ( S_FindRIFFChunk(file, "LIST") > 0)
|
2011-01-17 18:18:31 +00:00
|
|
|
{
|
2011-01-17 20:27:38 +00:00
|
|
|
Sys_FileSeekRelative(file, 20);
|
|
|
|
char marker[4];
|
|
|
|
Sys_FileRead(file, marker, 4);
|
|
|
|
|
|
|
|
if (!strncmp (marker, "mark", 4))
|
2011-01-17 18:18:31 +00:00
|
|
|
{ // this is not a proper parse, but it works with cooledit...
|
2011-01-17 20:27:38 +00:00
|
|
|
Sys_FileSeekRelative(file, -8);
|
|
|
|
int i = FGetLittleLong(file); // samples in loop
|
|
|
|
info->samples = info->loopstart + i;
|
2011-01-17 18:18:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-17 20:27:38 +00:00
|
|
|
else
|
|
|
|
{
|
2011-01-17 18:18:31 +00:00
|
|
|
info->loopstart = -1;
|
2011-01-17 20:27:38 +00:00
|
|
|
}
|
|
|
|
|
2011-01-17 01:35:22 +00:00
|
|
|
// Scan for the data chunk
|
2011-01-17 20:27:38 +00:00
|
|
|
Sys_FileSeek(file, wavstart);
|
2011-01-17 01:35:22 +00:00
|
|
|
if( (info->size = S_FindRIFFChunk(file, "data")) < 0)
|
|
|
|
{
|
2011-01-17 02:24:18 +00:00
|
|
|
Con_Printf( "ERROR: Couldn't find \"data\" chunk\n");
|
|
|
|
return false;
|
2011-01-17 01:35:22 +00:00
|
|
|
}
|
|
|
|
|
2011-01-17 20:27:38 +00:00
|
|
|
int samples = (info->size / info->width) / info->channels;
|
|
|
|
|
|
|
|
if (info->samples)
|
|
|
|
{
|
|
|
|
if (samples < info->samples)
|
|
|
|
{
|
|
|
|
Con_Printf ("Sound has a bad loop length \"%s\"\n", filename);
|
|
|
|
info->samples = samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
info->samples = samples;
|
|
|
|
|
|
|
|
info->dataofs = Sys_FileTell(file) - (wavstart - 12);
|
|
|
|
|
2011-01-17 02:24:18 +00:00
|
|
|
return true;
|
2011-01-17 01:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WAV codec
|
|
|
|
snd_codec_t wav_codec =
|
|
|
|
{
|
|
|
|
".wav",
|
|
|
|
S_WAV_CodecLoad,
|
|
|
|
S_WAV_CodecOpenStream,
|
|
|
|
S_WAV_CodecReadStream,
|
|
|
|
S_WAV_CodecCloseStream,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_WAV_CodecLoad
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
void *S_WAV_CodecLoad(const char *filename, snd_info_t *info)
|
|
|
|
{
|
2011-01-17 02:24:18 +00:00
|
|
|
int file;
|
2011-01-17 01:35:22 +00:00
|
|
|
void *buffer;
|
2011-01-17 20:54:17 +00:00
|
|
|
|
2011-01-17 01:35:22 +00:00
|
|
|
// Try to open the file
|
2011-01-17 05:25:39 +00:00
|
|
|
COM_OpenFile(filename, &file);
|
2011-01-17 02:24:18 +00:00
|
|
|
if(file == -1)
|
2011-01-17 01:35:22 +00:00
|
|
|
{
|
2011-01-17 02:24:18 +00:00
|
|
|
Con_Printf( "ERROR: Could not open \"%s\"\n",
|
2011-01-17 01:35:22 +00:00
|
|
|
filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the RIFF header
|
2011-01-17 20:27:38 +00:00
|
|
|
if(!S_ReadRIFFHeader(file, filename, info))
|
2011-01-17 01:35:22 +00:00
|
|
|
{
|
2011-01-17 03:53:50 +00:00
|
|
|
COM_CloseFile(file);
|
2011-01-17 02:24:18 +00:00
|
|
|
Con_Printf( "ERROR: Incorrect/unsupported format in \"%s\"\n",
|
2011-01-17 01:35:22 +00:00
|
|
|
filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate some memory
|
|
|
|
buffer = Z_Malloc(info->size);
|
|
|
|
if(!buffer)
|
|
|
|
{
|
2011-01-17 03:53:50 +00:00
|
|
|
COM_CloseFile(file);
|
2011-01-17 02:24:18 +00:00
|
|
|
Con_Printf( "ERROR: Out of memory reading \"%s\"\n",
|
2011-01-17 01:35:22 +00:00
|
|
|
filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read, byteswap
|
2011-01-17 02:24:18 +00:00
|
|
|
Sys_FileRead(file, buffer, info->size);
|
2011-01-17 01:35:22 +00:00
|
|
|
S_ByteSwapRawSamples(info->samples, info->width, info->channels, (byte *)buffer);
|
|
|
|
|
|
|
|
// Close and return
|
2011-01-17 03:53:50 +00:00
|
|
|
COM_CloseFile(file);
|
2011-01-17 01:35:22 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_WAV_CodecOpenStream
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
snd_stream_t *S_WAV_CodecOpenStream(const char *filename)
|
|
|
|
{
|
|
|
|
snd_stream_t *rv;
|
|
|
|
|
|
|
|
// Open
|
|
|
|
rv = S_CodecUtilOpen(filename, &wav_codec);
|
|
|
|
if(!rv)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Read the RIFF header
|
2011-01-17 20:54:17 +00:00
|
|
|
if(!S_ReadRIFFHeader(rv->file, filename, &rv->info))
|
2011-01-17 01:35:22 +00:00
|
|
|
{
|
|
|
|
S_CodecUtilClose(&rv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_WAV_CodecCloseStream
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
void S_WAV_CodecCloseStream(snd_stream_t *stream)
|
|
|
|
{
|
|
|
|
S_CodecUtilClose(&stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_WAV_CodecReadStream
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
int S_WAV_CodecReadStream(snd_stream_t *stream, int bytes, void *buffer)
|
|
|
|
{
|
|
|
|
int remaining = stream->info.size - stream->pos;
|
|
|
|
int samples;
|
|
|
|
|
|
|
|
if(remaining <= 0)
|
|
|
|
return 0;
|
|
|
|
if(bytes > remaining)
|
|
|
|
bytes = remaining;
|
|
|
|
stream->pos += bytes;
|
|
|
|
samples = (bytes / stream->info.width) / stream->info.channels;
|
2011-01-17 02:24:18 +00:00
|
|
|
Sys_FileRead(stream->file, buffer, bytes);
|
2011-01-17 01:35:22 +00:00
|
|
|
S_ByteSwapRawSamples(samples, stream->info.width, stream->info.channels, buffer);
|
|
|
|
return bytes;
|
|
|
|
}
|