[openal] Programmatic access to sounds played through OpenAL

Chris Robinson chris.kcat at gmail.com
Fri Sep 4 12:31:35 EDT 2020

On Thu, 3 Sep 2020 19:27:24 -0700
Karkala Hegde <khegde at usc.edu> wrote:

> Hi Chris,
> Thanks for your answer. I followed the same instructions you
> mentioned and tried writing the samples to a wave file, I assume I
> can create the buffer like so:
>     int test_buffer[1024];
> and then populate it using the code you provided.
> You mentioned that if we concatenated all the samples from each frame
> and played at normal speed it would sound the same. How do you
> convert the single dimension buffer array to a 2 channel stereo wav
> file (or two wav files, one for each channel)?

Normally audio is stored with interleaved channels, meaning the samples
for each channel follow consecutively.

buffer[0] | buffer[1] | buffer[2] | buffer[3] | ...
  Left-0  |  Right-0  |   Left-1  |  Right-1  | ...

This is how the loopback extension gives the audio. So if you have a
16-bit stereo sound stored in a

   short buffer[1024];

then to access sample 'n' of channel 'm', you would do

   buffer[n*2 + m]

where '2' can be replaced with the number of channels if different. You
can alternatively declare a multidimensional array as long as the
channel count is fixed:

   short stereo_buffer[512][2];

but that can only work if the channel count is known at compile time.
It won't work correctly if you get mono or other non-stereo data.

To concatenate interleaved audio buffers, just copy all the bytes
from each buffer as-is one after the other. The last channel's last
sample from the first buffer will be followed by the first channel's
first sample from the second buffer, which is correct.

More information about the openal mailing list