Skip to content

Commit ceefc33

Browse files
committed
Improved tutorial for sync audio with music, use two approaches.
1 parent e22a46f commit ceefc33

12 files changed

+128
-20
lines changed

audio/bpm_sync/Control.gd

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,53 @@ var playing = false
77
const COMPENSATE_FRAMES = 2
88
const COMPENSATE_HZ = 60.0
99

10+
const SYNC_SOURCE_SYSTEM_CLOCK = 0
11+
const SYNC_SOURCE_SOUND_CLOCK = 1
12+
13+
var sync_source = SYNC_SOURCE_SYSTEM_CLOCK
14+
15+
1016
func strsec(secs):
1117
var s = str(secs)
1218
if (s.length()==1):
1319
s="0"+s
1420
return s
1521

16-
# warning-ignore:unused_argument
22+
# used by system clock
23+
var time_begin
24+
var time_delay
25+
1726
func _process(delta):
1827
if (!playing or !$Player.playing):
1928
return
2029

21-
var time = $Player.get_mix_time() + AudioServer.get_time_since_last_mix() - AudioServer.get_output_latency() + (1/COMPENSATE_HZ)*COMPENSATE_FRAMES
30+
var time
31+
if (sync_source == SYNC_SOURCE_SYSTEM_CLOCK):
32+
# obtain from ticks
33+
time = (OS.get_ticks_usec() - time_begin) / 1000000.0
34+
# compensate
35+
time -= time_delay
36+
elif (sync_source == SYNC_SOURCE_SOUND_CLOCK):
37+
time = $Player.get_playback_position() + AudioServer.get_time_since_last_mix() - AudioServer.get_output_latency() + (1/COMPENSATE_HZ)*COMPENSATE_FRAMES
2238

2339
var beat = int(time * BPM / 60.0)
2440
var seconds = int(time)
2541
var seconds_total = int($Player.stream.get_length())
2642
$Label.text = str("BEAT: ",beat % BARS +1,"/",BARS," TIME: ",seconds/60,":",strsec(seconds%60)," / ",seconds_total/60,":",strsec(seconds_total%60))
2743

2844

29-
func _on_Button_pressed():
30-
print(AudioServer.get_output_latency())
45+
46+
func _on_PlaySystem_pressed():
47+
pass # Replace with function body.
48+
sync_source = SYNC_SOURCE_SYSTEM_CLOCK
49+
time_begin = OS.get_ticks_usec()
50+
time_delay = AudioServer.get_time_to_next_mix() + AudioServer.get_output_latency()
3151
playing=true
3252
$Player.play()
3353

54+
55+
func _on_PlaySound_pressed():
56+
sync_source = SYNC_SOURCE_SOUND_CLOCK
57+
playing=true
58+
$Player.play()
59+

audio/bpm_sync/play_button.png

-2.41 KB
Binary file not shown.

audio/bpm_sync/play_button_hl.png

-2.11 KB
Binary file not shown.
2.81 KB
Loading

audio/bpm_sync/play_button.png.import renamed to audio/bpm_sync/play_sound_button.png.import

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
importer="texture"
44
type="StreamTexture"
5-
path="res://.import/play_button.png-c6659eb2fbf2022d9e670864776e465a.stex"
5+
path="res://.import/play_sound_button.png-7e88216154de1a5cb6304cbd3751ed46.stex"
66
metadata={
77
"vram_texture": false
88
}
99

1010
[deps]
1111

12-
source_file="res://play_button.png"
13-
dest_files=[ "res://.import/play_button.png-c6659eb2fbf2022d9e670864776e465a.stex" ]
12+
source_file="res://play_sound_button.png"
13+
dest_files=[ "res://.import/play_sound_button.png-7e88216154de1a5cb6304cbd3751ed46.stex" ]
1414

1515
[params]
1616

2.21 KB
Loading
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/play_sound_button_hl.png-8d86f81fe37a5f2959088b0948283133.stex"
6+
metadata={
7+
"vram_texture": false
8+
}
9+
10+
[deps]
11+
12+
source_file="res://play_sound_button_hl.png"
13+
dest_files=[ "res://.import/play_sound_button_hl.png-8d86f81fe37a5f2959088b0948283133.stex" ]
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/bptc_ldr=0
21+
compress/normal_map=0
22+
flags/repeat=0
23+
flags/filter=true
24+
flags/mipmaps=false
25+
flags/anisotropic=false
26+
flags/srgb=2
27+
process/fix_alpha_border=true
28+
process/premult_alpha=false
29+
process/HDR_as_SRGB=false
30+
process/invert_color=false
31+
stream=false
32+
size_limit=0
33+
detect_3d=true
34+
svg/scale=1.0
3.12 KB
Loading

audio/bpm_sync/play_button_hl.png.import renamed to audio/bpm_sync/play_system_button.png.import

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
importer="texture"
44
type="StreamTexture"
5-
path="res://.import/play_button_hl.png-63644aa33cad39ca169b518c7eed7ae2.stex"
5+
path="res://.import/play_system_button.png-683c51b4d13189b67bb57e75cbb8ef56.stex"
66
metadata={
77
"vram_texture": false
88
}
99

1010
[deps]
1111

12-
source_file="res://play_button_hl.png"
13-
dest_files=[ "res://.import/play_button_hl.png-63644aa33cad39ca169b518c7eed7ae2.stex" ]
12+
source_file="res://play_system_button.png"
13+
dest_files=[ "res://.import/play_system_button.png-683c51b4d13189b67bb57e75cbb8ef56.stex" ]
1414

1515
[params]
1616

2.47 KB
Loading

0 commit comments

Comments
 (0)