# RhythmEngineCore

The main class that handles the playback and timing of a song. This script requires an empty audiosource to be attached to the same gameobject. This ISN'T the audio source that will be used for playback, this is just used to get the audio thread time.

## Fields

<table data-full-width="true"><thead><tr><th width="284">Field</th><th>Description</th></tr></thead><tbody><tr><td>AudioSource MusicSource</td><td>Audio source that the music is played from.</td></tr><tr><td>bool ManualMode</td><td>If true, the song won't start playing automatically, you'll have to call SetSong()/SetClip(), InitTime() and Play() manually</td></tr><tr><td>bool IsSourcePlaying</td><td>True if the music source is playing.</td></tr><tr><td>bool HasStarted</td><td>True if the current song time is greater than 0</td></tr><tr><td>bool IsPaused</td><td>True if the song is not playing</td></tr><tr><td>float CurrentBpm</td><td>Current BPM of the song</td></tr><tr><td>double DeltaTime</td><td>More accurate version of Time.DeltaTime, it should NOT be used for gameplay purposes, since it's not affected by the audio sync</td></tr><tr><td>double AudioDelta</td><td>More accurate version of Time.DeltaTime, it can be used for gameplay purposes, since it's affected by the audio sync. However, I would still recommend using stuff like Lerp() with GetCurrentAudioTime() instead of this, since it can skip frames</td></tr><tr><td>double SourceStartTime</td><td>(Read only) Time in seconds of the moment that the game will unpause on. If you want to set this time, use SetStartTime()</td></tr><tr><td>float SongPitch</td><td>Music source pitch</td></tr><tr><td><a href="song">Song </a>Song</td><td>Currently playing song</td></tr><tr><td>Action OnStartPlaying</td><td>Called alongside Play(), it's not exactly the same time the audio starts playing, it's just the time that the audio has been scheduled for playback</td></tr><tr><td>Action OnStopPlaying</td><td>Called on Pause() and Stop()</td></tr><tr><td>Action OnBpmChange</td><td>Called on bpm change, this is called AFTER the CurrentBpm value has been changed</td></tr><tr><td>Action OnLoop</td><td>Called when the song loops</td></tr></tbody></table>

## Methods

<table data-full-width="true"><thead><tr><th width="326">Method</th><th>Description</th></tr></thead><tbody><tr><td>InitTime(double time)</td><td>Initializes the time of the song, this should be called before playing the song<br><br>time - Time in seconds</td></tr><tr><td>SetSong(<a href="song">Song </a>song, bool setClip)</td><td>Set the song that'll be playing, if you'd rather set the clip directly, use SetClip().<br><br>song - Song that'll be playing<br>setClip - If true, also set the clip that'll be playing from the song</td></tr><tr><td>SetClip(AudioClip clip)</td><td>Set the clip of the music source. Can be used instead of SetSong() if you want to create your own gameplay back-end logic for songs.<br><br>clip - Audio clip of the music that'll be playing</td></tr><tr><td>SetOffset(int offsetInMs)</td><td>Set the offset of the song, this should be set per user, since it's different for everyone. This also should be preferably set before InitTime() and Play() is called.<br><br>offsetInMs - Offset in milliseconds</td></tr><tr><td>GetCurrentAudioTime()</td><td>Return current time of the audio playback, this is the most accurate time you can get from the audio thread, and it should be used for syncing gameplay</td></tr><tr><td>Play()</td><td>If the time has been initialized, schedules the audio playback. The audio won't start on the same frame as this is called, but the next frame that the audio thread runs.</td></tr><tr><td>Unpause()</td><td>Unpause the audio playback. Just calls Play()</td></tr><tr><td>Pause()</td><td>Pause the audio playback</td></tr><tr><td>Stop()</td><td>Stop the audio playback and reset the time to 0</td></tr><tr><td>SetStartTime(double time)</td><td>Sets the time that the audio will start playing at. Useful for song editors or gameplay that requires you to start at a specific time in the song</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rhythm-engine.gitbook.io/rhythm-engine/docs/core/rhythmenginecore.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
