Handmade Network»Forums
1 posts
To ring buffer or not to ring buffer.

Hello everyone, I hope you all are doing well. Please forgive me if this is a strange question, as I am a beginner in audio programming, and I am struggling to understand some of the concepts behind it. My question is basically what is the best way to send audio data to the audio buffer? In handmade hero Casey uses a ring buffer with direct sound, but does this implementation still hold up today? Libraries like mini audio (https://miniaud.io/index.html) and Sokol audio (https://github.com/floooh/sokol/blob/master/sokol_audio.h) both recommend that you send samples to the audio buffer through a callback function that is running on a separate thread. Personally as I am still a beginner I would rather not deal with multithreading just yet, but if sending samples this way is better than I will happily use callback functions. Alternatively, api's and libraries like ALSA and Sokol allow you to write (or push) samples into a buffer, except it is harder to manage this, as ALSA has much less control over the play and write cursors in the ring buffer (https://hero.handmade.network/forums/game-discussion/t/1030-using_alsa). So... which approach should be used? I'm sure each method has its pros and cons, and I'm interested to hear what you guys have to say about this.

Mārtiņš Možeiko
2446 posts / 2 projects
To ring buffer or not to ring buffer.

It's a bit wrong question to ask. It does not matter which approach is "better". You use the approach the audio API allows. If audio API you use offers callbacks, you use callbacks. If it offers ring-buffer, you use ring-buffer.

That said, you can always use different approach on top of system api. Like if system api offers only callback mode, you can simply have ring-buffer of samples in your own memory. Your main game code filles them up, and callback (whenever it happens) reads it from same memory to submit to back to system audio api.