make volume control actually control the volume, and allow per-sound gain settings

This commit is contained in:
darkrose 2015-04-08 01:39:20 +10:00
parent 29a3c99990
commit 1ca890756a
7 changed files with 69 additions and 81 deletions

View File

@ -2344,13 +2344,6 @@ void Client::playStepSound(int foot)
if (!m_sound)
return;
f32 volume = g_settings->getFloat("sound_volume");
if (volume < 1.0)
return;
if (volume > 100.0)
volume = 100.0;
volume /= 100.0;
v3f pf = m_env.getLocalPlayer()->getPosition();
v3s16 pp = floatToInt(pf + v3f(0, BS*0.1, 0), BS);
MapNode n = m_env.getMap().getNodeNoEx(pp);
@ -2397,7 +2390,7 @@ void Client::playStepSound(int foot)
}else{
snd += "-right";
}
m_sound->playSound(snd,false,volume);
m_sound->playSound(snd,false);
}
void Client::playDigSound(content_t c)
@ -2405,12 +2398,6 @@ void Client::playDigSound(content_t c)
if (!m_sound)
return;
f32 volume = g_settings->getFloat("sound_volume");
if (volume < 1.0)
return;
if (volume > 100.0)
volume = 100.0;
volume /= 100.0;
if (c == CONTENT_IGNORE) {
c = getPointedContent();
if ((c&CONTENT_MOB_MASK) != 0)
@ -2418,30 +2405,30 @@ void Client::playDigSound(content_t c)
}
ContentFeatures *f = &content_features(c);
if (f->sound_dig != "") {
m_sound->playSound(f->sound_dig,false,volume);
m_sound->playSound(f->sound_dig,false);
return;
}
switch (f->type) {
case CMT_PLANT:
m_sound->playSound("plant-dig",false,volume);
m_sound->playSound("plant-dig",false);
break;
case CMT_DIRT:
m_sound->playSound("dirt-dig",false,volume);
m_sound->playSound("dirt-dig",false);
break;
case CMT_STONE:
m_sound->playSound("stone-dig",false,volume);
m_sound->playSound("stone-dig",false);
break;
case CMT_LIQUID:
m_sound->playSound("liquid-dig",false,volume);
m_sound->playSound("liquid-dig",false);
break;
case CMT_WOOD:
m_sound->playSound("wood-dig",false,volume);
m_sound->playSound("wood-dig",false);
break;
case CMT_GLASS:
m_sound->playSound("glass-dig",false,volume);
m_sound->playSound("glass-dig",false);
break;
default:
m_sound->playSound("miss-dig",false,volume);
m_sound->playSound("miss-dig",false);
}
}
@ -2450,26 +2437,20 @@ void Client::playPlaceSound(content_t c)
if (!m_sound)
return;
f32 volume = g_settings->getFloat("sound_volume");
if (volume < 1.0)
return;
if (volume > 100.0)
volume = 100.0;
volume /= 100.0;
if (c == CONTENT_IGNORE)
c = getPointedContent();
ContentFeatures *f = &content_features(c);
if (f->sound_place != "") {
m_sound->playSound(f->sound_place,false,volume);
m_sound->playSound(f->sound_place,false);
return;
}
switch (f->type) {
case CMT_LIQUID:
m_sound->playSound("liquid-place",false,volume);
m_sound->playSound("liquid-place",false);
break;
default:
m_sound->playSound("place",false,volume);
m_sound->playSound("place",false);
}
}
@ -2477,24 +2458,12 @@ void Client::playSound(std::string &name, bool loop)
{
if (!m_sound)
return;
f32 volume = g_settings->getFloat("sound_volume");
if (volume < 1.0)
return;
if (volume > 100.0)
volume = 100.0;
volume /= 100.0;
m_sound->playSound(name,loop,volume);
m_sound->playSound(name,loop);
}
void Client::playSoundAt(std::string &name, v3f pos, bool loop)
{
if (!m_sound)
return;
f32 volume = g_settings->getFloat("sound_volume");
if (volume < 1.0)
return;
if (volume > 100.0)
volume = 100.0;
volume /= 100.0;
m_sound->playSoundAt(name,loop,volume,pos);
m_sound->playSoundAt(name,loop,pos);
}

View File

@ -43,6 +43,7 @@
#if USE_FREETYPE
#include "intlGUIEditBox.h"
#endif
#include "sound.h"
#include "gettext.h"
@ -50,12 +51,14 @@ GUIMainMenu::GUIMainMenu(gui::IGUIEnvironment* env,
gui::IGUIElement* parent, s32 id,
IMenuManager *menumgr,
MainMenuData *data,
IGameCallback *gamecallback
IGameCallback *gamecallback,
ISoundManager *sound
):
GUIModalMenu(env, parent, id, menumgr),
m_data(data),
m_accepted(false),
m_gamecallback(gamecallback)
m_gamecallback(gamecallback),
m_sound(sound)
{
assert(m_data);
this->env = env;
@ -1434,7 +1437,8 @@ bool GUIMainMenu::OnEvent(const SEvent& event)
gui::IGUIElement *vsb = getElementFromId(GUI_ID_VOLUME_SB);
if(vsb != NULL && vsb->getType() == gui::EGUIET_SCROLL_BAR) {
m_data->volume = (float)((gui::IGUIScrollBar*)vsb)->getPos();
g_settings->setFloat("sound_volume", m_data->volume);
if (m_sound)
m_sound->setListenerGain(m_data->volume/100.0);
}
return true;
}

View File

@ -174,6 +174,8 @@ struct MainMenuData
bool character_creator;
};
class ISoundManager;
class GUIMainMenu : public GUIModalMenu
{
public:
@ -181,7 +183,8 @@ public:
gui::IGUIElement* parent, s32 id,
IMenuManager *menumgr,
MainMenuData *data,
IGameCallback *gamecallback);
IGameCallback *gamecallback,
ISoundManager *sound);
~GUIMainMenu();
void removeChildren();
@ -210,6 +213,7 @@ private:
MainMenuData *m_data;
bool m_accepted;
IGameCallback *m_gamecallback;
ISoundManager *m_sound;
gui::IGUIEnvironment* env;
gui::IGUIElement* parent;

View File

@ -1251,7 +1251,7 @@ int main(int argc, char *argv[])
GUIMainMenu *menu =
new GUIMainMenu(guienv, guiroot, -1,
&g_menumgr, &menudata, g_gamecallback);
&g_menumgr, &menudata, g_gamecallback, sound);
menu->allowFocusRemoval(true);
if (error_message != L"") {
@ -1270,7 +1270,7 @@ int main(int argc, char *argv[])
infostream<<"Created main menu"<<std::endl;
#if USE_AUDIO == 1
sound->playMusic("bg-mainmenu",true,g_settings->getFloat("sound_volume"));
sound->playMusic("bg-mainmenu",true);
#endif
while (device->run() && kill == false) {
@ -1352,6 +1352,7 @@ int main(int argc, char *argv[])
g_settings->set("fullscreen", itos(menudata.fullscreen));
g_settings->set("enable_particles", itos(menudata.particles));
g_settings->set("old_hotbar", itos(menudata.hotbar));
g_settings->set("sound_volume",ftos(menudata.volume));
g_settings->set("game_mode", wide_to_narrow(menudata.game_mode));
g_settings->set("max_mob_level", wide_to_narrow(menudata.max_mob_level));
g_settings->set("initial_inventory", itos(menudata.initial_inventory));
@ -1386,7 +1387,7 @@ int main(int argc, char *argv[])
menu->allowFocusRemoval(true);
#if USE_AUDIO == 1
sound->playMusic("bg-charcreator",true,g_settings->getFloat("sound_volume"));
sound->playMusic("bg-charcreator",true);
#endif
while (device->run() && kill == false) {

View File

@ -53,8 +53,8 @@ void init_sounds(ISoundManager *sound)
sound->loadSound("wood-step-left","step_wood.3.ogg");
sound->loadSound("wood-step-right","step_wood.4.ogg");
// CMT_GLASS
sound->loadSound("glass-step-left","step_glass.1.ogg");
sound->loadSound("glass-step-right","step_glass.1.ogg");
sound->loadSound("glass-step-left","step_glass.1.ogg",0.3);
sound->loadSound("glass-step-right","step_glass.1.ogg",0.3);
// digging
// CMT_DIRT

View File

@ -39,20 +39,19 @@ public:
// Multiple sounds can be loaded per name; when played, the sound
// should be chosen randomly from alternatives
// Return value determines success/failure
virtual bool loadSound(const std::string &name,
const std::string &filepath) = 0;
virtual bool loadSound(const std::string &name, const std::string &filepath, float gain=1.0) = 0;
virtual void updateListener(v3f pos, v3f vel, v3f at, v3f up) = 0;
virtual void setListenerGain(float gain) = 0;
// playSound functions return -1 on failure, otherwise a handle to the
// sound. If name=="", call should be ignored without error.
virtual int playSound(const std::string &name, bool loop, float volume) = 0;
virtual int playSoundAt(const std::string &name, bool loop, float volume, v3f pos) = 0;
virtual int playSound(const std::string &name, bool loop) = 0;
virtual int playSoundAt(const std::string &name, bool loop, v3f pos) = 0;
virtual void stopSound(int sound) = 0;
virtual bool soundExists(int sound) = 0;
virtual bool playMusic(const std::string &name, bool loop, float volume) = 0;
virtual bool playMusic(const std::string &name, bool loop) = 0;
virtual void stopMusic() = 0;
virtual void updateSoundPosition(int sound, v3f pos) = 0;
@ -63,17 +62,17 @@ public:
class DummySoundManager: public ISoundManager
{
public:
virtual bool loadSound(const std::string &name, const std::string &filepath) {return true;}
virtual bool loadSound(const std::string &name, const std::string &filepath, float gain) {return true;}
void updateListener(v3f pos, v3f vel, v3f at, v3f up) {}
void setListenerGain(float gain) {}
int playSound(const std::string &name, bool loop, float volume) {return 0;}
int playSoundAt(const std::string &name, bool loop, float volume, v3f pos) {return 0;}
int playSound(const std::string &name, bool loop) {return 0;}
int playSoundAt(const std::string &name, bool loop, v3f pos) {return 0;}
void stopSound(int sound) {}
bool soundExists(int sound) {return false;}
bool playMusic(const std::string &name, bool loop, float volume) {return false;}
bool playMusic(const std::string &name, bool loop) {return false;}
void stopMusic() {}
void updateSoundPosition(int sound, v3f pos) {}

View File

@ -49,6 +49,8 @@
#include <vector>
#include "utility.h" // myrand()
#include "path.h"
#include "settings.h"
#include "main.h"
#define BUFFER_SIZE 30000
@ -105,6 +107,7 @@ struct SoundBuffer
ALsizei freq;
ALuint buffer_id;
std::vector<char> buffer;
float gain;
};
SoundBuffer* loadOggFile(const std::string &filepath)
@ -246,6 +249,16 @@ public:
alDistanceModel(AL_EXPONENT_DISTANCE);
{
f32 volume = g_settings->getFloat("sound_volume");
if (volume < 1.0)
return;
if (volume > 100.0)
volume = 100.0;
volume /= 100.0;
alListenerf(AL_GAIN, volume);
}
infostream<<"Audio: Initialized: OpenAL "<<alGetString(AL_VERSION)
<<", using "<<alcGetString(m_device, ALC_DEVICE_SPECIFIER)
<<std::endl;
@ -304,8 +317,7 @@ public:
return bufs[j];
}
PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop,
float volume)
PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop)
{
infostream<<"OpenALSoundManager: Creating playing sound"<<std::endl;
assert(buf);
@ -317,15 +329,14 @@ public:
alSource3f(sound->source_id, AL_POSITION, 0, 0, 0);
alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
volume = MYMAX(0.0, volume);
float volume = MYMAX(0.0, buf->gain);
alSourcef(sound->source_id, AL_GAIN, volume);
alSourcePlay(sound->source_id);
sound->should_delete = false;
return sound;
}
PlayingSound* createPlayingSoundAt(SoundBuffer *buf, bool loop,
float volume, v3f pos)
PlayingSound* createPlayingSoundAt(SoundBuffer *buf, bool loop, v3f pos)
{
infostream<<"OpenALSoundManager: Creating positional playing sound"
<<std::endl;
@ -342,17 +353,17 @@ public:
//alSourcef(sound->source_id, AL_ROLLOFF_FACTOR, 0.7);
alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
volume = MYMAX(0.0, volume);
float volume = MYMAX(0.0, buf->gain);
alSourcef(sound->source_id, AL_GAIN, volume);
alSourcePlay(sound->source_id);
sound->should_delete = false;
return sound;
}
int playSoundRaw(SoundBuffer *buf, bool loop, float volume)
int playSoundRaw(SoundBuffer *buf, bool loop)
{
assert(buf);
PlayingSound *sound = createPlayingSound(buf, loop, volume);
PlayingSound *sound = createPlayingSound(buf, loop);
if (!sound)
return -1;
int id = m_next_id++;
@ -360,10 +371,10 @@ public:
return id;
}
int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, v3f pos)
int playSoundRawAt(SoundBuffer *buf, bool loop, v3f pos)
{
assert(buf);
PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos);
PlayingSound *sound = createPlayingSoundAt(buf, loop, pos);
if (!sound)
return -1;
int id = m_next_id++;
@ -438,8 +449,7 @@ public:
}
}
bool loadSound(const std::string &name,
const std::string &filepath)
bool loadSound(const std::string &name, const std::string &filepath, float gain)
{
std::string path = getPath("sound",filepath,true);
if (path == "")
@ -447,6 +457,7 @@ public:
SoundBuffer *buf = loadOggFile(path);
if (buf == NULL)
return false;
buf->gain = gain;
addBuffer(name, buf);
return true;
}
@ -467,7 +478,7 @@ public:
alListenerf(AL_GAIN, gain);
}
int playSound(const std::string &name, bool loop, float volume)
int playSound(const std::string &name, bool loop)
{
if (name == "")
return 0;
@ -477,9 +488,9 @@ public:
<<std::endl;
return -1;
}
return playSoundRaw(buf, loop, volume);
return playSoundRaw(buf, loop);
}
int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
int playSoundAt(const std::string &name, bool loop, v3f pos)
{
if (name == "")
return 0;
@ -489,7 +500,7 @@ public:
<<std::endl;
return -1;
}
return playSoundRawAt(buf, loop, volume, pos);
return playSoundRawAt(buf, loop, pos);
}
void stopSound(int id)
{
@ -508,11 +519,11 @@ public:
return (m_sounds_playing.count(sound) != 0);
}
bool playMusic(const std::string &name, bool loop, float volume)
bool playMusic(const std::string &name, bool loop)
{
stopMusic();
m_music_id = playSound(name,loop,volume);
m_music_id = playSound(name,loop);
if (m_music_id > 0)
return true;
m_music_id = 0;