Dynamic Music in Unity3D

For the second iteration of the Incarnate prototype discussed in an earlier blog post, we added some dynamic music.

In the prototype, you have this character selection screen where you compose your own character by choosing a body, a mind, and a soul. Each of these correspond to a different part in the music (like the bass line, rhythm section, and main melody).

As you’re creating your character and trying different body/mind/soul combinations, the music is changed dynamically.

Technically speaking, this required us to stop a music loop, and replace it by starting a new music loop precisely at the point where the previous loop left off.
This is done by using the timeSampes value of the previous clip’s AudioSource, and setting it into the AudioSource of the new clip right before it’s played:

newAudioSource.timeSamples = previousAudioSource.timeSamples;

To explain how this works: Digital music is made up of a sequence of very short tones (for instance in the case of CD quality music, these are roughly 44,100 samples per second – or in other words a sample rate of 44.1 kHz).
The timeSamples value is the index of the currently playing sample. Modifying this value before playing a clip allows us to tell Unity’s audio engine exactly where in the clip it should start playing.

To facilitate this, we imposed two main limitations for the audio:

More about the creation of the audio for this prototype can be read on the blog of Somatone Interactive in their interview with Skeleton Hand (the company behind the IP): “Indie Game Feature – Incarnate: Body Mind and Soul”. Somatone Interactive is the company that created the music for the prototype.

One thought on “Dynamic Music in Unity3D”

  1. sebastian malitka says:


Leave a comment

Your email address will not be published. Required fields are marked *