Toot toot! We're still on the polishing train, so this week I added support for some sound effects and music.
Turns out sound is a battle and a half, but at least we've finally got some boom barrels going BOOM now.
Sounds
In my head, getting sounds working was going to go something like this:
- "Hey, I need a sound for a barrel exploding."
- Go to freesound.org1 and search for "explosion" to download a nice boom sound file.
- Open up text editor and type in "when barrel explodes, play the sound from that barrel explosion file".
- Run game and verify that barrels now go boom.
Ha. Ha. Haaaa.
How naive.
Finding sounds
When I actually started browsing Freesound for my barrel explosion, I realised something:
Now, there are real pros recording big booms out there, but they understandably need to recoup the cost of all the damage they're causing, so their stuff doesn't end up on Freesound.
And I can't really justify paying big dollars for a small game prototype, so finding the right sounds was going to take more work.
Ultimately I drew from different sound sources:
- "First taste is free" sampler libraries from big name brands.
- Pick of the lot: Sonniss's 230gb of GDC audio bundles.
- A few small sound effect sets I bought from Humble Bundle years ago.
- And yes, a few gems I found on Freesound. (thanks!)
Searching through gigabytes of downloaded sounds is painful, but I found Sononym helped a little.
Making sounds
Sometimes I couldn't find a decent sound, so - inspired by Jonas Tyroller - I had a go at making my own.
Here is a bow firing:
I rate myself a 1.5 out of 10 in anything-audio, so feedback and tips are appreciated!
Playing sounds
At least playing sounds should be easy, right? Well...
- You want different volume controls for (at least) music vs sound effects.
- Sound playback must be really efficient - otherwise it starts audibly stuttering if the game uses a lot of CPU.
- Sounds can be global or spatially positioned, so you can hear that you're being attacked from the left.
- Maybe effects should sound different in a big hallway, or when you're underwater?
- Should other sounds' volume be reduced ("ducked") when characters are talking to each other?
- Web browsers2 have very little support for modern audio codecs.
It turns out there are whole companies3 who just make software for better audio playback in games. Wow.
But I didn't have time to integrate any of that, so I just hacked something together:
- I play sounds using the very basic facilities provided by Macroquad (the game framework library I'm using).
- I added volume sliders for sound vs music.
- Super basic spatial positioning of sound: further away sounds are played quieter. 4
- To work around browsers' bad audio codec support (and avoid my web host getting angry at me), I disable music in the web build.
That's it! 5
Playable Audible web build
Try out the game with some sound effects here, and let me know in Discord or via email which sound effects are the worst.
I'm not done! Here are some sound effects I haven't added yet:
- Effects for spell summoning and casting - I've been putting these off because there are so many effects needed here.
- Atom-atom interactions like a roar from fire burning or a hiss of acid corroding.
- Physics body noises, like a clang or thud when a barrel or corpse hits the ground.
.. and also a lot of the sound effects I have added still need some tuning or replacement. But you gotta start somewhere.
Known issue: sometimes you hear the bow firing effect but the bow isn't actually fired because the enemy changes their mind and does something else instead before finishing firing. Probably I should make it so that once an enemy starts attacking, they have to actually execute their attack in full?
Also set the licensing filter to "Creative Commons 0" so I don't have to credit a hundred different people for making one sound each.
And just to be clear, when I say Web Browsers, I mean "Safari". It took them 10 years to add support for Ogg Vorbis, and that only shipped 2 weeks ago so I still can't use it.
Even this was a small can of worms: where is the "listener" when you have 2 player characters running around in local co-op?
Initially I thought it should be the center of the screen, since both players would be roughly near that - after all, the camera follows the players around. But that falls apart when the player is at the edge of a level, which causes the camera to snap to the edge of the level - then the player(s) are no longer centered in it, and their sounds get quieter.
Then I figured we should use the average position of players, but that doesn't work so well if two players are together and a third is further away.
Eventually I settled on "each player is a listener", so an effect's volume is determined by the player closest to where the effect was played from. (Seems like fmod takes that approach too.)
Well, I also had to fix a sound library bug that was stopping any sound from playing at all on the web build.
And add in some very basic plumbing to support rollback.
And decide how to manage sound assets.
And work around the sound library's inability to stop only a single instance of sound from playing.
And do some dirty hacks to make attack sounds play at the right time in attack animations.