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!
There are a couple of things we will need:
- The channel the sound is playing on FMOD API | Channel.
- The channel Group the channel belongs to FMOD API | Channel Group.
- The sound that our song is playing from FMOD API | Sound.
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
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).
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.
Hope this helps!