🔊 Adding Sound FX to a Ratatui Game #
In this post, we look at Ratatui audio with Rodio. I have been building a text-based User Interface (TUI) game in Ratatui. Ratatui is Rust tooling for Terminal apps. This is the third post in the series. I wrote the initial post once I had put together a minimal viable product, and outlined some next steps in that post. In the last post, I added fireworks to the victory screen by painting in a Ratatui canvas widget.
As well as fireworks, another enhancement that I identified (in the first post) was sound effects. I have built Rust games with sound before, but relied on the game’s tooling to add manage loading the audio assets and playing them (Macroquad and Bevy, for example, make this seamless). So, the first step was going to be finding a crate to play MP3 audio. I discovered Rodio, which worked well, and was quick to get going with.
In the rest of this post, I talk about the Rodio integration, and some next steps for the game. There is a link to the latest project repo, with full code further down.
🧱 Ratatui Audio with Rodio: What I Built #
I didn’t really want background music, just sound effects to play when the player starts the challenge, and then to provide audio feedback if their latest solution attempt was good or perfect. Finally, I wanted to play some audio when each firework on the victory screen ignited. I found some quite small wave files for each of these, and converted them to MP3s.
About Rodio #
Rodio uses a number of lower level crates under the hood, simplifying adding audio via their single higher level API. These, lower-level, crates include:
-
cpal
for playback; -
symphonia
for MP4 and AAC playback; -
hound
for WAV playback; -
lewton
for Vorbis playback; and -
claxon
for FLAC playback.
I just needed MP3s, so disabled default features, and only added symphonia-mp3
back in the project Cargo.toml
, to keep the binary size in
check:
🐎 Adding Rodio #
The Rodio docs give a couple of examples for getting going. Those examples read and decode an ogg file from a local folder within the project. This makes uses of the Rodio decoder struct, either appending it to a Rodio sink, or playing decoder output directly on an output stream. Either way, the audio is decoded and played straight away.
That setup works well for longer files, played once. For the game, I have a few sound effects that
might be played dozens of times. It seemed a little extravagant to read from a file and decode
each time I needed to play the same sound effect. Luckily, I found a Stack Overflow post with an alternative approach , letting you buffer the decoder output. Since the audio files were no bigger that 10 KB
, I was happy to buffer them as the app starts up and keep them in memory.
Rust Code #
I created a SoundEffects
struct for holding all the sound effects,
as there are only five of them. For an app with more, I would attempt a cleaner solution, but this
approach keeps things simple for what I have.
The default initializer for the SoundEffects
struct just creates a
buffered decoding for each of the effects, which can be called later from the app code.
In app code, I can then clone one of these buffers and add it to a sink to play it. For example in the main game loop:
We initialize the sink ahead of the main loop, then play the buffered sound from within it (line 19
), by calling sink.append()
. sink.append()
pushes the buffer into a queue and plays it immediately (if nothing is already playing), or waits
until the last sound has finished before starting. That setup works here, and if you need to play sounds
simultaneously, you can create multiple sinks. This setup also avoids borrow checker issues with consuming
the File
struct, which might arise when decoding the File
within the main loop.
🗳 Poll #
🙌🏽 Ratatui Audio with Rodio: Wrapping Up #
In this Ratatui audio with Rodio post, I briefly ran through how I added audio to the Ratatui Countdown game. In particular, I talked about:
- why I added Rodio;
- why you might buffer Rodio to avoid borrow checker issues in a game loop; and
- how I buffered MP3 sound FX with Rodio.
I hope you found this useful. As promised, you can get the full project code on the Rodney Lab GitHub repo . I would love to hear from you, if you are also new to Rust game development. Do you have alternative resources you found useful? How will you use this code in your own projects?
🙏🏽 Ratatui Audio with Rodio: Feedback #
If you have found this post useful, see links below for further related content on this site. Let me know if there are any ways I can improve on it. I hope you will use the code or starter in your own projects. Be sure to share your work on X, giving me a mention, so I can see what you did. Finally, be sure to let me know ideas for other short videos you would like to see. Read on to find ways to get in touch, further below. If you have found this post useful, even though you can only afford even a tiny contribution, please consider supporting me through Buy me a Coffee.
Just dropped a new post on adding audio to a Ratatui Rust Terminal app or game, using Rodio.
— Rodney (@askRodney) June 19, 2024
We look at Rodio Symphonia MP3 features and how you can buffer decoded MP3 audio.
Hope you find it useful!
#askRodneyhttps://t.co/FZRoEoTHdD
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on X (previously Twitter) and also, join the #rodney Element Matrix room. Also, see further ways to get in touch with Rodney Lab. I post regularly on Game Dev as well as Rust and C++ (among other topics). Also, subscribe to the newsletter to keep up-to-date with our latest projects.