Skip to content

Commit af0972c

Browse files
committed
audio: SDL_PutAudioStreamPlanarData should take a channel count.
Fixes #12894.
1 parent 5f03cb3 commit af0972c

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

examples/audio/05-planar-data/planar-data.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
6565
const SDL_FPoint point = { event->button.x, event->button.y };
6666
if (SDL_PointInRectFloat(&point, &rect_left_button)) { /* clicked left button? */
6767
const Uint8 *planes[] = { left, NULL }; /* specify NULL to say "this specific channel is silent" */
68-
SDL_PutAudioStreamPlanarData(stream, (const void * const *) planes, SDL_arraysize(left));
68+
SDL_PutAudioStreamPlanarData(stream, (const void * const *) planes, -1, SDL_arraysize(left));
6969
SDL_FlushAudioStream(stream); /* that's all we're playing until it completes. */
7070
playing_sound = -1; /* left is playing */
7171
} else if (SDL_PointInRectFloat(&point, &rect_right_button)) { /* clicked right button? */
7272
const Uint8 *planes[] = { NULL, right }; /* specify NULL to say "this specific channel is silent" */
73-
SDL_PutAudioStreamPlanarData(stream, (const void * const *) planes, SDL_arraysize(right));
73+
SDL_PutAudioStreamPlanarData(stream, (const void * const *) planes, -1, SDL_arraysize(right));
7474
SDL_FlushAudioStream(stream); /* that's all we're playing until it completes. */
7575
playing_sound = 1; /* right is playing */
7676
}

include/SDL3/SDL_audio.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,14 @@ extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream,
14311431
* individual array may be NULL; in this case, silence will be interleaved for
14321432
* that channel.
14331433
*
1434+
* `num_channels` specifies how many arrays are in `channel_buffers`. This can
1435+
* be used as a safety to prevent overflow, in case the stream format has
1436+
* changed elsewhere. If more channels are specified than the current input
1437+
* spec, they are ignored. If less channels are specified, the missing arrays
1438+
* are treated as if they are NULL (silence is written to those channels). If
1439+
* the count is -1, SDL will assume the array count matches the current input
1440+
* spec.
1441+
*
14341442
* Note that `num_samples` is the number of _samples per array_. This can also
14351443
* be thought of as the number of _sample frames_ to be queued. A value of 1
14361444
* with stereo arrays will queue two samples to the stream. This is different
@@ -1440,6 +1448,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream,
14401448
* \param stream the stream the audio data is being added to.
14411449
* \param channel_buffers a pointer to an array of arrays, one array per
14421450
* channel.
1451+
* \param num_channels the number of arrays in `channel_buffers` or -1.
14431452
* \param num_samples the number of _samples_ per array to write to the
14441453
* stream.
14451454
* \returns true on success or false on failure; call SDL_GetError() for more
@@ -1456,7 +1465,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamData(SDL_AudioStream *stream,
14561465
* \sa SDL_GetAudioStreamData
14571466
* \sa SDL_GetAudioStreamQueued
14581467
*/
1459-
extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamPlanarData(SDL_AudioStream *stream, const void * const *channel_buffers, int num_samples);
1468+
extern SDL_DECLSPEC bool SDLCALL SDL_PutAudioStreamPlanarData(SDL_AudioStream *stream, const void * const *channel_buffers, int num_channels, int num_samples);
14601469

14611470
/**
14621471
* Get converted/resampled data from the stream.

src/audio/SDL_audiocvt.c

+24-10
Original file line numberDiff line numberDiff line change
@@ -910,15 +910,29 @@ GENERIC_INTERLEAVE_WITH_NULLS_FUNCTION(32)
910910
//GENERIC_INTERLEAVE_WITH_NULLS_FUNCTION(64) (we don't have any 64-bit audio data types at the moment.)
911911
#undef GENERIC_INTERLEAVE_WITH_NULLS_FUNCTION
912912

913-
static void InterleaveAudioChannels(void *output, const void * const *channel_buffers, int num_samples, const SDL_AudioSpec *spec)
913+
static void InterleaveAudioChannels(void *output, const void * const *channel_buffers, int channels, int num_samples, const SDL_AudioSpec *spec)
914914
{
915-
const int channels = spec->channels;
916-
917915
bool have_null_channel = false;
918-
for (int i = 0; i < channels; i++) {
919-
if (channel_buffers[i] == NULL) {
920-
have_null_channel = true;
921-
break;
916+
const void *channels_full[16];
917+
918+
// if didn't specify enough channels, pad out a channel array with NULLs.
919+
if ((channels >= 0) && (channels < spec->channels)) {
920+
have_null_channel = true;
921+
SDL_assert(SDL_IsSupportedChannelCount(spec->channels));
922+
SDL_assert(spec->channels <= SDL_arraysize(channels_full));
923+
SDL_memcpy(channels_full, channel_buffers, channels * sizeof (*channel_buffers));
924+
SDL_memset(channels_full + channels, 0, (spec->channels - channels) * sizeof (*channel_buffers));
925+
channel_buffers = (const void * const *) channels_full;
926+
}
927+
928+
channels = spec->channels; // it's either < 0, needs to be clamped to spec->channels, or we just padded it out to spec->channels with channels_full.
929+
930+
if (!have_null_channel) {
931+
for (int i = 0; i < channels; i++) {
932+
if (channel_buffers[i] == NULL) {
933+
have_null_channel = true;
934+
break;
935+
}
922936
}
923937
}
924938

@@ -943,7 +957,7 @@ static void InterleaveAudioChannels(void *output, const void * const *channel_bu
943957
}
944958
}
945959

946-
bool SDL_PutAudioStreamPlanarData(SDL_AudioStream *stream, const void * const *channel_buffers, int num_samples)
960+
bool SDL_PutAudioStreamPlanarData(SDL_AudioStream *stream, const void * const *channel_buffers, int num_channels, int num_samples)
947961
{
948962
if (!stream) {
949963
return SDL_InvalidParamError("stream");
@@ -980,7 +994,7 @@ bool SDL_PutAudioStreamPlanarData(SDL_AudioStream *stream, const void * const *c
980994

981995
const int len = SDL_AUDIO_FRAMESIZE(spec) * num_samples;
982996
#if DEBUG_AUDIOSTREAM
983-
SDL_Log("AUDIOSTREAM: wants to put %d bytes of separated data", len);
997+
SDL_Log("AUDIOSTREAM: wants to put %d bytes of planar data", len);
984998
#endif
985999

9861000
// Is the data small enough to just interleave it on the stack and put it through the normal interface?
@@ -999,7 +1013,7 @@ bool SDL_PutAudioStreamPlanarData(SDL_AudioStream *stream, const void * const *c
9991013
callback = FreeAllocatedAudioBuffer;
10001014
}
10011015

1002-
InterleaveAudioChannels(data, channel_buffers, num_samples, &spec);
1016+
InterleaveAudioChannels(data, channel_buffers, num_channels, num_samples, &spec);
10031017

10041018
// it's okay if the stream format changed on another thread while we didn't hold the lock; PutAudioStreamBufferInternal will notice
10051019
// and set up a new track with the right format, and the next SDL_PutAudioStreamData will notice that stream->src_spec doesn't

src/dynapi/SDL_dynapi_procs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,6 @@ SDL_DYNAPI_PROC(bool,SDL_SetRenderTextureAddressMode,(SDL_Renderer *a,SDL_Textur
12831283
SDL_DYNAPI_PROC(bool,SDL_GetRenderTextureAddressMode,(SDL_Renderer *a,SDL_TextureAddressMode *b,SDL_TextureAddressMode *c),(a,b,c),return)
12841284
SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetGPUDeviceProperties,(SDL_GPUDevice *a),(a),return)
12851285
SDL_DYNAPI_PROC(SDL_Renderer*,SDL_CreateGPURenderer,(SDL_Window *a,SDL_GPUShaderFormat b,SDL_GPUDevice **c),(a,b,c),return)
1286-
SDL_DYNAPI_PROC(bool,SDL_PutAudioStreamPlanarData,(SDL_AudioStream *a,const void * const*b,int c),(a,b,c),return)
1286+
SDL_DYNAPI_PROC(bool,SDL_PutAudioStreamPlanarData,(SDL_AudioStream *a,const void * const*b,int c,int d),(a,b,c,d),return)
12871287
SDL_DYNAPI_PROC(bool,SDL_SetAudioIterationCallbacks,(SDL_AudioDeviceID a,SDL_AudioIterationCallback b,SDL_AudioIterationCallback c,void *d),(a,b,c,d),return)
12881288
SDL_DYNAPI_PROC(int,SDL_GetEventDescription,(const SDL_Event *a,char *b,int c),(a,b,c),return)

0 commit comments

Comments
 (0)