From c5ae2610b57e54216ee55214bd74368c9c7e22ee Mon Sep 17 00:00:00 2001 From: erihel Date: Sat, 18 Jan 2014 03:42:07 +0100 Subject: Minor changes to sound support. * changed channel limit from 64 to 2048 that will decrease if error is found while trying to play sound * added id to each channel to avoid collisions when more than 1 object tries to modify a sound * minor formatting changes --- src/sound/oalsound/alsound.cpp | 143 ++++++++++++++++++++++++++++++----------- src/sound/oalsound/alsound.h | 3 + src/sound/oalsound/buffer.cpp | 2 +- src/sound/oalsound/channel.cpp | 31 ++++++--- src/sound/oalsound/channel.h | 4 ++ 5 files changed, 135 insertions(+), 48 deletions(-) (limited to 'src/sound') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 5058141..3f8bade 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -33,6 +33,7 @@ ALSound::ALSound() m_lookat.LoadZero(); m_previousMusic.fadeTime = 0.0f; m_previousMusic.music = nullptr; + m_channels_limit = 2048; } @@ -243,11 +244,16 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) for (auto it : m_channels) { if (it.second->IsPlaying()) + { continue; + } if (it.second->GetSoundType() != sound) + { continue; + } it.second->SetPriority(priority); + it.second->Reset(); channel = it.first; bAlreadyLoaded = it.second->IsLoaded(); return true; @@ -261,6 +267,7 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) if (chn->IsReady()) { chn->SetPriority(priority); + chn->Reset(); m_channels[1] = chn; channel = 1; bAlreadyLoaded = false; @@ -271,8 +278,8 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) return false; } - // Seeks a channel completely free. - if (m_channels.size() < 64) + // Assigns new channel within limit + if (m_channels.size() < m_channels_limit) { auto it = m_channels.end(); it--; @@ -286,13 +293,14 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) if (chn->IsReady()) { chn->SetPriority(priority); + chn->Reset(); m_channels[++i] = chn; channel = i; bAlreadyLoaded = false; return true; } delete chn; - GetLogger()->Warn("Could not open additional channel to play sound!\n"); + GetLogger()->Debug("Could not open additional channel to play sound!\n"); } } } @@ -304,6 +312,7 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) { GetLogger()->Debug("Sound channel with lower priority will be reused.\n"); channel = it.first; + it.second->Reset(); return true; } if (it.second->GetPriority() <= priority) @@ -313,11 +322,12 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) if (lowerOrEqual != -1) { channel = lowerOrEqual; + m_channels[channel]->Reset(); GetLogger()->Debug("Sound channel with lower or equal priority will be reused.\n"); return true; } - GetLogger()->Warn("Could not find free buffer to use.\n"); + GetLogger()->Debug("Could not find free buffer to use.\n"); return false; } @@ -343,7 +353,9 @@ int ALSound::Play(Sound sound, const Math::Vector &pos, float amplitude, float f int channel; bool bAlreadyLoaded = false; if (!SearchFreeBuffer(sound, channel, bAlreadyLoaded)) + { return -1; + } if (!bAlreadyLoaded) { @@ -354,7 +366,7 @@ int ALSound::Play(Sound sound, const Math::Vector &pos, float amplitude, float f } } - Position(channel, pos); + m_channels[channel]->SetPosition(pos); m_channels[channel]->SetVolumeAtrib(1.0f); // setting initial values @@ -365,15 +377,25 @@ int ALSound::Play(Sound sound, const Math::Vector &pos, float amplitude, float f m_channels[channel]->SetFrequency(frequency); m_channels[channel]->SetVolume(powf(amplitude * m_channels[channel]->GetVolumeAtrib(), 0.2f) * m_audioVolume); m_channels[channel]->SetLoop(bLoop); - m_channels[channel]->Play(); - return channel; + if (!m_channels[channel]->Play()) + { + GetLogger()->Debug("Changing channel limit to %u.\n", --m_channels_limit); + auto it = m_channels.find(channel); + Channel *ch = it->second; + m_channels.erase(it); + delete ch; + + return -1; + } + + return channel | ((m_channels[channel]->GetId() & 0xffff) << 16); } bool ALSound::FlushEnvelope(int channel) { - if (m_channels.find(channel) == m_channels.end()) + if (!CheckChannel(channel)) { return false; } @@ -385,10 +407,7 @@ bool ALSound::FlushEnvelope(int channel) bool ALSound::AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper) { - if (!m_enabled) - return false; - - if (m_channels.find(channel) == m_channels.end()) + if (!CheckChannel(channel)) { return false; } @@ -407,10 +426,7 @@ bool ALSound::AddEnvelope(int channel, float amplitude, float frequency, float t bool ALSound::Position(int channel, const Math::Vector &pos) { - if (!m_enabled) - return false; - - if (m_channels.find(channel) == m_channels.end()) + if (!CheckChannel(channel)) { return false; } @@ -422,10 +438,7 @@ bool ALSound::Position(int channel, const Math::Vector &pos) bool ALSound::Frequency(int channel, float frequency) { - if (!m_enabled) - return false; - - if (m_channels.find(channel) == m_channels.end()) + if (!CheckChannel(channel)) { return false; } @@ -437,10 +450,7 @@ bool ALSound::Frequency(int channel, float frequency) bool ALSound::Stop(int channel) { - if (!m_enabled) - return false; - - if (m_channels.find(channel) == m_channels.end()) + if (!CheckChannel(channel)) { return false; } @@ -455,7 +465,9 @@ bool ALSound::Stop(int channel) bool ALSound::StopAll() { if (!m_enabled) + { return false; + } for (auto channel : m_channels) { @@ -470,7 +482,9 @@ bool ALSound::StopAll() bool ALSound::MuteAll(bool bMute) { if (!m_enabled) + { return false; + } for (auto it : m_channels) { @@ -479,7 +493,7 @@ bool ALSound::MuteAll(bool bMute) it.second->Mute(bMute); } } - + return true; } @@ -487,7 +501,9 @@ bool ALSound::MuteAll(bool bMute) void ALSound::FrameMove(float delta) { if (!m_enabled) + { return; + } float progress; float volume, frequency; @@ -550,19 +566,25 @@ void ALSound::FrameMove(float delta) for (auto& it : m_oldMusic) { - if (it.currentTime >= it.fadeTime) { + if (it.currentTime >= it.fadeTime) + { delete it.music; toRemove.push_back(it); - } else { + } + else + { it.currentTime += delta; it.music->SetVolume(((it.fadeTime-it.currentTime) / it.fadeTime) * m_musicVolume); } } if (m_previousMusic.fadeTime > 0.0f) { - if (m_previousMusic.currentTime >= m_previousMusic.fadeTime) { + if (m_previousMusic.currentTime >= m_previousMusic.fadeTime) + { m_previousMusic.music->Pause(); - } else { + } + else + { m_previousMusic.currentTime += delta; m_previousMusic.music->SetVolume(((m_previousMusic.fadeTime-m_previousMusic.currentTime) / m_previousMusic.fadeTime) * m_musicVolume); } @@ -585,6 +607,7 @@ void ALSound::SetListener(const Math::Vector &eye, const Math::Vector &lookat) alListenerfv(AL_ORIENTATION, orientation); } + bool ALSound::PlayMusic(int rank, bool bRepeat, float fadeTime) { std::stringstream filename; @@ -592,15 +615,19 @@ bool ALSound::PlayMusic(int rank, bool bRepeat, float fadeTime) return PlayMusic(filename.str(), bRepeat, fadeTime); } + bool operator<(const OldMusic & l, const OldMusic & r) { return l.currentTime < r.currentTime; } + + bool operator==(const OldMusic & l, const OldMusic & r) { return l.currentTime == r.currentTime; } + bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTime) { if (!m_enabled) @@ -632,8 +659,9 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTim GetLogger()->Debug("Music loaded from cache\n"); buffer = m_music[filename]; } - - if (m_currentMusic) { + + if (m_currentMusic) + { OldMusic old; old.music = m_currentMusic; old.fadeTime = fadeTime; @@ -650,17 +678,22 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTim return true; } + bool ALSound::PlayPauseMusic(const std::string &filename) { - if (m_previousMusic.fadeTime > 0.0f) { + if (m_previousMusic.fadeTime > 0.0f) + { OldMusic old; old.music = m_currentMusic; old.fadeTime = 2.0f; old.currentTime = 0.0f; m_oldMusic.push_back(old); m_currentMusic = nullptr; - } else { - if (m_currentMusic) { + } + else + { + if (m_currentMusic) + { m_previousMusic.music = m_currentMusic; m_previousMusic.fadeTime = 2.0f; m_previousMusic.currentTime = 0.0f; @@ -670,16 +703,20 @@ bool ALSound::PlayPauseMusic(const std::string &filename) return PlayMusic(filename, true); } + void ALSound::StopPauseMusic() { - if (m_previousMusic.fadeTime > 0.0f) { + if (m_previousMusic.fadeTime > 0.0f) + { StopMusic(); - + m_currentMusic = m_previousMusic.music; m_previousMusic.music = nullptr; - if(m_currentMusic != nullptr) { + if(m_currentMusic != nullptr) + { m_currentMusic->SetVolume(m_musicVolume); - if(m_previousMusic.currentTime >= m_previousMusic.fadeTime) { + if(m_previousMusic.currentTime >= m_previousMusic.fadeTime) + { m_currentMusic->Play(); } } @@ -700,6 +737,7 @@ bool ALSound::RestartMusic() return true; } + void ALSound::StopMusic(float fadeTime) { if (!m_enabled || !m_currentMusic) @@ -737,3 +775,32 @@ void ALSound::SuspendMusic() m_currentMusic->Stop(); } + + +bool ALSound::CheckChannel(int &channel) +{ + int id = (channel >> 16) & 0xffff; + channel &= 0xffff; + + if (!m_enabled) + { + return false; + } + + if (m_channels.find(channel) == m_channels.end()) + { + return false; + } + + if (m_audioVolume == 0) + { + return false; + } + + if (m_channels[channel]->GetId() != id) + { + return false; + } + + return true; +} diff --git a/src/sound/oalsound/alsound.h b/src/sound/oalsound/alsound.h index 6fb832e..4113098 100644 --- a/src/sound/oalsound/alsound.h +++ b/src/sound/oalsound/alsound.h @@ -81,6 +81,8 @@ public: bool PlayPauseMusic(const std::string &filename); void StopPauseMusic(); + bool CheckChannel(int &channel); + private: void CleanUp(); int GetPriority(Sound); @@ -89,6 +91,7 @@ private: bool m_enabled; float m_audioVolume; float m_musicVolume; + unsigned int m_channels_limit; ALCdevice* m_device; ALCcontext* m_context; std::map m_sounds; diff --git a/src/sound/oalsound/buffer.cpp b/src/sound/oalsound/buffer.cpp index 0047f91..b27029c 100644 --- a/src/sound/oalsound/buffer.cpp +++ b/src/sound/oalsound/buffer.cpp @@ -32,7 +32,7 @@ Buffer::~Buffer() { alDeleteBuffers(1, &m_buffer); if (alCheck()) - GetLogger()->Warn("Failed to unload buffer. Code %d\n", alGetCode()); + GetLogger()->Debug("Failed to unload buffer. Code %d\n", alGetCode()); } } diff --git a/src/sound/oalsound/channel.cpp b/src/sound/oalsound/channel.cpp index e58ab54..7021c2f 100644 --- a/src/sound/oalsound/channel.cpp +++ b/src/sound/oalsound/channel.cpp @@ -23,7 +23,7 @@ Channel::Channel() if (alCheck()) { - GetLogger()->Warn("Failed to create sound source. Code: %d\n", alGetCode()); + GetLogger()->Debug("Failed to create sound source. Code: %d\n", alGetCode()); m_ready = false; } else @@ -40,6 +40,7 @@ Channel::Channel() m_startFrequency = 0.0f; m_changeFrequency = 0.0f; m_volume = 0.0f; + m_id = 0; } @@ -51,7 +52,7 @@ Channel::~Channel() alSourcei(m_source, AL_BUFFER, 0); alDeleteSources(1, &m_source); if (alCheck()) - GetLogger()->Warn("Failed to delete sound source. Code: %d\n", alGetCode()); + GetLogger()->Debug("Failed to delete sound source. Code: %d\n", alGetCode()); } } @@ -69,7 +70,7 @@ bool Channel::Play() alSourcePlay(m_source); if (alCheck()) { - GetLogger()->Warn("Could not play audio sound source. Code: %d\n", alGetCode()); + GetLogger()->Debug("Could not play audio sound source. Code: %d\n", alGetCode()); } return true; } @@ -84,7 +85,7 @@ bool Channel::Pause() alSourcePause(m_source); if (alCheck()) { - GetLogger()->Warn("Could not pause audio sound source. Code: %d\n", alGetCode()); + GetLogger()->Debug("Could not pause audio sound source. Code: %d\n", alGetCode()); } return true; } @@ -100,7 +101,7 @@ bool Channel::SetPosition(const Math::Vector &pos) alSource3f(m_source, AL_POSITION, pos.x, pos.y, pos.z); if (alCheck()) { - GetLogger()->Warn("Could not set sound position. Code: %d\n", alGetCode()); + GetLogger()->Debug("Could not set sound position. Code: %d\n", alGetCode()); return false; } return true; @@ -117,7 +118,7 @@ bool Channel::SetFrequency(float freq) alSourcef(m_source, AL_PITCH, freq); if (alCheck()) { - GetLogger()->Warn("Could not set sound pitch to '%f'. Code: %d\n", freq, alGetCode()); + GetLogger()->Debug("Could not set sound pitch to '%f'. Code: %d\n", freq, alGetCode()); return false; } return true; @@ -135,7 +136,7 @@ float Channel::GetFrequency() alGetSourcef(m_source, AL_PITCH, &freq); if (alCheck()) { - GetLogger()->Warn("Could not get sound pitch. Code: %d\n", alGetCode()); + GetLogger()->Debug("Could not get sound pitch. Code: %d\n", alGetCode()); return 0; } @@ -153,7 +154,7 @@ bool Channel::SetVolume(float vol) alSourcef(m_source, AL_GAIN, vol); if (alCheck()) { - GetLogger()->Warn("Could not set sound volume to '%f'. Code: %d\n", vol, alGetCode()); + GetLogger()->Debug("Could not set sound volume to '%f'. Code: %d\n", vol, alGetCode()); return false; } return true; @@ -171,7 +172,7 @@ float Channel::GetVolume() alGetSourcef(m_source, AL_GAIN, &vol); if (alCheck()) { - GetLogger()->Warn("Could not get sound volume. Code: %d\n", alGetCode()); + GetLogger()->Debug("Could not get sound volume. Code: %d\n", alGetCode()); return 0; } @@ -434,3 +435,15 @@ bool Channel::IsMuted() return m_mute; } + +void Channel::Reset() +{ + m_id++; +} + + +int Channel::GetId() +{ + return m_id; +} + diff --git a/src/sound/oalsound/channel.h b/src/sound/oalsound/channel.h index f973198..1d988c9 100644 --- a/src/sound/oalsound/channel.h +++ b/src/sound/oalsound/channel.h @@ -97,11 +97,15 @@ public: void Mute(bool); bool IsMuted(); + void Reset(); + int GetId(); + private: Buffer *m_buffer; ALuint m_source; int m_priority; + int m_id; float m_startAmplitude; float m_startFrequency; float m_changeFrequency; -- cgit v1.2.3-1-g7c22