Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Panning Extension for Stereo formats #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions AL/al.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ typedef void ALvoid;
*/
#define AL_CONE_OUTER_GAIN 0x1022

/**
* Balance extension.
* Type: ALfloat
* Range: [-1.0 - 1.0]
* Default: 0.0
*
* Extension for balance applied to the source to enable direct panning even
* with stereo sounds.
*/
#define AL_EXT_BALANCE 0xF001

/**
* Source maximum distance.
* Type: ALfloat
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ add_test_executable(testopenalinfo)
add_test_executable(testqueueing)
add_test_executable(testcapture)
add_test_executable(testposition)
add_test_executable(testpanning)


14 changes: 13 additions & 1 deletion mojoal.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ SIMDALIGNEDSTRUCT ALsource
ALfloat cone_inner_angle;
ALfloat cone_outer_angle;
ALfloat cone_outer_gain;
ALfloat EXT_balance; /* stereo linear balance extension */
ALbuffer *buffer;
SDL_AudioStream *stream; /* for resampling. */
SDL_atomic_t total_queued_buffers; /* everything queued, playing and processed. AL_BUFFERS_QUEUED value. */
Expand Down Expand Up @@ -1710,7 +1711,12 @@ static void calculate_channel_gains(const ALCcontext *ctx, const ALsource *src,

/* this goes through the steps the AL spec dictates for gain and distance attenuation... */

if (!spatialize) {
if (src->queue_channels == 2) {
gain = SDL_min(SDL_max(src->gain, src->min_gain), src->max_gain) * ctx->listener.gain;
gains[0] = gain * SDL_min(1.f, 1.f - src->EXT_balance); // left channel
gains[1] = gain * SDL_min(1.f, 1.f + src->EXT_balance); // right channel
return;
} else if (!spatialize) {
/* simpler path through the same AL spec details if not spatializing. */
gain = SDL_min(SDL_max(src->gain, src->min_gain), src->max_gain) * ctx->listener.gain;
gains[0] = gains[1] = gain; /* no spatialization, but AL_GAIN (etc) is still applied. */
Expand Down Expand Up @@ -3727,6 +3733,8 @@ static void _alSourcefv(const ALuint name, const ALenum param, const ALfloat *va
source_set_offset(src, param, *values);
break;

case AL_EXT_BALANCE: src->EXT_balance = *values; break;

default: set_al_error(ctx, AL_INVALID_ENUM); return;

}
Expand All @@ -3751,6 +3759,7 @@ static void _alSourcef(const ALuint name, const ALenum param, const ALfloat valu
case AL_SEC_OFFSET:
case AL_SAMPLE_OFFSET:
case AL_BYTE_OFFSET:
case AL_EXT_BALANCE:
_alSourcefv(name, param, &value);
break;

Expand Down Expand Up @@ -3932,6 +3941,8 @@ static void _alGetSourcefv(const ALuint name, const ALenum param, ALfloat *value
*values = source_get_offset(src, param);
break;

case AL_EXT_BALANCE: *values = src->EXT_balance; break;

default: set_al_error(ctx, AL_INVALID_ENUM); break;
}
}
Expand All @@ -3953,6 +3964,7 @@ static void _alGetSourcef(const ALuint name, const ALenum param, ALfloat *value)
case AL_SEC_OFFSET:
case AL_SAMPLE_OFFSET:
case AL_BYTE_OFFSET:
case AL_EXT_BALANCE:
_alGetSourcefv(name, param, value);
break;
default: set_al_error(get_current_context(), AL_INVALID_ENUM); break;
Expand Down
Loading