How do I get the "Position" of the Song between beats?

Hi everyone!

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!

Hi,

There are a couple of things we will need:

  1. The channel the sound is playing on FMOD API | Channel.
  2. The channel Group the channel belongs to FMOD API | Channel Group.
  3. 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 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.

Hope this helps!

Hi,

Thank you for your explanation! I am currently facing the same issue where I would like to get the Position of current playback.

I took your suggestion and looked up this script example provided by FMOD https://www.fmod.com/docs/2.02/unity/examples-dsp-capture.html. In the Update function, I have a script like following

        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?

Thanks!

I figured it out, using eventInstance API could achieve what I wanted. The simple line of code goes:
musicInstance.getTimelinePosition(out timelineInfo.position);

1 Like

Hi,

Thank you for sharing the solution!