Add reverb

Adds reverb functionality to sound system

TODO: Make a proper sfxMixer api that will route voices to channels that have the effects slots added to them. this is just a place holder for a more complete implementation
This commit is contained in:
marauder2k7 2026-03-13 18:55:55 +00:00
parent 15a7b8cce0
commit acda0354d6
11 changed files with 830 additions and 1002 deletions

View file

@ -208,7 +208,10 @@ SFXALDevice::SFXALDevice(U32 providerIndex)
mDistanceModel(SFXDistanceModelLinear),
mDistanceFactor(1.0f),
mRolloffFactor(1.0f),
mUserRolloffFactor(1.0f)
mUserRolloffFactor(1.0f),
mHasEFX(false),
mEffect(0),
mAuxSlot(0)
{
SFXProvider* p = SFXSystem::getProvider(providerIndex);
@ -282,7 +285,7 @@ SFXALDevice::SFXALDevice(U32 providerIndex)
#undef LOAD_PROC
// generate our auxiliary slot for the effects.
// alGenAuxiliaryEffectSlots(1, &mAuxSlot);
alGenAuxiliaryEffectSlots(1, &mAuxSlot);
}
// --- Device frequency ---
@ -309,6 +312,12 @@ SFXALDevice::~SFXALDevice()
{
_releaseAllResources();
if (alIsEffect(mEffect))
{
alDeleteAuxiliaryEffectSlots(1, &mAuxSlot);
alDeleteEffects(1, &mEffect);
}
///cleanup of effects ends
alcMakeContextCurrent( NULL );
alcDestroyContext( mContext );
@ -449,6 +458,90 @@ void SFXALDevice::setDistanceModel( SFXDistanceModel model )
//-----------------------------------------------------------------------------
void SFXALDevice::setReverb(const SFXReverbProperties& r)
{
if (!(mCaps & CAPS_Reverb))
return;
ALenum err;
/* Clear error state. */
alGetError();
if (alIsEffect(mEffect))
alDeleteEffects(1, &mEffect);
/* Create the effect object and check if we can do EAX reverb. */
alGenEffects(1, &mEffect);
alEffecti(mEffect, AL_EFFECT_TYPE, AL_EFFECT_EAXREVERB);
err = alGetError();
// Map your engine's properties to EFX parameters.
// Using EAX Reverb as an example:
if (err == AL_NO_ERROR)
{
alEffectf(mEffect, AL_EAXREVERB_DENSITY, r.flDensity);
alEffectf(mEffect, AL_EAXREVERB_DIFFUSION, r.flDiffusion);
alEffectf(mEffect, AL_EAXREVERB_GAIN, r.flGain);
alEffectf(mEffect, AL_EAXREVERB_GAINHF, r.flGainHF);
alEffectf(mEffect, AL_EAXREVERB_GAINLF, r.flGainLF);
alEffectf(mEffect, AL_EAXREVERB_DECAY_TIME, r.flDecayTime);
alEffectf(mEffect, AL_EAXREVERB_DECAY_HFRATIO, r.flDecayHFRatio);
alEffectf(mEffect, AL_EAXREVERB_DECAY_LFRATIO, r.flDecayLFRatio);
alEffectf(mEffect, AL_EAXREVERB_REFLECTIONS_GAIN, r.flReflectionsGain);
alEffectf(mEffect, AL_EAXREVERB_REFLECTIONS_DELAY, r.flReflectionsDelay);
alEffectfv(mEffect, AL_EAXREVERB_REFLECTIONS_PAN, r.flReflectionsPan);
alEffectf(mEffect, AL_EAXREVERB_LATE_REVERB_GAIN, r.flLateReverbGain);
alEffectf(mEffect, AL_EAXREVERB_LATE_REVERB_DELAY, r.flLateReverbDelay);
alEffectfv(mEffect, AL_EAXREVERB_LATE_REVERB_PAN, r.flLateReverbPan);
alEffectf(mEffect, AL_EAXREVERB_ECHO_TIME, r.flEchoTime);
alEffectf(mEffect, AL_EAXREVERB_ECHO_DEPTH, r.flEchoDepth);
alEffectf(mEffect, AL_EAXREVERB_MODULATION_TIME, r.flModulationTime);
alEffectf(mEffect, AL_EAXREVERB_MODULATION_DEPTH, r.flModulationDepth);
alEffectf(mEffect, AL_EAXREVERB_AIR_ABSORPTION_GAINHF, r.flAirAbsorptionGainHF);
alEffectf(mEffect, AL_EAXREVERB_HFREFERENCE, r.flHFReference);
alEffectf(mEffect, AL_EAXREVERB_LFREFERENCE, r.flLFReference);
alEffectf(mEffect, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR, r.flRoomRolloffFactor);
alEffecti(mEffect, AL_EAXREVERB_DECAY_HFLIMIT, r.iDecayHFLimit);
}
else
{
alEffecti(mEffect, AL_EFFECT_TYPE, AL_EFFECT_REVERB);
alEffectf(mEffect, AL_REVERB_DENSITY, r.flDensity);
alEffectf(mEffect, AL_REVERB_DIFFUSION, r.flDiffusion);
alEffectf(mEffect, AL_REVERB_GAIN, r.flGain);
alEffectf(mEffect, AL_REVERB_GAINHF, r.flGainHF);
alEffectf(mEffect, AL_REVERB_DECAY_TIME, r.flDecayTime);
alEffectf(mEffect, AL_REVERB_DECAY_HFRATIO, r.flDecayHFRatio);
alEffectf(mEffect, AL_REVERB_REFLECTIONS_GAIN, r.flReflectionsGain);
alEffectf(mEffect, AL_REVERB_REFLECTIONS_DELAY, r.flReflectionsDelay);
alEffectf(mEffect, AL_REVERB_LATE_REVERB_GAIN, r.flLateReverbGain);
alEffectf(mEffect, AL_REVERB_LATE_REVERB_DELAY, r.flLateReverbDelay);
alEffectf(mEffect, AL_REVERB_AIR_ABSORPTION_GAINHF, r.flAirAbsorptionGainHF);
alEffectf(mEffect, AL_REVERB_ROOM_ROLLOFF_FACTOR, r.flRoomRolloffFactor);
alEffecti(mEffect, AL_REVERB_DECAY_HFLIMIT, r.iDecayHFLimit);
}
err = alGetError();
if (err != AL_NO_ERROR)
{
if (alIsEffect(mEffect))
alDeleteEffects(1, &mEffect);
}
else
{
// Bind updated effect to slot
alAuxiliaryEffectSloti(mAuxSlot, AL_EFFECTSLOT_EFFECT, (ALint)mEffect);
}
}
//-----------------------------------------------------------------------------
void SFXALDevice::setDopplerFactor( F32 factor )
{
alDopplerFactor( factor );

View file

@ -80,6 +80,10 @@ class SFXALDevice : public SFXDevice
F32 mDistanceFactor;
F32 mRolloffFactor;
F32 mUserRolloffFactor;
ALuint mEffect;
ALuint mAuxSlot;
bool mHasEFX;
void _setRolloffFactor( F32 factor );
@ -91,9 +95,12 @@ class SFXALDevice : public SFXDevice
void setListener( U32 index, const SFXListenerProperties& listener ) override;
void setDistanceModel( SFXDistanceModel model ) override;
void setDopplerFactor( F32 factor ) override;
void setReverb(const SFXReverbProperties& reverb) override;
void setRolloffFactor( F32 factor ) override;
void resetReverb() override {}
void setSpeedOfSound(F32 speedOfSound) override;
ALuint getDeviceAuxSlot() { return mAuxSlot; }
};
#endif // _SFXALDEVICE_H_

View file

@ -60,6 +60,13 @@ SFXALVoice* SFXALVoice::create( SFXALDevice* device, SFXALBuffer *buffer )
SFXALVoice *voice = new SFXALVoice( buffer,
sourceName );
// does our device support reverb
if (device->mCaps & SFXDevice::ECaps::CAPS_Reverb)
{
voice->mDeviceAuxSlot = (ALint)device->getDeviceAuxSlot();
alSource3i(sourceName, AL_AUXILIARY_SEND_FILTER, voice->mDeviceAuxSlot, 0, AL_FILTER_NULL);
}
return voice;
}
@ -69,7 +76,9 @@ SFXALVoice::SFXALVoice( SFXALBuffer *buffer,
: Parent( buffer ),
mSourceName( sourceName ),
mResumeAtSampleOffset( -1.0f ),
mSampleOffset( 0 )
mDeviceAuxSlot(0),
mSampleOffset( 0 ),
mUseReverb(false)
{
AL_SANITY_CHECK();
}
@ -226,6 +235,19 @@ void SFXALVoice::setTransform( const MatrixF& transform )
alSourcefv( mSourceName, AL_DIRECTION, dir );
}
void SFXALVoice::setReverb(bool useReverb)
{
if (useReverb == mUseReverb)
return;
if (!useReverb)
alSource3i(mSourceName, AL_AUXILIARY_SEND_FILTER, AL_EFFECTSLOT_NULL, 0, AL_FILTER_NULL);
else
alSource3i(mSourceName, AL_AUXILIARY_SEND_FILTER, mDeviceAuxSlot, 0, AL_FILTER_NULL);
mUseReverb = useReverb;
}
void SFXALVoice::setVolume( F32 volume )
{
AL_SANITY_CHECK();

View file

@ -54,6 +54,8 @@ class SFXALVoice : public SFXVoice
/// Buggy OAL jumps around when pausing. Save playback cursor here.
F32 mResumeAtSampleOffset;
bool mUseReverb;
/// Amount by which OAL's reported sample position is offset.
///
@ -62,6 +64,8 @@ class SFXALVoice : public SFXVoice
/// queue we are.
U32 mSampleOffset;
ALint mDeviceAuxSlot;
Mutex mMutex;
///
@ -95,6 +99,7 @@ class SFXALVoice : public SFXVoice
void play( bool looping ) override;
void setVelocity( const VectorF& velocity ) override;
void setTransform( const MatrixF& transform ) override;
void setReverb(bool useReverb) override;
void setVolume( F32 volume ) override;
void setPitch( F32 pitch ) override;
void setCone( F32 innerAngle, F32 outerAngle, F32 outerVolume ) override;