I’ve just started to use FMOD in Unity and, using the TIMELINE_BEAT_PROPERTIES, have created the base for a rhythm game, which starts events whenever a beat is hit thanks to FMOD communication with Unity. It currently works like a Metronome, so to say.
However, something I would want for this game is to find the position that the song is in at every given moment between each beat. The reason why I would want this is because I would like a slider to indicate the “position” between a cycle of 4 beats, similar to Parappa the Rapper where to player can see where they will place their inputs.
Any help on achieving this effect of a slider essentially moving with the song in realtime would be greatly appreciated, thanks!
Before we do anything we will need to give the FMOD system a second to start up correctly so I would avoid calling any of these functions in the Start() function.
We use FMODUnity.RuntimeManager.CoreSystem.getMasterChannelGroup(out FMOD.ChannelGroup masterCG), the Master Channel Group is where all sounds ultimately route to. From there, dig down using getGroup() till you find the channel group with the channel playing the sound. With the channel group, you can get the current channel using currentChannelGroup.getChannel(out FMOD.Channel currentChannel) which will contain our sound currentChannel.getSound(out FMOD.Sound currentSound).
The sound will have the length of the song which we will use to assign the maxValue of the slider. currentSound.getLength(out uint length, FMOD.TIMEUNIT.MS).
In the Update() loop once the sound has been retrieved you can use currentSoundChannel.getPosition(out uint currentPlaybackPos, FMOD.TIMEUNIT.MS) to assign the slider.value which will display the current position of the song every frame.
If you need any more assistance I am happy to provide more code snippets.
FMOD.ChannelGroup masterCG;
if (FMODUnity.RuntimeManager.CoreSystem.getMasterChannelGroup(out masterCG) == FMOD.RESULT.OK)
masterCG.getChannel(mChannels - 1, out currentSoundChannel);
currentSoundChannel.getPosition(out currentPlaybackPos, FMOD.TIMEUNIT.MS);
Debug.Log("Current playback ms : " + currentPlaybackPos);
}
But what I could see from the Log is that currentPlaybackPos is always 0, the music gets played well in the game though which I assume that the sound should be successfully routed to the ChannelGroup. Could you provide any tips how to investigate further?
I figured it out, using eventInstance API could achieve what I wanted. The simple line of code goes: musicInstance.getTimelinePosition(out timelineInfo.position);