Permalink
Browse files

Use logarithmic music and sound volume when using OpenAL.

  • Loading branch information...
1 parent 178f0f1 commit f8b1332e14730f188389245b2f4acb15d4852be4 @Deiz Deiz committed Apr 20, 2011
Showing with 128 additions and 15 deletions.
  1. +17 −1 src/music.c
  2. +1 −0 src/music.h
  3. +18 −4 src/music_openal.c
  4. +1 −0 src/music_openal.h
  5. +54 −6 src/options.c
  6. +18 −1 src/sound.c
  7. +1 −0 src/sound.h
  8. +17 −3 src/sound_openal.c
  9. +1 −0 src/sound_openal.h
View
@@ -86,6 +86,7 @@ void (*music_sys_free) (void) = NULL;
/* Music control. */
int (*music_sys_volume)( const double vol ) = NULL;
double (*music_sys_getVolume) (void) = NULL;
+double (*music_sys_getVolumeLog) (void) = NULL;
void (*music_sys_play) (void) = NULL;
void (*music_sys_stop) (void) = NULL;
void (*music_sys_pause) (void) = NULL;
@@ -188,6 +189,7 @@ int music_init (void)
/* Music control. */
music_sys_volume = music_mix_volume;
music_sys_getVolume = music_mix_getVolume;
+ music_sys_getVolumeLog = music_mix_getVolume;
music_sys_load = music_mix_load;
music_sys_play = music_mix_play;
music_sys_stop = music_mix_stop;
@@ -215,6 +217,7 @@ int music_init (void)
/* Music control. */
music_sys_volume = music_al_volume;
music_sys_getVolume = music_al_getVolume;
+ music_sys_getVolumeLog = music_al_getVolumeLog;
music_sys_load = music_al_load;
music_sys_play = music_al_play;
music_sys_stop = music_al_stop;
@@ -369,7 +372,7 @@ int music_volume( const double vol )
/**
- * @brief Gets the current music volume.
+ * @brief Gets the current music volume (linear).
*
* @return The current music volume.
*/
@@ -383,6 +386,19 @@ double music_getVolume (void)
/**
+ * @brief Gets the current music volume (logarithmic).
+ *
+ * @return The current music volume.
+ */
+double music_getVolumeLog(void)
+{
+ if (music_disabled)
+ return 0.;
+ return music_sys_getVolumeLog();
+}
+
+
+/**
* @brief Loads the music by name.
*
* @param name Name of the file to load.
View
@@ -32,6 +32,7 @@ void music_exit (void);
*/
int music_volume( const double vol );
double music_getVolume (void);
+double music_getVolumeLog(void);
int music_load( const char* name );
void music_play (void);
void music_stop (void);
View
@@ -116,7 +116,8 @@ ALuint music_source = 0; /**< Source assosciated to music. */
/*
* volume
*/
-static ALfloat music_vol = 1.; /**< Current volume level. */
+static ALfloat music_vol = 1.; /**< Current volume level (logarithmic). */
+static ALfloat music_vol_lin = 1.; /**< Current volume level (linear). */
/*
@@ -802,12 +803,16 @@ int music_al_volume( double vol )
{
soundLock();
- music_vol = vol;
+ music_vol_lin = vol;
+ if (vol > 0.) /* Floor of -48 dB (0.00390625 amplitude) */
+ music_vol = 1 / pow(2, (1 - vol) * 8 );
+ else
+ music_vol = 0.;
/* only needed if playing */
if (music_al_isPlaying()) {
- alSourcef( music_source, AL_GAIN, vol );
+ alSourcef( music_source, AL_GAIN, music_vol );
/* Check for errors. */
al_checkErr();
@@ -820,10 +825,19 @@ int music_al_volume( double vol )
/**
- * @brief Gets the volume.
+ * @brief Gets the volume (linear).
*/
double music_al_getVolume (void)
{
+ return music_vol_lin;
+}
+
+
+/**
+ * @brief Gets the volume (logarithmic).
+ */
+double music_al_getVolumeLog(void)
+{
return music_vol;
}
View
@@ -41,6 +41,7 @@ void music_al_free (void);
*/
int music_al_volume( const double vol );
double music_al_getVolume (void);
+double music_al_getVolumeLog(void);
void music_al_play (void);
void music_al_stop (void);
void music_al_pause (void);
View
@@ -85,7 +85,9 @@ static void opt_audioSave( unsigned int wid, char *str );
static void opt_audioDefaults( unsigned int wid, char *str );
static void opt_audioUpdate( unsigned int wid, char *str );
static void opt_setSFXLevel( unsigned int wid, char *str );
+static void opt_soundLevelStr( char *buf, int max, double pos );
static void opt_setMusicLevel( unsigned int wid, char *str );
+static void opt_musicLevelStr( char *buf, int max, double pos );
/* Keybind menu. */
static void opt_keybinds( unsigned int wid );
static void menuKeybinds_getDim( unsigned int wid, int *w, int *h,
@@ -611,41 +613,87 @@ static void opt_keyDefaults( unsigned int wid, char *str )
*/
static void opt_setSFXLevel( unsigned int wid, char *str )
{
- char buf[32];
+ char buf[64];
double vol;
/* Set fader. */
vol = window_getFaderValue(wid, str);
sound_volume(vol);
/* Update message. */
- snprintf( buf, sizeof(buf), "Sound Volume: %.2f", sound_getVolume() );
+ opt_soundLevelStr( buf, sizeof(buf), vol );
window_modifyText( wid, "txtSound", buf );
}
/**
+ * @brief Sets the sound volume string based on level and sound backend.
+ *
+ * @param[out] buf Buffer to use.
+ * @param max Maximum length of the buffer.
+ * @param pos Position of the fader calling the function.
+ */
+static void opt_soundLevelStr( char *buf, int max, double pos )
+{
+ double vol, magic;
+
+ vol = sound_getVolumeLog();
+ if (vol == 0.)
+ snprintf( buf, max, "Sound Volume: Muted" );
+ else if (strcmp(conf.sound_backend,"openal")==0) {
+ magic = -48. / log(0.00390625); /* -48 dB minimum divided by logarithm of volume floor. */
+ snprintf( buf, max, "Sound Volume: %.2f (%.0f dB)", pos, log(vol) * magic );
+ }
+ else if (strcmp(conf.sound_backend,"sdlmix")==0)
+ snprintf( buf, max, "Sound Volume: %.2f", pos );
+}
+
+
+/**
* @brief Callback to set the music level.
*
* @param wid Window calling the callback.
* @param str Name of the widget calling the callback.
*/
static void opt_setMusicLevel( unsigned int wid, char *str )
{
- char buf[32];
+ char buf[64];
double vol;
/* Update fader. */
vol = window_getFaderValue(wid, str);
music_volume(vol);
/* Update message. */
- snprintf( buf, sizeof(buf), "Music Volume: %.2f", music_getVolume() );
+ opt_musicLevelStr( buf, sizeof(buf), vol );
window_modifyText( wid, "txtMusic", buf );
}
/**
+ * @brief Sets the music volume string based on level and sound backend.
+ *
+ * @param[out] buf Buffer to use.
+ * @param max Maximum length of the buffer.
+ * @param pos Position of the fader calling the function.
+ */
+static void opt_musicLevelStr( char *buf, int max, double pos )
+{
+ double vol, magic;
+
+ vol = music_getVolumeLog();
+ if (vol == 0.)
+ snprintf( buf, max, "Music Volume: Muted" );
+ else if (strcmp(conf.sound_backend,"openal")==0) {
+ magic = -48. / log(0.00390625); /* -48 dB minimum divided by logarithm of volume floor. */
+ snprintf( buf, max, "Music Volume: %.2f (%.0f dB)", pos, log(vol) * magic );
+ }
+ else if (strcmp(conf.sound_backend,"sdlmix")==0)
+ snprintf( buf, max, "Music Volume: %.2f", pos );
+}
+
+
+/**
* @brief Opens the audio settings menu.
*/
static void opt_audio( unsigned int wid )
@@ -721,7 +769,7 @@ static void opt_audio( unsigned int wid )
y -= 30;
/* Sound fader. */
- snprintf( buf, sizeof(buf), "Sound Volume: %.2f", sound_getVolume() );
+ opt_soundLevelStr( buf, sizeof(buf), sound_getVolume() );
window_addText( wid, x, y, cw, 20, 1, "txtSound",
NULL, NULL, buf );
y -= 20;
@@ -730,7 +778,7 @@ static void opt_audio( unsigned int wid )
y -= 40;
/* Music fader. */
- snprintf( buf, sizeof(buf), "Music Volume: %.2f", music_getVolume() );
+ opt_musicLevelStr( buf, sizeof(buf), music_getVolume() );
window_addText( wid, x, y, cw, 20, 1, "txtMusic",
NULL, NULL, buf );
y -= 20;
View
@@ -74,6 +74,7 @@ void (*sound_sys_free) ( alSound *snd ) = NULL;
/* Sound settings. */
int (*sound_sys_volume) ( const double vol ) = NULL;
double (*sound_sys_getVolume) (void) = NULL;
+double (*sound_sys_getVolumeLog) (void) = NULL;
/* Sound playing. */
int (*sound_sys_play) ( alVoice *v, alSound *s ) = NULL;
int (*sound_sys_playPos) ( alVoice *v, alSound *s,
@@ -145,6 +146,7 @@ int sound_init (void)
/* Sound settings. */
sound_sys_volume = sound_al_volume;
sound_sys_getVolume = sound_al_getVolume;
+ sound_sys_getVolumeLog = sound_al_getVolumeLog;
/* Sound playing. */
sound_sys_play = sound_al_play;
sound_sys_playPos = sound_al_playPos;
@@ -185,6 +187,7 @@ int sound_init (void)
/* Sound settings. */
sound_sys_volume = sound_mix_volume;
sound_sys_getVolume = sound_mix_getVolume;
+ sound_sys_getVolumeLog = sound_mix_getVolume;
/* Sound playing. */
sound_sys_play = sound_mix_play;
sound_sys_playPos = sound_mix_playPos;
@@ -702,7 +705,7 @@ int sound_volume( const double vol )
/**
- * @brief Gets the current sound volume.
+ * @brief Gets the current sound volume (linear).
*
* @return The current sound volume level.
*/
@@ -716,6 +719,20 @@ double sound_getVolume (void)
/**
+ * @brief Gets the current sound volume (logarithmic).
+ *
+ * @return The current sound volume level.
+ */
+double sound_getVolumeLog(void)
+{
+ if (sound_disabled)
+ return 0.;
+
+ return sound_sys_getVolumeLog();
+}
+
+
+/**
* @brief Loads a sound into the sound_list.
*
* @param snd Sound to load into.
View
@@ -40,6 +40,7 @@ int sound_get( char* name );
double sound_length( int sound );
int sound_volume( const double vol );
double sound_getVolume (void);
+double sound_getVolumeLog (void);
int sound_play( int sound );
int sound_playPos( int sound, double px, double py, double vx, double vy );
void sound_stop( int voice );
View
@@ -68,7 +68,8 @@ SDL_mutex *sound_lock = NULL; /**< Global sound lock, always lock this before
*/
static ALCcontext *al_context = NULL; /**< OpenAL context. */
static ALCdevice *al_device = NULL; /**< OpenAL device. */
-static ALfloat svolume = 1.; /**< Sound global volume. */
+static ALfloat svolume = 1.; /**< Sound global volume (logarithmic). */
+static ALfloat svolume_lin = 1.; /**< Sound global volume (linear). */
alInfo_t al_info; /**< OpenAL context info. */
@@ -907,8 +908,12 @@ void sound_al_free( alSound *snd )
int sound_al_volume( double vol )
{
int i;
- svolume = (ALfloat) vol;
+ svolume_lin = vol;
+ if (vol > 0.) /* Floor of -48 dB (0.00390625 amplitude) */
+ svolume = (ALfloat) 1 / pow(2, (1 - vol) * 8);
+ else
+ svolume = 0.;
soundLock();
for (i=0; i<source_nall; i++)
alSourcef( source_all[i], AL_GAIN, svolume );
@@ -919,10 +924,19 @@ int sound_al_volume( double vol )
/**
- * @brief Gets the current volume level.
+ * @brief Gets the current volume level (linear).
*/
double sound_al_getVolume (void)
{
+ return svolume_lin;
+}
+
+
+/**
+ * @brief Gets the current volume level (logarithmic).
+ */
+double sound_al_getVolumeLog(void)
+{
return svolume;
}
View
@@ -76,6 +76,7 @@ void sound_al_free( alSound *snd );
*/
int sound_al_volume( const double vol );
double sound_al_getVolume (void);
+double sound_al_getVolumeLog(void);
/*

0 comments on commit f8b1332

Please sign in to comment.