Sound Effects

After playing through the combat demo a few times, I felt a little sound would really liven things up. This turned into a bigger job than expected (doesn’t it always?) but I’m very happy with the result. This post talks about the new audio features in brief, and may be slightly technical if you’re not familiar with Unity.

So how does this work? To start with, let’s take a look at adding a sound manually. What you need to do is add the component DaggerfallAudioSource to any of your GameObjects. This scripts buddies up with Unity’s AudioSource component and feeds it sounds loaded dynamically from Daggerfall’s sound effects library.

Once the component is added, you set a preset (OnDemand, Looping, or None for no changes) and a Sound Index then click Apply(). This will load in the sound from Daggerfall as a regular AudioClip and apply it, along with your preset settings, to the Unity AudioSource. From there you just treat it as a normal audio source like any other and don’t have to worry about it any more. You can even preview sounds directly from the editor by index, id, or name.

Behind the scenes, things are a bit more complicated. As these audio clips are created dynamically they don’t have a matching file in the project hierarchy, and for some reason Unity doesn’t like to serialize procedural sound clips into the scene in the way it serializes stuff like mesh data. The other complication is that settings like “Play On Awake” don’t work as expected as the clip is not present in memory until after the component is running.

To handle all this, the DaggerfallAudioSource component will check to see if your AudioClip has been lost (from starting/stopping play, serialization/deserialization, recompile, etc.) then loads it back into memory again. It also handles booting the sound manually to simulate Play On Awake.

This is very lightweight and under normal play sessions the clip will only be loaded once at start. It’s only inside the editor where the clip may need to be loaded multiple times a session due to various reasons the editor dumps volatile data. It all just works as expected, even when using the little speaker to preview scene audio.

The other thing which caused some grief was actually converting the 8-bit PCM audio from Daggerfall into floats for AudioClip.SetData(). This needed special wrangling of the float values to convert bit depths properly, and the Unity docs weren’t much help here. A bit of Googling and face-desking saw me through to the end. Mostly, this was only hard because I can be an idiot sometimes.

On the support front, there’s a few other goodies coming with the sound update. I’ve created a SoundClips enum with about 80% of Daggerfall’s effects given a plain text, descriptive name. Where possible, I have grouped these by usage as I understand them. I also managed to work out how Daggerfall assigns sounds to enemies and action triggers, and put this into the toolset and API where appropriate. See below for updated DaggerfallAction editor window.

New DaggerfallAction Editor Window

The new number above is “Action Sound”. This is the sound ID to play when this action is triggered. I had to break out my hex editor again, as this action-sound mapping was previously unknown.

The next bit of support is you can import sound effects with dungeons, enemies, etc. Daggerfall Tools for Unity wires up all the audio sources for you to automatically bring your scenes to life. You can also create, change, and play sounds completely at run-time.

Last but not least, I’ve created a “spooky sound player” example component to randomly play those wonderful dungeon sounds (e.g wind blowing, doors opening, water dripping, etc.). This will later be joined by other example scripts as part of the tutorial series I’m working on.

I should have a new demo ready for you over the weekend with a full complement of sound effects to enjoy.

Posted in Daggerfall Tools, Daggerfall Tools for Unity, Technical Content.

One Comment

Comments are closed.