icculus / mojoAL

An SDL2-based implementation of OpenAL in a single C file.
https://icculus.org/mojoAL/
zlib License
158 stars 21 forks source link

Potential problem with calculating mojoAl's Buffer size in samples #8

Open ivan-mogilko opened 2 years ago

ivan-mogilko commented 2 years ago

In our program we calculate full playback offset by adding number of samples from every processed buffer. We calculate samples as buffer_samples = (buffer_size * 8) / (buffer_bits * buffer_channels); where buffer values are retrieved using alGetBufferi with AL_SIZE, AL_BITS and AL_CHANNELS respectively.

Recently I've met an issue where this resulted in a wrong number of samples, and found out that this is because mojoAL internally stores samples as float32 in the generated buffer. If we pass a 16-bit sound data into alBufferData (AL_FORMAT_STEREO16 / AL_FORMAT_MONO16), Buffer still reports original input bitness as 16, but at the same time it has samples stores as 32-bit, therefore less samples fit into the same buffer size. This makes it impossible to calculate number samples correctly without "secret knowledge" about mojoAL's implementation, where we have to always use 32 as "buffer bits".

I must admit that I'm not very experienced with OpenAL yet, so still unsure whether this is a valid bug, or we are using a method we are not supposed to.