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

@ -173,7 +173,8 @@ SoundAsset::SoundAsset()
mProfileDesc.mPriority = 1.0f;
mProfileDesc.mSourceGroup = NULL;
mProfileDesc.mFadeInEase = EaseF();
mProfileDesc.mReverb = SFXSoundReverbProperties();
mProfileDesc.mSourceGroup = dynamic_cast<SFXSource*>(Sim::findObject("AudioChannelMaster"));
dMemset(mProfileDesc.mParameters, 0, sizeof(mProfileDesc.mParameters));
mIsPlaylist = false;

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;

View file

@ -173,7 +173,6 @@ SFXDescription::SFXDescription( const SFXDescription& desc )
mStreamPacketSize( desc.mStreamPacketSize ),
mUseReverb( desc.mUseReverb ),
mStreamReadAhead( desc.mStreamReadAhead ),
mReverb( desc.mReverb ),
mScatterDistance( desc.mScatterDistance ),
mPriority( desc.mPriority )
{
@ -206,7 +205,6 @@ SFXDescription::SFXDescription(const SFXDescription& other, bool temp_clone)
mStreamPacketSize( other.mStreamPacketSize ),
mStreamReadAhead( other.mStreamReadAhead ),
mUseReverb( other.mUseReverb ),
mReverb( other.mReverb ),
mPriority( other.mPriority ),
mScatterDistance( other.mScatterDistance )
{
@ -396,48 +394,6 @@ void SFXDescription::initPersistFields()
"a custom reverb setup can be defined using the \"Reverb\" properties that will then be assigned "
"to sounds playing with the description.\n\n"
"@ref SFX_reverb");
addFieldV("reverbDensity", TypeRangedF32, Offset(mReverb.flDensity, SFXDescription), &CommonValidators::PositiveFloat,
"Density of reverb environment.");
addFieldV("reverbDiffusion", TypeRangedF32, Offset(mReverb.flDiffusion, SFXDescription), &CommonValidators::PositiveFloat,
"Environment diffusion.");
addFieldV("reverbGain", TypeRangedF32, Offset(mReverb.flGain, SFXDescription), &CommonValidators::PositiveFloat,
"Reverb Gain Level.");
addFieldV("reverbGainHF", TypeRangedF32, Offset(mReverb.flGainHF, SFXDescription), &CommonValidators::PositiveFloat,
"Reverb Gain to high frequencies");
addFieldV("reverbGainLF", TypeRangedF32, Offset(mReverb.flGainLF, SFXDescription), &CommonValidators::PositiveFloat,
"Reverb Gain to high frequencies");
addFieldV("reverbDecayTime", TypeRangedF32, Offset(mReverb.flDecayTime, SFXDescription), &CommonValidators::PositiveFloat,
"Decay time for the reverb.");
addFieldV("reverbDecayHFRatio", TypeRangedF32, Offset(mReverb.flDecayHFRatio, SFXDescription), &CommonValidators::PositiveFloat,
"High frequency decay time ratio.");
addFieldV("reverbDecayLFRatio", TypeRangedF32, Offset(mReverb.flDecayLFRatio, SFXDescription), &CommonValidators::PositiveFloat,
"High frequency decay time ratio.");
addFieldV("reflectionsGain", TypeRangedF32, Offset(mReverb.flReflectionsGain, SFXDescription), &CommonValidators::PositiveFloat,
"Reflection Gain.");
addFieldV("reflectionDelay", TypeRangedF32, Offset(mReverb.flReflectionsDelay, SFXDescription), &CommonValidators::PositiveFloat,
"How long to delay reflections.");
addFieldV("lateReverbGain", TypeRangedF32, Offset(mReverb.flLateReverbGain, SFXDescription), &CommonValidators::PositiveFloat,
"Late reverb gain amount.");
addFieldV("lateReverbDelay", TypeRangedF32, Offset(mReverb.flLateReverbDelay, SFXDescription), &CommonValidators::PositiveFloat,
"Late reverb delay time.");
addFieldV("reverbEchoTime", TypeRangedF32, Offset(mReverb.flEchoTime, SFXDescription), &CommonValidators::PositiveFloat,
"Reverb echo time.");
addFieldV("reverbEchoDepth", TypeRangedF32, Offset(mReverb.flEchoDepth, SFXDescription), &CommonValidators::PositiveFloat,
"Reverb echo depth.");
addFieldV("reverbModTime", TypeRangedF32, Offset(mReverb.flModulationTime, SFXDescription), &CommonValidators::PositiveFloat,
"Reverb Modulation time.");
addFieldV("reverbModDepth", TypeRangedF32, Offset(mReverb.flModulationDepth, SFXDescription), &CommonValidators::NormalizedFloat,
"Reverb Modulation Depth.");
addFieldV("airAbsorbtionGainHF", TypeRangedF32, Offset(mReverb.flAirAbsorptionGainHF, SFXDescription), &CommonValidators::PositiveFloat,
"High Frequency air absorbtion");
addFieldV("reverbHFRef", TypeRangedF32, Offset(mReverb.flHFReference, SFXDescription), &CommonValidators::PositiveFloat,
"Reverb High Frequency Reference.");
addFieldV("reverbLFRef", TypeRangedF32, Offset(mReverb.flLFReference, SFXDescription), &CommonValidators::PositiveFloat,
"Reverb Low Frequency Reference.");
addFieldV("roomRolloffFactor", TypeRangedF32, Offset(mReverb.flRoomRolloffFactor, SFXDescription), &CommonValidators::NegDefaultF32,
"Rolloff factor for reverb.");
addFieldV("decayHFLimit", TypeRangedS32, Offset(mReverb.iDecayHFLimit, SFXDescription), &CommonValidators::PositiveInt,
"High Frequency decay limit.");
endGroup("Reverb");
Parent::initPersistFields();
@ -496,10 +452,6 @@ void SFXDescription::validate()
mConeOutsideAngle = mClamp( mConeOutsideAngle, mConeInsideAngle, 360 );
mConeOutsideVolume = mClampF( mConeOutsideVolume, 0, 1 );
if( !mIs3D )
mUseReverb = false;
mReverb.validate();
}
//-----------------------------------------------------------------------------
@ -534,30 +486,6 @@ void SFXDescription::packData( BitStream *stream )
stream->writeFloat( mConeOutsideVolume, 6 );
if( mUseReverb )
{
stream->write(mReverb.flDensity);
stream->write(mReverb.flDiffusion);
stream->write(mReverb.flGain);
stream->write(mReverb.flGainHF);
stream->write(mReverb.flGainLF);
stream->write(mReverb.flDecayTime);
stream->write(mReverb.flDecayHFRatio);
stream->write(mReverb.flDecayLFRatio);
stream->write(mReverb.flReflectionsGain);
stream->write(mReverb.flReflectionsDelay);
stream->write(mReverb.flLateReverbGain);
stream->write(mReverb.flLateReverbDelay);
stream->write(mReverb.flEchoTime);
stream->write(mReverb.flEchoDepth);
stream->write(mReverb.flModulationTime);
stream->write(mReverb.flModulationDepth);
stream->write(mReverb.flAirAbsorptionGainHF);
stream->write(mReverb.flHFReference);
stream->write(mReverb.flLFReference);
stream->write(mReverb.flRoomRolloffFactor);
stream->write(mReverb.iDecayHFLimit);
}
}
stream->write( mFadeInTime );
@ -607,30 +535,6 @@ void SFXDescription::unpackData( BitStream *stream )
mConeOutsideVolume = stream->readFloat( 6 );
if( mUseReverb )
{
stream->read(&mReverb.flDensity);
stream->read(&mReverb.flDiffusion);
stream->read(&mReverb.flGain);
stream->read(&mReverb.flGainHF);
stream->read(&mReverb.flGainLF);
stream->read(&mReverb.flDecayTime);
stream->read(&mReverb.flDecayHFRatio);
stream->read(&mReverb.flDecayLFRatio);
stream->read(&mReverb.flReflectionsGain);
stream->read(&mReverb.flReflectionsDelay);
stream->read(&mReverb.flLateReverbGain);
stream->read(&mReverb.flLateReverbDelay);
stream->read(&mReverb.flEchoTime);
stream->read(&mReverb.flEchoDepth);
stream->read(&mReverb.flModulationTime);
stream->read(&mReverb.flModulationDepth);
stream->read(&mReverb.flAirAbsorptionGainHF);
stream->read(&mReverb.flHFReference);
stream->read(&mReverb.flLFReference);
stream->read(&mReverb.flRoomRolloffFactor);
stream->read(&mReverb.iDecayHFLimit);
}
}
stream->read( &mFadeInTime );

View file

@ -170,9 +170,6 @@ class SFXDescription : public SimDataBlock
/// The number of streaming packets to read and buffer in advance.
/// Only relevant if "isStreaming" is true.
U32 mStreamReadAhead;
/// Reverb properties for sound playback.
SFXSoundReverbProperties mReverb;
/// Parameters to which sources playing with this description should automatically
/// connect when created.
@ -202,4 +199,4 @@ class SFXDescription : public SimDataBlock
};
#endif // _SFXDESCRIPTION_H_
#endif // _SFXDESCRIPTION_H_

View file

@ -261,11 +261,6 @@ bool SFXSound::_allocVoice( SFXDevice* device )
_setCone( mConeInsideAngle, mConeOutsideAngle, mConeOutsideVolume );
}
// Set reverb, if enabled.
if( mDescription->mUseReverb )
mVoice->setReverb( mDescription->mReverb );
// Update the duration... it shouldn't have changed, but
// its probably better that we're accurate if it did.
mDuration = mBuffer->getDuration();
@ -395,8 +390,10 @@ void SFXSound::_play()
{
Parent::_play();
if( mVoice )
mVoice->play( isLooping() );
if (mVoice)
{
mVoice->play(isLooping());
}
else
{
// To ensure the fastest possible reaction
@ -447,6 +444,11 @@ void SFXSound::_updateStatus()
if( mVoice )
{
// properties can change while a voice is playing, respect that.
if (getSourceGroup())
mVoice->setReverb(getSourceGroup()->getDescription()->mUseReverb);
SFXStatus voiceStatus = mVoice->getStatus();
// Filter out SFXStatusBlocked.

View file

@ -210,9 +210,9 @@ class SFXVoice : public StrongRefBase,
F32 outerAngle,
F32 outerVolume ) = 0;
/// Set the reverb properties for playback of this sound.
/// Allow this sound to use SFXEnvironment reverb.
/// @note Has no effect on devices that do not support reverb.
virtual void setReverb( const SFXSoundReverbProperties& reverb ) {}
virtual void setReverb( bool useReverb ) {}
/// Set the priority of this voice. Default 1.0.
/// @note Has no effect on devices that do not support voice management.

View file

@ -24,7 +24,11 @@
// Source groups.
//-----------------------------------------------------------------------------
singleton SFXDescription( AudioMaster );
singleton SFXDescription( AudioMaster )
{
useCustomReverb = false;
};
singleton SFXSource( AudioChannelMaster )
{
description = AudioMaster;
@ -33,8 +37,16 @@ singleton SFXSource( AudioChannelMaster )
singleton SFXDescription( AudioChannel )
{
sourceGroup = AudioChannelMaster;
useCustomReverb = false;
};
singleton SFXDescription( AudioEffectChannel )
{
sourceGroup = AudioChannelMaster;
useCustomReverb = true;
};
singleton SFXSource( AudioChannelDefault )
{
description = AudioChannel;
@ -45,7 +57,7 @@ singleton SFXSource( AudioChannelGui )
};
singleton SFXSource( AudioChannelEffects )
{
description = AudioChannel;
description = AudioEffectChannel;
};
singleton SFXSource( AudioChannelMessages )
{
@ -66,7 +78,7 @@ AudioChannelMusic.play();
AudioChannelMessages.play();
// Stop in-game effects channels.
AudioChannelEffects.stop();
AudioChannelEffects.play();
//-----------------------------------------------------------------------------
// Master SFXDescriptions.
@ -440,4 +452,4 @@ function sfxResume( %pauseSet )
// Clear our pause set... the caller is left
// to clear his own if he passed one.
%pauseSet.clear();
}*/
}*/