Permalink
Browse files

* Compression noise thingy seems to work.

  • Loading branch information...
1 parent 2fc1cfa commit 8fa268f5148f0b227be423e6df5519094c911be5 @bobbens committed Apr 25, 2011
Showing with 132 additions and 34 deletions.
  1. +23 −3 src/sound.c
  2. +52 −17 src/sound_openal.c
  3. +1 −0 src/sound_openal.h
  4. +55 −14 src/sound_sdlmix.c
  5. +1 −0 src/sound_sdlmix.h
View
@@ -64,8 +64,9 @@ static SDL_mutex *voice_mutex = NULL; /**< Lock for voices. */
/*
* Internally used sounds.
*/
-static int snd_compression = -1;
-static int snd_compressionG = -1;
+static int snd_compression = -1; /**< Compression sound. */
+static int snd_compressionG = -1; /**< Compression sound group. */
+static double snd_compression_gain = 0.; /**< Current compression gain. */
@@ -94,7 +95,8 @@ void (*sound_sys_update) (void) = NULL;
void (*sound_sys_stop) ( alVoice *v ) = NULL;
void (*sound_sys_pause) (void) = NULL;
void (*sound_sys_resume) (void) = NULL;
-void (*sound_sys_setSpeed) (double s ) = NULL;
+void (*sound_sys_setSpeed) ( double s ) = NULL;
+void (*sound_sys_setSpeedVolume) ( double vol ) = NULL;
/* Listener. */
int (*sound_sys_updateListener) ( double dir, double px, double py,
double vx, double vy ) = NULL;
@@ -167,6 +169,7 @@ int sound_init (void)
sound_sys_pause = sound_al_pause;
sound_sys_resume = sound_al_resume;
sound_sys_setSpeed = sound_al_setSpeed;
+ sound_sys_setSpeedVolume = sound_al_setSpeedVolume;
/* Listener. */
sound_sys_updateListener = sound_al_updateListener;
/* Groups. */
@@ -210,6 +213,7 @@ int sound_init (void)
sound_sys_pause = sound_mix_pause;
sound_sys_resume = sound_mix_resume;
sound_sys_setSpeed = sound_mix_setSpeed;
+ sound_sys_setSpeedVolume = sound_mix_setSpeedVolume;
/* Listener. */
sound_sys_updateListener = sound_mix_updateListener;
/* Groups. */
@@ -632,9 +636,25 @@ int sound_updateListener( double dir, double px, double py,
*/
void sound_setSpeed( double s )
{
+ double v;
+ int playing;
+
if (sound_disabled)
return;
+ /* We implement the brown noise here. */
+ playing = (snd_compression_gain > 0.);
+ v = CLAMP( 0., 1., (s-2.)/10. );
+ if (v > 0.) {
+ if (!playing)
+ sound_playGroup( snd_compressionG, snd_compression, 0 );
+ sound_volumeGroup( snd_compressionG, v );
+ sound_sys_setSpeedVolume( 1.-v );
+ }
+ else if (playing)
+ sound_stopGroup( snd_compressionG );
+ snd_compression_gain = v;
+
return sound_sys_setSpeed( s );
}
View
@@ -69,6 +69,7 @@ static ALCcontext *al_context = NULL; /**< OpenAL context. */
static ALCdevice *al_device = NULL; /**< OpenAL device. */
static ALfloat svolume = 1.; /**< Sound global volume (logarithmic). */
static ALfloat svolume_lin = 1.; /**< Sound global volume (linear). */
+static ALfloat svolume_speed = 1.; /**< Sound global volume modulator for speed. */
alInfo_t al_info; /**< OpenAL context info. */
@@ -149,6 +150,10 @@ static void al_resumev( ALint n, ALuint *s );
* Groups.
*/
static alGroup_t *sound_al_getGroup( int group );
+/*
+ * Misc.
+ */
+static void sound_al_volumeUpdate (void);
/*
@@ -936,31 +941,45 @@ void sound_al_free( alSound *snd )
/**
- * @brief Sets all the sounds volume to vol
+ * @brief Internal volume update function.
*/
-int sound_al_volume( double vol )
+static void sound_al_volumeUpdate (void)
{
int i, j;
alGroup_t *g;
-
- /* Calculate volume level. */
- svolume_lin = vol;
- if (vol > 0.) /* Floor of -48 dB (0.00390625 amplitude) */
- svolume = (ALfloat) 1 / pow(2, (1 - vol) * 8);
- else
- svolume = 0.;
+ double v;
soundLock();
/* Do generic ones. */
for (i=0; i<source_ntotal; i++)
- alSourcef( source_total[i], AL_GAIN, svolume );
+ alSourcef( source_total[i], AL_GAIN, svolume*svolume_speed );
/* Do specific groups. */
for (i=0; i<al_ngroups; i++) {
g = &al_groups[i];
+ v = svolume * g->volume;
+ if (g->speed)
+ v *= svolume_speed;
for (j=0; j<g->nsources; j++)
- alSourcef( g->sources[j], AL_PITCH, svolume*g->volume );
+ alSourcef( g->sources[j], AL_GAIN, v );
}
soundUnlock();
+}
+
+
+/**
+ * @brief Sets all the sounds volume to vol
+ */
+int sound_al_volume( double vol )
+{
+ /* Calculate volume level. */
+ svolume_lin = vol;
+ if (vol > 0.) /* Floor of -48 dB (0.00390625 amplitude) */
+ svolume = (ALfloat) 1 / pow(2, (1 - vol) * 8);
+ else
+ svolume = 0.;
+
+ /* Update volume. */
+ sound_al_volumeUpdate();
return 0;
}
@@ -1036,7 +1055,7 @@ static int al_playVoice( alVoice *v, alSound *s,
v->u.al.vel[2] = 0.;
/* Set up properties. */
- alSourcef( v->u.al.source, AL_GAIN, svolume );
+ alSourcef( v->u.al.source, AL_GAIN, svolume*svolume_speed );
alSourcefv( v->u.al.source, AL_POSITION, v->u.al.pos );
alSourcefv( v->u.al.source, AL_VELOCITY, v->u.al.vel );
@@ -1141,7 +1160,7 @@ void sound_al_updateVoice( alVoice *v )
}
/* Set up properties. */
- alSourcef( v->u.al.source, AL_GAIN, svolume );
+ alSourcef( v->u.al.source, AL_GAIN, svolume*svolume_speed );
alSourcefv( v->u.al.source, AL_POSITION, v->u.al.pos );
alSourcefv( v->u.al.source, AL_VELOCITY, v->u.al.vel );
@@ -1223,6 +1242,16 @@ void sound_al_setSpeed( double s )
/**
+ * @brief Set the speed volume.
+ */
+void sound_al_setSpeedVolume( double vol )
+{
+ svolume_speed = vol;
+ sound_al_volumeUpdate();
+}
+
+
+/**
* @brief Updates the listener.
*/
int sound_al_updateListener( double dir, double px, double py,
@@ -1428,7 +1457,7 @@ int sound_al_playGroup( int group, alSound *s, int once )
if (j == g->nsources-1) {
if (state != AL_STOPPED) {
alSourceStop( g->sources[j] );
- alSourcef( g->sources[j], AL_GAIN, svolume * g->volume );
+ alSourcef( g->sources[j], AL_GAIN, svolume_speed * svolume * g->volume );
}
}
/* Ignore playing/paused. */
@@ -1582,7 +1611,7 @@ void sound_al_update (void)
{
int i, j;
alGroup_t *g;
- ALfloat d;
+ ALfloat d, v;
unsigned int t, f;
t = SDL_GetTicks();
@@ -1597,20 +1626,26 @@ void sound_al_update (void)
f = t - g->fade_timer;
if (f < SOUND_FADEOUT) {
d = 1. - (ALfloat) f / (ALfloat) SOUND_FADEOUT;
+ v = d * svolume * g->volume;
+ if (g->speed)
+ v *= svolume_speed;
soundLock();
for (j=0; j<g->nsources; j++)
- alSourcef( g->sources[j], AL_GAIN, d*svolume );
+ alSourcef( g->sources[j], AL_GAIN, v );
/* Check for errors. */
al_checkErr();
soundUnlock();
}
/* Fadeout done. */
else {
soundLock();
+ v = d * svolume * g->volume;
+ if (g->speed)
+ v *= svolume_speed;
for (j=0; j<g->nsources; j++) {
alSourceStop( g->sources[j] );
alSourcei( g->sources[j], AL_BUFFER, AL_NONE );
- alSourcef( g->sources[j], AL_GAIN, svolume );
+ alSourcef( g->sources[j], AL_GAIN, v );
}
/* Check for errors. */
al_checkErr();
View
@@ -97,6 +97,7 @@ void sound_al_stop( alVoice *v );
void sound_al_pause (void);
void sound_al_resume (void);
void sound_al_setSpeed( double s );
+void sound_al_setSpeedVolume( double vol );
/*
View
@@ -33,7 +33,8 @@
/*
* Global sound properties.
*/
-static double sound_curVolume = 0.; /**< Current sound volume. */
+static double sound_curVolume = 1.; /**< Current sound volume. */
+static double sound_speedVolume = 1.; /**< Speed volume. */
static unsigned char sound_mixVolume = 0; /**< Actual in-game used volume. */
static double sound_pos[3]; /**< Position of listener. */
static double sound_speed = 1.; /**< Speed of the sound. */
@@ -63,6 +64,7 @@ static void print_MixerVersion (void);
/* Voices. */
static int sound_mix_updatePosVoice( alVoice *v, double x, double y );
static void voice_mix_markStopped( int channel );
+static void sound_mix_volumeUpdate (void);
/**
@@ -335,27 +337,43 @@ int sound_mix_updateListener( double dir, double px, double py,
/**
- * @brief Sets the volume.
- *
- * @param vol Volume to set to.
- * @return 0 on success.
+ * @brief Updates the volume.
*/
-int sound_mix_volume( const double vol )
+static void sound_mix_volumeUpdate (void)
{
- mixGroup_t *g;
int i, j;
+ mixGroup_t *g;
+ double v;
+ unsigned char cv;
- /* Calculate volume. */
- sound_curVolume = MIX_MAX_VOLUME * CLAMP(0., 1., vol);
- sound_mixVolume = (unsigned char) sound_curVolume;
/* Set volume for all sources. */
Mix_Volume( -1, sound_mixVolume );
/* Set volume for groups. */
for (j=0; j<ngroups; j++) {
g = &groups[j];
+ v = sound_curVolume*g->volume;
+ if (g->speed)
+ v *= sound_speedVolume;
+ cv = (unsigned char) (MIX_MAX_VOLUME*v);
for (i=g->start; g->end; i++)
- Mix_Volume( i, (unsigned char) MIX_MAX_VOLUME * CLAMP(0., 1., vol*g->volume) );
+ Mix_Volume( i, cv );
}
+}
+
+
+/**
+ * @brief Sets the volume.
+ *
+ * @param vol Volume to set to.
+ * @return 0 on success.
+ */
+int sound_mix_volume( const double vol )
+{
+ /* Calculate volume. */
+ sound_curVolume = CLAMP(0., 1., vol);
+ sound_mixVolume = (unsigned char) (MIX_MAX_VOLUME * CLAMP(0., 1., sound_speedVolume*sound_curVolume));
+ /* Update volume. */
+ sound_mix_volumeUpdate();
return 0;
}
@@ -367,7 +385,7 @@ int sound_mix_volume( const double vol )
*/
double sound_mix_getVolume (void)
{
- return sound_curVolume / MIX_MAX_VOLUME;
+ return sound_curVolume;
}
@@ -500,6 +518,9 @@ static mixGroup_t* sound_mix_getGroup( int group )
int sound_mix_playGroup( int group, alSound *s, int once )
{
int ret, channel;
+ double v;
+ unsigned char cv;
+ mixGroup_t *g;
/* Get the channel. */
channel = Mix_GroupAvailable(group);
@@ -518,8 +539,18 @@ int sound_mix_playGroup( int group, alSound *s, int once )
s->name, group, Mix_GetError());
return -1;
}
- else
- Mix_Volume( channel, sound_mixVolume );
+ else {
+ g = sound_mix_getGroup( group );
+ if (g == NULL) {
+ WARN("Group '%d' does not exist!", group);
+ return 0;
+ }
+ v = sound_curVolume*g->volume;
+ if (g->speed)
+ v *= sound_speedVolume;
+ cv = (unsigned char) (MIX_MAX_VOLUME*v);
+ Mix_Volume( channel, cv );
+ }
return 0;
}
@@ -612,6 +643,16 @@ void sound_mix_setSpeed( double s )
/**
+ * @brief Sets the speed volume.
+ */
+void sound_mix_setSpeedVolume( double vol )
+{
+ sound_speedVolume = CLAMP( 0., 1., vol );
+ sound_mix_volumeUpdate();
+}
+
+
+/**
* @brief Does nothing atm.
*
* @param v Unused.
View
@@ -52,6 +52,7 @@ void sound_mix_stop( alVoice *v );
void sound_mix_pause (void);
void sound_mix_resume (void);
void sound_mix_setSpeed( double s );
+void sound_mix_setSpeedVolume( double vol );
/*

0 comments on commit 8fa268f

Please sign in to comment.