Audio support & music streaming!
This is another update on current NEON86 tech. With the introduction of audio support backed by DirectSound, there was no difficulty loading a WAV sound file and play your favorite sound, set frequency, volume, or speaker panning. This functionality works well for sound effects.
However, games often feel empty if the music is absent. Unfortunately, the WAV file format is unsuitable for distributing our music, as for instance, we want to have at least 5-6 songs present in the game, which would increase the game size dramatically.
The first thing that came to my mind was to grab the stb_vorbis and decode the whole OGG file into my sound buffer. This approach works, but at a cost: decoder speed. Ideally, you don't want to decode a whole OGG song mid-play to avoid stutters, so you would do it at startup, however with 5-6 songs this becomes an apparent problem, as the load times increase significantly. It was obvious we need to seek an alternative solution.
Here comes music streaming! With a slight refactor, we now present 2 distinct sound classes for various purposes:
- Sound: usually used for playing sound effects, supports WAV and OGG format, has more advanced controls such as an ability to set playback position, set playback frequency, future support for 3D audio.
- Music: plays OGG files only, suitable for music, decodes data on demand, very performance-friendly.
Music streaming uses buffer indicators to emit events that get processed by the engine. These indicators get fired when the buffer play cursor reaches the 0.5 secs and 1.5 secs marks of the 2 seconds long playback buffer.
Once we detect a fired event, we ask the stb_vorbis decoder to pull more data from file and we store it to the playback buffer. The playback is set on loop so it can process new data written before play cursor as well, this ensures seamless transition between pulled sound samples and distract-free music playback.
These events also effectively detect if we're at the end of playback and stop the buffer from being played.
All in all, these changes ensure painless music playback with finer control. You can look at an example use case of playing a music playlist in a game at NEON SLAYER game.
I plan to expose DirectSound 3D audio support later on, to further expand the possibilities with the audio in this engine.
Get NEON 86
Leave a comment
Log in with itch.io to leave a comment.