Skip to content

Instantly share code, notes, and snippets.

@NoodleSushi
Last active January 31, 2021 06:23
Show Gist options
  • Save NoodleSushi/3dc4575df3c94c1666be4129688021d2 to your computer and use it in GitHub Desktop.
Save NoodleSushi/3dc4575df3c94c1666be4129688021d2 to your computer and use it in GitHub Desktop.
extends Object
class_name Rhythmer
class TimeSeeker:
var begin : float = 0
var delay : float = 0
func reset() -> void:
begin = OS.get_ticks_usec()
delay = AudioServer.get_time_to_next_mix() + AudioServer.get_output_latency()
func update() -> float:
var time = (OS.get_ticks_usec() - begin) / 1000000.0
time -= delay
return time
static func asp_time(ASP : AudioStreamPlayer, is_scaled : bool = false) -> float:
return (ASP.get_playback_position() + AudioServer.get_time_since_last_mix() - AudioServer.get_output_latency()) / (ASP.pitch_scale if is_scaled else 1.0)
static func beat2sec(beat : float, bpm : int, time_scale : float = 1) -> float:
return beat * 60.0 / bpm * time_scale
static func sec2beat(sec : float, bpm : int, time_scale : float = 1) -> float:
return sec * bpm / time_scale * 60.0
static func get_play_latency() -> float:
return AudioServer.get_time_since_last_mix() - AudioServer.get_output_latency()
static func get_playback_offset(current_time: float, mission_timestamp: float, allocated_time: float, music_pitch_scale: float = 1.0, slave_pitch_scale: float = 1.0) -> float:
return allocated_time - get_time_offset(current_time, mission_timestamp, music_pitch_scale, slave_pitch_scale)
static func get_time_offset(current_time: float, mission_timestamp: float, music_pitch_scale: float = 1.0, slave_pitch_scale: float = 1.0) -> float:
var time_playback_latency : float = AudioServer.get_time_since_last_mix()
return ((mission_timestamp - current_time - AudioServer.get_output_latency()) * slave_pitch_scale + time_playback_latency) / music_pitch_scale
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment