Refactor the SDL part of S_LoadSound into the SDL backend

This commit is contained in:
Yamagi Burmeister 2013-04-20 10:26:31 +02:00
parent 0c93ecbddb
commit 2c356d142e
3 changed files with 36 additions and 57 deletions

View File

@ -799,66 +799,68 @@ SDL_UpdateScaletable(void)
* necessary endianess convertions are
* performed.
*/
void
SDL_Cache(sfx_t *sfx, int inrate, int inwidth, byte *data)
qboolean
SDL_Cache(sfx_t *sfx, wavinfo_t *info, byte *data)
{
int outcount;
int srcsample;
float stepscale;
int i;
int len;
int sample;
unsigned int samplefrac, fracstep;
int srcsample;
sfxcache_t *sc;
unsigned int samplefrac = 0;
sc = sfx->cache;
stepscale = (float)info->rate / dma.speed;
len = (int)(info->samples / stepscale);
if ((info->samples == 0) || (len == 0))
{
Com_Printf("WARNING: Zero length sound encountered: %s\n", sfx->name);
return false;
}
len = len * info->width * info->channels;
sc = sfx->cache = Z_Malloc(len + sizeof(sfxcache_t));
if (!sc)
{
return;
return false;
}
stepscale = (float)inrate / dma.speed;
outcount = (int)(sc->length / stepscale);
sc->loopstart = info->loopstart;
sc->stereo = 0;
sc->length = (int)(info->samples / stepscale);
sc->speed = dma.speed;
if (outcount == 0)
if ((int)(info->samples / stepscale) == 0)
{
Com_Printf("ResampleSfx: Invalid sound file '%s' (zero length)\n", sfx->name);
Z_Free(sfx->cache);
sfx->cache = NULL;
return;
return false;
}
sc->length = outcount;
if (sc->loopstart != -1)
{
sc->loopstart = (int)(sc->loopstart / stepscale);
}
sc->speed = dma.speed;
if (s_loadas8bit->value)
{
sc->width = 1;
}
else
{
sc->width = inwidth;
sc->width = info->width;
}
sc->stereo = 0;
/* resample / decimate to the current source rate */
samplefrac = 0;
fracstep = (int)(stepscale * 256);
for (i = 0; i < outcount; i++)
for (i = 0; i < (int)(info->samples / stepscale); i++)
{
srcsample = samplefrac >> 8;
samplefrac += fracstep;
samplefrac += (int)(stepscale * 256);
if (inwidth == 2)
if (info->width == 2)
{
sample = LittleShort(((short *)data)[srcsample]);
}
@ -878,6 +880,8 @@ SDL_Cache(sfx_t *sfx, int inrate, int inwidth, byte *data)
((signed char *)sc->data)[i] = sample >> 8;
}
}
return true;
}
/* ------------------------------------------------------------------ */

View File

@ -174,7 +174,7 @@ void SDL_UpdateScaletable(void);
sfxcache_t *S_LoadSound(sfx_t *s);
void S_IssuePlaysound(playsound_t *ps);
void SDL_PaintChannels(int endtime);
void SDL_Cache(sfx_t *sfx, int inrate, int inwidth, byte *data);
qboolean SDL_Cache(sfx_t *sfx, wavinfo_t *info, byte *data);
/* picks a channel based on priorities, empty slots, number of channels */
channel_t *S_PickChannel(int entnum, int entchannel);

View File

@ -257,8 +257,6 @@ S_LoadSound(sfx_t *s)
char namebuffer[MAX_QPATH];
byte *data;
wavinfo_t info;
int len;
float stepscale;
sfxcache_t *sc;
int size;
char *name;
@ -314,34 +312,6 @@ S_LoadSound(sfx_t *s)
return NULL;
}
if (sound_started != SS_OAL)
{
stepscale = (float)info.rate / dma.speed;
len = (int)(info.samples / stepscale);
if ((info.samples == 0) || (len == 0))
{
Com_Printf("WARNING: Zero length sound encountered: %s\n", s->name);
FS_FreeFile(data);
return NULL;
}
len = len * info.width * info.channels;
sc = s->cache = Z_Malloc(len + sizeof(sfxcache_t));
if (!sc)
{
FS_FreeFile(data);
return NULL;
}
sc->length = info.samples;
sc->loopstart = info.loopstart;
sc->speed = info.rate;
sc->width = info.width;
sc->stereo = info.channels;
}
#if USE_OPENAL
if (sound_started == SS_OAL)
{
@ -350,7 +320,12 @@ S_LoadSound(sfx_t *s)
else
#endif
{
SDL_Cache(s, sc->speed, sc->width, data + info.dataofs);
if (!SDL_Cache(s, &info, data + info.dataofs))
{
Com_Printf("Pansen!\n");
FS_FreeFile(data);
return NULL;
}
}
FS_FreeFile(data);