diff options
author | Piotr Dziwinski <piotrdz@gmail.com> | 2013-03-30 15:01:00 +0100 |
---|---|---|
committer | Piotr Dziwinski <piotrdz@gmail.com> | 2013-03-30 15:23:05 +0100 |
commit | e218dcfdf2c58f8841e7ebd220527d08e870a6d5 (patch) | |
tree | a567db04a8a51e2db87f80e259faf222a7d4becd /src/sound | |
parent | bfc701e286259d9a7aa2ff8240704eab2ba3a237 (diff) | |
parent | e607fed265070be8c518d4302d8b76f738421fb3 (diff) | |
download | colobot-e218dcfdf2c58f8841e7ebd220527d08e870a6d5.tar.gz colobot-e218dcfdf2c58f8841e7ebd220527d08e870a6d5.tar.bz2 colobot-e218dcfdf2c58f8841e7ebd220527d08e870a6d5.zip |
Merge branch 'dev'
Merging content from dev
Diffstat (limited to 'src/sound')
-rw-r--r-- | src/sound/oalsound/alsound.cpp | 200 | ||||
-rw-r--r-- | src/sound/oalsound/alsound.h | 14 | ||||
-rw-r--r-- | src/sound/oalsound/buffer.cpp | 44 | ||||
-rw-r--r-- | src/sound/oalsound/buffer.h | 5 | ||||
-rw-r--r-- | src/sound/oalsound/channel.cpp | 97 | ||||
-rw-r--r-- | src/sound/oalsound/channel.h | 10 | ||||
-rw-r--r-- | src/sound/sound.h | 26 |
7 files changed, 274 insertions, 122 deletions
diff --git a/src/sound/oalsound/alsound.cpp b/src/sound/oalsound/alsound.cpp index f683a62..8c1cb81 100644 --- a/src/sound/oalsound/alsound.cpp +++ b/src/sound/oalsound/alsound.cpp @@ -15,31 +15,24 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/. -// alsound.cpp - #include "alsound.h" - #define MIN(a, b) (a > b ? b : a) ALSound::ALSound() { mEnabled = false; m3D = false; - mAudioVolume = MAXVOLUME; + mAudioVolume = 1.0f; + mMusicVolume = 1.0f; mMute = false; - auto pointer = CInstanceManager::GetInstancePointer(); - if (pointer != nullptr) - CInstanceManager::GetInstancePointer()->AddInstance(CLASS_SOUND, this); + mCurrentMusic = nullptr; } ALSound::~ALSound() { - auto pointer = CInstanceManager::GetInstancePointer(); - if (pointer != nullptr) - CInstanceManager::GetInstancePointer()->DeleteInstance(CLASS_SOUND, this); CleanUp(); } @@ -50,11 +43,20 @@ void ALSound::CleanUp() GetLogger()->Info("Unloading files and closing device...\n"); StopAll(); - for (auto item : mSounds) + for (auto channel : mChannels) { + delete channel.second; + } + + for (auto item : mSounds) { delete item.second; + } mEnabled = false; - alutExit(); + + mCurrentMusic->FreeBuffer(); + delete mCurrentMusic; + alcDestroyContext(mContext); + alcCloseDevice(mDevice); } } @@ -67,13 +69,21 @@ bool ALSound::Create(bool b3D) return true; GetLogger()->Info("Opening audio device...\n"); - if (!alutInit(NULL, NULL)) { - ALenum error = alutGetError(); - GetLogger()->Error("Could not open audio device! Reason: %s\n", alutGetErrorString(error)); + mDevice = alcOpenDevice(NULL); + if (!mDevice) { + GetLogger()->Error("Could not open audio device!\n"); return false; } - GetLogger()->Info("Done.\n"); + mContext = alcCreateContext(mDevice, NULL); + if (!mContext) { + GetLogger()->Error("Could not create audio context!\n"); + return false; + } + alcMakeContextCurrent(mContext); + + mCurrentMusic = new Channel(); + GetLogger()->Info("Done.\n"); mEnabled = true; return true; } @@ -100,7 +110,7 @@ bool ALSound::GetSound3DCap() } -bool ALSound::RetEnable() +bool ALSound::GetEnable() { return mEnabled; } @@ -108,35 +118,35 @@ bool ALSound::RetEnable() void ALSound::SetAudioVolume(int volume) { - alListenerf(AL_GAIN, MIN(volume, MAXVOLUME) * 0.01f); - mAudioVolume = MIN(volume, MAXVOLUME); + mAudioVolume = MIN(static_cast<float>(volume) / MAXVOLUME, 1.0f); + alListenerf(AL_GAIN, mAudioVolume); } int ALSound::GetAudioVolume() { - float volume; if ( !mEnabled ) return 0; - alGetListenerf(AL_GAIN, &volume); - return volume * MAXVOLUME; + return mAudioVolume * MAXVOLUME; } void ALSound::SetMusicVolume(int volume) { - // TODO stub! Add music support + mMusicVolume = MIN(static_cast<float>(volume) / MAXVOLUME, 1.0f); + if (mCurrentMusic) { + mCurrentMusic->SetVolume(mMusicVolume * mAudioVolume); + } } int ALSound::GetMusicVolume() { - // TODO stub! Add music support if ( !mEnabled ) - return 0; + return 0.0f; - return 0; + return mMusicVolume * MAXVOLUME; } @@ -213,7 +223,7 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) it.second->SetPriority(priority); channel = it.first; - bAlreadyLoaded = true; + bAlreadyLoaded = it.second->IsLoaded(); return true; } @@ -238,8 +248,7 @@ bool ALSound::SearchFreeBuffer(Sound sound, int &channel, bool &bAlreadyLoaded) auto it = mChannels.end(); it--; int i = (*it).first; - while (++i) - { + while (++i) { if (mChannels.find(i) == mChannels.end()) { Channel *chn = new Channel(); // check if channel is ready to play music, if not destroy it and seek free one @@ -286,37 +295,38 @@ 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) { - if (!mEnabled) - return -1; - - if (mAudioVolume <= 0.0f) + if (!mEnabled) { return -1; + } if (mSounds.find(sound) == mSounds.end()) { GetLogger()->Warn("Sound %d was not loaded!\n", sound); return -1; } - - GetLogger()->Trace("ALSound::Play sound: %d volume: %f frequency: %f\n", sound, amplitude, frequency); int channel; - bool bAlreadyLoaded; + bool bAlreadyLoaded = false; if (!SearchFreeBuffer(sound, channel, bAlreadyLoaded)) return -1; - if ( !bAlreadyLoaded ) { - mChannels[channel]->SetBuffer(mSounds[sound]); - } + if (!bAlreadyLoaded) { + if (!mChannels[channel]->SetBuffer(mSounds[sound])) { + mChannels[channel]->SetBuffer(nullptr); + return -1; + } + } Position(channel, pos); // setting initial values - mChannels[channel]->SetStartAmplitude(mAudioVolume); + mChannels[channel]->SetStartAmplitude(amplitude); mChannels[channel]->SetStartFrequency(frequency); mChannels[channel]->SetChangeFrequency(1.0f); mChannels[channel]->ResetOper(); - mChannels[channel]->AdjustFrequency(frequency); - mChannels[channel]->AdjustVolume(amplitude * mAudioVolume); + mChannels[channel]->SetFrequency(frequency); + mChannels[channel]->SetVolume(amplitude * mAudioVolume); + mChannels[channel]->SetLoop(bLoop); mChannels[channel]->Play(); + return channel; } @@ -340,15 +350,16 @@ bool ALSound::AddEnvelope(int channel, float amplitude, float frequency, float t if (mChannels.find(channel) == mChannels.end()) { return false; } - + SoundOper op; op.finalAmplitude = amplitude; op.finalFrequency = frequency; op.totalTime = time; op.nextOper = oper; + op.currentTime = 0.0f; mChannels[channel]->AddOper(op); - return false; + return true; } @@ -375,7 +386,8 @@ bool ALSound::Frequency(int channel, float frequency) return false; } - mChannels[channel]->SetFrequency(frequency); + mChannels[channel]->SetFrequency(frequency * mChannels[channel]->GetInitFrequency()); + mChannels[channel]->SetChangeFrequency(frequency); return true; } @@ -422,13 +434,12 @@ bool ALSound::MuteAll(bool bMute) volume = mAudioVolume; for (auto channel : mChannels) { - channel.second->SetVolume(volume); + channel.second->SetVolume(volume * mAudioVolume); } return true; } - void ALSound::FrameMove(float delta) { if (!mEnabled) @@ -437,35 +448,38 @@ void ALSound::FrameMove(float delta) float progress; float volume, frequency; for (auto it : mChannels) { - if (!it.second->IsPlaying()) + if (!it.second->IsPlaying()) { continue; + } if (!it.second->HasEnvelope()) continue; - //it.second->GetEnvelope().currentTime += delta; - SoundOper oper = it.second->GetEnvelope(); - progress = it.second->GetCurrentTime() / oper.totalTime; + SoundOper &oper = it.second->GetEnvelope(); + oper.currentTime += delta; + progress = oper.currentTime / oper.totalTime; progress = MIN(progress, 1.0f); - + // setting volume - volume = progress * abs(oper.finalAmplitude - it.second->GetStartAmplitude()); - it.second->AdjustVolume(volume * mAudioVolume); + volume = progress * (oper.finalAmplitude - it.second->GetStartAmplitude()); + volume = (volume + it.second->GetStartAmplitude()) * mAudioVolume; + it.second->SetVolume(volume); // setting frequency - frequency = progress * abs(oper.finalFrequency - it.second->GetStartFrequency()) * it.second->GetStartFrequency() * it.second->GetChangeFrequency(); + frequency = progress * (oper.finalFrequency - it.second->GetStartFrequency()) * it.second->GetStartFrequency() * it.second->GetChangeFrequency() * it.second->GetInitFrequency(); it.second->AdjustFrequency(frequency); - if (it.second->GetEnvelope().totalTime <= it.second->GetCurrentTime()) { - + if (oper.totalTime <= oper.currentTime) { if (oper.nextOper == SOPER_LOOP) { - GetLogger()->Trace("ALSound::FrameMove oper: replay.\n"); - it.second->SetCurrentTime(0.0f); + oper.currentTime = 0.0f; it.second->Play(); } else { - GetLogger()->Trace("ALSound::FrameMove oper: next.\n"); it.second->SetStartAmplitude(oper.finalAmplitude); it.second->SetStartFrequency(oper.finalFrequency); + if (oper.nextOper == SOPER_STOP) { + it.second->Stop(); + } + it.second->PopEnvelope(); } } @@ -483,32 +497,86 @@ void ALSound::SetListener(Math::Vector eye, Math::Vector lookat) bool ALSound::PlayMusic(int rank, bool bRepeat) { - // TODO stub! Add music support + if (!mEnabled) { + return false; + } + + if (static_cast<int>(mCurrentMusic->GetSoundType()) != rank) { + // check if we have music in cache + for (auto music : mMusicCache) { + if (static_cast<int>(music->GetSoundType()) == rank) { + GetLogger()->Debug("Music loaded from cache\n"); + mCurrentMusic->SetBuffer(music); + + mCurrentMusic->SetVolume(mMusicVolume * mAudioVolume); + mCurrentMusic->SetLoop(bRepeat); + mCurrentMusic->Play(); + return true; + } + } + + // we cache only 3 music files + if (mMusicCache.size() == 3) { + mCurrentMusic->FreeBuffer(); + mMusicCache.pop_back(); + } + + if (mMusic.find(rank) == mMusic.end()) { + GetLogger()->Info("Requested music %d was not found.\n", rank); + return false; + } + + Buffer *buffer = new Buffer(); + mMusicCache.push_front(buffer); + buffer->LoadFromFile(mMusic.at(rank), static_cast<Sound>(rank)); + mCurrentMusic->SetBuffer(buffer); + mMusicCache[rank] = buffer; + } + + mCurrentMusic->SetVolume(mMusicVolume * mAudioVolume); + mCurrentMusic->SetLoop(bRepeat); + mCurrentMusic->Play(); + return true; } bool ALSound::RestartMusic() { - // TODO stub! Add music support + if (!mEnabled || !mCurrentMusic) { + return false; + } + + mCurrentMusic->Stop(); + mCurrentMusic->Play(); return true; } void ALSound::StopMusic() { - // TODO stub! Add music support + if (!mEnabled || !mCurrentMusic) { + return; + } + SuspendMusic(); } bool ALSound::IsPlayingMusic() { - // TODO stub! Add music support - return true; + if (!mEnabled || !mCurrentMusic) { + return false; + } + + return mCurrentMusic->IsPlaying(); } void ALSound::SuspendMusic() { - // TODO stub! Add music support + if (!mEnabled || !mCurrentMusic) { + return; + } + + mCurrentMusic->Stop(); } diff --git a/src/sound/oalsound/alsound.h b/src/sound/oalsound/alsound.h index 7d24ba6..bdf06b1 100644 --- a/src/sound/oalsound/alsound.h +++ b/src/sound/oalsound/alsound.h @@ -22,9 +22,8 @@ #include <map> #include <string> -#include <AL/alut.h> +#include <AL/al.h> -#include "common/iman.h" #include "common/logger.h" #include "sound/sound.h" @@ -42,7 +41,7 @@ class ALSound : public CSoundInterface bool Create(bool b3D); bool Cache(Sound, std::string); - bool RetEnable(); + bool GetEnable(); void SetSound3D(bool bMode); bool GetSound3D(); @@ -86,9 +85,12 @@ class ALSound : public CSoundInterface bool mEnabled; bool m3D; bool mMute; - int mAudioVolume; - ALCdevice* audioDevice; - ALCcontext* audioContext; + float mAudioVolume; + float mMusicVolume; + ALCdevice* mDevice; + ALCcontext* mContext; std::map<Sound, Buffer*> mSounds; std::map<int, Channel*> mChannels; + std::deque<Buffer*> mMusicCache; + Channel *mCurrentMusic; }; diff --git a/src/sound/oalsound/buffer.cpp b/src/sound/oalsound/buffer.cpp index dbfdca2..edc3d74 100644 --- a/src/sound/oalsound/buffer.cpp +++ b/src/sound/oalsound/buffer.cpp @@ -14,13 +14,12 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/. -// buffer.cpp #include "buffer.h" Buffer::Buffer() { mLoaded = false; - mDuration = 0; + mDuration = 0.0f; } @@ -35,26 +34,43 @@ Buffer::~Buffer() { bool Buffer::LoadFromFile(std::string filename, Sound sound) { mSound = sound; - GetLogger()->Debug("Loading audio file: %s\n", filename.c_str()); - mBuffer = alutCreateBufferFromFile(filename.c_str()); - ALenum error = alutGetError(); - if (error) { - GetLogger()->Warn("Failed to load file. Reason: %s\n", alutGetErrorString(error)); + SF_INFO fileInfo; + SNDFILE *file = sf_open(filename.c_str(), SFM_READ, &fileInfo); + + GetLogger()->Trace(" channels %d\n", fileInfo.channels); + GetLogger()->Trace(" format %d\n", fileInfo.format); + GetLogger()->Trace(" frames %d\n", fileInfo.frames); + GetLogger()->Trace(" samplerate %d\n", fileInfo.samplerate); + GetLogger()->Trace(" sections %d\n", fileInfo.sections); + + if (!file) { + GetLogger()->Warn("Could not load file. Reason: %s\n", sf_strerror(file)); mLoaded = false; return false; } - ALint size, bits, channels, freq; - - alGetBufferi(mBuffer, AL_SIZE, &size); - alGetBufferi(mBuffer, AL_BITS, &bits); - alGetBufferi(mBuffer, AL_CHANNELS, &channels); - alGetBufferi(mBuffer, AL_FREQUENCY, &freq); + alGenBuffers(1, &mBuffer); + if (!mBuffer) { + GetLogger()->Warn("Could not create audio buffer\n"); + mLoaded = false; + sf_close(file); + return false; + } - mDuration = static_cast<ALfloat>(size) / channels / bits / 8 / static_cast<ALfloat>(freq); + // read chunks of 4096 samples + std::vector<uint16_t> data; + std::array<int16_t, 4096> buffer; + data.reserve(fileInfo.frames); + size_t read = 0; + while ((read = sf_read_short(file, buffer.data(), buffer.size())) != 0) { + data.insert(data.end(), buffer.begin(), buffer.begin() + read); + } + sf_close(file); + alBufferData(mBuffer, fileInfo.channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, &data.front(), data.size() * sizeof(uint16_t), fileInfo.samplerate); + mDuration = static_cast<float>(fileInfo.frames) / fileInfo.samplerate; mLoaded = true; return true; } diff --git a/src/sound/oalsound/buffer.h b/src/sound/oalsound/buffer.h index 8c4a2d3..7286deb 100644 --- a/src/sound/oalsound/buffer.h +++ b/src/sound/oalsound/buffer.h @@ -19,8 +19,11 @@ #pragma once #include <string> +#include <vector> +#include <array> -#include <AL/alut.h> +#include <AL/al.h> +#include <sndfile.h> #include "sound/sound.h" #include "common/logger.h" diff --git a/src/sound/oalsound/channel.cpp b/src/sound/oalsound/channel.cpp index 7d8244b..19394c6 100644 --- a/src/sound/oalsound/channel.cpp +++ b/src/sound/oalsound/channel.cpp @@ -14,10 +14,10 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/. -// channel.cpp #include "channel.h" +#define MIN(a, b) (a > b ? b : a) Channel::Channel() { alGenSources(1, &mSource); @@ -31,11 +31,17 @@ Channel::Channel() { mPriority = 0; mBuffer = nullptr; + mLoop = false; + mInitFrequency = 0.0f; + mStartAmplitude = 0.0f; + mStartFrequency = 0.0f; + mChangeFrequency = 0.0f; } Channel::~Channel() { if (mReady) { + alSourceStop(mSource); alSourcei(mSource, AL_BUFFER, 0); alDeleteSources(1, &mSource); if (alCheck()) @@ -45,9 +51,10 @@ Channel::~Channel() { bool Channel::Play() { - if (!mReady) + if (!mReady || mBuffer == nullptr) return false; - + + alSourcei(mSource, AL_LOOPING, static_cast<ALint>(mLoop)); alSourcePlay(mSource); if (alCheck()) GetLogger()->Warn("Could not play audio sound source. Code: %d\n", alGetCode()); @@ -56,7 +63,7 @@ bool Channel::Play() { bool Channel::SetPosition(Math::Vector pos) { - if (!mReady) + if (!mReady || mBuffer == nullptr) return false; alSource3f(mSource, AL_POSITION, pos.x, pos.y, pos.z); @@ -70,7 +77,7 @@ bool Channel::SetPosition(Math::Vector pos) { bool Channel::SetFrequency(float freq) { - if (!mReady) + if (!mReady || mBuffer == nullptr) return false; alSourcef(mSource, AL_PITCH, freq); @@ -82,10 +89,19 @@ bool Channel::SetFrequency(float freq) } +bool Channel::AdjustFrequency(float freq) +{ + if (!mReady || mBuffer == nullptr) + return false; + + return SetFrequency(mInitFrequency + fabs(freq)); +} + + float Channel::GetFrequency() { ALfloat freq; - if (!mReady) + if (!mReady || mBuffer == nullptr) return 0; alGetSourcef(mSource, AL_PITCH, &freq); @@ -100,10 +116,10 @@ float Channel::GetFrequency() bool Channel::SetVolume(float vol) { - if (!mReady || vol < 0) + if (!mReady || vol < 0 || mBuffer == nullptr) return false; - alSourcef(mSource, AL_GAIN, vol / MAXVOLUME); + alSourcef(mSource, AL_GAIN, MIN(powf(vol, 0.2f), 1.0f)); if (alCheck()) { GetLogger()->Warn("Could not set sound volume to '%f'. Code: %d\n", vol, alGetCode()); return false; @@ -115,7 +131,7 @@ bool Channel::SetVolume(float vol) float Channel::GetVolume() { ALfloat vol; - if (!mReady) + if (!mReady || mBuffer == nullptr) return 0; alGetSourcef(mSource, AL_GAIN, &vol); @@ -124,7 +140,7 @@ float Channel::GetVolume() return 0; } - return vol * MAXVOLUME; + return vol; } @@ -143,6 +159,7 @@ void Channel::SetPriority(int pri) void Channel::SetStartAmplitude(float gain) { mStartAmplitude = gain; + SetVolume(mStartAmplitude); } @@ -158,12 +175,6 @@ void Channel::SetChangeFrequency(float freq) } -void Channel::SetInitFrequency(float freq) -{ - mInitFrequency = freq; -} - - float Channel::GetStartAmplitude() { return mStartAmplitude; @@ -201,6 +212,9 @@ void Channel::ResetOper() Sound Channel::GetSoundType() { + if (!mReady || mBuffer == nullptr) + return SOUND_NONE; + return mBuffer->GetSoundType(); } @@ -208,9 +222,14 @@ Sound Channel::GetSoundType() { bool Channel::SetBuffer(Buffer *buffer) { if (!mReady) return false; - - assert(buffer); + + Stop(); mBuffer = buffer; + if (buffer == nullptr) { + alSourcei(mSource, AL_BUFFER, 0); + return true; + } + alSourcei(mSource, AL_BUFFER, buffer->GetBuffer()); if (alCheck()) { GetLogger()->Warn("Could not set sound buffer. Code: %d\n", alGetCode()); @@ -221,19 +240,26 @@ bool Channel::SetBuffer(Buffer *buffer) { } -void Channel::AdjustFrequency(float freq) { - SetFrequency(freq * mInitFrequency); -} - +bool Channel::FreeBuffer() { + if (!mReady) + return false; + + if (!mBuffer) { + return false; + } -void Channel::AdjustVolume(float volume) { - SetVolume(mStartAmplitude * volume); + alSourceStop(mSource); + alSourcei(mSource, AL_BUFFER, 0); + delete mBuffer; + mBuffer = nullptr; + return true; } bool Channel::IsPlaying() { ALint status; - if (!mReady) return false; + if (!mReady || mBuffer == nullptr) + return false; alGetSourcei(mSource, AL_SOURCE_STATE, &status); if (alCheck()) { @@ -249,8 +275,15 @@ bool Channel::IsReady() { return mReady; } +bool Channel::IsLoaded() { + return mBuffer != nullptr; +} + bool Channel::Stop() { + if (!mReady || mBuffer == nullptr) + return false; + alSourceStop(mSource); if (alCheck()) { GetLogger()->Warn("Could not stop sound. Code: %d\n", alGetCode()); @@ -262,6 +295,9 @@ bool Channel::Stop() { float Channel::GetCurrentTime() { + if (!mReady || mBuffer == nullptr) + return 0.0f; + ALfloat current; alGetSourcef(mSource, AL_SEC_OFFSET, ¤t); if (alCheck()) { @@ -274,6 +310,9 @@ float Channel::GetCurrentTime() void Channel::SetCurrentTime(float current) { + if (!mReady || mBuffer == nullptr) + return; + alSourcef(mSource, AL_SEC_OFFSET, current); if (alCheck()) GetLogger()->Warn("Could not get source current play time. Code: %d\n", alGetCode()); @@ -282,6 +321,9 @@ void Channel::SetCurrentTime(float current) float Channel::GetDuration() { + if (!mReady || mBuffer == nullptr) + return 0.0f; + return mBuffer->GetDuration(); } @@ -302,3 +344,8 @@ void Channel::PopEnvelope() { mOper.pop_front(); } + + +void Channel::SetLoop(bool loop) { + mLoop = loop; +} diff --git a/src/sound/oalsound/channel.h b/src/sound/oalsound/channel.h index 165ff50..8965306 100644 --- a/src/sound/oalsound/channel.h +++ b/src/sound/oalsound/channel.h @@ -35,6 +35,7 @@ struct SoundOper float finalAmplitude; float finalFrequency; float totalTime; + float currentTime; SoundNext nextOper; }; @@ -51,6 +52,7 @@ class Channel bool SetFrequency(float); float GetFrequency(); + bool AdjustFrequency(float); float GetCurrentTime(); void SetCurrentTime(float); @@ -60,8 +62,11 @@ class Channel float GetVolume(); bool IsPlaying(); bool IsReady(); + bool IsLoaded(); bool SetBuffer(Buffer *); + bool FreeBuffer(); + bool HasEnvelope(); SoundOper& GetEnvelope(); void PopEnvelope(); @@ -72,7 +77,6 @@ class Channel void SetStartAmplitude(float); void SetStartFrequency(float); void SetChangeFrequency(float); - void SetInitFrequency(float); float GetStartAmplitude(); float GetStartFrequency(); @@ -82,8 +86,7 @@ class Channel void AddOper(SoundOper); void ResetOper(); Sound GetSoundType(); - void AdjustFrequency(float); - void AdjustVolume(float); + void SetLoop(bool); private: Buffer *mBuffer; @@ -96,4 +99,5 @@ class Channel float mInitFrequency; std::deque<SoundOper> mOper; bool mReady; + bool mLoop; }; diff --git a/src/sound/sound.h b/src/sound/sound.h index 518e2ad..70139ea 100644 --- a/src/sound/sound.h +++ b/src/sound/sound.h @@ -22,22 +22,23 @@ #pragma once +#include <boost/filesystem.hpp> #include "math/vector.h" -#include "common/iman.h" #include "common/logger.h" #include <string> #include <iostream> #include <iomanip> #include <sstream> +#include <map> /*! * Maximum possible audio volume */ -#define MAXVOLUME 100 +#define MAXVOLUME 100.0f /** @@ -47,6 +48,7 @@ **/ enum Sound { + SOUND_NONE = -1, SOUND_CLICK = 0, SOUND_BOUM = 1, SOUND_EXPLO = 2, @@ -153,11 +155,8 @@ enum SoundNext class CSoundInterface { public: - inline CSoundInterface() { - CInstanceManager::GetInstance().AddInstance(CLASS_SOUND, this); - //m_iMan->AddInstance(CLASS_SOUND, this); - }; - inline virtual ~CSoundInterface() {}; + inline CSoundInterface() {} + inline virtual ~CSoundInterface() {} /** Function to initialize sound device * \param b3D - enable support for 3D sound @@ -176,6 +175,16 @@ class CSoundInterface } }; + /** Function called to add all music files to list */ + inline void AddMusicFiles(std::string path) { + for ( int i = 1; i <= 12; i++ ) { + std::stringstream filename; + filename << path << "/music" << std::setfill('0') << std::setw(3) << i << ".ogg"; + if (boost::filesystem::exists(filename.str())) + mMusic[i] = filename.str(); + } + }; + /** Function called to cache sound effect file. * This function is called by plugin interface for each file. * \param bSound - id of a file, will be used to identify sound files @@ -327,5 +336,8 @@ class CSoundInterface * \return return true if music is playing */ inline virtual bool IsPlayingMusic() {return true;}; + + protected: + std::map<int, std::string> mMusic; }; |