123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- extends Panel
- const BPM = 116
- const BARS = 4
- var playing = false
- const COMPENSATE_FRAMES = 2
- const COMPENSATE_HZ = 60.0
- enum SyncSource {
- SYSTEM_CLOCK,
- SOUND_CLOCK,
- }
- var sync_source = SyncSource.SYSTEM_CLOCK
- # Used by system clock.
- var time_begin
- var time_delay
- func strsec(secs):
- var s = str(secs)
- if (secs < 10):
- s = "0" + s
- return s
- func _process(_delta):
- if not playing or not $Player.playing:
- return
- var time = 0.0
- if sync_source == SyncSource.SYSTEM_CLOCK:
- # Obtain from ticks.
- time = (Time.get_ticks_usec() - time_begin) / 1000000.0
- # Compensate.
- time -= time_delay
- elif sync_source == SyncSource.SOUND_CLOCK:
- time = $Player.get_playback_position() + AudioServer.get_time_since_last_mix() - AudioServer.get_output_latency() + (1 / COMPENSATE_HZ) * COMPENSATE_FRAMES
- var beat = int(time * BPM / 60.0)
- var seconds = int(time)
- var seconds_total = int($Player.stream.get_length())
- @warning_ignore("integer_division")
- $Label.text = str("BEAT: ", beat % BARS + 1, "/", BARS, " TIME: ", seconds / 60, ":", strsec(seconds % 60), " / ", seconds_total / 60, ":", strsec(seconds_total % 60))
- func _on_PlaySystem_pressed():
- sync_source = SyncSource.SYSTEM_CLOCK
- time_begin = Time.get_ticks_usec()
- time_delay = AudioServer.get_time_to_next_mix() + AudioServer.get_output_latency()
- playing = true
- $Player.play()
- func _on_PlaySound_pressed():
- sync_source = SyncSource.SOUND_CLOCK
- playing = true
- $Player.play()
|