kcat/openal-soft

using STEREO_ANGLES as a way to pan a stereo sound

Cheemsandfriends opened this issue · 3 comments

I'm trying to use the stereo angles extension for basically 2D panning, but I can't seem to crack down the math for it to work as completely intended, I tried to play around with it but I can't seem to get the sound for it to be completely on the right and viceversa, it can still be heard if I make it to hear it from one channel externally

What are the literal values that could achieve a hard panning to the left/right speakers?

kcat commented

There aren't any, really. Sound positions (including the individual channels in a multi-channel sound) are virtualized, so they're placed in a virtual space around the listener, which OpenAL Soft then does what it can to pan/render sounds in those positions regardless of the speaker layout, where different configurations may have different speaker positions. For instance, playing a quadraphonic track with 5.1 output, the quad channels will be panned to sound as if they're in the +/-45 and +/-135 degree positions to recreate the intended sound, rather than being locked to the speakers which would be around +/-30 and +/-110 degrees, a decent bit off from the intended positions. In some output modes, there may not be a left or right speaker, or it may be stereo headphones which shouldn't be treated the same as stereo speakers.

You can bypass that virtualization using the direct_channels extension, but then the STEREO_ANGLES extension won't work since the source channels will be mapped directly to output channels, independent of their positions. There may/will be a separate panning extension to attenuate the left or right channels for 2D panning, but it's not complete or intended for public use yet.

I see, so stereo angles is more complex than that...

or it may be stereo headphones which shouldn't be treated the same as stereo speakers

it shouldn't? what's the difference between the headphone speakers and normal stereo speakers?
looking more at it, I thought stereo angles would be a lot more simpler, I guess I was wrong

kcat commented

it shouldn't? what's the difference between the headphone speakers and normal stereo speakers?

Headphones are placed directly on the ears, so a sound playing on the left channel plays directly on the left ear with little to no sound reaching the right, and vice-versa. Whereas normal stereo speakers are placed at a distance in front of the listener, so a sound playing on the left channel plays from a point in front and slightly left (where the sound will reach the left ear first followed by a delayed and attenuated "echo" reaching the right ear; this delay and attenuation between the two ears, along with the angle of the sound waves interacting with the outer ear to subtly filter the sound, is what the brain uses to determine the direction of the sound).

Placing a sound directly on the left or right output channel and panning between them causes a different auditory response depending if it's played on external speakers, vs headphones that are directly on the listener's ears. Sounds playing directly on individual ears is something our brains aren't accustomed to hearing, creating proximity or inside-the-head effects that the brain has difficulty understanding, causing listening fatigue when done for extended periods.

Binaural mixing, or HRTF filters, aim to alleviate that by applying filters that simulate how sound interacts with the head and ears. For a given sound and direction, it will first play the sound on the ear closest to the source, applying an appropriate filter given the incoming angle, followed by a delayed echo on the opposite ear appropriately attenuated and filtered. This tricks the brain into perceiving the sound as coming from an external point around you, rather than directly on the ears or in the head, which reduces listening fatigue and creates a more consistent auditory response for sounds coming from around you (though headphones/HRTF has the advantage of being able to simulate many more points around you than surround sound speaker setups, including above and below).

looking more at it, I thought stereo angles would be a lot more simpler, I guess I was wrong

It is conceptually pretty simple. A stereo sound plays at two points in front of the listener, at +30 degrees for the left channel and -30 degrees for the right going counter-clockwise (giving 60 degrees of separation, or a 60-degree front stage), which is expected of normal stereo content. The stereo angles extension lets you rotate those points around the listener.

The trick is to think about it in terms of the input/source channels, rather than the output. Think of a stereo sound as playing from two virtual speakers in front of the listener (which you can move with the stereo angles extension). The output is under the control of OpenAL, whose task it is to take the given inputs in virtual space and mix them to whatever output the user has, to give the best representation possible of that virtual space.

As a quick and dirty 2D panning method with stereo angles, what you can do is start with the defaults {pi/6.0f, -pi/6.0f} for a sound that is center-panned. When the sound pans left, move the right channel to the left position ({pi/6.0f, -pi/6.0f} -> {pi/6.0f, 0.0f} -> {pi/6.0f, pi/6.0f}) and when a sound pans right, move the left channel to the right position ({pi/6.0f, -pi/6.0f} -> {0.0f, -pi/6.0f} -> {-pi/6.0f, -pi/6.0f}). This won't sound like normal 2D panning since it moves the left/right channels rather than attenuating them, so both channels will remain audible and the overall energy stays equal, but it's an option if you want it. More classic 2D panning will need to wait for the appropriate extension.