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/oalsound/channel.cpp | |
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/oalsound/channel.cpp')
-rw-r--r-- | src/sound/oalsound/channel.cpp | 97 |
1 files changed, 72 insertions, 25 deletions
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; +} |