2005-11-13 18:58:14 +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 "client.h"
|
|
|
|
#include "snd_codec.h"
|
|
|
|
|
|
|
|
static snd_codec_t *codecs;
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
2011-05-09 22:40:25 +00:00
|
|
|
S_CodecGetSound
|
2005-11-13 18:58:14 +00:00
|
|
|
|
2011-05-09 22:40:25 +00:00
|
|
|
Opens/loads a sound, tries codec based on the sound's file extension
|
|
|
|
then tries all supported codecs.
|
2005-11-13 18:58:14 +00:00
|
|
|
=================
|
|
|
|
*/
|
2011-05-09 22:40:25 +00:00
|
|
|
static void *S_CodecGetSound(const char *filename, snd_info_t *info)
|
2005-11-13 18:58:14 +00:00
|
|
|
{
|
2011-05-09 22:40:25 +00:00
|
|
|
snd_codec_t *codec;
|
|
|
|
snd_codec_t *orgCodec = NULL;
|
|
|
|
qboolean orgNameFailed = qfalse;
|
|
|
|
char localName[ MAX_QPATH ];
|
|
|
|
const char *ext;
|
|
|
|
char altName[ MAX_QPATH ];
|
|
|
|
void *rtn = NULL;
|
|
|
|
|
|
|
|
Q_strncpyz(localName, filename, MAX_QPATH);
|
2005-11-13 18:58:14 +00:00
|
|
|
|
2011-05-12 10:53:36 +00:00
|
|
|
ext = COM_GetExtension(localName);
|
2011-05-09 22:40:25 +00:00
|
|
|
|
|
|
|
if( *ext )
|
2005-11-13 18:58:14 +00:00
|
|
|
{
|
2011-05-09 22:40:25 +00:00
|
|
|
// Look for the correct loader and use it
|
|
|
|
for( codec = codecs; codec; codec = codec->next )
|
2005-11-13 18:58:14 +00:00
|
|
|
{
|
2011-05-09 22:40:25 +00:00
|
|
|
if( !Q_stricmp( ext, codec->ext ) )
|
|
|
|
{
|
|
|
|
// Load
|
|
|
|
if( info )
|
|
|
|
rtn = codec->load(localName, info);
|
|
|
|
else
|
|
|
|
rtn = codec->open(localName);
|
|
|
|
break;
|
|
|
|
}
|
2005-11-13 18:58:14 +00:00
|
|
|
}
|
|
|
|
|
2011-05-09 22:40:25 +00:00
|
|
|
// A loader was found
|
|
|
|
if( codec )
|
|
|
|
{
|
|
|
|
if( !rtn )
|
|
|
|
{
|
|
|
|
// Loader failed, most likely because the file isn't there;
|
|
|
|
// try again without the extension
|
|
|
|
orgNameFailed = qtrue;
|
|
|
|
orgCodec = codec;
|
|
|
|
COM_StripExtension( filename, localName, MAX_QPATH );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Something loaded
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
}
|
2005-11-13 18:58:14 +00:00
|
|
|
}
|
|
|
|
|
2011-05-09 22:40:25 +00:00
|
|
|
// Try and find a suitable match using all
|
|
|
|
// the sound codecs supported
|
|
|
|
for( codec = codecs; codec; codec = codec->next )
|
2005-11-13 18:58:14 +00:00
|
|
|
{
|
2011-05-09 22:40:25 +00:00
|
|
|
if( codec == orgCodec )
|
|
|
|
continue;
|
|
|
|
|
2011-05-12 12:25:36 +00:00
|
|
|
Com_sprintf( altName, sizeof (altName), "%s.%s", localName, codec->ext );
|
2011-05-09 22:40:25 +00:00
|
|
|
|
|
|
|
// Load
|
|
|
|
if( info )
|
|
|
|
rtn = codec->load(altName, info);
|
|
|
|
else
|
|
|
|
rtn = codec->open(altName);
|
|
|
|
|
|
|
|
if( rtn )
|
|
|
|
{
|
|
|
|
if( orgNameFailed )
|
|
|
|
{
|
|
|
|
Com_DPrintf(S_COLOR_YELLOW "WARNING: %s not present, using %s instead\n",
|
|
|
|
filename, altName );
|
|
|
|
}
|
|
|
|
|
|
|
|
return rtn;
|
|
|
|
}
|
2005-11-13 18:58:14 +00:00
|
|
|
}
|
|
|
|
|
2011-05-09 22:40:25 +00:00
|
|
|
Com_Printf(S_COLOR_YELLOW "WARNING: Failed to %s sound %s!\n", info ? "load" : "open", filename);
|
|
|
|
|
2005-11-13 18:58:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_CodecInit
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
void S_CodecInit()
|
|
|
|
{
|
|
|
|
codecs = NULL;
|
2011-05-09 22:40:25 +00:00
|
|
|
|
2007-09-22 20:32:11 +00:00
|
|
|
#ifdef USE_CODEC_VORBIS
|
2005-11-13 18:58:14 +00:00
|
|
|
S_CodecRegister(&ogg_codec);
|
|
|
|
#endif
|
2011-05-09 22:40:25 +00:00
|
|
|
|
|
|
|
// Register wav codec last so that it is always tried first when a file extension was not found
|
|
|
|
S_CodecRegister(&wav_codec);
|
2005-11-13 18:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_CodecShutdown
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
void S_CodecShutdown()
|
|
|
|
{
|
|
|
|
codecs = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_CodecRegister
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
void S_CodecRegister(snd_codec_t *codec)
|
|
|
|
{
|
|
|
|
codec->next = codecs;
|
|
|
|
codecs = codec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_CodecLoad
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
void *S_CodecLoad(const char *filename, snd_info_t *info)
|
|
|
|
{
|
2011-05-09 22:40:25 +00:00
|
|
|
return S_CodecGetSound(filename, info);
|
2005-11-13 18:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_CodecOpenStream
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
snd_stream_t *S_CodecOpenStream(const char *filename)
|
|
|
|
{
|
2011-05-09 22:40:25 +00:00
|
|
|
return S_CodecGetSound(filename, NULL);
|
2005-11-13 18:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void S_CodecCloseStream(snd_stream_t *stream)
|
|
|
|
{
|
|
|
|
stream->codec->close(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
int S_CodecReadStream(snd_stream_t *stream, int bytes, void *buffer)
|
|
|
|
{
|
|
|
|
return stream->codec->read(stream, bytes, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
//=======================================================================
|
|
|
|
// Util functions (used by codecs)
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_CodecUtilOpen
|
|
|
|
=================
|
|
|
|
*/
|
|
|
|
snd_stream_t *S_CodecUtilOpen(const char *filename, snd_codec_t *codec)
|
|
|
|
{
|
|
|
|
snd_stream_t *stream;
|
|
|
|
fileHandle_t hnd;
|
2006-01-06 17:36:03 +00:00
|
|
|
int length;
|
2005-11-13 18:58:14 +00:00
|
|
|
|
|
|
|
// Try to open the file
|
2006-01-06 17:36:03 +00:00
|
|
|
length = FS_FOpenFileRead(filename, &hnd, qtrue);
|
2005-11-13 18:58:14 +00:00
|
|
|
if(!hnd)
|
|
|
|
{
|
2011-06-25 09:35:03 +00:00
|
|
|
Com_DPrintf("Can't read sound file %s\n", filename);
|
2005-11-13 18:58:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a stream
|
|
|
|
stream = Z_Malloc(sizeof(snd_stream_t));
|
|
|
|
if(!stream)
|
|
|
|
{
|
|
|
|
FS_FCloseFile(hnd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy over, return
|
|
|
|
stream->codec = codec;
|
|
|
|
stream->file = hnd;
|
2006-01-06 17:36:03 +00:00
|
|
|
stream->length = length;
|
2005-11-13 18:58:14 +00:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
|
|
|
S_CodecUtilClose
|
|
|
|
=================
|
|
|
|
*/
|
2007-11-18 01:06:46 +00:00
|
|
|
void S_CodecUtilClose(snd_stream_t **stream)
|
2005-11-13 18:58:14 +00:00
|
|
|
{
|
2007-11-18 01:06:46 +00:00
|
|
|
FS_FCloseFile((*stream)->file);
|
|
|
|
Z_Free(*stream);
|
|
|
|
*stream = NULL;
|
2005-11-13 18:58:14 +00:00
|
|
|
}
|