kcat / alure

Alure is a utility library for OpenAL, providing a C++ API and managing common tasks that include file loading, caching, and streaming
zlib License
70 stars 20 forks source link

Request: Callback for accessing data before sound buffers are initialized #6

Open Silverlan opened 7 years ago

Silverlan commented 7 years ago

I've recently switched from using OpenAL Soft directly to using alure, and I've run into a small problem: When loading sound data, I need to be able to convert some stereo sound data to mono before it's being written to a buffer with alBufferData. Alure doesn't provide any way of accessing the data before the buffer is initialized, so I had to make some modifications to the alure source code to be able to do that. It would be nice to have some sort of callback or something like that in the future.

kcat commented 6 years ago

The MessageHandler class has a bufferLoading callback which provides the audio data that's about to be loaded. It's read-only access though, because Alure has already checked that everything looks good and doesn't want it meddled with before OpenAL gets it, which would otherwise add to the risk of causing an error.

Out of curiosity, is there a reason you need to convert the sound from stereo to mono? If it's to make the sound play in 3D regardless, you can use the source's set3DSpatialize method to make non-mono sounds play in 3D (which utilizes the AL_SOFT_source_spatialize extension to do the downmix at runtime). This has the benefit of allowing the sound to still be stereo when it's not in 3D and dynamically become mono when it's positioned in 3D space.

Alternatively you can use the context's createBufferFrom method with a decoder wrapper that downmixes the audio. Create the main decoder using the createDecoder method and have a custom Decoder type that forwards calls to it, overriding getChannelConfig to always return Mono and do downmixing in the read method.