# Rythm game and latency

**URL:** <https://qa.fmod.com/t/rythm-game-and-latency/20973>\
**Category:** Unity\
**Tags:** unity\
**Created:** [December 10, 2023, 11:01am UTC](https://qa.fmod.com/t/rythm-game-and-latency/20973 "2023-12-10T11:01:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![VincentAbert](https://avatars.discourse-cdn.com/v4/letter/v/0ea827/32.png) [@VincentAbert](https://qa.fmod.com/u/VincentAbert)\
**Post date:** [December 10, 2023, 11:01am UTC](https://qa.fmod.com/t/rythm-game-and-latency/20973/1 "2023-12-10T11:01:31Z")

</div>

Hello everyone!

Part of an upcoming project will include a rythm mini-game. My approach so far was to simply play a music event in FMOD, and in parallel inside of Unity trigger pre-determined rythms. It is mostly okay, but as you might expect there is a bit of latency between the two systems that are completely independent, and it gets worse with frame drops. I did see we could edit the DSP Buffer Size to diminsh latency, but the documentation seems to say it shouldn’t be used, and since this minigame is just a small part of the project, I wouldn’t want to ruin everything for this. So my question is “Is there a way to make sure a specific event is played with very little latency ?”

Or do you see another way to achieve my goal ?

Thank you !

---

<div class="post-metadata">

**Author:** ![Leah\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/leah_fmod/32/3685_2.png) [@Leah\_FMOD](https://qa.fmod.com/u/Leah_FMOD)\
**Post date:** [December 12, 2023, 6:16am UTC](https://qa.fmod.com/t/rythm-game-and-latency/20973/2 "2023-12-12T06:16:22Z")

</div>

> [@VincentAbert](#):
>
> Is there a way to make sure a specific event is played with very little latency ?

The main this is to ensure your event instance is created, and its sample data loaded, ahead of time before starting your rhythm game - see the [Sample Data Loading](https://www.fmod.com/docs/2.02/api/studio-guide.html#sample-data-loading) section of the Studio API Guide

Note that if you’re using the [default bank loading setting](https://www.fmod.com/docs/2.02/unity/settings.html#load-bank-sample-data) for the FMOD for Unity plugin, your banks and therefore will have their sample data loaded ahead of time, which will minimize this being an issue.

However, instead of the event latency, you may wish to consider driving triggering your Unity rhythms from FMOD, as this will ensure the systems are working in tandem as opposed to in parallel. You can do so with timeline callbacks, which you can set to trigger on specific timeline markers, as well as beats/bars given a specific tempo and time signature. Please take a look at the [Timeline Callback scripting example](https://www.fmod.com/docs/2.02/unity/examples-timeline-callbacks.html) in our Unity documentation for a basic example of doing this.

---

<div class="post-metadata">

**Author:** ![VincentAbert](https://avatars.discourse-cdn.com/v4/letter/v/0ea827/32.png) [@VincentAbert](https://qa.fmod.com/u/VincentAbert)\
**Post date:** [December 12, 2023, 11:06am UTC](https://qa.fmod.com/t/rythm-game-and-latency/20973/3 "2023-12-12T11:06:23Z")

</div>

Thanks a lot for the help! The only issue with timeline callbacks is that they are not continuous, right, they only happen on beats ? That would be an issue because I want the notes to move constantly, with some look ahead (exactly like in guitar hero for example). But I did realise I could just use getTimelinePosition, which seems to work as intended 🙂

Are there some drawbacks to reading the timeline position every frame, or is it fine ?

Thanks again !

---

<div class="post-metadata">

**Author:** ![Leah\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/leah_fmod/32/3685_2.png) [@Leah\_FMOD](https://qa.fmod.com/u/Leah_FMOD)\
**Post date:** [December 14, 2023, 1:20am UTC](https://qa.fmod.com/t/rythm-game-and-latency/20973/4 "2023-12-14T01:20:32Z")

</div>

> [@VincentAbert](#):
>
> The only issue with timeline callbacks is that they are not continuous, right, they only happen on beats ?

This is correct, but you could, for example, schedule beat-length or bar-length portions of a patterns ingame based on the timeline callback.

> [@VincentAbert](#):
>
> But I did realise I could just use getTimelinePosition, which seems to work as intended 🙂
> 
> Are there some drawbacks to reading the timeline position every frame, or is it fine ?

It’s completely fine, there shouldn’t be any notable drawbacks. It’s mostly a matter of what best suits your use-case.

---

<div class="post-metadata">

**Author:** ![VincentAbert](https://avatars.discourse-cdn.com/v4/letter/v/0ea827/32.png) [@VincentAbert](https://qa.fmod.com/u/VincentAbert)\
**Post date:** [December 14, 2023, 8:32am UTC](https://qa.fmod.com/t/rythm-game-and-latency/20973/5 "2023-12-14T08:32:56Z")

</div>

Thanks a lot, that’s super helpful! I do have an additional issue that came up, which I’ll ask here since it’s still related to the thread : so I have the main song playing, and the player has to press specific keys in rythm. I’d like the key pressing to trigger an audio feedback, but that means it has to have minimum latency, otherwise it’s very jarring when compared to the main song playing. So I have this placeholder click sound (very short, very light), it doesn’t stream, the samples are loaded beforehand as you recommended, but there is still a noticeable delay which makes playing it very annoying. I tried using PlayOneShot as well as just cache an Instance that I replay, but it doesn’t seem to change anything… Is this kind of musical precision just not possible with FMOD ?

Thanks anyway for your help!

---

<div class="post-metadata">

**Author:** ![Leah\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/leah_fmod/32/3685_2.png) [@Leah\_FMOD](https://qa.fmod.com/u/Leah_FMOD)\
**Post date:** [December 19, 2023, 4:25am UTC](https://qa.fmod.com/t/rythm-game-and-latency/20973/6 "2023-12-19T04:25:27Z")

</div>

That kind of precision is possible, but due to the asynchronous nature of the Studio system, depending on your settings and how an event is set up you may observe a higher amount of latency than you expect. As previously noted, normally a combination of preload sample data, and creating your event instance ahead of time and then starting as needed should mitigate most potential latency. Exactly how much latency are you observing?

As a side note, you can see how much latency is being introduced by the Studio system by recording a profiler session via live update, observing when a specific API command is processed in the [API calls view](https://www.fmod.com/docs/2.02/studio/profiling.html#api-calls-view), and comparing it to when the audible effect of that command occurs.
