Skip to content

Commit 9b2ce30

Browse files
committed
Add Panning Extension for Stereo formats
1 parent bfaf2f2 commit 9b2ce30

4 files changed

Lines changed: 375 additions & 1 deletion

File tree

AL/al.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,17 @@ typedef void ALvoid;
296296
*/
297297
#define AL_CONE_OUTER_GAIN 0x1022
298298

299+
/**
300+
* Balance extension.
301+
* Type: ALfloat
302+
* Range: [-1.0 - 1.0]
303+
* Default: 0.0
304+
*
305+
* Extension for balance applied to the source to enable direct panning even
306+
* with stereo sounds.
307+
*/
308+
#define AL_EXT_BALANCE 0xF001
309+
299310
/**
300311
* Source maximum distance.
301312
* Type: ALfloat

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ add_test_executable(testopenalinfo)
2222
add_test_executable(testqueueing)
2323
add_test_executable(testcapture)
2424
add_test_executable(testposition)
25+
add_test_executable(testpanning)
2526

2627

mojoal.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ SIMDALIGNEDSTRUCT ALsource
471471
ALfloat cone_inner_angle;
472472
ALfloat cone_outer_angle;
473473
ALfloat cone_outer_gain;
474+
ALfloat EXT_balance; /* stereo linear balance extension */
474475
ALbuffer *buffer;
475476
SDL_AudioStream *stream; /* for resampling. */
476477
SDL_atomic_t total_queued_buffers; /* everything queued, playing and processed. AL_BUFFERS_QUEUED value. */
@@ -1710,7 +1711,12 @@ static void calculate_channel_gains(const ALCcontext *ctx, const ALsource *src,
17101711

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

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

3736+
case AL_EXT_BALANCE: src->EXT_balance = *values; break;
3737+
37303738
default: set_al_error(ctx, AL_INVALID_ENUM); return;
37313739

37323740
}
@@ -3751,6 +3759,7 @@ static void _alSourcef(const ALuint name, const ALenum param, const ALfloat valu
37513759
case AL_SEC_OFFSET:
37523760
case AL_SAMPLE_OFFSET:
37533761
case AL_BYTE_OFFSET:
3762+
case AL_EXT_BALANCE:
37543763
_alSourcefv(name, param, &value);
37553764
break;
37563765

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

3944+
case AL_EXT_BALANCE: *values = src->EXT_balance; break;
3945+
39353946
default: set_al_error(ctx, AL_INVALID_ENUM); break;
39363947
}
39373948
}
@@ -3953,6 +3964,7 @@ static void _alGetSourcef(const ALuint name, const ALenum param, ALfloat *value)
39533964
case AL_SEC_OFFSET:
39543965
case AL_SAMPLE_OFFSET:
39553966
case AL_BYTE_OFFSET:
3967+
case AL_EXT_BALANCE:
39563968
_alGetSourcefv(name, param, value);
39573969
break;
39583970
default: set_al_error(get_current_context(), AL_INVALID_ENUM); break;

0 commit comments

Comments
 (0)