summaryrefslogtreecommitdiffstats
path: root/src/sound
diff options
context:
space:
mode:
authorkrzys-h <krzys_h@interia.pl>2014-07-01 12:15:38 +0200
committerkrzys-h <krzys_h@interia.pl>2014-07-01 12:16:32 +0200
commitd9fee8b2adad613cf8c10d153cd5cd7b261b7863 (patch)
treef6b71cc2daa719c10c1ce31cf738e1c6ec934a73 /src/sound
parent0f2adf05fd2b2b38c2b84aa8d72b3fd756dcfcd4 (diff)
parent1835d2ae580525603308206f7b8e6b4552b3ca0f (diff)
downloadcolobot-d9fee8b2adad613cf8c10d153cd5cd7b261b7863.tar.gz
colobot-d9fee8b2adad613cf8c10d153cd5cd7b261b7863.tar.bz2
colobot-d9fee8b2adad613cf8c10d153cd5cd7b261b7863.zip
Release 0.1.3-alpha
Diffstat (limited to 'src/sound')
-rw-r--r--src/sound/oalsound/alsound.cpp387
-rw-r--r--src/sound/oalsound/alsound.h42
-rw-r--r--src/sound/oalsound/buffer.cpp2
-rw-r--r--src/sound/oalsound/channel.cpp58
-rw-r--r--src/sound/oalsound/channel.h9
-rw-r--r--src/sound/sound.cpp47
-rw-r--r--src/sound/sound.h62
7 files changed, 327 insertions, 280 deletions
diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp
index 992b8b2..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 <algorithm>
#include <iomanip>
@@ -26,12 +28,14 @@
ALSound::ALSound()
{
m_enabled = false;
- m_3D = false;
m_audioVolume = 1.0f;
m_musicVolume = 1.0f;
m_currentMusic = nullptr;
m_eye.LoadZero();
m_lookat.LoadZero();
+ m_previousMusic.fadeTime = 0.0f;
+ m_previousMusic.music = nullptr;
+ m_channels_limit = 2048;
}
@@ -58,6 +62,16 @@ void ALSound::CleanUp()
{
delete m_currentMusic;
}
+
+ for (auto item : m_oldMusic)
+ {
+ delete item.music;
+ }
+
+ if (m_previousMusic.music)
+ {
+ delete m_previousMusic.music;
+ }
for (auto item : m_sounds)
{
@@ -77,7 +91,7 @@ void ALSound::CleanUp()
}
-bool ALSound::Create(bool b3D)
+bool ALSound::Create()
{
CleanUp();
@@ -102,32 +116,12 @@ bool ALSound::Create(bool b3D)
alListenerf(AL_GAIN, m_audioVolume);
alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED);
- m_currentMusic = new Channel();
GetLogger()->Info("Done.\n");
m_enabled = true;
return true;
}
-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,10 +162,10 @@ 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))
+ if (buffer->LoadFromFile(CGameData::GetInstancePointer()->GetFilePath(DIR_SOUND, filename), sound))
{
m_sounds[sound] = buffer;
return true;
@@ -179,14 +173,12 @@ 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())
{
Buffer *buffer = new Buffer();
- std::stringstream file;
- file << m_soundPath << "/" << filename;
- if (buffer->LoadFromFile(file.str(), static_cast<Sound>(-1)))
+ if (buffer->LoadFromFile(CGameData::GetInstancePointer()->GetFilePath(DIR_MUSIC, filename), static_cast<Sound>(-1)))
{
m_music[filename] = buffer;
return true;
@@ -252,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;
@@ -270,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;
@@ -280,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--;
@@ -295,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");
}
}
}
@@ -313,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)
@@ -322,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;
}
@@ -337,7 +338,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)
{
@@ -345,14 +346,16 @@ int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequenc
}
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;
}
int channel;
bool bAlreadyLoaded = false;
if (!SearchFreeBuffer(sound, channel, bAlreadyLoaded))
+ {
return -1;
+ }
if (!bAlreadyLoaded)
{
@@ -363,15 +366,8 @@ 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]->SetPosition(pos);
+ m_channels[channel]->SetVolumeAtrib(1.0f);
// setting initial values
m_channels[channel]->SetStartAmplitude(amplitude);
@@ -381,15 +377,26 @@ int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequenc
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())
+ {
+ 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;
+ 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;
}
@@ -401,10 +408,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;
}
@@ -421,40 +425,21 @@ 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;
-
- if (m_channels.find(channel) == m_channels.end())
+ if (!CheckChannel(channel))
{
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;
}
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;
}
@@ -466,10 +451,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;
}
@@ -484,7 +466,9 @@ bool ALSound::Stop(int channel)
bool ALSound::StopAll()
{
if (!m_enabled)
+ {
return false;
+ }
for (auto channel : m_channels)
{
@@ -499,7 +483,9 @@ bool ALSound::StopAll()
bool ALSound::MuteAll(bool bMute)
{
if (!m_enabled)
+ {
return false;
+ }
for (auto it : m_channels)
{
@@ -509,14 +495,6 @@ bool ALSound::MuteAll(bool bMute)
}
}
- if (bMute)
- {
- m_currentMusic->SetVolume(0.0f);
- }
- else
- {
- m_currentMusic->SetVolume(m_musicVolume);
- }
return true;
}
@@ -524,7 +502,9 @@ bool ALSound::MuteAll(bool bMute)
void ALSound::FrameMove(float delta)
{
if (!m_enabled)
+ {
return;
+ }
float progress;
float volume, frequency;
@@ -534,7 +514,6 @@ void ALSound::FrameMove(float delta)
{
continue;
}
-
if (it.second->IsMuted())
{
it.second->SetVolume(0.0f);
@@ -582,79 +561,117 @@ void ALSound::FrameMove(float delta)
}
}
}
+
+ std::list<OldMusic> 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);
+ }
+ }
+
+ 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);
}
-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)
+
+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 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(std::string filename, bool bRepeat)
+
+bool ALSound::PlayMusic(const std::string &filename, bool bRepeat, float fadeTime)
{
if (!m_enabled)
{
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()->Warn("Music %s was not cached!\n", filename.c_str());
- if (!boost::filesystem::exists(file.str()))
+ GetLogger()->Debug("Music %s was not cached!\n", filename.c_str());
+ if (!boost::filesystem::exists(file))
{
- 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 *buffer = new Buffer();
- buffer->LoadFromFile(file.str(), static_cast<Sound>(-1));
- m_currentMusic->SetBuffer(buffer);
+
+ buffer = new Buffer();
+ if (!buffer->LoadFromFile(file, static_cast<Sound>(-1)))
+ {
+ return false;
+ }
+ m_music[filename] = 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();
@@ -663,6 +680,55 @@ bool ALSound::PlayMusic(std::string filename, bool bRepeat)
}
+bool ALSound::PlayPauseMusic(const std::string &filename, bool repeat)
+{
+ if (m_previousMusic.fadeTime > 0.0f)
+ {
+ 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
+ {
+ if (m_currentMusic)
+ {
+ m_previousMusic.music = m_currentMusic;
+ m_previousMusic.fadeTime = 2.0f;
+ m_previousMusic.currentTime = 0.0f;
+ m_currentMusic = nullptr;
+ }
+ }
+ return PlayMusic(filename, repeat);
+}
+
+
+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()
{
if (!m_enabled || !m_currentMusic)
@@ -675,14 +741,21 @@ 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;
}
@@ -708,64 +781,30 @@ void ALSound::SuspendMusic()
}
-void ALSound::ComputeVolumePan2D(int channel, Math::Vector &pos)
+bool ALSound::CheckChannel(int &channel)
{
- float dist, a, g;
- m_channels[channel]->SetPosition(pos);
+ int id = (channel >> 16) & 0xffff;
+ channel &= 0xffff;
- if (VectorsEqual(pos, m_eye))
+ if (!m_enabled)
{
- m_channels[channel]->SetVolumeAtrib(1.0f); // maximum volume
- m_channels[channel]->SetPan(Math::Vector()); // at the center
- return;
+ return false;
}
- 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?
+ if (m_channels.find(channel) == m_channels.end())
{
- m_channels[channel]->SetVolumeAtrib(1.0f); // maximum volume
- m_channels[channel]->SetPan(Math::Vector()); // at the center
- return;
+ return false;
}
- 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 )
+ if (m_audioVolume == 0)
{
- a += Math::PI * 2.0f;
- }
- if ( g < 0.0f )
- {
- g += Math::PI * 2.0f;
+ return false;
}
- if ( a < g )
+ if (m_channels[channel]->GetId() != id)
{
- 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;
- }
+ return false;
}
- m_channels[channel]->SetPan( Math::Vector(0.0f, 0.0f, sinf(g - a)) );
+ return true;
}
-
diff --git a/src/sound/oalsound/alsound.h b/src/sound/oalsound/alsound.h
index cd44ae9..bb9bf28 100644
--- a/src/sound/oalsound/alsound.h
+++ b/src/sound/oalsound/alsound.h
@@ -31,73 +31,75 @@
#include <map>
#include <string>
+#include <list>
#include <al.h>
+struct OldMusic {
+ Channel* music;
+ float fadeTime;
+ float currentTime;
+};
+
class ALSound : public CSoundInterface
{
public:
ALSound();
~ALSound();
- bool Create(bool b3D);
- bool Cache(Sound, std::string);
- bool CacheMusic(std::string);
+ bool Create();
+ bool Cache(Sound, const std::string &);
+ bool CacheMusic(const std::string &);
bool GetEnable();
- void SetSound3D(bool bMode);
- bool GetSound3D();
- bool GetSound3DCap();
-
void SetAudioVolume(int volume);
int GetAudioVolume();
void SetMusicVolume(int volume);
int GetMusicVolume();
- void SetListener(Math::Vector eye, Math::Vector lookat);
+ void SetListener(const Math::Vector &eye, const Math::Vector &lookat);
void FrameMove(float rTime);
int Play(Sound sound, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false);
- int Play(Sound sound, Math::Vector pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false);
+ int Play(Sound sound, const Math::Vector &pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false);
bool FlushEnvelope(int channel);
bool AddEnvelope(int channel, float amplitude, float frequency, float time, SoundNext oper);
- bool Position(int channel, Math::Vector pos);
+ bool Position(int channel, const Math::Vector &pos);
bool Frequency(int channel, float frequency);
bool Stop(int channel);
bool StopAll();
bool MuteAll(bool bMute);
- bool PlayMusic(int rank, bool bRepeat);
- bool PlayMusic(std::string filename, bool bRepeat);
+ bool PlayMusic(int rank, bool bRepeat, float fadeTime=2.0f);
+ bool PlayMusic(const std::string &filename, bool bRepeat, float fadeTime=2.0f);
bool RestartMusic();
void SuspendMusic();
- void StopMusic();
+ void StopMusic(float fadeTime=2.0f);
bool IsPlayingMusic();
+ bool PlayPauseMusic(const std::string &filename, bool repeat);
+ void StopPauseMusic();
- // plugin interface
- std::string PluginName();
- int PluginVersion();
- void InstallPlugin();
- bool UninstallPlugin(std::string &);
+ bool CheckChannel(int &channel);
private:
void CleanUp();
int GetPriority(Sound);
bool SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded);
- void ComputeVolumePan2D(int channel, Math::Vector &pos);
bool m_enabled;
- bool m_3D;
float m_audioVolume;
float m_musicVolume;
+ unsigned int m_channels_limit;
ALCdevice* m_device;
ALCcontext* m_context;
std::map<Sound, Buffer*> m_sounds;
std::map<std::string, Buffer*> m_music;
std::map<int, Channel*> m_channels;
Channel *m_currentMusic;
+ std::list<OldMusic> m_oldMusic;
+ OldMusic m_previousMusic;
Math::Vector m_eye;
Math::Vector m_lookat;
};
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 4d89df5..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,38 +70,41 @@ 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;
}
-
-bool Channel::SetPan(Math::Vector pos)
+bool Channel::Pause()
{
- if (!m_ready || m_buffer == nullptr)
+ if(!m_ready || !IsPlaying())
{
return false;
}
-
- alSource3f(m_source, AL_POSITION, pos.x, pos.y, pos.z);
+
+ alSourcePause(m_source);
if (alCheck())
{
- GetLogger()->Warn("Could not set sound position. Code: %d\n", alGetCode());
- return false;
+ GetLogger()->Debug("Could not pause audio sound source. Code: %d\n", alGetCode());
}
return true;
}
-void Channel::SetPosition(Math::Vector pos)
+bool Channel::SetPosition(const Math::Vector &pos)
{
- m_position = pos;
-}
-
+ if (!m_ready || m_buffer == nullptr)
+ {
+ return false;
+ }
-Math::Vector Channel::GetPosition()
-{
- return m_position;
+ alSource3f(m_source, AL_POSITION, pos.x, pos.y, pos.z);
+ if (alCheck())
+ {
+ GetLogger()->Debug("Could not set sound position. Code: %d\n", alGetCode());
+ return false;
+ }
+ return true;
}
@@ -114,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;
@@ -132,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;
}
@@ -150,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;
@@ -168,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;
}
@@ -431,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 2930f26..1d988c9 100644
--- a/src/sound/oalsound/channel.h
+++ b/src/sound/oalsound/channel.h
@@ -50,11 +50,10 @@ public:
~Channel();
bool Play();
+ bool Pause();
bool Stop();
- bool SetPan(Math::Vector);
- void SetPosition(Math::Vector);
- Math::Vector GetPosition();
+ bool SetPosition(const Math::Vector &);
bool SetFrequency(float);
float GetFrequency();
@@ -98,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;
diff --git a/src/sound/sound.cpp b/src/sound/sound.cpp
index d392612..9cae1fd 100644
--- a/src/sound/sound.cpp
+++ b/src/sound/sound.cpp
@@ -36,37 +36,36 @@ CSoundInterface::~CSoundInterface()
{
}
-bool CSoundInterface::Create(bool b3D)
+bool CSoundInterface::Create()
{
return true;
}
-void CSoundInterface::CacheAll(std::string path)
+void CSoundInterface::CacheAll()
{
for ( int i = 1; i < SOUND_MAX; i++ )
{
std::stringstream filename;
- filename << path << "/sound" << std::setfill('0') << std::setw(3) << i << ".wav";
+ filename << "sound" << std::setfill('0') << std::setw(3) << i << ".wav";
if ( !Cache(static_cast<Sound>(i), filename.str()) )
GetLogger()->Warn("Unable to load audio: %s\n", filename.str().c_str());
}
}
-void CSoundInterface::AddMusicFiles(std::string path)
+void CSoundInterface::AddMusicFiles()
{
- m_soundPath = path;
CacheMusic("Intro1.ogg");
CacheMusic("Intro2.ogg");
CacheMusic("music010.ogg");
CacheMusic("music011.ogg");
}
-bool CSoundInterface::Cache(Sound bSound, std::string bFile)
+bool CSoundInterface::Cache(Sound bSound, const std::string &bFile)
{
return true;
}
-bool CSoundInterface::CacheMusic(std::string bFile)
+bool CSoundInterface::CacheMusic(const std::string &bFile)
{
return true;
}
@@ -76,20 +75,6 @@ bool CSoundInterface::GetEnable()
return true;
}
-void CSoundInterface::SetSound3D(bool bMode)
-{
-}
-
-bool CSoundInterface::GetSound3D()
-{
- return true;
-}
-
-bool CSoundInterface::GetSound3DCap()
-{
- return true;
-}
-
void CSoundInterface::SetAudioVolume(int volume)
{
}
@@ -108,7 +93,7 @@ int CSoundInterface::GetMusicVolume()
return 0;
}
-void CSoundInterface::SetListener(Math::Vector eye, Math::Vector lookat)
+void CSoundInterface::SetListener(const Math::Vector &eye, const Math::Vector &lookat)
{
}
@@ -121,7 +106,7 @@ int CSoundInterface::Play(Sound sound, float amplitude, float frequency, bool bL
return 0;
}
-int CSoundInterface::Play(Sound sound, Math::Vector pos, float amplitude, float frequency, bool bLoop)
+int CSoundInterface::Play(Sound sound, const Math::Vector &pos, float amplitude, float frequency, bool bLoop)
{
return 0;
}
@@ -136,7 +121,7 @@ bool CSoundInterface::AddEnvelope(int channel, float amplitude, float frequency,
return true;
}
-bool CSoundInterface::Position(int channel, Math::Vector pos)
+bool CSoundInterface::Position(int channel, const Math::Vector &pos)
{
return true;
}
@@ -161,12 +146,12 @@ bool CSoundInterface::MuteAll(bool bMute)
return true;
}
-bool CSoundInterface::PlayMusic(int rank, bool bRepeat)
+bool CSoundInterface::PlayMusic(int rank, bool bRepeat, float fadeTime)
{
return true;
}
-bool CSoundInterface::PlayMusic(std::string filename, bool bRepeat)
+bool CSoundInterface::PlayMusic(const std::string &filename, bool bRepeat, float fadeTime)
{
return true;
}
@@ -180,7 +165,7 @@ void CSoundInterface::SuspendMusic()
{
}
-void CSoundInterface::StopMusic()
+void CSoundInterface::StopMusic(float fadeTime)
{
}
@@ -189,3 +174,11 @@ bool CSoundInterface::IsPlayingMusic()
return true;
}
+bool CSoundInterface::PlayPauseMusic(const std::string &filename, bool repeat)
+{
+ return true;
+}
+
+void CSoundInterface::StopPauseMusic()
+{
+}
diff --git a/src/sound/sound.h b/src/sound/sound.h
index 9ccc1b6..22a5ee2 100644
--- a/src/sound/sound.h
+++ b/src/sound/sound.h
@@ -153,17 +153,16 @@ public:
virtual ~CSoundInterface();
/** Function to initialize sound device
- * \param b3D - enable support for 3D sound
*/
- virtual bool Create(bool b3D);
+ virtual bool Create();
/** Function called to cache all sound effect files.
* Function calls \link CSoundInterface::Cache() \endlink for each file
*/
- void CacheAll(std::string path);
+ void CacheAll();
/** Function called to add all music files to list */
- void AddMusicFiles(std::string path);
+ void AddMusicFiles();
/** Function called to cache sound effect file.
* This function is called by plugin interface for each file.
@@ -171,35 +170,20 @@ public:
* \param bFile - file to load
* \return return true on success
*/
- virtual bool Cache(Sound bSound, std::string bFile);
+ virtual bool Cache(Sound bSound, const std::string &bFile);
/** Function called to cache music file.
* This function is called by CRobotMain for each file used in the mission.
* \param bFile - file to load
* \return return true on success
*/
- virtual bool CacheMusic(std::string bFile);
+ virtual bool CacheMusic(const std::string &bFile);
/** Return if plugin is enabled
* \return return true if plugin is enabled
*/
virtual bool GetEnable();
- /** Change sound mode to 2D/3D
- * \param bMode - true to enable 3D sound
- */
- virtual void SetSound3D(bool bMode);
-
- /** Return if we use 3D sound
- * \return true if we have 3D sound enabled
- */
- virtual bool GetSound3D();
-
- /** Return if we have 3D sound capable card
- * \return true for 3D sound support
- */
- virtual bool GetSound3DCap();
-
/** Change global sound volume
* \param volume - range from 0 to MAXVOLUME
*/
@@ -224,7 +208,7 @@ public:
* \param eye - position of listener
* \param lookat - direction listener is looking at
*/
- virtual void SetListener(Math::Vector eye, Math::Vector lookat);
+ virtual void SetListener(const Math::Vector &eye, const Math::Vector &lookat);
/** Update data each frame
* \param rTime - time since last update
@@ -248,7 +232,7 @@ public:
* \param bLoop - loop sound
* \return identifier of channel that sound will be played on
*/
- virtual int Play(Sound sound, Math::Vector pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false);
+ virtual int Play(Sound sound, const Math::Vector &pos, float amplitude=1.0f, float frequency=1.0f, bool bLoop = false);
/** Remove all operations that would be made on sound in channel.
* \param channel - channel to work on
@@ -271,7 +255,7 @@ public:
* \param pos - new positino of a sound
* \return return true on success
*/
- virtual bool Position(int channel, Math::Vector pos);
+ virtual bool Position(int channel, const Math::Vector &pos);
/** Set sound frequency
* \param channel - channel to work on
@@ -300,38 +284,48 @@ public:
/** Start playing music
* \param rank - track number
* \param bRepeat - repeat playing
+ * \param fadeTime - time of transition between music
* \return return true on success
*/
- virtual bool PlayMusic(int rank, bool bRepeat);
+ virtual bool PlayMusic(int rank, bool bRepeat, float fadeTime=2.0f);
/** Start playing music
* \param filename - name of file to play
* \param bRepeat - repeat playing
+ * \param fadeTime - time of transition between music
* \return return true on success
*/
- virtual bool PlayMusic(std::string filename, bool bRepeat);
+ virtual bool PlayMusic(const std::string &filename, bool bRepeat, float fadeTime=2.0f);
/** Restart music
- * @return return true on success
+ * \return return true on success
*/
virtual bool RestartMusic();
- /** Susspend paying music
- * \return return true on success
+ /** Susspend playing music
+ * \return nothing
*/
virtual void SuspendMusic();
/** Stop playing music
- * \return return true on success
+ * \return nothing
*/
- virtual void StopMusic();
+ virtual void StopMusic(float fadeTime=2.0f);
/** Check if music if playing
* \return return true if music is playing
*/
virtual bool IsPlayingMusic();
-
-protected:
- std::string m_soundPath;
+
+ /** Start playing pause music
+ * \param filename - name of file to play
+ * \return return true on success
+ */
+ virtual bool PlayPauseMusic(const std::string &filename, bool repeat);
+
+ /** Stop playing pause music and return to the mission music
+ * \return nothing
+ */
+ virtual void StopPauseMusic();
};