Fade sounds in and out

master
Brandon 2015-07-24 22:11:33 -05:00
parent 1ecefadfca
commit 26426105a1
4 changed files with 107 additions and 7 deletions

View File

@ -146,6 +146,7 @@ void Ambiance::doAmbiance(float dtime, u32 tod){
static bool newEnv = false;
m_timeOfDay = tod;
tickDelay += dtime;
doFades(dtime);
if ( startDelay > 3 && tickDelay > .1 ) { // delay ambiance 3 seconds from launch of game
lastPlay += dtime;
tickDelay = 0;
@ -160,7 +161,8 @@ void Ambiance::doAmbiance(float dtime, u32 tod){
newEnv = true;
actionstream << "[AMBIANCE] readEnvironment Results: " << a_env[currentEnv].name << std::endl;
if ( !a_env[currentEnv].background_sound.empty() ){
m_background_sound = m_sound->playSound(a_env[currentEnv].background_sound,true,0.6);
m_background_sound = m_sound->playSound(a_env[currentEnv].background_sound,true,0.0);
fadeSound(m_background_sound,1.25,0.6);
} else {
m_background_sound = 0;
}
@ -200,7 +202,8 @@ void Ambiance::doAmbiance(float dtime, u32 tod){
}
// type is 0 - normal sound, 1 start sound, 2 stop sound
bool Ambiance::playSound(std::string name, float gain){
bool Ambiance::playSound(std::string name, float gain, bool fade){
float oGain = 0;
verbosestream << "[AMBIANCE] playSound(" << name << ")" << std::endl;
std::map<std::string, int>::iterator i = m_sounds_playing.find(name);
if ( i != m_sounds_playing.end() ){
@ -214,12 +217,65 @@ bool Ambiance::playSound(std::string name, float gain){
m_sounds_playing.erase(name);
}
}
if ( fade == true ){
oGain = gain;
gain = 0;
}
int id = m_sound->playSound(name, false, gain);
m_sounds_playing[name] = id;
if ( fade == true ){
fadeSound(id, 1.25, oGain);
}
lastPlay = 0;
return true;
}
void Ambiance::fadeSound(int soundid, float step, float gain){
float cGain = m_sound->getSoundGain(soundid);
fade_status f = fade_status(step,cGain,gain);
m_sounds_fading[soundid] = f;
}
void Ambiance::doFades(float dtime){
static float fadeDelay = 0;
fadeDelay += dtime;
if ( fadeDelay > 0.2 ){
float chkGain = 0;
for ( m_fading_it i = m_sounds_fading.begin(); i != m_sounds_fading.end(); i++ ){
if ( i->second.step < 0 )
chkGain = i->second.current_gain * -1;
else
chkGain = i->second.current_gain;
if ( chkGain < i->second.target_gain ){
i->second.current_gain += (i->second.step * fadeDelay);
if ( i->second.current_gain < 0 )
i->second.current_gain = 0;
if ( i->second.current_gain > 1 )
i->second.current_gain = 1;
m_sound->updateSoundGain(i->first,i->second.current_gain);
} else {
if ( i->second.target_gain <= 0 ){
m_sound->stopSound(i->first);
}
for ( m_sounds_it ii = m_sounds_playing.begin(); ii != m_sounds_playing.end(); ii++ ){
if ( ii->second == i->first )
m_sounds_playing.erase(ii->first);
}
m_sounds_fading.erase(i->first);
}
}
fadeDelay = 0;
}
}
int Ambiance::readEnvironment(){
//actionstream << "[AMBIANCE] readEnvironment() ";
ascending = false;
@ -247,7 +303,6 @@ int Ambiance::readEnvironment(){
if ( t > 19500 || t < 5000 )
TOD = NIGHT;
//verbosestream << t << std::endl;
if ( pos.Y > ( lastpos.Y + .5 ) )
ascending = true;
@ -372,11 +427,14 @@ int Ambiance::getNodeId(v3s16 pos){
void Ambiance::stopEnvironment(int env){
// not going to bother seeing if the sounds playing are part of a certain environment just yet, just stop all sounds
for ( m_sounds_it i = m_sounds_playing.begin(); i != m_sounds_playing.end(); i++ ){
m_sound->stopSound(i->second);
//m_sound->stopSound(i->second);
fadeSound(i->second,-1.25,0);
m_sounds_playing.erase(i->first); // go ahead and erase it here so we don't get double fade outs
}
if ( m_background_sound != 0 ){
m_sound->stopSound(m_background_sound);
fadeSound(m_background_sound,-0.5,0);
//m_sound->stopSound(m_background_sound);
m_background_sound = 0;
}

View File

@ -20,12 +20,14 @@ public:
Ambiance(ISoundManager *sound, ClientEnvironment &e);
void doAmbiance(float dtime, u32 tod);
private:
bool playSound(std::string name, float gain);
bool playSound(std::string name, float gain, bool fade = false);
void doFades(float dtime);
bool nodesInRange(v3s16 pos, short searchRange, short searchAbove, short searchBelow, std::string nodeName, short accuracy, int count);
std::string getNodeName(v3s16 pos);
int getNodeId(v3s16 pos);
void stopEnvironment(int env);
int readEnvironment();
void fadeSound(int soundid, float step, float gain);
u32 m_timeOfDay;
@ -37,6 +39,20 @@ private:
std::map<std::string, int> m_sounds_playing;
typedef std::map<std::string, int>::iterator m_sounds_it;
struct fade_status {
fade_status() {}
fade_status(float step, float current_gain, float target_gain):
step(step),
current_gain(current_gain),
target_gain(target_gain){}
float step;
float current_gain;
float target_gain;
};
std::map<int, fade_status> m_sounds_fading; // 0 = step, 1 = current gain, 2 = goal gain
typedef std::map<int, fade_status>::iterator m_fading_it;
v3f lastpos;
bool ascending;
bool descending;

View File

@ -48,7 +48,7 @@ class ISoundManager
{
public:
virtual ~ISoundManager(){}
// Multiple sounds can be loaded per name; when played, the sound
// should be chosen randomly from alternatives
// Return value determines success/failure
@ -69,6 +69,8 @@ public:
virtual void stopSound(int sound) = 0;
virtual bool soundExists(int sound) = 0;
virtual void updateSoundPosition(int sound, v3f pos) = 0;
virtual bool updateSoundGain(int id, float gain) = 0;
virtual float getSoundGain(int id) = 0;
int playSound(const SimpleSoundSpec &spec, bool loop)
{ return playSound(spec.name, loop, spec.gain); }
@ -92,6 +94,8 @@ public:
void stopSound(int sound) {}
bool soundExists(int sound) {return false;}
void updateSoundPosition(int sound, v3f pos) {}
bool updateSoundGain(int id, float gain) { return false; }
float getSoundGain(int id) { return 0; }
};
// Global DummySoundManager singleton

View File

@ -537,6 +537,28 @@ public:
alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
}
bool updateSoundGain(int id, float gain){
std::map<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
if(i == m_sounds_playing.end())
return false;
PlayingSound *sound = i->second;
alSourcef(sound->source_id, AL_GAIN, gain);
return true;
}
float getSoundGain(int id){
std::map<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
if(i == m_sounds_playing.end())
return 0;
actionstream << "getSoundGain(" << id << ") = ";
PlayingSound *sound = i->second;
ALfloat gain;
alGetSourcef(sound->source_id,AL_GAIN, &gain);
actionstream << gain << std::endl;
return gain;
}
};
ISoundManager *createOpenALSoundManager(OnDemandSoundFetcher *fetcher)