2010-06-19 18:59:33 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1997-2001 Id Software, Inc.
|
|
|
|
*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software
|
2010-07-13 18:19:42 +00:00
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
|
|
* USA.
|
2010-06-19 18:59:33 +00:00
|
|
|
*
|
|
|
|
* =======================================================================
|
|
|
|
*
|
|
|
|
* This file implements a subset of the WAVE audio file format
|
|
|
|
*
|
|
|
|
* =======================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../header/client.h"
|
2010-06-19 19:10:31 +00:00
|
|
|
#include "header/local.h"
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2012-04-15 02:58:01 +00:00
|
|
|
// FIXME: this code is really fucked up, those global variables make me sick.
|
|
|
|
// someone should clean this up one day..
|
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
byte *data_p;
|
|
|
|
byte *iff_end;
|
|
|
|
byte *last_chunk;
|
|
|
|
byte *iff_data;
|
|
|
|
int iff_chunk_len;
|
|
|
|
|
|
|
|
short
|
|
|
|
GetLittleShort ( void )
|
|
|
|
{
|
2010-06-19 18:59:33 +00:00
|
|
|
short val = 0;
|
2010-10-14 06:29:53 +00:00
|
|
|
|
2010-06-19 18:59:33 +00:00
|
|
|
val = *data_p;
|
2010-10-14 06:29:53 +00:00
|
|
|
val = val + ( *( data_p + 1 ) << 8 );
|
2010-06-19 18:59:33 +00:00
|
|
|
data_p += 2;
|
2010-10-14 06:29:53 +00:00
|
|
|
return ( val );
|
2010-06-19 18:59:33 +00:00
|
|
|
}
|
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
int
|
|
|
|
GetLittleLong ( void )
|
|
|
|
{
|
2010-06-19 18:59:33 +00:00
|
|
|
int val = 0;
|
2010-10-14 06:29:53 +00:00
|
|
|
|
2010-06-19 18:59:33 +00:00
|
|
|
val = *data_p;
|
2010-10-14 06:29:53 +00:00
|
|
|
val = val + ( *( data_p + 1 ) << 8 );
|
|
|
|
val = val + ( *( data_p + 2 ) << 16 );
|
|
|
|
val = val + ( *( data_p + 3 ) << 24 );
|
2010-06-19 18:59:33 +00:00
|
|
|
data_p += 4;
|
2010-10-14 06:29:53 +00:00
|
|
|
return ( val );
|
2010-06-19 18:59:33 +00:00
|
|
|
}
|
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
void
|
|
|
|
FindNextChunk ( char *name )
|
|
|
|
{
|
|
|
|
while ( 1 )
|
|
|
|
{
|
|
|
|
data_p = last_chunk;
|
2010-10-13 17:07:29 +00:00
|
|
|
data_p += 4;
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
if ( data_p >= iff_end )
|
2010-10-13 17:07:29 +00:00
|
|
|
{
|
2010-06-19 18:59:33 +00:00
|
|
|
data_p = NULL;
|
|
|
|
return;
|
2010-10-14 06:29:53 +00:00
|
|
|
}
|
2010-06-19 18:59:33 +00:00
|
|
|
|
|
|
|
iff_chunk_len = GetLittleLong();
|
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
if ( iff_chunk_len < 0 )
|
|
|
|
{
|
2010-06-19 18:59:33 +00:00
|
|
|
data_p = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_p -= 8;
|
2010-10-14 06:29:53 +00:00
|
|
|
last_chunk = data_p + 8 + ( ( iff_chunk_len + 1 ) & ~1 );
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
if ( !strncmp( (const char *) data_p, name, 4 ) )
|
|
|
|
{
|
2010-06-19 18:59:33 +00:00
|
|
|
return;
|
2010-10-14 06:29:53 +00:00
|
|
|
}
|
2010-06-19 18:59:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
void
|
|
|
|
FindChunk ( char *name )
|
|
|
|
{
|
2010-06-19 18:59:33 +00:00
|
|
|
last_chunk = iff_data;
|
2010-10-14 06:29:53 +00:00
|
|
|
FindNextChunk( name );
|
2010-06-19 18:59:33 +00:00
|
|
|
}
|
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
wavinfo_t
|
|
|
|
GetWavinfo ( char *name, byte *wav, int wavlength )
|
|
|
|
{
|
|
|
|
wavinfo_t info;
|
|
|
|
int i;
|
|
|
|
int format;
|
|
|
|
int samples;
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
memset( &info, 0, sizeof ( info ) );
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
if ( !wav )
|
|
|
|
{
|
|
|
|
return ( info );
|
|
|
|
}
|
2010-06-19 18:59:33 +00:00
|
|
|
|
|
|
|
iff_data = wav;
|
|
|
|
iff_end = wav + wavlength;
|
|
|
|
|
|
|
|
/* find "RIFF" chunk */
|
2010-10-14 06:29:53 +00:00
|
|
|
FindChunk( "RIFF" );
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
if ( !( data_p && !strncmp( (const char *) data_p + 8, "WAVE", 4 ) ) )
|
|
|
|
{
|
|
|
|
Com_Printf( "Missing RIFF/WAVE chunks\n" );
|
|
|
|
return ( info );
|
2010-06-19 18:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get "fmt " chunk */
|
|
|
|
iff_data = data_p + 12;
|
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
FindChunk( "fmt " );
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
if ( !data_p )
|
|
|
|
{
|
|
|
|
Com_Printf( "Missing fmt chunk\n" );
|
|
|
|
return ( info );
|
2010-06-19 18:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data_p += 8;
|
|
|
|
format = GetLittleShort();
|
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
if ( format != 1 )
|
|
|
|
{
|
|
|
|
Com_Printf( "Microsoft PCM format only\n" );
|
|
|
|
return ( info );
|
2010-06-19 18:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info.channels = GetLittleShort();
|
|
|
|
info.rate = GetLittleLong();
|
2010-10-14 06:29:53 +00:00
|
|
|
data_p += 4 + 2;
|
2010-06-19 18:59:33 +00:00
|
|
|
info.width = GetLittleShort() / 8;
|
|
|
|
|
|
|
|
/* get cue chunk */
|
2010-10-14 06:29:53 +00:00
|
|
|
FindChunk( "cue " );
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
if ( data_p )
|
|
|
|
{
|
2010-06-19 18:59:33 +00:00
|
|
|
data_p += 32;
|
|
|
|
info.loopstart = GetLittleLong();
|
|
|
|
|
|
|
|
/* if the next chunk is a LIST chunk, look for a cue length marker */
|
2010-10-14 06:29:53 +00:00
|
|
|
FindNextChunk( "LIST" );
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
if ( data_p )
|
|
|
|
{
|
|
|
|
if ( ( ( data_p - wav ) + 32 <= wavlength ) && !strncmp( (const char *) data_p + 28, "mark", 4 ) )
|
2010-10-13 17:07:29 +00:00
|
|
|
{
|
2010-06-19 18:59:33 +00:00
|
|
|
/* this is not a proper parse, but it works with cooledit... */
|
|
|
|
data_p += 24;
|
2010-10-14 06:29:53 +00:00
|
|
|
i = GetLittleLong(); /* samples in loop */
|
2010-06-19 18:59:33 +00:00
|
|
|
info.samples = info.loopstart + i;
|
|
|
|
}
|
|
|
|
}
|
2010-10-14 06:29:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-06-19 18:59:33 +00:00
|
|
|
info.loopstart = -1;
|
2010-10-14 06:29:53 +00:00
|
|
|
}
|
2010-06-19 18:59:33 +00:00
|
|
|
|
|
|
|
/* find data chunk */
|
2010-10-14 06:29:53 +00:00
|
|
|
FindChunk( "data" );
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
if ( !data_p )
|
|
|
|
{
|
|
|
|
Com_Printf( "Missing data chunk\n" );
|
|
|
|
return ( info );
|
2010-06-19 18:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data_p += 4;
|
2010-10-14 06:29:53 +00:00
|
|
|
samples = GetLittleLong() / info.width;
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
if ( info.samples )
|
|
|
|
{
|
|
|
|
if ( samples < info.samples )
|
|
|
|
{
|
|
|
|
Com_Error( ERR_DROP, "Sound %s has a bad loop length", name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-06-19 18:59:33 +00:00
|
|
|
info.samples = samples;
|
2010-10-14 06:29:53 +00:00
|
|
|
}
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
info.dataofs = (int) ( data_p - wav );
|
2010-06-19 18:59:33 +00:00
|
|
|
|
2010-10-14 06:29:53 +00:00
|
|
|
return ( info );
|
2010-06-19 18:59:33 +00:00
|
|
|
}
|