Custom Audio with Accurate Timings! #20
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is my implementation of a custom audio architecture to suit the creative needs of the game. We want to be able to play many sounds in a way where they can sync up in time perfectly with one another and play with consistent timings (typically based on game ticks). This will allow many sound effects to be layered and combined in creative ways, and for rhythmic patterns to be sequenced.
The meat of this solution is a custom Mixer (implemented as a custom rodio sound source that can be hooked up into bevy audio), which keeps track of time internally and can have any number of sounds queued up to play with metadata about the exact time when they should start playing (among other things). The mixer will get audio samples from all the sources and mix them while respecting the specified timestamps perfectly.
The mixer is not aware of any "global clock". It doesn't actually know our game tick number or real time or anything. It has its own counter that can be reset at any time, and we should try to initialize it so that it runs with a slight delay (in real time) compared to our game's update loop. This will give a bit of an effective "buffer", ensuring sounds don't get cut off.
Implementation progress: