[openal] 3d sound?
Chris Robinson
chris.kcat at gmail.com
Wed Aug 10 17:48:25 EDT 2016
On 08/10/2016 05:48 AM, Matthew Swartz wrote:
> Hi,
> I've been using openal soft in my project for a while now, but I guess I'm
> a bit confused about a couple of features.
>
> I want to use 3d sound positioning in my game, as well as have stereo audio
> sources that have proper distance functions so it doesn't always sound like
> it's playing right at the player.
Hi.
OpenAL (and OpenAL Soft) doesn't apply distance attenuation to
multi-channel sources. However, you can simulate it by changing the
source's AL_GAIN given the distance between the source and listener.
Where you'd normally set the position, instead set the gain based on the
distance:
ALfloat dist = distance(mPos, ListenerPos);
if(dist <= mRefDist)
alSourcef(mSource, AL_GAIN, mVolume);
else
alSourcef(mSource, AL_GAIN, mVolume * (mRefDist / dist));
Where mRefDist is what you set on AL_REFERENCE_DISTANCE (default: 1.0f),
mVolume is the normal volume the source would have without distance
attenuation, mPos and ListenerPos are the up-to-date positions for the
sound source and listener, and distance() calculates the distance
between two 3D points. This is effectively what OpenAL Soft would do
internally.
I can consider adding an extension which would do this automatically.
Something like
alSourcei(mSource, AL_MC_DISTANCE_ATTENUATION, AL_TRUE);
which would cause the source to calculate distance-related attenuation
for stereo, quad, rear, 5.1, 6.1, 7.1, and b-format sources (mono would
be unaffected by this extension, and this would only affect attenuation,
not panning/rotation; stereo has AL_EXT_STEREO_ANGLES for that, and
b-format comes with its own property for it).
I wouldn't mind hearing from others if there's any concerns about this idea.
More information about the openal
mailing list