Skip to content

Commit 9efc1f1

Browse files
committed
Add Panning Extension for Stereo formats
1 parent e08dbf3 commit 9efc1f1

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
@@ -468,6 +468,7 @@ SIMDALIGNEDSTRUCT ALsource
468468
ALfloat cone_inner_angle;
469469
ALfloat cone_outer_angle;
470470
ALfloat cone_outer_gain;
471+
ALfloat EXT_balance; /* stereo linear balance extension */
471472
ALbuffer *buffer;
472473
SDL_AudioStream *stream; /* for resampling. */
473474
BufferQueue buffer_queue;
@@ -1706,7 +1707,12 @@ static void calculate_channel_gains(const ALCcontext *ctx, const ALsource *src,
17061707

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

1709-
if (!spatialize) {
1710+
if (src->queue_channels == 2) {
1711+
gain = SDL_min(SDL_max(src->gain, src->min_gain), src->max_gain) * ctx->listener.gain;
1712+
gains[0] = gain * SDL_min(1.f, 1.f - src->EXT_balance); // left channel
1713+
gains[1] = gain * SDL_min(1.f, 1.f + src->EXT_balance); // right channel
1714+
return;
1715+
} else if (!spatialize) {
17101716
/* simpler path through the same AL spec details if not spatializing. */
17111717
gain = SDL_min(SDL_max(src->gain, src->min_gain), src->max_gain) * ctx->listener.gain;
17121718
gains[0] = gains[1] = gain; /* no spatialization, but AL_GAIN (etc) is still applied. */
@@ -3712,6 +3718,8 @@ static void _alSourcefv(const ALuint name, const ALenum param, const ALfloat *va
37123718
source_set_offset(src, param, *values);
37133719
break;
37143720

3721+
case AL_EXT_BALANCE: src->EXT_balance = *values; break;
3722+
37153723
default: set_al_error(ctx, AL_INVALID_ENUM); return;
37163724

37173725
}
@@ -3736,6 +3744,7 @@ static void _alSourcef(const ALuint name, const ALenum param, const ALfloat valu
37363744
case AL_SEC_OFFSET:
37373745
case AL_SAMPLE_OFFSET:
37383746
case AL_BYTE_OFFSET:
3747+
case AL_EXT_BALANCE:
37393748
_alSourcefv(name, param, &value);
37403749
break;
37413750

@@ -3917,6 +3926,8 @@ static void _alGetSourcefv(const ALuint name, const ALenum param, ALfloat *value
39173926
*values = source_get_offset(src, param);
39183927
break;
39193928

3929+
case AL_EXT_BALANCE: *values = src->EXT_balance; break;
3930+
39203931
default: set_al_error(ctx, AL_INVALID_ENUM); break;
39213932
}
39223933
}
@@ -3938,6 +3949,7 @@ static void _alGetSourcef(const ALuint name, const ALenum param, ALfloat *value)
39383949
case AL_SEC_OFFSET:
39393950
case AL_SAMPLE_OFFSET:
39403951
case AL_BYTE_OFFSET:
3952+
case AL_EXT_BALANCE:
39413953
_alGetSourcefv(name, param, value);
39423954
break;
39433955
default: set_al_error(get_current_context(), AL_INVALID_ENUM); break;

0 commit comments

Comments
 (0)