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
dev-ui
erihel 2014-01-18 03:42:07 +01:00
parent 652dc6081d
commit c5ae2610b5
5 changed files with 135 additions and 48 deletions

View File

@ -33,6 +33,7 @@ ALSound::ALSound()
m_lookat.LoadZero(); m_lookat.LoadZero();
m_previousMusic.fadeTime = 0.0f; m_previousMusic.fadeTime = 0.0f;
m_previousMusic.music = nullptr; 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) for (auto it : m_channels)
{ {
if (it.second->IsPlaying()) if (it.second->IsPlaying())
{
continue; continue;
}
if (it.second->GetSoundType() != sound) if (it.second->GetSoundType() != sound)
{
continue; continue;
}
it.second->SetPriority(priority); it.second->SetPriority(priority);
it.second->Reset();
channel = it.first; channel = it.first;
bAlreadyLoaded = it.second->IsLoaded(); bAlreadyLoaded = it.second->IsLoaded();
return true; return true;
@ -261,6 +267,7 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded)
if (chn->IsReady()) if (chn->IsReady())
{ {
chn->SetPriority(priority); chn->SetPriority(priority);
chn->Reset();
m_channels[1] = chn; m_channels[1] = chn;
channel = 1; channel = 1;
bAlreadyLoaded = false; bAlreadyLoaded = false;
@ -271,8 +278,8 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded)
return false; return false;
} }
// Seeks a channel completely free. // Assigns new channel within limit
if (m_channels.size() < 64) if (m_channels.size() < m_channels_limit)
{ {
auto it = m_channels.end(); auto it = m_channels.end();
it--; it--;
@ -286,13 +293,14 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded)
if (chn->IsReady()) if (chn->IsReady())
{ {
chn->SetPriority(priority); chn->SetPriority(priority);
chn->Reset();
m_channels[++i] = chn; m_channels[++i] = chn;
channel = i; channel = i;
bAlreadyLoaded = false; bAlreadyLoaded = false;
return true; return true;
} }
delete chn; 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"); GetLogger()->Debug("Sound channel with lower priority will be reused.\n");
channel = it.first; channel = it.first;
it.second->Reset();
return true; return true;
} }
if (it.second->GetPriority() <= priority) if (it.second->GetPriority() <= priority)
@ -313,11 +322,12 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded)
if (lowerOrEqual != -1) if (lowerOrEqual != -1)
{ {
channel = lowerOrEqual; channel = lowerOrEqual;
m_channels[channel]->Reset();
GetLogger()->Debug("Sound channel with lower or equal priority will be reused.\n"); GetLogger()->Debug("Sound channel with lower or equal priority will be reused.\n");
return true; return true;
} }
GetLogger()->Warn("Could not find free buffer to use.\n"); GetLogger()->Debug("Could not find free buffer to use.\n");
return false; return false;
} }
@ -343,7 +353,9 @@ int ALSound::Play(Sound sound, const Math::Vector &pos, float amplitude, float f
int channel; int channel;
bool bAlreadyLoaded = false; bool bAlreadyLoaded = false;
if (!SearchFreeBuffer(sound, channel, bAlreadyLoaded)) if (!SearchFreeBuffer(sound, channel, bAlreadyLoaded))
{
return -1; return -1;
}
if (!bAlreadyLoaded) 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); m_channels[channel]->SetVolumeAtrib(1.0f);
// setting initial values // 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]->SetFrequency(frequency);
m_channels[channel]->SetVolume(powf(amplitude * m_channels[channel]->GetVolumeAtrib(), 0.2f) * m_audioVolume); m_channels[channel]->SetVolume(powf(amplitude * m_channels[channel]->GetVolumeAtrib(), 0.2f) * m_audioVolume);
m_channels[channel]->SetLoop(bLoop); 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) bool ALSound::FlushEnvelope(int channel)
{ {
if (m_channels.find(channel) == m_channels.end()) if (!CheckChannel(channel))
{ {
return false; return false;
} }
@ -385,10 +407,7 @@ bool ALSound::FlushEnvelope(int channel)
bool ALSound::AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper) bool ALSound::AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper)
{ {
if (!m_enabled) if (!CheckChannel(channel))
return false;
if (m_channels.find(channel) == m_channels.end())
{ {
return false; 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) bool ALSound::Position(int channel, const Math::Vector &pos)
{ {
if (!m_enabled) if (!CheckChannel(channel))
return false;
if (m_channels.find(channel) == m_channels.end())
{ {
return false; return false;
} }
@ -422,10 +438,7 @@ bool ALSound::Position(int channel, const Math::Vector &pos)
bool ALSound::Frequency(int channel, float frequency) bool ALSound::Frequency(int channel, float frequency)
{ {
if (!m_enabled) if (!CheckChannel(channel))
return false;
if (m_channels.find(channel) == m_channels.end())
{ {
return false; return false;
} }
@ -437,10 +450,7 @@ bool ALSound::Frequency(int channel, float frequency)
bool ALSound::Stop(int channel) bool ALSound::Stop(int channel)
{ {
if (!m_enabled) if (!CheckChannel(channel))
return false;
if (m_channels.find(channel) == m_channels.end())
{ {
return false; return false;
} }
@ -455,7 +465,9 @@ bool ALSound::Stop(int channel)
bool ALSound::StopAll() bool ALSound::StopAll()
{ {
if (!m_enabled) if (!m_enabled)
{
return false; return false;
}
for (auto channel : m_channels) for (auto channel : m_channels)
{ {
@ -470,7 +482,9 @@ bool ALSound::StopAll()
bool ALSound::MuteAll(bool bMute) bool ALSound::MuteAll(bool bMute)
{ {
if (!m_enabled) if (!m_enabled)
{
return false; return false;
}
for (auto it : m_channels) for (auto it : m_channels)
{ {
@ -479,7 +493,7 @@ bool ALSound::MuteAll(bool bMute)
it.second->Mute(bMute); it.second->Mute(bMute);
} }
} }
return true; return true;
} }
@ -487,7 +501,9 @@ bool ALSound::MuteAll(bool bMute)
void ALSound::FrameMove(float delta) void ALSound::FrameMove(float delta)
{ {
if (!m_enabled) if (!m_enabled)
{
return; return;
}
float progress; float progress;
float volume, frequency; float volume, frequency;
@ -550,19 +566,25 @@ void ALSound::FrameMove(float delta)
for (auto& it : m_oldMusic) for (auto& it : m_oldMusic)
{ {
if (it.currentTime >= it.fadeTime) { if (it.currentTime >= it.fadeTime)
{
delete it.music; delete it.music;
toRemove.push_back(it); toRemove.push_back(it);
} else { }
else
{
it.currentTime += delta; it.currentTime += delta;
it.music->SetVolume(((it.fadeTime-it.currentTime) / it.fadeTime) * m_musicVolume); it.music->SetVolume(((it.fadeTime-it.currentTime) / it.fadeTime) * m_musicVolume);
} }
} }
if (m_previousMusic.fadeTime > 0.0f) { if (m_previousMusic.fadeTime > 0.0f) {
if (m_previousMusic.currentTime >= m_previousMusic.fadeTime) { if (m_previousMusic.currentTime >= m_previousMusic.fadeTime)
{
m_previousMusic.music->Pause(); m_previousMusic.music->Pause();
} else { }
else
{
m_previousMusic.currentTime += delta; m_previousMusic.currentTime += delta;
m_previousMusic.music->SetVolume(((m_previousMusic.fadeTime-m_previousMusic.currentTime) / m_previousMusic.fadeTime) * m_musicVolume); 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); alListenerfv(AL_ORIENTATION, orientation);
} }
bool ALSound::PlayMusic(int rank, bool bRepeat, float fadeTime) bool ALSound::PlayMusic(int rank, bool bRepeat, float fadeTime)
{ {
std::stringstream filename; std::stringstream filename;
@ -592,15 +615,19 @@ bool ALSound::PlayMusic(int rank, bool bRepeat, float fadeTime)
return PlayMusic(filename.str(), bRepeat, fadeTime); return PlayMusic(filename.str(), bRepeat, fadeTime);
} }
bool operator<(const OldMusic & l, const OldMusic & r) bool operator<(const OldMusic & l, const OldMusic & r)
{ {
return l.currentTime < r.currentTime; return l.currentTime < r.currentTime;
} }
bool operator==(const OldMusic & l, const OldMusic & r) bool operator==(const OldMusic & l, const OldMusic & r)
{ {
return l.currentTime == r.currentTime; return l.currentTime == r.currentTime;
} }
bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTime) bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTime)
{ {
if (!m_enabled) 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"); GetLogger()->Debug("Music loaded from cache\n");
buffer = m_music[filename]; buffer = m_music[filename];
} }
if (m_currentMusic) { if (m_currentMusic)
{
OldMusic old; OldMusic old;
old.music = m_currentMusic; old.music = m_currentMusic;
old.fadeTime = fadeTime; old.fadeTime = fadeTime;
@ -650,17 +678,22 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTim
return true; return true;
} }
bool ALSound::PlayPauseMusic(const std::string &filename) bool ALSound::PlayPauseMusic(const std::string &filename)
{ {
if (m_previousMusic.fadeTime > 0.0f) { if (m_previousMusic.fadeTime > 0.0f)
{
OldMusic old; OldMusic old;
old.music = m_currentMusic; old.music = m_currentMusic;
old.fadeTime = 2.0f; old.fadeTime = 2.0f;
old.currentTime = 0.0f; old.currentTime = 0.0f;
m_oldMusic.push_back(old); m_oldMusic.push_back(old);
m_currentMusic = nullptr; m_currentMusic = nullptr;
} else { }
if (m_currentMusic) { else
{
if (m_currentMusic)
{
m_previousMusic.music = m_currentMusic; m_previousMusic.music = m_currentMusic;
m_previousMusic.fadeTime = 2.0f; m_previousMusic.fadeTime = 2.0f;
m_previousMusic.currentTime = 0.0f; m_previousMusic.currentTime = 0.0f;
@ -670,16 +703,20 @@ bool ALSound::PlayPauseMusic(const std::string &filename)
return PlayMusic(filename, true); return PlayMusic(filename, true);
} }
void ALSound::StopPauseMusic() void ALSound::StopPauseMusic()
{ {
if (m_previousMusic.fadeTime > 0.0f) { if (m_previousMusic.fadeTime > 0.0f)
{
StopMusic(); StopMusic();
m_currentMusic = m_previousMusic.music; m_currentMusic = m_previousMusic.music;
m_previousMusic.music = nullptr; m_previousMusic.music = nullptr;
if(m_currentMusic != nullptr) { if(m_currentMusic != nullptr)
{
m_currentMusic->SetVolume(m_musicVolume); m_currentMusic->SetVolume(m_musicVolume);
if(m_previousMusic.currentTime >= m_previousMusic.fadeTime) { if(m_previousMusic.currentTime >= m_previousMusic.fadeTime)
{
m_currentMusic->Play(); m_currentMusic->Play();
} }
} }
@ -700,6 +737,7 @@ bool ALSound::RestartMusic()
return true; return true;
} }
void ALSound::StopMusic(float fadeTime) void ALSound::StopMusic(float fadeTime)
{ {
if (!m_enabled || !m_currentMusic) if (!m_enabled || !m_currentMusic)
@ -737,3 +775,32 @@ void ALSound::SuspendMusic()
m_currentMusic->Stop(); 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;
}

View File

@ -81,6 +81,8 @@ public:
bool PlayPauseMusic(const std::string &filename); bool PlayPauseMusic(const std::string &filename);
void StopPauseMusic(); void StopPauseMusic();
bool CheckChannel(int &channel);
private: private:
void CleanUp(); void CleanUp();
int GetPriority(Sound); int GetPriority(Sound);
@ -89,6 +91,7 @@ private:
bool m_enabled; bool m_enabled;
float m_audioVolume; float m_audioVolume;
float m_musicVolume; float m_musicVolume;
unsigned int m_channels_limit;
ALCdevice* m_device; ALCdevice* m_device;
ALCcontext* m_context; ALCcontext* m_context;
std::map<Sound, Buffer*> m_sounds; std::map<Sound, Buffer*> m_sounds;

View File

@ -32,7 +32,7 @@ Buffer::~Buffer()
{ {
alDeleteBuffers(1, &m_buffer); alDeleteBuffers(1, &m_buffer);
if (alCheck()) if (alCheck())
GetLogger()->Warn("Failed to unload buffer. Code %d\n", alGetCode()); GetLogger()->Debug("Failed to unload buffer. Code %d\n", alGetCode());
} }
} }

View File

@ -23,7 +23,7 @@ Channel::Channel()
if (alCheck()) 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; m_ready = false;
} }
else else
@ -40,6 +40,7 @@ Channel::Channel()
m_startFrequency = 0.0f; m_startFrequency = 0.0f;
m_changeFrequency = 0.0f; m_changeFrequency = 0.0f;
m_volume = 0.0f; m_volume = 0.0f;
m_id = 0;
} }
@ -51,7 +52,7 @@ Channel::~Channel()
alSourcei(m_source, AL_BUFFER, 0); alSourcei(m_source, AL_BUFFER, 0);
alDeleteSources(1, &m_source); alDeleteSources(1, &m_source);
if (alCheck()) 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); alSourcePlay(m_source);
if (alCheck()) 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; return true;
} }
@ -84,7 +85,7 @@ bool Channel::Pause()
alSourcePause(m_source); alSourcePause(m_source);
if (alCheck()) 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; return true;
} }
@ -100,7 +101,7 @@ bool Channel::SetPosition(const Math::Vector &pos)
alSource3f(m_source, AL_POSITION, pos.x, pos.y, pos.z); alSource3f(m_source, AL_POSITION, pos.x, pos.y, pos.z);
if (alCheck()) 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 false;
} }
return true; return true;
@ -117,7 +118,7 @@ bool Channel::SetFrequency(float freq)
alSourcef(m_source, AL_PITCH, freq); alSourcef(m_source, AL_PITCH, freq);
if (alCheck()) 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 false;
} }
return true; return true;
@ -135,7 +136,7 @@ float Channel::GetFrequency()
alGetSourcef(m_source, AL_PITCH, &freq); alGetSourcef(m_source, AL_PITCH, &freq);
if (alCheck()) 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; return 0;
} }
@ -153,7 +154,7 @@ bool Channel::SetVolume(float vol)
alSourcef(m_source, AL_GAIN, vol); alSourcef(m_source, AL_GAIN, vol);
if (alCheck()) 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 false;
} }
return true; return true;
@ -171,7 +172,7 @@ float Channel::GetVolume()
alGetSourcef(m_source, AL_GAIN, &vol); alGetSourcef(m_source, AL_GAIN, &vol);
if (alCheck()) 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; return 0;
} }
@ -434,3 +435,15 @@ bool Channel::IsMuted()
return m_mute; return m_mute;
} }
void Channel::Reset()
{
m_id++;
}
int Channel::GetId()
{
return m_id;
}

View File

@ -97,11 +97,15 @@ public:
void Mute(bool); void Mute(bool);
bool IsMuted(); bool IsMuted();
void Reset();
int GetId();
private: private:
Buffer *m_buffer; Buffer *m_buffer;
ALuint m_source; ALuint m_source;
int m_priority; int m_priority;
int m_id;
float m_startAmplitude; float m_startAmplitude;
float m_startFrequency; float m_startFrequency;
float m_changeFrequency; float m_changeFrequency;