summaryrefslogtreecommitdiffstats
path: root/src/sound/oalsound/alsound.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/sound/oalsound/alsound.cpp')
-rw-r--r--src/sound/oalsound/alsound.cpp143
1 files changed, 105 insertions, 38 deletions
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;
+}