SDL3 Audio: Does PutAudioStreamData expect interleaved PCM data?

I’m trying to send audio packets coming from a CEF browser to an SDL AudioStream, but am unsure of what conversion needs to happen between them, if any.

AFAICT, CEF is providing an array of array floats, where the outer array contains the channels, and the inner arrays contain their respective PCM data. I assume that SDL expects the PCM data to be interleaved into a flat array, but I didn’t see any documentation confirming this, just the void* argument of PutAudioStreamData that seems to imply it.

So I guess my first question is just whether this assumption is correct, and second is what would be the most performant way to apply this conversion if necessary.

As for the audio spec of the stream, I’m using { format: SDL_AUDIO_F32, channels: 2, freq: 44100 }, to match the default audio parameters set by CEF.

Yes, AFAIK SDL uses interleaved sample data everywhere. The documentation for SDL_AudioSpec says:

Channel data is interleaved. Stereo samples are stored in left/right ordering. Quad is stored in front-left/front-right/rear-left/rear-right order. 5.1 is stored in front-left/front-right/center/low-freq/rear-left/rear-right ordering (“low-freq” is the “.1” speaker).

(This was all a bit less clear in SDL2, since SDL_NewAudioStream does not use SDL_AudioSpec, and the audio spec documentation is the only place where interleaved samples are specified explicitly.)

I don’t know the answer to your second question for sure, but doubt you can beat a simple loop in which you assign alternating left and right channel samples to an interleaved buffer one at a time. Depending on what you’re using SDL_AudioStream for, you could do your resampling/conversions on each channel independently in mono, and then only bother with creating an interleaved buffer later if you need to.

1 Like

Thanks! I was looking at the sdl_audio header file, so I missed the wiki’s SDL_AudioSpec documentation.

Yup, that worked. I’ve had issues keeping up with audio callbacks in managed languages so I thought I might need something more clever. However, the simple algorithm as you described seems to be working just fine. :slight_smile:

So yeah, this is neat, and proof that the new interface is not only cleaner but also opens up new opportunities for higher level languages.

1 Like