( Headphones recommended!
)
We recently decided to roll our own audio loop for Swap using cpal and it has been very fun to think and learn about audio code. We switched away from using the higher level rodio library which has served us well up until this point.
The motivation for the switch was to have more control over filters and effects we can dynamically apply to the game’s sound to make environments in Swap feel more immersive.
One step towards more immersion was implementing reverb – specifically I decided to implement a convolution-based reverb.
It turns out that if you use a microphone and record a very short immediate signal, sort of a short clap/snap/click – you get what’s called the impulse response of the place you recorded it. This impulse response encodes qualities of how the place you did the recording echoes/reverbs/affects sounds at that location.
Now, using the impulse response from for example a cave/church/forest (some of which can be freely obtained from the openair database https://www.openair.hosted.york.ac.uk/), we can convolve any sound effect in the game with the impulse response in frequency space and make them feel as if they were played in the location
A bonus is also that we can now have a nice visualization of the audio signal:
While most of the code has been “learn-as-we-go” “trial and error”, some useful resources have been:
- The initial Handmade Hero episode on audio as a high-level introduction https://www.youtube.com/watch?v=uiW1D1Vc7IQ
- Sound decoding libraries in rust to turn files into samples: minimp3 for MP3, hound for WAV, lewton for OGG
- quad-snd as a reference implementation of how to structure a simple audio loop with a tx/rx channel to have audio controls in the game code
- The cpal beep example to get started with that specific library
- Good discussion with Gustav Almström who had more experience writing audio code than me: https://gustavalmstrom.com/
If you have any questions don’t hesitate to reach out to us at idnointeractive@gmail.com
Follow the development of Swap by joining our discord! discord.idno.se/
Leave a Reply