# dspClockTime is out of sync after event instantiation?

**URL:** https://qa.fmod.com/t/dspclocktime-is-out-of-sync-after-event-instantiation/16523
**Category:** FMOD Engine
**Created:** [December 4, 2020, 10:49pm UTC](https://qa.fmod.com/t/dspclocktime-is-out-of-sync-after-event-instantiation/16523 "2020-12-04T22:49:32Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![pk1234](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@pk1234](https://qa.fmod.com/u/pk1234)
#### Post date: [December 4, 2020, 10:49pm UTC](https://qa.fmod.com/t/dspclocktime-is-out-of-sync-after-event-instantiation/16523/1 "2020-12-04T22:49:32Z")

</div>

Hey guys, I’m working on a synchronization game in Unity that requires precise knowledge of the time of the song that is playing, for event triggering and beat related things etc.

I’m using getDSPClock to get the dspClock time of the song event and it works relatively fine, but there’s this weird thing that when the event is first instantiated, the initial dsp time is delayed by around 2 seconds.

A simple example of what I mean - I have code similar to this in Update()

ulong dspClockTime;  
ulong parentClockTime;  
ChannelGroup musicChannelGroup;  
eventInstance.getChannelGroup(out musicChannelGroup);  
musicChannelGroup.getDSPClock(out dspClockTime, out parentClockTime);  
var value = ((double)dspClockTime) / sampleRate;  
Debug.Log("time: " + value);

and 0 is printed initially.

Then when I call (not starting the event, only instantiating it!)  
eventInstance = RuntimeManager.CreateInstance(musicEventRef);

the block of code mentioned above starts logging “time: 2.1” or so. The values are somewhere around 2 seconds, and they are relatively random. Re-instantiating the event keeps adding time to the dsp clock.

I’d like to understand what’s happening, and how to deal with this issue and get precise time of the song.

One simple idea is that I guess I could just save this “delay” amount that the dspClock returns after event instantiating, and always decrease the time I get by this amount, to get the “real” timeline of the playing song.  
But I’m not sure whether maybe there won’t be any extra delay in the dspClock once I actually start the event - maybe after I call start() on the event instance, the dspClock will again keep increasing or something for a while, without anything actually happening - so perhaps I should save the “delay”/dspClock time once the playback state returned by the event instance is “Playing”?

So basically, how should I get rid of the delay? When/how can I figure out what the final “delay” is that I should subtract from the resulting value, to get the real, precise time of the song playing?

---

<div class="post-metadata">

### Author: ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)
#### Post date: [December 14, 2020, 3:34am UTC](https://qa.fmod.com/t/dspclocktime-is-out-of-sync-after-event-instantiation/16523/2 "2020-12-14T03:34:09Z")

</div>

Are you able to make use of [Studio::EventInstance::getTimelinePosition](https://fmod.com/resources/documentation-api?version=2.1&page=studio-api-eventinstance.html#studio_eventinstance_gettimelineposition) instead?

It looks like the dspClock is being inherited from the channelGroups parent and there does not appear to be a way to stop that from happening.

When using the Studio API it is recommended to not dig into the Core API as it can cause some strange behavior.

---

<div class="post-metadata">

### Author: ![pk1234](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@pk1234](https://qa.fmod.com/u/pk1234)
#### Post date: [December 14, 2020, 8:00pm UTC](https://qa.fmod.com/t/dspclocktime-is-out-of-sync-after-event-instantiation/16523/3 "2020-12-14T20:00:13Z")

</div>

Hello, thanks for the reply.

I could use getTimelinePosition but I was under the impression that dspClock is more suitable for precise rhythm related things, and that there could be issues with pausing/unpausing if using the studio api method (and perhaps other issues/possible stutter etc.) - I made this conclusion based off this post:

> [@Off sync on position "getTimelinePosition" when using "setPaused" method.](https://qa.fmod.com/t/off-sync-on-position-gettimelineposition-when-using-setpaused-method/13069/2):
>
> Hi HJ It’s difficult to reason about how the timeline position is going out of sync with the game without understanding what in-sync with the game means. I’m guessing you have some sort of timer in your game which you pause at the same time that you call FMOD.Studio.EventInstance.setPaused? FMOD.Studio.EventInstance.setPaused is an asynchronous operation - calling setPaused posts a command to the FMOD studio update thread and returns immediately, there is then some latency before the command …

I’d be willing to not use the Studio API in this specific script if that helps, though I’d definitely like to use the studio api elsewhere. I have no idea how these things work on a lower level though so it might be a bad idea, but the event is just a song without any effects, so it might not be too problematic?

I’m also willing to just subtract the time in some fashion, but I’d like to reliably know what it is that I should subtract. It’s hacky, but I’m fine with it if it works.

---

<div class="post-metadata">

### Author: ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)
#### Post date: [December 16, 2020, 5:04am UTC](https://qa.fmod.com/t/dspclocktime-is-out-of-sync-after-event-instantiation/16523/4 "2020-12-16T05:04:47Z")

</div>

It is fine to use the Core API along side the Studio API to create and use sounds, just not to dig into the the Studio side of things.

Depending on what you are wanting to do, you could also look into using Timeline Callbacks on an event.  
[`FMOD_STUDIO_TIMELINE_BEAT_PROPERTIES`](https://fmod.com/resources/documentation-api?version=2.1&page=studio-api-eventinstance.html#fmod_studio_timeline_beat_properties) & [`FMOD_STUDIO_TIMELINE_MARKER_PROPERTIES`](https://fmod.com/resources/documentation-api?version=2.1&page=studio-api-eventinstance.html#fmod_studio_timeline_marker_properties), there is also an example on using them in Unity: [FMOD - Scripting Examples | Timeline Callbacks](https://fmod.com/resources/documentation-unity?version=2.1&page=examples-timeline-callbacks.html).

---

<div class="post-metadata">

### Author: ![pk1234](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@pk1234](https://qa.fmod.com/u/pk1234)
#### Post date: [December 16, 2020, 6:34pm UTC](https://qa.fmod.com/t/dspclocktime-is-out-of-sync-after-event-instantiation/16523/5 "2020-12-16T18:34:25Z")

</div>

I see. Using timeline callbacks isn’t really an option for me, but thank you for the idea.

It seems like that the recommended options are to either stick with the Studio API getTimelinePosition call, or create my own sound solution for this one sound, fully inside of Core API, do I understand this correctly?

I’m sorry to insist about this, but would you explain what the issue is with simply subtracting the timeline position upon playing? If parent dsp clock time is reliably added upon initialization of the event, I can’t see much of a problem with just subtracting it. It would save me a lot of time to use a method like this, as I wouldn’t have to learn how to use the Core API for actual sound playing etc.

---

<div class="post-metadata">

### Author: ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)
#### Post date: [December 17, 2020, 12:48am UTC](https://qa.fmod.com/t/dspclocktime-is-out-of-sync-after-event-instantiation/16523/6 "2020-12-17T00:48:59Z")

</div>

> [@pk1234](#):
>
> It seems like that the recommended options are to either stick with the Studio API getTimelinePosition call, or create my own sound solution for this one sound, fully inside of Core API, do I understand this correctly?

Yeah, basically.

> [@pk1234](#):
>
> I’m sorry to insist about this, but would you explain what the issue is with simply subtracting the timeline position upon playing?

It looks like both the clock times returned from `getDSPClock` will always be the same, although caching the initial value of the clock and subtract that from the new value returned should be fine.

---

<div class="post-metadata">

### Author: ![pk1234](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@pk1234](https://qa.fmod.com/u/pk1234)
#### Post date: [December 17, 2020, 6:03pm UTC](https://qa.fmod.com/t/dspclocktime-is-out-of-sync-after-event-instantiation/16523/8 "2020-12-17T18:03:53Z")

</div>

Thanks a ton for your help!
