# Coupling Player Position and Music

**URL:** <https://qa.fmod.com/t/coupling-player-position-and-music/22276>\
**Category:** FMOD Engine\
**Tags:** cpp\
**Created:** [November 8, 2024, 4:15pm UTC](https://qa.fmod.com/t/coupling-player-position-and-music/22276 "2024-11-08T16:15:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrNoob](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/drnoob/32/7518_2.png) [@DrNoob](https://qa.fmod.com/u/DrNoob)\
**Post date:** [November 8, 2024, 4:15pm UTC](https://qa.fmod.com/t/coupling-player-position-and-music/22276/1 "2024-11-08T16:15:35Z")

</div>

Hi!

In my game I want to couple the player’s position to the music, similar to what the game _Braid_ did in one of its worlds. In _Braid_ the player’s x-position was bound to the music in such a way that the music played at regular speed (1x) when the player is running at nominal speed, otherwise the music would slow down (down to 0 when the player doesn’t move).

I guess the easiest way to achieve this in FMOD would be to use the ratio of actual player speed to nominal speed and use that as input for a channel’s _setPitch_ or _setFrequency_. However, in my game it is very important that player position and music do not get out of sync even if the music is 10mins long. That is, I want to ensure that even when the player’s interactivity leads to the music being constantly slowed down and up again, that a specific player position still corresponds to exactly that part of the music (a tolerance of 10-20ms seems to be acceptable).

What would be your suggestion for this kind of scenario? Would using _setPosition_ be an alternative? If so, how often could one call this function? Certainly not every update cycle, right? (50 Hz in my game.) And what would you recommend in terms of buffer size for the corresponding music stream(s)?

Thank you!

Thomas

---

<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:** [November 13, 2024, 3:17am UTC](https://qa.fmod.com/t/coupling-player-position-and-music/22276/2 "2024-11-13T03:17:32Z")

</div>

Using `setFrequency` is definitely the thing to use for matching the playback speed to the player’s speed. The specificity of the correlation between x position and the audio’s playback position is where the complexity comes in, depending on your exact needs.

A simple implementation might do something like just rely on `setFrequency` when the player is moving, and then at specific x values/checkpoints/respawn, manually set the audio’s playback position with `setPosition` to an exact synchronized value. Another good time to sync like this would be when the player’s speed reaches 0, and as a result no audio is playing.

A more complex implementation might do something like calculate the expected playback position vs the current x value, and then perform some kind of drift compensation to interpolate to the target playback position. This _will_ lead to a “more accurate” match between the x position and playback position, but will introduce audible warping, which may be undesirable. A C# example of the math required for this can be found in the [Video Playback](https://www.fmod.com/docs/2.02/unity/examples-video-playback.html) example in our Unity docs, specifically in `SampleFramesAvailable()`.

Combining both of the above methods is also a possibility, but again, it depends on your exact needs. A couple of other things to note though - I likely wouldn’t call `setPosition` every update cycle, but for `setFrequency` it should be fine, as 50hz/20ms is the default update period used for FMOD Studio. Also, if you’re wanting to use `setFrequency` with negative frequency values for reversed playback when the player moves backwards. the sound you’re playing will have to be [created as a sample](https://www.fmod.com/docs/2.02/api/core-api-common.html#fmod_createsample) instead of a stream.

---

<div class="post-metadata">

**Author:** ![DrNoob](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/drnoob/32/7518_2.png) [@DrNoob](https://qa.fmod.com/u/DrNoob)\
**Post date:** [November 14, 2024, 12:46pm UTC](https://qa.fmod.com/t/coupling-player-position-and-music/22276/3 "2024-11-14T12:46:22Z")

</div>

Thank you very much for the detailed explanation and suggestions. I highly appreciate it!

I’ll check how war we can get by “hiding” synchronizations through `setPosition` in several game scenarios and keep the active drift compensation in mind as a fallback.

I have a related question in the following post so I’d greatly appreciate it if you could take a look at that as well:

> [@Channel Frequency of 0 not Respected for Streams](https://qa.fmod.com/t/channel-frequency-of-0-not-respected-for-streams/22290):
>
> Hi! I continuously manipulate the frequency of a channel through game interaction but for some odd reason when I get close to and eventually precisely to 0 the actual frequency stays at 100. I confirmed that I literally call setFrequency with a float value of 0 but if I query the value afterwards using getFrequency I get back 100. I noticed that the problem only happens when I load the corresponding music via FMOD\_CREATESTREAM. If I load it via FMOD\_CREATESAMPLE the problem is not there. The…

---

<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:** [November 18, 2024, 2:43am UTC](https://qa.fmod.com/t/coupling-player-position-and-music/22276/5 "2024-11-18T02:43:33Z")

</div>

No problem!

I’ve also responded to your other post in the thread you linked.
