* Fixed pitch calculation (sound in cut scenes will work as well as robot tracks sound)
* Fixed applying effects to sounds * Changed volume to range 0.0-1.0 except for values in UIdev-ui
parent
5a6b3f005a
commit
983373f150
|
@ -20,14 +20,13 @@
|
|||
|
||||
#include "alsound.h"
|
||||
|
||||
|
||||
#define MIN(a, b) (a > b ? b : a)
|
||||
|
||||
ALSound::ALSound()
|
||||
{
|
||||
mEnabled = false;
|
||||
m3D = false;
|
||||
mAudioVolume = MAXVOLUME;
|
||||
mAudioVolume = 1.0f;
|
||||
mMute = false;
|
||||
auto pointer = CInstanceManager::GetInstancePointer();
|
||||
if (pointer != nullptr)
|
||||
|
@ -105,7 +104,7 @@ bool ALSound::GetSound3DCap()
|
|||
}
|
||||
|
||||
|
||||
bool ALSound::RetEnable()
|
||||
bool ALSound::GetEnable()
|
||||
{
|
||||
return mEnabled;
|
||||
}
|
||||
|
@ -113,8 +112,8 @@ bool ALSound::RetEnable()
|
|||
|
||||
void ALSound::SetAudioVolume(int volume)
|
||||
{
|
||||
alListenerf(AL_GAIN, MIN(volume, MAXVOLUME) * 0.01f);
|
||||
mAudioVolume = MIN(volume, MAXVOLUME);
|
||||
alListenerf(AL_GAIN, MIN(static_cast<float>(volume) / MAXVOLUME, 1.0f));
|
||||
mAudioVolume = MIN(static_cast<float>(volume) / MAXVOLUME, 1.0f);
|
||||
}
|
||||
|
||||
|
||||
|
@ -291,18 +290,14 @@ 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;
|
||||
|
@ -312,7 +307,7 @@ int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequenc
|
|||
bAlreadyLoaded = false;
|
||||
if (!bAlreadyLoaded) {
|
||||
if (!mChannels[channel]->SetBuffer(mSounds[sound])) {
|
||||
GetLogger()->Trace("ALSound::Play SetBuffer failed\n");
|
||||
mChannels[channel]->SetBuffer(nullptr);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
@ -320,12 +315,13 @@ int ALSound::Play(Sound sound, Math::Vector pos, float amplitude, float frequenc
|
|||
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]->GetFrequency());
|
||||
mChannels[channel]->SetVolume(amplitude);
|
||||
mChannels[channel]->SetLoop(bLoop);
|
||||
mChannels[channel]->Play();
|
||||
return channel;
|
||||
}
|
||||
|
@ -350,15 +346,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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -438,7 +435,6 @@ bool ALSound::MuteAll(bool bMute)
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
void ALSound::FrameMove(float delta)
|
||||
{
|
||||
if (!mEnabled)
|
||||
|
@ -447,36 +443,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);
|
||||
|
||||
// setting frequency
|
||||
frequency = progress * abs(oper.finalFrequency - it.second->GetStartFrequency()) * it.second->GetStartFrequency() * it.second->GetChangeFrequency();
|
||||
volume = progress * (oper.finalAmplitude - it.second->GetStartAmplitude());
|
||||
it.second->SetVolume(volume + it.second->GetStartAmplitude());
|
||||
|
||||
// setting frequency
|
||||
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);
|
||||
it.second->PopEnvelope();
|
||||
it.second->SetStartAmplitude(oper.finalAmplitude);
|
||||
it.second->SetStartFrequency(oper.finalFrequency);
|
||||
if (oper.nextOper == SOPER_STOP) {
|
||||
it.second->Stop();
|
||||
}
|
||||
|
||||
it.second->PopEnvelope();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,7 +42,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,7 +86,7 @@ class ALSound : public CSoundInterface
|
|||
bool mEnabled;
|
||||
bool m3D;
|
||||
bool mMute;
|
||||
int mAudioVolume;
|
||||
float mAudioVolume;
|
||||
ALCdevice* audioDevice;
|
||||
ALCcontext* audioContext;
|
||||
std::map<Sound, Buffer*> mSounds;
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
|
||||
Buffer::Buffer() {
|
||||
mLoaded = false;
|
||||
mDuration = 0;
|
||||
mDuration = 0.0f;
|
||||
}
|
||||
|
||||
|
||||
|
@ -53,7 +53,7 @@ bool Buffer::LoadFromFile(std::string filename, Sound sound) {
|
|||
alGetBufferi(mBuffer, AL_CHANNELS, &channels);
|
||||
alGetBufferi(mBuffer, AL_FREQUENCY, &freq);
|
||||
|
||||
mDuration = static_cast<ALfloat>(size) / channels / bits / 8 / static_cast<ALfloat>(freq);
|
||||
mDuration = static_cast<ALfloat>(size) * 8 / channels / bits / static_cast<ALfloat>(freq);
|
||||
|
||||
mLoaded = true;
|
||||
return true;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
#include "channel.h"
|
||||
|
||||
#define MIN(a, b) (a > b ? b : a)
|
||||
|
||||
Channel::Channel() {
|
||||
alGenSources(1, &mSource);
|
||||
|
@ -31,6 +32,8 @@ Channel::Channel() {
|
|||
|
||||
mPriority = 0;
|
||||
mBuffer = nullptr;
|
||||
mLoop = false;
|
||||
mInitFrequency = 0.0f;
|
||||
}
|
||||
|
||||
|
||||
|
@ -49,6 +52,7 @@ bool Channel::Play() {
|
|||
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());
|
||||
|
@ -83,6 +87,15 @@ bool Channel::SetFrequency(float freq)
|
|||
}
|
||||
|
||||
|
||||
bool Channel::AdjustFrequency(float freq)
|
||||
{
|
||||
if (!mReady || mBuffer == nullptr)
|
||||
return false;
|
||||
|
||||
return SetFrequency(mInitFrequency - freq);
|
||||
}
|
||||
|
||||
|
||||
float Channel::GetFrequency()
|
||||
{
|
||||
ALfloat freq;
|
||||
|
@ -104,7 +117,7 @@ bool Channel::SetVolume(float vol)
|
|||
if (!mReady || vol < 0 || mBuffer == nullptr)
|
||||
return false;
|
||||
|
||||
alSourcef(mSource, AL_GAIN, vol / MAXVOLUME);
|
||||
alSourcef(mSource, AL_GAIN, MIN(vol, 1.0f));
|
||||
if (alCheck()) {
|
||||
GetLogger()->Warn("Could not set sound volume to '%f'. Code: %d\n", vol, alGetCode());
|
||||
return false;
|
||||
|
@ -125,7 +138,7 @@ float Channel::GetVolume()
|
|||
return 0;
|
||||
}
|
||||
|
||||
return vol * MAXVOLUME;
|
||||
return vol;
|
||||
}
|
||||
|
||||
|
||||
|
@ -144,6 +157,7 @@ void Channel::SetPriority(int pri)
|
|||
void Channel::SetStartAmplitude(float gain)
|
||||
{
|
||||
mStartAmplitude = gain;
|
||||
SetVolume(mStartAmplitude);
|
||||
}
|
||||
|
||||
|
||||
|
@ -159,12 +173,6 @@ void Channel::SetChangeFrequency(float freq)
|
|||
}
|
||||
|
||||
|
||||
void Channel::SetInitFrequency(float freq)
|
||||
{
|
||||
mInitFrequency = freq;
|
||||
}
|
||||
|
||||
|
||||
float Channel::GetStartAmplitude()
|
||||
{
|
||||
return mStartAmplitude;
|
||||
|
@ -213,8 +221,12 @@ bool Channel::SetBuffer(Buffer *buffer) {
|
|||
if (!mReady)
|
||||
return false;
|
||||
|
||||
assert(buffer);
|
||||
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());
|
||||
|
@ -225,16 +237,6 @@ bool Channel::SetBuffer(Buffer *buffer) {
|
|||
}
|
||||
|
||||
|
||||
void Channel::AdjustFrequency(float freq) {
|
||||
SetFrequency(freq * mInitFrequency);
|
||||
}
|
||||
|
||||
|
||||
void Channel::AdjustVolume(float volume) {
|
||||
SetVolume(mStartAmplitude * volume);
|
||||
}
|
||||
|
||||
|
||||
bool Channel::IsPlaying() {
|
||||
ALint status;
|
||||
if (!mReady || mBuffer == nullptr)
|
||||
|
@ -323,3 +325,8 @@ void Channel::PopEnvelope()
|
|||
{
|
||||
mOper.pop_front();
|
||||
}
|
||||
|
||||
|
||||
void Channel::SetLoop(bool loop) {
|
||||
mLoop = loop;
|
||||
}
|
|
@ -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);
|
||||
|
@ -73,7 +75,6 @@ class Channel
|
|||
void SetStartAmplitude(float);
|
||||
void SetStartFrequency(float);
|
||||
void SetChangeFrequency(float);
|
||||
void SetInitFrequency(float);
|
||||
|
||||
float GetStartAmplitude();
|
||||
float GetStartFrequency();
|
||||
|
@ -83,8 +84,7 @@ class Channel
|
|||
void AddOper(SoundOper);
|
||||
void ResetOper();
|
||||
Sound GetSoundType();
|
||||
void AdjustFrequency(float);
|
||||
void AdjustVolume(float);
|
||||
void SetLoop(bool);
|
||||
|
||||
private:
|
||||
Buffer *mBuffer;
|
||||
|
@ -97,4 +97,5 @@ class Channel
|
|||
float mInitFrequency;
|
||||
std::deque<SoundOper> mOper;
|
||||
bool mReady;
|
||||
bool mLoop;
|
||||
};
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
/*!
|
||||
* Maximum possible audio volume
|
||||
*/
|
||||
#define MAXVOLUME 100
|
||||
#define MAXVOLUME 100.0f
|
||||
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue