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

Setting the number of channels for a Buffer #21

Closed karamellpelle closed 6 years ago

karamellpelle commented 6 years ago

If I load a file into a Buffer, is it possible to set the number of channels for this Buffer regardless of the number of channels in the file (or is this a feature which could be implemented?)?

Because according to the OpenAL reference(5.3.4. Specifying Buffer Content), "Buffers containing audio data with more than one channel will be played without 3D spatialization features – these formats are normally used for background music.". If we had this feature, it would be easy to define a sound as a 2D or 3D sound for any file.

kcat commented 6 years ago

Source objects have the set3DSpatialize method. Given the AL_SOFT_source_spatialize extension (available since OpenAL Soft 1.18), that allows 3D spatialization to be applied regardless of the buffer's channel count; the sound will be dynamically downmixed to mono and panned as expected when not placed exactly on the listener. You can also force spatialization off, so even mono sounds avoid panning, distance attenuation, and doppler effects. Or set to auto (default), it behaves like standard OpenAL.

This way avoids having to potentially load the file twice with the extra buffer management (one for stereo/multichannel, and another for the mono copy). It also doesn't have to be concerned with unnecessary conversions (e.g. stereo to 5.1).

karamellpelle commented 6 years ago

This seems to be the solution - thanks!