If it’s not obvious by previous posts or some of my posts on Twitter, I’ve been playing with the Unity platform for a while. The idea is to learn enough of it to be able to create a Unity game for Ludum DareÂ (which, incidentally, will take place again this weekend).
My previous Ludum Dare games where created in Flash, and while I was somewhat happy with my coding effort, the games didn’t have any audio. I decided to make things different next time, and have some simple audio feedback on whatever I built.
In a nutshell, sfxr is an engine for dynamic generation of game-like audio effects. Thomas portedÂ Tomas Pettersson’s originalÂ sfxrÂ to ActionScript, creating a nice API for dynamic audio generation. This API was the basis for usfxr – and really, much of the code in usfxr is a direct translation of as3sfxr, albeit in a more C#-friendly, Unity-happy way.
In practice, what does usfxr do, though? It allows you to publish a game with no static audio assets (all effects are defined by a parameter string), and to generate small variations of the same audio in real time, thus making the audio playback a bit more colorful.
Code for a normal audio effect looks like this:
SfxrSynth synth = new SfxrSynth(); synth.parameters.SetSettingsString("0,,0.032,0.4138,0.4365,0.834,,,,,,0.3117,0.6925,,,,,,1,,,,,0.5"); synth.Play();
While “mutated” versions of the audio can be played like so:
The library is a pure code port, with no GUI whatsoever (you can generate audio strings in as3sfxr‘s GUI, copy it, come back to your C# code, and paste the string) (update July 14th, 2014: usfxr has its own GUI now for generating audio right inside Unity). Other than that, the biggest difference is that audio data is (almost) always generated on a separate thread, so the need for pre-emptive caching is diminished.
I have an example of usfxr in action in aÂ (terrible-looking)Â Shmup example (requires Unity web player):
The full code is available on GitHub. I’ll probably fix a few more things – mainly write proper documentation, add a different sample, and maybe make it faster – but overall it’s working. And obviously, I’ll be using it on Ludum Dare this weekend.
Some input would be welcomed, of course – I’m not a experienced Unity developer.