summaryrefslogtreecommitdiff
path: root/src/snd_al.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/snd_al.c')
-rw-r--r--src/snd_al.c211
1 files changed, 112 insertions, 99 deletions
diff --git a/src/snd_al.c b/src/snd_al.c
index bb4f337..d91d966 100644
--- a/src/snd_al.c
+++ b/src/snd_al.c
@@ -8,7 +8,7 @@ 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.
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
@@ -32,48 +32,50 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
static ALuint s_srcnums[MAX_CHANNELS];
static int s_framecount;
-void AL_SoundInfo( void ) {
- Com_Printf( "AL_VENDOR: %s\n", qalGetString( AL_VENDOR ) );
- Com_Printf( "AL_RENDERER: %s\n", qalGetString( AL_RENDERER ) );
- Com_Printf( "AL_VERSION: %s\n", qalGetString( AL_VERSION ) );
- Com_Printf( "AL_EXTENSIONS: %s\n", qalGetString( AL_EXTENSIONS ) );
- Com_Printf( "Number of sources: %d\n", s_numchannels );
+void AL_SoundInfo(void)
+{
+ Com_Printf("AL_VENDOR: %s\n", qalGetString(AL_VENDOR));
+ Com_Printf("AL_RENDERER: %s\n", qalGetString(AL_RENDERER));
+ Com_Printf("AL_VERSION: %s\n", qalGetString(AL_VERSION));
+ Com_Printf("AL_EXTENSIONS: %s\n", qalGetString(AL_EXTENSIONS));
+ Com_Printf("Number of sources: %d\n", s_numchannels);
}
-qboolean AL_Init( void ) {
+qboolean AL_Init(void)
+{
int i;
- Com_DPrintf( "Initializing OpenAL\n" );
+ Com_DPrintf("Initializing OpenAL\n");
- if( !QAL_Init() ) {
+ if (!QAL_Init()) {
return qfalse;
}
// check for linear distance extension
- if( !qalIsExtensionPresent( "AL_EXT_LINEAR_DISTANCE" ) ) {
- Com_DPrintf( "AL_EXT_LINEAR_DISTANCE extension is missing\n" );
+ if (!qalIsExtensionPresent("AL_EXT_LINEAR_DISTANCE")) {
+ Com_DPrintf("AL_EXT_LINEAR_DISTANCE extension is missing\n");
goto fail;
}
// generate source names
qalGetError();
- for( i = 0; i < MAX_CHANNELS; i++ ) {
- qalGenSources( 1, &s_srcnums[i] );
- if( qalGetError() != AL_NO_ERROR ) {
+ for (i = 0; i < MAX_CHANNELS; i++) {
+ qalGenSources(1, &s_srcnums[i]);
+ if (qalGetError() != AL_NO_ERROR) {
break;
}
}
- Com_DPrintf( "Got %d AL sources\n", i );
+ Com_DPrintf("Got %d AL sources\n", i);
- if( i < MIN_CHANNELS ) {
- Com_DPrintf( "Insufficient number of sources\n" );
+ if (i < MIN_CHANNELS) {
+ Com_DPrintf("Insufficient number of sources\n");
goto fail;
}
s_numchannels = i;
- Com_Printf( "OpenAL initialized.\n" );
+ Com_Printf("OpenAL initialized.\n");
return qtrue;
fail:
@@ -81,40 +83,42 @@ fail:
return qfalse;
}
-void AL_Shutdown( void ) {
- Com_Printf( "Shutting down OpenAL.\n" );
+void AL_Shutdown(void)
+{
+ Com_Printf("Shutting down OpenAL.\n");
- if( s_numchannels ) {
+ if (s_numchannels) {
// delete source names
- qalDeleteSources( s_numchannels, s_srcnums );
- memset( s_srcnums, 0, sizeof( s_srcnums ) );
+ qalDeleteSources(s_numchannels, s_srcnums);
+ memset(s_srcnums, 0, sizeof(s_srcnums));
s_numchannels = 0;
}
QAL_Shutdown();
}
-sfxcache_t *AL_UploadSfx( sfx_t *s ) {
+sfxcache_t *AL_UploadSfx(sfx_t *s)
+{
sfxcache_t *sc;
ALsizei size = s_info.samples * s_info.width;
ALenum format = s_info.width == 2 ? AL_FORMAT_MONO16 : AL_FORMAT_MONO8;
ALuint name;
- if( !size ) {
+ if (!size) {
s->error = Q_ERR_TOO_FEW;
return NULL;
}
qalGetError();
- qalGenBuffers( 1, &name );
- qalBufferData( name, format, s_info.data, size, s_info.rate );
- if( qalGetError() != AL_NO_ERROR ) {
+ qalGenBuffers(1, &name);
+ qalBufferData(name, format, s_info.data, size, s_info.rate);
+ if (qalGetError() != AL_NO_ERROR) {
s->error = Q_ERR_LIBRARY_ERROR;
return NULL;
}
// allocate placeholder sfxcache
- sc = s->cache = S_Malloc( sizeof( *sc ) );
+ sc = s->cache = S_Malloc(sizeof(*sc));
sc->length = s_info.samples * 1000 / s_info.rate; // in msec
sc->loopstart = s_info.loopstart;
sc->width = s_info.width;
@@ -124,75 +128,80 @@ sfxcache_t *AL_UploadSfx( sfx_t *s ) {
return sc;
}
-void AL_DeleteSfx( sfx_t *s ) {
+void AL_DeleteSfx(sfx_t *s)
+{
sfxcache_t *sc;
ALuint name;
sc = s->cache;
- if( !sc ) {
+ if (!sc) {
return;
}
name = sc->bufnum;
- qalDeleteBuffers( 1, &name );
+ qalDeleteBuffers(1, &name);
}
-static void AL_Spatialize( channel_t *ch ) {
+static void AL_Spatialize(channel_t *ch)
+{
vec3_t origin;
// anything coming from the view entity will always be full volume
// no attenuation = no spatialization
- if( ch->entnum == -1 || ch->entnum == listener_entnum || !ch->dist_mult ) {
- VectorCopy( listener_origin, origin );
- } else if( ch->fixed_origin ) {
- VectorCopy( ch->origin, origin );
+ if (ch->entnum == -1 || ch->entnum == listener_entnum || !ch->dist_mult) {
+ VectorCopy(listener_origin, origin);
+ } else if (ch->fixed_origin) {
+ VectorCopy(ch->origin, origin);
} else {
- CL_GetEntitySoundOrigin( ch->entnum, origin );
+ CL_GetEntitySoundOrigin(ch->entnum, origin);
}
- qalSource3f( ch->srcnum, AL_POSITION, AL_UnpackVector( origin ) );
+ qalSource3f(ch->srcnum, AL_POSITION, AL_UnpackVector(origin));
}
-void AL_StopChannel( channel_t *ch ) {
+void AL_StopChannel(channel_t *ch)
+{
#ifdef _DEBUG
if (s_show->integer > 1)
- Com_Printf("%s: %s\n", __func__, ch->sfx->name );
+ Com_Printf("%s: %s\n", __func__, ch->sfx->name);
#endif
// stop it
- qalSourceStop( ch->srcnum );
- qalSourcei( ch->srcnum, AL_BUFFER, AL_NONE );
- memset (ch, 0, sizeof(*ch));
+ qalSourceStop(ch->srcnum);
+ qalSourcei(ch->srcnum, AL_BUFFER, AL_NONE);
+ memset(ch, 0, sizeof(*ch));
}
-void AL_PlayChannel( channel_t *ch ) {
+void AL_PlayChannel(channel_t *ch)
+{
sfxcache_t *sc = ch->sfx->cache;
#ifdef _DEBUG
if (s_show->integer > 1)
- Com_Printf("%s: %s\n", __func__, ch->sfx->name );
+ Com_Printf("%s: %s\n", __func__, ch->sfx->name);
#endif
ch->srcnum = s_srcnums[ch - channels];
qalGetError();
- qalSourcei( ch->srcnum, AL_BUFFER, sc->bufnum );
- //qalSourcei( ch->srcnum, AL_LOOPING, sc->loopstart == -1 ? AL_FALSE : AL_TRUE );
- qalSourcei( ch->srcnum, AL_LOOPING, ch->autosound ? AL_TRUE : AL_FALSE );
- qalSourcef( ch->srcnum, AL_GAIN, ch->master_vol );
- qalSourcef( ch->srcnum, AL_REFERENCE_DISTANCE, SOUND_FULLVOLUME );
- qalSourcef( ch->srcnum, AL_MAX_DISTANCE, 8192 );
- qalSourcef( ch->srcnum, AL_ROLLOFF_FACTOR, ch->dist_mult * ( 8192 - SOUND_FULLVOLUME ) );
+ qalSourcei(ch->srcnum, AL_BUFFER, sc->bufnum);
+ //qalSourcei(ch->srcnum, AL_LOOPING, sc->loopstart == -1 ? AL_FALSE : AL_TRUE);
+ qalSourcei(ch->srcnum, AL_LOOPING, ch->autosound ? AL_TRUE : AL_FALSE);
+ qalSourcef(ch->srcnum, AL_GAIN, ch->master_vol);
+ qalSourcef(ch->srcnum, AL_REFERENCE_DISTANCE, SOUND_FULLVOLUME);
+ qalSourcef(ch->srcnum, AL_MAX_DISTANCE, 8192);
+ qalSourcef(ch->srcnum, AL_ROLLOFF_FACTOR, ch->dist_mult * (8192 - SOUND_FULLVOLUME));
- AL_Spatialize( ch );
+ AL_Spatialize(ch);
// play it
- qalSourcePlay( ch->srcnum );
- if( qalGetError() != AL_NO_ERROR ) {
- AL_StopChannel( ch );
+ qalSourcePlay(ch->srcnum);
+ if (qalGetError() != AL_NO_ERROR) {
+ AL_StopChannel(ch);
}
}
-static void AL_IssuePlaysounds( void ) {
+static void AL_IssuePlaysounds(void)
+{
playsound_t *ps;
// start any playsounds
@@ -202,35 +211,37 @@ static void AL_IssuePlaysounds( void ) {
break; // no more pending sounds
if (ps->begin > paintedtime)
break;
- S_IssuePlaysound (ps);
+ S_IssuePlaysound(ps);
}
}
-void AL_StopAllChannels( void ) {
+void AL_StopAllChannels(void)
+{
int i;
channel_t *ch;
ch = channels;
- for( i = 0; i < s_numchannels; i++, ch++ ) {
+ for (i = 0; i < s_numchannels; i++, ch++) {
if (!ch->sfx)
continue;
- AL_StopChannel( ch );
+ AL_StopChannel(ch);
}
}
-static channel_t *AL_FindLoopingSound( int entnum, sfx_t *sfx ) {
+static channel_t *AL_FindLoopingSound(int entnum, sfx_t *sfx)
+{
int i;
channel_t *ch;
ch = channels;
- for( i = 0; i < s_numchannels; i++, ch++ ) {
- if( !ch->sfx )
+ for (i = 0; i < s_numchannels; i++, ch++) {
+ if (!ch->sfx)
continue;
- if( !ch->autosound )
+ if (!ch->autosound)
continue;
- if( ch->entnum != entnum )
+ if (ch->entnum != entnum)
continue;
- if( ch->sfx != sfx )
+ if (ch->sfx != sfx)
continue;
return ch;
}
@@ -238,7 +249,8 @@ static channel_t *AL_FindLoopingSound( int entnum, sfx_t *sfx ) {
return NULL;
}
-static void AL_AddLoopSounds( void ) {
+static void AL_AddLoopSounds(void)
+{
int i;
int sounds[MAX_PACKET_ENTITIES];
channel_t *ch;
@@ -247,28 +259,28 @@ static void AL_AddLoopSounds( void ) {
int num;
entity_state_t *ent;
- if( cls.state != ca_active || sv_paused->integer || !s_ambient->integer ) {
+ if (cls.state != ca_active || sv_paused->integer || !s_ambient->integer) {
return;
}
- S_BuildSoundList( sounds );
+ S_BuildSoundList(sounds);
- for( i = 0; i < cl.frame.numEntities; i++ ) {
+ for (i = 0; i < cl.frame.numEntities; i++) {
if (!sounds[i])
continue;
- sfx = S_SfxForHandle( cl.sound_precache[sounds[i]] );
+ sfx = S_SfxForHandle(cl.sound_precache[sounds[i]]);
if (!sfx)
continue; // bad sound effect
sc = sfx->cache;
if (!sc)
continue;
- num = ( cl.frame.firstEntity + i ) & PARSE_ENTITIES_MASK;
+ num = (cl.frame.firstEntity + i) & PARSE_ENTITIES_MASK;
ent = &cl.entityStates[num];
- ch = AL_FindLoopingSound( ent->number, sfx );
- if( ch ) {
+ ch = AL_FindLoopingSound(ent->number, sfx);
+ if (ch) {
ch->autoframe = s_framecount;
ch->end = paintedtime + sc->length;
continue;
@@ -287,56 +299,57 @@ static void AL_AddLoopSounds( void ) {
ch->dist_mult = SOUND_LOOPATTENUATE;
ch->end = paintedtime + sc->length;
- AL_PlayChannel( ch );
+ AL_PlayChannel(ch);
}
}
-void AL_Update( void ) {
+void AL_Update(void)
+{
int i;
channel_t *ch;
vec_t orientation[6];
- if( !s_active ) {
+ if (!s_active) {
return;
}
paintedtime = cl.time;
// set listener parameters
- qalListener3f( AL_POSITION, AL_UnpackVector( listener_origin ) );
- AL_CopyVector( listener_forward, orientation );
- AL_CopyVector( listener_up, orientation + 3 );
- qalListenerfv( AL_ORIENTATION, orientation );
- qalListenerf( AL_GAIN, s_volume->value );
- qalDistanceModel( AL_LINEAR_DISTANCE_CLAMPED );
-
- // update spatialization for dynamic sounds
+ qalListener3f(AL_POSITION, AL_UnpackVector(listener_origin));
+ AL_CopyVector(listener_forward, orientation);
+ AL_CopyVector(listener_up, orientation + 3);
+ qalListenerfv(AL_ORIENTATION, orientation);
+ qalListenerf(AL_GAIN, s_volume->value);
+ qalDistanceModel(AL_LINEAR_DISTANCE_CLAMPED);
+
+ // update spatialization for dynamic sounds
ch = channels;
- for( i = 0; i < s_numchannels; i++, ch++ ) {
- if( !ch->sfx )
+ for (i = 0; i < s_numchannels; i++, ch++) {
+ if (!ch->sfx)
continue;
- if( ch->autosound ) {
+ if (ch->autosound) {
// autosounds are regenerated fresh each frame
- if( ch->autoframe != s_framecount ) {
- AL_StopChannel( ch );
+ if (ch->autoframe != s_framecount) {
+ AL_StopChannel(ch);
continue;
}
} else {
ALenum state;
qalGetError();
- qalGetSourcei( ch->srcnum, AL_SOURCE_STATE, &state );
- if( qalGetError() != AL_NO_ERROR || state == AL_STOPPED ) {
- AL_StopChannel( ch );
+ qalGetSourcei(ch->srcnum, AL_SOURCE_STATE, &state);
+ if (qalGetError() != AL_NO_ERROR || state == AL_STOPPED) {
+ AL_StopChannel(ch);
continue;
}
}
#ifdef _DEBUG
if (s_show->integer) {
- Com_Printf ("%.1f %s\n", ch->master_vol, ch->sfx->name);
- // total++;
+ Com_Printf("%.1f %s\n", ch->master_vol, ch->sfx->name);
+ // total++;
}
#endif
@@ -346,7 +359,7 @@ void AL_Update( void ) {
s_framecount++;
// add loopsounds
- AL_AddLoopSounds ();
+ AL_AddLoopSounds();
AL_IssuePlaysounds();
}