From fe3f9ea38cfae89fdd83d4c3fe79ea355f50bce9 Mon Sep 17 00:00:00 2001 From: erihel Date: Thu, 19 Dec 2013 22:41:16 +0100 Subject: Sound support changes * removed 2d sound * fixed listener orientation (propably issue #235) * removed unused code and minor refactoring --- src/sound/oalsound/alsound.cpp | 153 ++++------------------------------------- 1 file changed, 14 insertions(+), 139 deletions(-) (limited to 'src/sound/oalsound/alsound.cpp') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 992b8b2..7f325c1 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -26,7 +26,6 @@ ALSound::ALSound() { m_enabled = false; - m_3D = false; m_audioVolume = 1.0f; m_musicVolume = 1.0f; m_currentMusic = nullptr; @@ -77,7 +76,7 @@ void ALSound::CleanUp() } -bool ALSound::Create(bool b3D) +bool ALSound::Create() { CleanUp(); @@ -109,25 +108,6 @@ bool ALSound::Create(bool b3D) } -void ALSound::SetSound3D(bool bMode) -{ - m_3D = bMode; -} - - -bool ALSound::GetSound3D() -{ - return m_3D; -} - - -bool ALSound::GetSound3DCap() -{ - // TODO stub! need to be implemented - return true; -} - - bool ALSound::GetEnable() { return m_enabled; @@ -168,7 +148,7 @@ int ALSound::GetMusicVolume() } -bool ALSound::Cache(Sound sound, std::string filename) +bool ALSound::Cache(Sound sound, const std::string &filename) { Buffer *buffer = new Buffer(); if (buffer->LoadFromFile(filename, sound)) @@ -179,7 +159,7 @@ bool ALSound::Cache(Sound sound, std::string filename) return false; } -bool ALSound::CacheMusic(std::string filename) +bool ALSound::CacheMusic(const std::string &filename) { if (m_music.find(filename) == m_music.end()) { @@ -337,7 +317,7 @@ int ALSound::Play(Sound sound, float amplitude, float frequency, bool bLoop) } -int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequency, bool bLoop) +int ALSound::Play(Sound sound, const Math::Vector &pos, float amplitude, float frequency, bool bLoop) { if (!m_enabled) { @@ -364,14 +344,7 @@ int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequenc } Position(channel, pos); - if (!m_3D) - { - ComputeVolumePan2D(channel, pos); - } - else - { - m_channels[channel]->SetVolumeAtrib(1.0f); - } + m_channels[channel]->SetVolumeAtrib(1.0f); // setting initial values m_channels[channel]->SetStartAmplitude(amplitude); @@ -421,7 +394,7 @@ bool ALSound::AddEnvelope(int channel, float amplitude, float frequency, float t } -bool ALSound::Position(int channel, Math::Vector pos) +bool ALSound::Position(int channel, const Math::Vector &pos) { if (!m_enabled) return false; @@ -431,20 +404,7 @@ bool ALSound::Position(int channel, Math::Vector pos) return false; } - if (m_3D) - { - m_channels[channel]->SetPan(pos); - } - else - { - ComputeVolumePan2D(channel, pos); - - if (!m_channels[channel]->HasEnvelope()) - { - float volume = m_channels[channel]->GetStartAmplitude(); - m_channels[channel]->SetVolume(powf(volume * m_channels[channel]->GetVolumeAtrib(), 0.2f) * m_audioVolume); - } - } + m_channels[channel]->SetPosition(pos); return true; } @@ -585,38 +545,16 @@ void ALSound::FrameMove(float delta) } -void ALSound::SetListener(Math::Vector eye, Math::Vector lookat) +void ALSound::SetListener(const Math::Vector &eye, const Math::Vector &lookat) { m_eye = eye; m_lookat = lookat; - if (m_3D) - { - float orientation[] = {lookat.x, lookat.y, lookat.z, 0.f, 1.f, 0.f}; - alListener3f(AL_POSITION, eye.x, eye.y, eye.z); - alListenerfv(AL_ORIENTATION, orientation); - } - else - { - float orientation[] = {0.0f, 0.0f, 0.0f, 0.f, 1.f, 0.f}; - alListener3f(AL_POSITION, 0.0f, 0.0f, 0.0f); - alListenerfv(AL_ORIENTATION, orientation); + Math::Vector forward = lookat - eye; + forward.Normalize(); + float orientation[] = {forward.x, forward.y, forward.z, 0.f, -1.0f, 0.0f}; - // recalculate sound position - for (auto it : m_channels) - { - if (it.second->IsPlaying()) - { - Math::Vector pos = it.second->GetPosition(); - ComputeVolumePan2D(it.first, pos); - - if (!it.second->HasEnvelope()) - { - float volume = it.second->GetStartAmplitude(); - it.second->SetVolume(powf(volume * it.second->GetVolumeAtrib(), 0.2f) * m_audioVolume); - } - } - } - } + alListener3f(AL_POSITION, eye.x, eye.y, eye.z); + alListenerfv(AL_ORIENTATION, orientation); } bool ALSound::PlayMusic(int rank, bool bRepeat) @@ -626,7 +564,7 @@ bool ALSound::PlayMusic(int rank, bool bRepeat) return PlayMusic(filename.str(), bRepeat); } -bool ALSound::PlayMusic(std::string filename, bool bRepeat) +bool ALSound::PlayMusic(const std::string &filename, bool bRepeat) { if (!m_enabled) { @@ -706,66 +644,3 @@ void ALSound::SuspendMusic() m_currentMusic->Stop(); } - - -void ALSound::ComputeVolumePan2D(int channel, Math::Vector &pos) -{ - float dist, a, g; - m_channels[channel]->SetPosition(pos); - - if (VectorsEqual(pos, m_eye)) - { - m_channels[channel]->SetVolumeAtrib(1.0f); // maximum volume - m_channels[channel]->SetPan(Math::Vector()); // at the center - return; - } - - dist = Distance(pos, m_eye); - if ( dist >= 110.0f ) // very far? - { - m_channels[channel]->SetVolumeAtrib(0.0f); // silence - m_channels[channel]->SetPan(Math::Vector()); // at the center - return; - } - else if ( dist <= 10.0f ) // very close? - { - m_channels[channel]->SetVolumeAtrib(1.0f); // maximum volume - m_channels[channel]->SetPan(Math::Vector()); // at the center - return; - } - m_channels[channel]->SetVolumeAtrib(1.0f - ((dist - 10.0f) / 100.0f)); - - Math::Vector one = Math::Vector(1.0f, 0.0f, 0.0f); - float angle_a = Angle(Math::Vector(m_lookat.x - m_eye.x, m_lookat.z - m_eye.z, 0.0f), one); - float angle_g = Angle(Math::Vector(pos.x - m_eye.x, pos.z - m_eye.z, 0.0f), one); - - a = fmodf(angle_a, Math::PI * 2.0f); - g = fmodf(angle_g, Math::PI * 2.0f); - - if ( a < 0.0f ) - { - a += Math::PI * 2.0f; - } - if ( g < 0.0f ) - { - g += Math::PI * 2.0f; - } - - if ( a < g ) - { - if (a + Math::PI * 2.0f - g < g - a ) - { - a += Math::PI * 2.0f; - } - } - else - { - if ( g + Math::PI * 2.0f - a < a - g ) - { - g += Math::PI * 2.0f; - } - } - - m_channels[channel]->SetPan( Math::Vector(0.0f, 0.0f, sinf(g - a)) ); -} - -- cgit v1.2.3-1-g7c22 From e9addb5a5e072b28eecfa1739ae38d67b68a2b23 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sat, 28 Dec 2013 12:30:46 +0100 Subject: Added smooth transition in music - issue #205 --- src/sound/oalsound/alsound.cpp | 81 +++++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 16 deletions(-) (limited to 'src/sound/oalsound/alsound.cpp') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 7f325c1..ef53236 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -57,6 +57,11 @@ void ALSound::CleanUp() { delete m_currentMusic; } + + for (auto item : m_oldMusic) + { + delete item.music; + } for (auto item : m_sounds) { @@ -101,7 +106,6 @@ bool ALSound::Create() alListenerf(AL_GAIN, m_audioVolume); alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED); - m_currentMusic = new Channel(); GetLogger()->Info("Done.\n"); m_enabled = true; return true; @@ -469,13 +473,15 @@ bool ALSound::MuteAll(bool bMute) } } - if (bMute) - { - m_currentMusic->SetVolume(0.0f); - } - else - { - m_currentMusic->SetVolume(m_musicVolume); + if (m_currentMusic) { + if (bMute) + { + m_currentMusic->SetVolume(0.0f); + } + else + { + m_currentMusic->SetVolume(m_musicVolume); + } } return true; } @@ -542,6 +548,22 @@ void ALSound::FrameMove(float delta) } } } + + std::list toRemove; + + for (auto& it : m_oldMusic) + { + if (it.currentTime >= it.fadeTime) { + delete it.music; + toRemove.push_back(it); + } else { + it.currentTime += delta; + it.music->SetVolume(((it.fadeTime-it.currentTime) / it.fadeTime) * m_musicVolume); + } + } + + for (auto it : toRemove) + m_oldMusic.remove(it); } @@ -557,14 +579,23 @@ void ALSound::SetListener(const Math::Vector &eye, const Math::Vector &lookat) alListenerfv(AL_ORIENTATION, orientation); } -bool ALSound::PlayMusic(int rank, bool bRepeat) +bool ALSound::PlayMusic(int rank, bool bRepeat, float fadeTime) { std::stringstream filename; filename << "music" << std::setfill('0') << std::setw(3) << rank << ".ogg"; - return PlayMusic(filename.str(), bRepeat); + return PlayMusic(filename.str(), bRepeat, fadeTime); } -bool ALSound::PlayMusic(const std::string &filename, bool bRepeat) +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) { @@ -573,6 +604,8 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat) std::stringstream file; file << m_soundPath << "/" << filename; + + Buffer *buffer; // check if we have music in cache if (m_music.find(filename) == m_music.end()) @@ -583,16 +616,26 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat) GetLogger()->Warn("Requested music %s was not found.\n", filename.c_str()); return false; } - Buffer *buffer = new Buffer(); + + buffer = new Buffer(); buffer->LoadFromFile(file.str(), static_cast(-1)); - m_currentMusic->SetBuffer(buffer); } else { GetLogger()->Debug("Music loaded from cache\n"); - m_currentMusic->SetBuffer(m_music[filename]); + buffer = m_music[filename]; + } + + if (m_currentMusic) { + OldMusic old; + old.music = m_currentMusic; + old.fadeTime = fadeTime; + old.currentTime = 0.0f; + m_oldMusic.push_back(old); } + m_currentMusic = new Channel(); + m_currentMusic->SetBuffer(buffer); m_currentMusic->SetVolume(m_musicVolume); m_currentMusic->SetLoop(bRepeat); m_currentMusic->Play(); @@ -613,14 +656,20 @@ bool ALSound::RestartMusic() return true; } -void ALSound::StopMusic() +void ALSound::StopMusic(float fadeTime) { if (!m_enabled || !m_currentMusic) { return; } - SuspendMusic(); + OldMusic old; + old.music = m_currentMusic; + old.fadeTime = fadeTime; + old.currentTime = 0.0f; + m_oldMusic.push_back(old); + + m_currentMusic = nullptr; } -- cgit v1.2.3-1-g7c22 From 9631f26270d14d3b53ce988868a8809378824dee Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sat, 28 Dec 2013 16:45:11 +0100 Subject: Don't stop music when SatCom or pause menu is opened --- src/sound/oalsound/alsound.cpp | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) (limited to 'src/sound/oalsound/alsound.cpp') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index ef53236..7f5d1f1 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -472,17 +472,7 @@ bool ALSound::MuteAll(bool bMute) it.second->Mute(bMute); } } - - if (m_currentMusic) { - if (bMute) - { - m_currentMusic->SetVolume(0.0f); - } - else - { - m_currentMusic->SetVolume(m_musicVolume); - } - } + return true; } -- cgit v1.2.3-1-g7c22 From 999490e88bc699b671b94b88c9a4327d963db378 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Tue, 31 Dec 2013 16:58:21 +0100 Subject: Code for changing music in pause mode As requested by @Emxx52. Only code for now, we don't have the music yet. Temporairly in developements builds music will change to Prototype (in CBot editor) and Constructive Destruction (in SatCom) --- src/sound/oalsound/alsound.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'src/sound/oalsound/alsound.cpp') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 7f5d1f1..5058141 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -31,6 +31,8 @@ ALSound::ALSound() m_currentMusic = nullptr; m_eye.LoadZero(); m_lookat.LoadZero(); + m_previousMusic.fadeTime = 0.0f; + m_previousMusic.music = nullptr; } @@ -62,6 +64,11 @@ void ALSound::CleanUp() { delete item.music; } + + if (m_previousMusic.music) + { + delete m_previousMusic.music; + } for (auto item : m_sounds) { @@ -552,6 +559,15 @@ void ALSound::FrameMove(float delta) } } + if (m_previousMusic.fadeTime > 0.0f) { + if (m_previousMusic.currentTime >= m_previousMusic.fadeTime) { + m_previousMusic.music->Pause(); + } else { + m_previousMusic.currentTime += delta; + m_previousMusic.music->SetVolume(((m_previousMusic.fadeTime-m_previousMusic.currentTime) / m_previousMusic.fadeTime) * m_musicVolume); + } + } + for (auto it : toRemove) m_oldMusic.remove(it); } @@ -609,6 +625,7 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTim buffer = new Buffer(); buffer->LoadFromFile(file.str(), static_cast(-1)); + m_music[filename] = buffer; } else { @@ -633,6 +650,43 @@ 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) { + 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) { + m_previousMusic.music = m_currentMusic; + m_previousMusic.fadeTime = 2.0f; + m_previousMusic.currentTime = 0.0f; + m_currentMusic = nullptr; + } + } + return PlayMusic(filename, true); +} + +void ALSound::StopPauseMusic() +{ + if (m_previousMusic.fadeTime > 0.0f) { + StopMusic(); + + m_currentMusic = m_previousMusic.music; + m_previousMusic.music = nullptr; + if(m_currentMusic != nullptr) { + m_currentMusic->SetVolume(m_musicVolume); + if(m_previousMusic.currentTime >= m_previousMusic.fadeTime) { + m_currentMusic->Play(); + } + } + m_previousMusic.fadeTime = 0.0f; + } +} + bool ALSound::RestartMusic() { -- cgit v1.2.3-1-g7c22 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 ++++++++++++++++++++++++++++++----------- 1 file changed, 105 insertions(+), 38 deletions(-) (limited to 'src/sound/oalsound/alsound.cpp') 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; +} -- cgit v1.2.3-1-g7c22 From d84d38280b3ed706ff2371fc88e43a2a7245d3dd Mon Sep 17 00:00:00 2001 From: erihel Date: Sun, 2 Feb 2014 01:50:34 +0100 Subject: Changed max number of sound channels --- src/sound/oalsound/alsound.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/sound/oalsound/alsound.cpp') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 3f8bade..9ef9341 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -20,6 +20,7 @@ #include #include +#include #include @@ -380,7 +381,8 @@ int ALSound::Play(Sound sound, const Math::Vector &pos, float amplitude, float f if (!m_channels[channel]->Play()) { - GetLogger()->Debug("Changing channel limit to %u.\n", --m_channels_limit); + m_channels_limit = std::min(m_channels.size() - 1, m_channels_limit - 1); + 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); -- cgit v1.2.3-1-g7c22 From f21ab91184cf5b6a97bc5ae62c49ac8ac68a8b05 Mon Sep 17 00:00:00 2001 From: erihel Date: Sun, 2 Feb 2014 02:01:42 +0100 Subject: Changed max channels to fix compilation error --- src/sound/oalsound/alsound.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/sound/oalsound/alsound.cpp') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 9ef9341..fab0445 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -20,7 +20,6 @@ #include #include -#include #include @@ -381,7 +380,7 @@ int ALSound::Play(Sound sound, const Math::Vector &pos, float amplitude, float f if (!m_channels[channel]->Play()) { - m_channels_limit = std::min(m_channels.size() - 1, m_channels_limit - 1); + m_channels_limit = m_channels.size() - 1; GetLogger()->Debug("Changing channel limit to %u.\n", m_channels_limit); auto it = m_channels.find(channel); Channel *ch = it->second; -- cgit v1.2.3-1-g7c22 From 2433cb595acacf8433405fc9e446757280bd3108 Mon Sep 17 00:00:00 2001 From: erihel Date: Fri, 21 Feb 2014 10:13:48 +0100 Subject: Added a check when trying to play a music file --- src/sound/oalsound/alsound.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/sound/oalsound/alsound.cpp') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index fab0445..ce109e4 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -346,7 +346,7 @@ int ALSound::Play(Sound sound, const Math::Vector &pos, float amplitude, float f } if (m_sounds.find(sound) == m_sounds.end()) { - GetLogger()->Warn("Sound %d was not loaded!\n", sound); + GetLogger()->Debug("Sound %d was not loaded!\n", sound); return -1; } @@ -638,21 +638,23 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTim std::stringstream file; file << m_soundPath << "/" << filename; - Buffer *buffer; // check if we have music in cache if (m_music.find(filename) == m_music.end()) { - GetLogger()->Warn("Music %s was not cached!\n", filename.c_str()); + GetLogger()->Debug("Music %s was not cached!\n", filename.c_str()); if (!boost::filesystem::exists(file.str())) { - GetLogger()->Warn("Requested music %s was not found.\n", filename.c_str()); + GetLogger()->Debug("Requested music %s was not found.\n", filename.c_str()); return false; } - + buffer = new Buffer(); - buffer->LoadFromFile(file.str(), static_cast(-1)); + if (!buffer->LoadFromFile(file.str(), static_cast(-1))) + { + return false; + } m_music[filename] = buffer; } else -- cgit v1.2.3-1-g7c22 From bb2a9bcfd3f63434c4bfb01a31f2ab0d64d6260b Mon Sep 17 00:00:00 2001 From: krzys-h Date: Fri, 21 Feb 2014 14:19:58 +0100 Subject: Added option to set pause audio in Scene --- src/sound/oalsound/alsound.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/sound/oalsound/alsound.cpp') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index ce109e4..ad03970 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -682,7 +682,7 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTim } -bool ALSound::PlayPauseMusic(const std::string &filename) +bool ALSound::PlayPauseMusic(const std::string &filename, bool repeat) { if (m_previousMusic.fadeTime > 0.0f) { @@ -703,7 +703,7 @@ bool ALSound::PlayPauseMusic(const std::string &filename) m_currentMusic = nullptr; } } - return PlayMusic(filename, true); + return PlayMusic(filename, repeat); } -- cgit v1.2.3-1-g7c22 From 1a0dcedf83d83e02403ebe79469c70c9fb8f35fb Mon Sep 17 00:00:00 2001 From: andreymal Date: Sun, 2 Mar 2014 18:00:02 +0400 Subject: fixed m_oldMusic.push_back(nullptr) and segfault --- src/sound/oalsound/alsound.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/sound/oalsound/alsound.cpp') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index ad03970..23476d4 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -514,7 +514,6 @@ void ALSound::FrameMove(float delta) { continue; } - if (it.second->IsMuted()) { it.second->SetVolume(0.0f); @@ -686,12 +685,15 @@ bool ALSound::PlayPauseMusic(const std::string &filename, bool repeat) { 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; + if(m_currentMusic) + { + OldMusic old; + old.music = m_currentMusic; + old.fadeTime = 2.0f; + old.currentTime = 0.0f; + m_oldMusic.push_back(old); + m_currentMusic = nullptr; + } } else { -- cgit v1.2.3-1-g7c22 From f0d97bfdb91a2c0a17d1697b145d4df930280dbb Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sun, 18 May 2014 12:12:47 +0200 Subject: Better datadir mod support --- src/sound/oalsound/alsound.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/sound/oalsound/alsound.cpp') diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index 23476d4..8afbdd2 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -18,6 +18,8 @@ #include "sound/oalsound/alsound.h" +#include "app/gamedata.h" + #include #include @@ -163,7 +165,7 @@ int ALSound::GetMusicVolume() bool ALSound::Cache(Sound sound, const std::string &filename) { Buffer *buffer = new Buffer(); - if (buffer->LoadFromFile(filename, sound)) + if (buffer->LoadFromFile(CGameData::GetInstancePointer()->GetFilePath(DIR_SOUND, filename), sound)) { m_sounds[sound] = buffer; return true; @@ -176,9 +178,7 @@ bool ALSound::CacheMusic(const std::string &filename) if (m_music.find(filename) == m_music.end()) { Buffer *buffer = new Buffer(); - std::stringstream file; - file << m_soundPath << "/" << filename; - if (buffer->LoadFromFile(file.str(), static_cast(-1))) + if (buffer->LoadFromFile(CGameData::GetInstancePointer()->GetFilePath(DIR_MUSIC, filename), static_cast(-1))) { m_music[filename] = buffer; return true; @@ -635,22 +635,21 @@ bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTim return false; } - std::stringstream file; - file << m_soundPath << "/" << filename; + std::string file = CGameData::GetInstancePointer()->GetFilePath(DIR_MUSIC, filename); Buffer *buffer; // check if we have music in cache if (m_music.find(filename) == m_music.end()) { GetLogger()->Debug("Music %s was not cached!\n", filename.c_str()); - if (!boost::filesystem::exists(file.str())) + if (!boost::filesystem::exists(file)) { GetLogger()->Debug("Requested music %s was not found.\n", filename.c_str()); return false; } buffer = new Buffer(); - if (!buffer->LoadFromFile(file.str(), static_cast(-1))) + if (!buffer->LoadFromFile(file, static_cast(-1))) { return false; } -- cgit v1.2.3-1-g7c22