- Formatiere neu

- Vereinfache Code
This commit is contained in:
Yamagi Burmeister 2010-10-14 06:08:10 +00:00
parent 86e7ecb0f4
commit 2b405405dc

View file

@ -19,10 +19,8 @@
*
* =======================================================================
*
* The sound caching. This files manages a double linked list (a circle
* buffer) which caches the sound samples and provides their memory
* adresses to the low level sound backend. It also implements the WAV
* fileformat and functions for loading the samples into the list.
* The sound caching. This file contains support functions for loading
* the sound samples into the memory.
*
* =======================================================================
*/
@ -30,141 +28,169 @@
#include "../header/client.h"
#include "header/local.h"
void ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte *data) {
int outcount;
int srcsample;
float stepscale;
int i;
int sample;
void
ResampleSfx ( sfx_t *sfx, int inrate, int inwidth, byte *data )
{
int outcount;
int srcsample;
float stepscale;
int i;
int sample;
unsigned int samplefrac, fracstep;
sfxcache_t *sc;
sfxcache_t *sc;
sc = sfx->cache;
if (!sc)
return;
stepscale = (float)inrate / dma.speed; /* this is usually 0.5, 1, or 2 */
outcount = (int)(sc->length / stepscale);
if (outcount == 0)
if ( !sc )
{
Com_Printf ("ResampleSfx: Invalid sound file '%s' (zero length)\n", sfx->name);
Z_Free (sfx->cache);
return;
}
stepscale = (float) inrate / dma.speed; /* this is usually 0.5, 1, or 2 */
outcount = (int) ( sc->length / stepscale );
if ( outcount == 0 )
{
Com_Printf( "ResampleSfx: Invalid sound file '%s' (zero length)\n", sfx->name );
Z_Free( sfx->cache );
sfx->cache = NULL;
return;
}
sc->length = outcount;
if (sc->loopstart != -1)
sc->loopstart = (int)(sc->loopstart / stepscale);
if ( sc->loopstart != -1 )
{
sc->loopstart = (int) ( sc->loopstart / stepscale );
}
sc->speed = dma.speed;
if (s_loadas8bit->value)
if ( s_loadas8bit->value )
{
sc->width = 1;
}
else
{
sc->width = inwidth;
}
sc->stereo = 0;
/* resample / decimate to the current source rate */
if (stepscale == 1 && inwidth == 1 && sc->width == 1) {
/* fast special case */
for (i=0 ; i<outcount ; i++)
((signed char *)sc->data)[i]
= (int)( (unsigned char)(data[i]) - 128);
samplefrac = 0;
fracstep = (int) ( stepscale * 256 );
} else {
/* general case */
samplefrac = 0;
fracstep = (int)(stepscale*256);
for ( i = 0; i < outcount; i++ )
{
srcsample = samplefrac >> 8;
samplefrac += fracstep;
for (i=0 ; i<outcount ; i++) {
srcsample = samplefrac >> 8;
samplefrac += fracstep;
if ( inwidth == 2 )
{
sample = LittleShort( ( (short *) data ) [ srcsample ] );
}
if (inwidth == 2)
sample = LittleShort ( ((short *)data)[srcsample] );
else
{
sample = (int) ( (unsigned char) ( data [ srcsample ] ) - 128 ) << 8;
}
else
sample = (int)( (unsigned char)(data[srcsample]) - 128) << 8;
if ( sc->width == 2 )
{
( (short *) sc->data ) [ i ] = sample;
}
if (sc->width == 2)
((short *)sc->data)[i] = sample;
else
((signed char *)sc->data)[i] = sample >> 8;
else
{
( (signed char *) sc->data ) [ i ] = sample >> 8;
}
}
}
sfxcache_t *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;
sfxcache_t *
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;
if (s->name[0] == '*')
return NULL;
if ( s->name [ 0 ] == '*' )
{
return ( NULL );
}
/* see if still in memory */
sc = s->cache;
if (sc)
return sc;
if ( sc )
{
return ( sc );
}
/* load it in */
if (s->truename)
if ( s->truename )
{
name = s->truename;
}
else
{
name = s->name;
}
if (name[0] == '#')
strcpy(namebuffer, &name[1]);
if ( name [ 0 ] == '#' )
{
strcpy( namebuffer, &name [ 1 ] );
}
else
Com_sprintf (namebuffer, sizeof(namebuffer), "sound/%s", name);
{
Com_sprintf( namebuffer, sizeof ( namebuffer ), "sound/%s", name );
}
size = FS_LoadFile (namebuffer, (void **)&data);
size = FS_LoadFile( namebuffer, (void **) &data );
if (!data) {
if ( !data )
{
s->cache = NULL;
Com_DPrintf ("Couldn't load %s\n", namebuffer);
return NULL;
Com_DPrintf( "Couldn't load %s\n", namebuffer );
return ( NULL );
}
info = GetWavinfo (s->name, data, size);
info = GetWavinfo( s->name, data, size );
if (info.channels != 1) {
Com_Printf ("%s is a stereo sample\n",s->name);
FS_FreeFile (data);
return NULL;
if ( info.channels != 1 )
{
Com_Printf( "%s is a stereo sample\n", s->name );
FS_FreeFile( data );
return ( NULL );
}
stepscale = (float)info.rate / dma.speed;
len = (int)(info.samples / stepscale);
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;
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));
sc = s->cache = Z_Malloc( len + sizeof ( sfxcache_t ) );
if (!sc) {
FS_FreeFile (data);
return NULL;
if ( !sc )
{
FS_FreeFile( data );
return ( NULL );
}
sc->length = info.samples;
@ -173,10 +199,10 @@ sfxcache_t *S_LoadSound (sfx_t *s) {
sc->width = info.width;
sc->stereo = info.channels;
ResampleSfx (s, sc->speed, sc->width, data + info.dataofs);
ResampleSfx( s, sc->speed, sc->width, data + info.dataofs );
FS_FreeFile (data);
FS_FreeFile( data );
return sc;
return ( sc );
}