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;
previousAudioSource.Stop();
newAudioSource.Play();
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:
- Each loop needs to have the same sample rate
- Each loop needs to have the same length (precisely the same amount of samples)
- The audio needs to be used uncompressed in memory, to avoid potential issues with compressed audio (see the documentation of an Audio Clip’s “Audio Format” and “Load Type” in the Unity Manual).
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.
hehe