SFX API Changes

DSound has since been deprecated and xaudio2 would require us to write our own 3d spatialization and mixer
Load devices the same way we load in the gfx end
setup sfx provider
run sfx devices on startup
various fixes around sfx null device
added the bitrate and samplerate globals
added the hrtf global code is in to use this but not setup yet
Adds speed of sound to the sound system
SFXAmbience now has a property for speed of sound for different mediums, can also be set directly
This commit is contained in:
marauder2k7 2026-03-13 08:05:42 +00:00
parent 1c6409a485
commit 974f217b96
53 changed files with 1885 additions and 6409 deletions

View file

@ -28,7 +28,7 @@
#ifdef TORQUE_DEBUG
# define AL_SANITY_CHECK() \
AssertFatal( mOpenAL.alIsSource( mSourceName ), "AL Source Sanity Check Failed!" );
AssertFatal( alIsSource( mSourceName ), "AL Source Sanity Check Failed!" );
#else
# define AL_SANITY_CHECK()
#endif
@ -42,8 +42,8 @@ SFXALVoice* SFXALVoice::create( SFXALDevice* device, SFXALBuffer *buffer )
AssertFatal( buffer, "SFXALVoice::create() - Got null buffer!" );
ALuint sourceName;
device->mOpenAL.alGenSources( 1, &sourceName );
AssertFatal( device->mOpenAL.alIsSource( sourceName ), "AL Source Sanity Check Failed!" );
alGenSources( 1, &sourceName );
AssertFatal( alIsSource( sourceName ), "AL Source Sanity Check Failed!" );
// Is this 3d?
// Okay, this looks odd, but bear with me for a moment. AL_SOURCE_RELATIVE does NOT indicate
@ -52,34 +52,32 @@ SFXALVoice* SFXALVoice::create( SFXALDevice* device, SFXALBuffer *buffer )
// does do is dictate if the position of THIS SOURCE is relative to the listener. If AL_SOURCE_RELATIVE is AL_TRUE
// and the source's position is 0, 0, 0, then the source is directly on top of the listener at all times, which is what
// we want for non-3d sounds.
device->mOpenAL.alSourcei( sourceName, AL_SOURCE_RELATIVE, ( buffer->mIs3d ? AL_FALSE : AL_TRUE ) );
alSourcei( sourceName, AL_SOURCE_RELATIVE, ( buffer->mIs3d ? AL_FALSE : AL_TRUE ) );
if( buffer->mIs3d )
device->mOpenAL.alSourcef( sourceName, AL_ROLLOFF_FACTOR, device->mRolloffFactor );
alSourcef( sourceName, AL_ROLLOFF_FACTOR, device->mRolloffFactor );
SFXALVoice *voice = new SFXALVoice( device->mOpenAL,
buffer,
SFXALVoice *voice = new SFXALVoice( buffer,
sourceName );
return voice;
}
SFXALVoice::SFXALVoice( const OPENALFNTABLE &oalft,
SFXALBuffer *buffer,
SFXALVoice::SFXALVoice( SFXALBuffer *buffer,
ALuint sourceName )
: Parent( buffer ),
mSourceName( sourceName ),
mResumeAtSampleOffset( -1.0f ),
mSampleOffset( 0 ),
mOpenAL( oalft )
mSampleOffset( 0 )
{
AL_SANITY_CHECK();
}
SFXALVoice::~SFXALVoice()
{
mOpenAL.alDeleteSources( 1, &mSourceName );
alSourcei(mSourceName, AL_BUFFER, 0);
alDeleteSources( 1, &mSourceName );
}
void SFXALVoice::_lateBindStaticBufferIfNecessary()
@ -87,9 +85,9 @@ void SFXALVoice::_lateBindStaticBufferIfNecessary()
if( !mBuffer->isStreaming() )
{
ALint bufferId;
mOpenAL.alGetSourcei( mSourceName, AL_BUFFER, &bufferId );
alGetSourcei( mSourceName, AL_BUFFER, &bufferId );
if( !bufferId )
mOpenAL.alSourcei( mSourceName, AL_BUFFER, _getBuffer()->mALBuffer );
alSourcei( mSourceName, AL_BUFFER, _getBuffer()->mALBuffer );
}
}
@ -99,7 +97,7 @@ SFXStatus SFXALVoice::_status() const
AL_SANITY_CHECK();
ALint state;
mOpenAL.alGetSourcei( mSourceName, AL_SOURCE_STATE, &state );
alGetSourcei( mSourceName, AL_SOURCE_STATE, &state );
switch( state )
{
@ -118,18 +116,15 @@ void SFXALVoice::_play()
#ifdef DEBUG_SPEW
Platform::outputDebugString( "[SFXALVoice] Starting playback" );
#endif
#if defined(AL_ALEXT_PROTOTYPES)
//send every voice that plays to the alauxiliary slot that has the reverb
mOpenAL.alSource3i(mSourceName, AL_AUXILIARY_SEND_FILTER, 1, 0, AL_FILTER_NULL);
#endif
mOpenAL.alSourcePlay( mSourceName );
alSourcePlay( mSourceName );
//WORKAROUND: Adjust play cursor for buggy OAL when resuming playback. Do this after alSourcePlay
// as it is the play function that will cause the cursor to jump.
if( mResumeAtSampleOffset != -1.0f )
{
mOpenAL.alSourcef( mSourceName, AL_SAMPLE_OFFSET, mResumeAtSampleOffset );
alSourcef( mSourceName, AL_SAMPLE_OFFSET, mResumeAtSampleOffset );
mResumeAtSampleOffset = -1.0f;
}
}
@ -142,12 +137,12 @@ void SFXALVoice::_pause()
Platform::outputDebugString( "[SFXALVoice] Pausing playback" );
#endif
mOpenAL.alSourcePause( mSourceName );
alSourcePause( mSourceName );
//WORKAROUND: Another workaround for buggy OAL. Resuming playback of a paused source will cause the
// play cursor to jump. Save the cursor so we can manually move it into position in _play(). Sigh.
mOpenAL.alGetSourcef( mSourceName, AL_SAMPLE_OFFSET, &mResumeAtSampleOffset );
alGetSourcef( mSourceName, AL_SAMPLE_OFFSET, &mResumeAtSampleOffset );
}
void SFXALVoice::_stop()
@ -158,7 +153,7 @@ void SFXALVoice::_stop()
Platform::outputDebugString( "[SFXALVoice] Stopping playback" );
#endif
mOpenAL.alSourceStop( mSourceName );
alSourceStop( mSourceName );
mSampleOffset = 0;
mResumeAtSampleOffset = -1.0f;
@ -169,7 +164,7 @@ void SFXALVoice::_seek( U32 sample )
AL_SANITY_CHECK();
_lateBindStaticBufferIfNecessary();
mOpenAL.alSourcei( mSourceName, AL_SAMPLE_OFFSET, sample );
alSourcei( mSourceName, AL_SAMPLE_OFFSET, sample );
mResumeAtSampleOffset = -1.0f;
}
@ -183,7 +178,7 @@ U32 SFXALVoice::_tell() const
mBuffer->write( NULL, 0 );
ALint pos;
mOpenAL.alGetSourcei( mSourceName, AL_SAMPLE_OFFSET, &pos );
alGetSourcei( mSourceName, AL_SAMPLE_OFFSET, &pos );
return ( pos + mSampleOffset );
}
@ -191,17 +186,17 @@ void SFXALVoice::setMinMaxDistance( F32 min, F32 max )
{
AL_SANITY_CHECK();
mOpenAL.alSourcef( mSourceName, AL_REFERENCE_DISTANCE, min );
mOpenAL.alSourcef( mSourceName, AL_MAX_DISTANCE, max );
alSourcef( mSourceName, AL_REFERENCE_DISTANCE, min );
alSourcef( mSourceName, AL_MAX_DISTANCE, max );
}
void SFXALVoice::play( bool looping )
{
AL_SANITY_CHECK();
mOpenAL.alSourceStop( mSourceName );
alSourceStop( mSourceName );
if( !mBuffer->isStreaming() )
mOpenAL.alSourcei( mSourceName, AL_LOOPING, ( looping ? AL_TRUE : AL_FALSE ) );
alSourcei( mSourceName, AL_LOOPING, ( looping ? AL_TRUE : AL_FALSE ) );
Parent::play( looping );
}
@ -213,7 +208,7 @@ void SFXALVoice::setVelocity( const VectorF& velocity )
// Torque and OpenAL are both right handed
// systems, so no coordinate flipping is needed.
mOpenAL.alSourcefv( mSourceName, AL_VELOCITY, velocity );
alSourcefv( mSourceName, AL_VELOCITY, velocity );
}
void SFXALVoice::setTransform( const MatrixF& transform )
@ -227,34 +222,34 @@ void SFXALVoice::setTransform( const MatrixF& transform )
transform.getColumn( 3, &pos );
transform.getColumn( 1, &dir );
mOpenAL.alSourcefv( mSourceName, AL_POSITION, pos );
mOpenAL.alSourcefv( mSourceName, AL_DIRECTION, dir );
alSourcefv( mSourceName, AL_POSITION, pos );
alSourcefv( mSourceName, AL_DIRECTION, dir );
}
void SFXALVoice::setVolume( F32 volume )
{
AL_SANITY_CHECK();
mOpenAL.alSourcef( mSourceName, AL_GAIN, volume );
alSourcef( mSourceName, AL_GAIN, volume );
}
void SFXALVoice::setPitch( F32 pitch )
{
AL_SANITY_CHECK();
mOpenAL.alSourcef( mSourceName, AL_PITCH, pitch );
alSourcef( mSourceName, AL_PITCH, pitch );
}
void SFXALVoice::setCone( F32 innerAngle, F32 outerAngle, F32 outerVolume )
{
AL_SANITY_CHECK();
mOpenAL.alSourcef( mSourceName, AL_CONE_INNER_ANGLE, innerAngle );
mOpenAL.alSourcef( mSourceName, AL_CONE_OUTER_ANGLE, outerAngle );
mOpenAL.alSourcef( mSourceName, AL_CONE_OUTER_GAIN, outerVolume );
alSourcef( mSourceName, AL_CONE_INNER_ANGLE, innerAngle );
alSourcef( mSourceName, AL_CONE_OUTER_ANGLE, outerAngle );
alSourcef( mSourceName, AL_CONE_OUTER_GAIN, outerVolume );
}
void SFXALVoice::setRolloffFactor( F32 factor )
{
mOpenAL.alSourcef( mSourceName, AL_ROLLOFF_FACTOR, factor );
alSourcef( mSourceName, AL_ROLLOFF_FACTOR, factor );
}