#include <AL/alc.h>
#include <AL/alext.h>
#endif
+#include <cmath>
#include <vorbis/vorbisfile.h>
#include <cassert>
#include "log.h"
std::unordered_map<int, PlayingSound*> m_sounds_playing;
v3f m_listener_pos;
struct FadeState {
- FadeState() {}
+ FadeState() = default;
+
FadeState(float step, float current_gain, float target_gain):
step(step),
current_gain(current_gain),
return;
}
- alDistanceModel(AL_INVERSE_DISTANCE);
+ alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
infostream<<"Audio: Initialized: OpenAL "<<alGetString(AL_VERSION)
<<", using "<<alcGetString(m_device, ALC_DEVICE_SPECIFIER)
alcCloseDevice(m_device);
m_device = NULL;
- for (std::unordered_map<std::string, std::vector<SoundBuffer*>>::iterator i =
- m_buffers.begin(); i != m_buffers.end(); ++i) {
- for (std::vector<SoundBuffer*>::iterator iter = (*i).second.begin();
- iter != (*i).second.end(); ++iter) {
- delete *iter;
+ for (auto &buffer : m_buffers) {
+ for (SoundBuffer *sb : buffer.second) {
+ delete sb;
}
- (*i).second.clear();
+ buffer.second.clear();
}
m_buffers.clear();
infostream<<"Audio: Deinitialized."<<std::endl;
std::vector<SoundBuffer*> bufs;
bufs.push_back(buf);
m_buffers[name] = bufs;
- return;
}
SoundBuffer* getBuffer(const std::string &name)
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);
+ volume = std::fmax(0.0f, volume);
alSourcef(sound->source_id, AL_GAIN, volume);
alSourcef(sound->source_id, AL_PITCH, pitch);
alSourcePlay(sound->source_id);
alSourcei(sound->source_id, AL_SOURCE_RELATIVE, false);
alSource3f(sound->source_id, AL_POSITION, pos.X, pos.Y, pos.Z);
alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
- alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
+ // Use alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED) and set reference
+ // distance to clamp gain at <1 node distance, to avoid excessive
+ // volume when closer
+ alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 10.0f);
alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
- volume = MYMAX(0.0, volume);
+ // Multiply by 3 to compensate for reducing AL_REFERENCE_DISTANCE from
+ // the previous value of 30 to the new value of 10
+ volume = std::fmax(0.0f, volume * 3.0f);
alSourcef(sound->source_id, AL_GAIN, volume);
alSourcef(sound->source_id, AL_PITCH, pitch);
alSourcePlay(sound->source_id);
return id;
}
- int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, v3f pos, float pitch)
+ int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, const v3f &pos,
+ float pitch)
{
assert(buf);
PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos, pitch);
std::set<std::string> paths;
std::set<std::string> datas;
m_fetcher->fetchSounds(name, paths, datas);
- for(std::set<std::string>::iterator i = paths.begin();
- i != paths.end(); ++i){
- loadSoundFile(name, *i);
+ for (const std::string &path : paths) {
+ loadSoundFile(name, path);
}
- for(std::set<std::string>::iterator i = datas.begin();
- i != datas.end(); ++i){
- loadSoundData(name, *i);
+ for (const std::string &data : datas) {
+ loadSoundData(name, data);
}
return getBuffer(name);
}
<<m_sounds_playing.size()<<" playing sounds, "
<<m_buffers.size()<<" sound names loaded"<<std::endl;
std::set<int> del_list;
- for(std::unordered_map<int, PlayingSound*>::iterator i = m_sounds_playing.begin();
- i != m_sounds_playing.end(); ++i) {
- int id = i->first;
- PlayingSound *sound = i->second;
+ for (auto &sp : m_sounds_playing) {
+ int id = sp.first;
+ PlayingSound *sound = sp.second;
// If not playing, remove it
{
ALint state;
if(!del_list.empty())
verbosestream<<"OpenALSoundManager::maintain(): deleting "
<<del_list.size()<<" playing sounds"<<std::endl;
- for(std::set<int>::iterator i = del_list.begin();
- i != del_list.end(); ++i)
- {
- deleteSound(*i);
+ for (int i : del_list) {
+ deleteSound(i);
}
}
int playSound(const std::string &name, bool loop, float volume, float fade, float pitch)
{
maintain();
- if(name == "")
+ if (name.empty())
return 0;
SoundBuffer *buf = getFetchBuffer(name);
if(!buf){
int playSoundAt(const std::string &name, bool loop, float volume, v3f pos, float pitch)
{
maintain();
- if(name == "")
+ if (name.empty())
return 0;
SoundBuffer *buf = getFetchBuffer(name);
if(!buf){