{"assets":[],"author_link":"author\/zeh\/","author_name":"zeh","cat":"LD #26","categories":["LD #26"],"comments":[],"epoch":1366619820,"event":"LD26","likes":15,"metadata":{"p_key":"78907","p_author":"zeh","p_authorkey":"0","p_urlkey":"287917","p_title":"Introducing usfxr: generate audio effects dynamically inside Unity games","p_cat":"LD #26","p_event":"LD26","p_time":"1366619820","p_likes":"15","p_comments":"0","p_status":"WAYBACK","us_key":null,"us_name":null,"us_username":null,"event_start":"1366934400","event_key":"16","event_name":"LD26"},"source_url":"2013\/04\/22\/introducing-usfxr-generate-audio-effects-dynamically-inside-unity\/","text":"<p>In preparation for the next Ludum Dare, I\u2019ve playing around with Unity for the past few months and there\u2019s something I\u2019d like to share with other aspiring game developers.<\/p>\n<p>My previous Ludum Dare games where created in Flash, and while I was somewhat happy with my coding effort, the games didn\u2019t have any audio. I decided to make things different next time, and have some simple audio feedback on whatever I built.<\/p>\n<p>To get myself acclimated with the language, I decided to port Thomas Vian\u2019s <a href=\"https:\/\/code.google.com\/p\/as3sfxr\/\">as3sfxr<\/a> to Unity. What\u2019s as3sfxr, you ask?<\/p>\n<p><a href=\"https:\/\/vimeo.com\/15769163\">This short video<\/a> from Flash on the Beach 2010 has a great explanation. In a nutshell, however, sfxr is an engine for dynamic generation of game-like audio effects. Thomas ported Tomas Pettersson\u2019s original <a href=\"http:\/\/www.drpetter.se\/project_sfxr.html\">sfxr<\/a> to ActionScript, creating a nice API for dynamic audio generation. This API was the basis for usfxr \u2013 and really, much of the code in usfxr is a direct translation of as3sfxr, albeit in a more C#-friendly, Unity-happy way.<\/p>\n<p>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.<\/p>\n<p>Code for a normal audio effect looks like this:<\/p>\n<pre>SfxrSynth synth = new SfxrSynth();\r\nsynth.parameters.SetSettingsString(\"0,,0.032,0.4138,0.4365,0.834,,,,,,0.3117,0.6925,,,,,,1,,,,,0.5\");\r\nsynth.Play();<\/pre>\n<p>While \u201cmutated\u201d versions of the audio can be played like so:<\/p>\n<pre>synth.PlayMutated();<\/pre>\n<p>The library is a pure code port, with no GUI whatsoever (you can generate audio strings in <a href=\"http:\/\/www.superflashbros.net\/as3sfxr\/\">as3sfxr<\/a>\u2018s GUI, copy it, come back to your C# code, and paste the string). 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.<\/p>\n<p>I have an example of usfxr in action in a <a href=\"http:\/\/hosted.zehfernando.com\/ludumdare\/usfxr\">(terrible-looking) Shmup example<\/a> (requires Unity web player):<\/p>\n<p><a href=\"http:\/\/hosted.zehfernando.com\/ludumdare\/usfxr\/\"><img alt=\"usfxr SpaceGame example\" class=\"aligncenter size-full wp-image-1441\" height=\"375\" src=\"http:\/\/zehfernando.com\/wp-content\/uploads\/2013\/04\/usfxr.png\" width=\"600\"\/><\/a><\/p>\n<p>The full code is <a href=\"https:\/\/github.com\/zeh\/usfxr\">available on GitHub<\/a>. I\u2019ll probably fix a few more things \u2013 mainly write proper documentation, add a different sample, and maybe make it faster \u2013 but overall it\u2019s working. And obviously, I\u2019ll be using it on Ludum Dare this weekend.<\/p>\n<p>Some input would be welcomed, of course \u2013 I\u2019m not a experienced Unity developer.<\/p>\n<p><small><em>This post is a shortened version of something I\u2019ve originally published <a href=\"http:\/\/zehfernando.com\/2013\/introducing-usfxr\/\">on my own website<\/a>.<\/em><\/small><\/p>","time":"April 22nd, 2013 8:37 am","title":"Introducing usfxr: generate audio effects dynamically inside Unity games","title_was_empty":false}