[Feature Request] Create Empty Buffer and Get Pointer to Buffer Data
Hey,
I was wondering if it were possible to create an empty buffer with X length, and then get a pointer to this region of memory, similar to glMapBuffer in OpenGL.
At the moment on startup, I load all audio resources into memory in a background thread, and then run alBufferData on one audio resource per frame on the main thread, for smooth resource loading while the game starts.
This involves one extra memory copy, i.e. disk -> game memory -> OpenAL memory, whereas it would be great if I could load from disk straight into an OpenAL buffer.
Not a huge priority feature, but it would be nice to have :)
There is an experimental AL_SOFTX_map_buffer extension. Among other things, it defines the functions
AL_API void AL_APIENTRY alBufferStorageSOFT(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq, ALbitfieldSOFT flags);
AL_API void* AL_APIENTRY alMapBufferSOFT(ALuint buffer, ALsizei offset, ALsizei length, ALbitfieldSOFT access);
AL_API void AL_APIENTRY alUnmapBufferSOFT(ALuint buffer);
that you can get with alGetProcAddress. So you can load sounds directly into the buffer like this:
/* Set up the internal storage for the buffer, with the appropriate format. Passing NULL for data leaves
* the stored data undefined. Declare that the buffer can be mapped for writing only. This is called
* instead of alBufferData.
*/
alBufferStorageSOFT(buffer, format, NULL, size /* in bytes */, samplerate, AL_MAP_WRITE_BIT_SOFT);
/* Map the whole buffer, getting a writable memory pointer (trying to read from it is undefined behavior). */
void *bufferptr = alMapBufferSOFT(buffer, 0, size, AL_MAP_WRITE_BIT_SOFT);
/* Now fill in bufferptr, according to the previously specified format. */
...
/* Now unmap the buffer, committing what was written. The pointer is no longer valid to use. The buffer
* can now be attached to a source and used like normal.
*/
alUnmapBufferSOFT(buffer);
The extension is experimental, so it is subject to change in incompatible ways in future releases (though unlikely to in the foreseeable future), but you're welcome to try it out and give feedback on how it works or if there could be any improvements.