2010-12-17 00:37:07 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 1996-2001 Id Software, Inc.
|
|
|
|
Copyright (C) 2002-2009 John Fitzgibbons and others
|
|
|
|
|
|
|
|
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
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
// snd_mem.c: sound caching
|
|
|
|
|
|
|
|
#include "quakedef.h"
|
2011-01-17 18:18:31 +00:00
|
|
|
#include "snd_codec.h"
|
2010-12-17 00:37:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
S_LoadSound
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
sfxcache_t *S_LoadSound (sfx_t *s)
|
|
|
|
{
|
2011-01-17 18:18:31 +00:00
|
|
|
snd_info_t info;
|
2010-12-17 00:37:07 +00:00
|
|
|
char namebuffer[256];
|
|
|
|
byte *data;
|
|
|
|
int len;
|
|
|
|
sfxcache_t *sc;
|
|
|
|
|
|
|
|
// see if still in memory
|
|
|
|
sc = (sfxcache_t *) Cache_Check (&s->cache);
|
|
|
|
if (sc)
|
|
|
|
return sc;
|
|
|
|
|
|
|
|
//Con_Printf ("S_LoadSound: %x\n", (int)stackbuf);
|
|
|
|
// load it in
|
|
|
|
Q_strcpy(namebuffer, "sound/");
|
|
|
|
Q_strcat(namebuffer, s->name);
|
|
|
|
|
|
|
|
// Con_Printf ("loading %s\n",namebuffer);
|
|
|
|
|
2011-01-17 18:18:31 +00:00
|
|
|
// load it in
|
|
|
|
data = S_CodecLoad(namebuffer, &info);
|
2010-12-17 00:37:07 +00:00
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
Con_Printf ("Couldn't load %s\n", namebuffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-01-17 18:18:31 +00:00
|
|
|
|
2010-12-17 00:37:07 +00:00
|
|
|
if (info.channels != 1)
|
|
|
|
{
|
|
|
|
Con_Printf ("%s is a stereo sample\n",s->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-17 18:18:31 +00:00
|
|
|
int resampledNumSamples;
|
|
|
|
void *resampled = Snd_Resample(info.rate, info.width, info.samples, info.channels, data, shm->speed, shm->samplebits/8, &resampledNumSamples);
|
|
|
|
|
2011-01-18 20:31:30 +00:00
|
|
|
if (info.rate == shm->speed && info.width == shm->samplebits/8)
|
|
|
|
{
|
|
|
|
if (resampledNumSamples != info.samples) exit(5);
|
|
|
|
if (0 != memcmp(data, resampled, (info.width * info.samples * info.channels))) exit(6);
|
|
|
|
}
|
|
|
|
|
2011-01-17 18:18:31 +00:00
|
|
|
len = resampledNumSamples * (shm->samplebits/8) * info.channels;
|
2010-12-17 00:37:07 +00:00
|
|
|
|
|
|
|
sc = (sfxcache_t *) Cache_Alloc ( &s->cache, len + sizeof(sfxcache_t), s->name);
|
|
|
|
if (!sc)
|
|
|
|
return NULL;
|
|
|
|
|
2011-01-17 20:54:17 +00:00
|
|
|
float ratio = (float)info.rate / (float)shm->speed;
|
2011-01-17 18:18:31 +00:00
|
|
|
|
|
|
|
sc->length = resampledNumSamples;
|
|
|
|
sc->loopstart = (info.loopstart == -1 ? -1 : info.loopstart / ratio); // reposition loop marker to take resampling into account
|
|
|
|
sc->speed = shm->speed;
|
|
|
|
sc->width = (shm->samplebits/8);
|
2010-12-17 00:37:07 +00:00
|
|
|
sc->stereo = info.channels;
|
2011-01-17 18:18:31 +00:00
|
|
|
memcpy(sc->data, resampled, len);
|
|
|
|
|
|
|
|
free(resampled);
|
|
|
|
Z_Free(data);
|
|
|
|
|
2010-12-17 00:37:07 +00:00
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|