Improved tutorial for sync audio with music, use two approaches.

This commit is contained in:
Juan Linietsky
2019-04-27 15:26:48 -03:00
parent e22a46f301
commit ceefc33fe4
12 changed files with 129 additions and 21 deletions

View File

@@ -7,18 +7,34 @@ var playing = false
const COMPENSATE_FRAMES = 2
const COMPENSATE_HZ = 60.0
const SYNC_SOURCE_SYSTEM_CLOCK = 0
const SYNC_SOURCE_SOUND_CLOCK = 1
var sync_source = SYNC_SOURCE_SYSTEM_CLOCK
func strsec(secs):
var s = str(secs)
if (s.length()==1):
s="0"+s
return s
# warning-ignore:unused_argument
# used by system clock
var time_begin
var time_delay
func _process(delta):
if (!playing or !$Player.playing):
return
var time = $Player.get_mix_time() + AudioServer.get_time_since_last_mix() - AudioServer.get_output_latency() + (1/COMPENSATE_HZ)*COMPENSATE_FRAMES
var time
if (sync_source == SYNC_SOURCE_SYSTEM_CLOCK):
# obtain from ticks
time = (OS.get_ticks_usec() - time_begin) / 1000000.0
# compensate
time -= time_delay
elif (sync_source == SYNC_SOURCE_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)
@@ -26,8 +42,18 @@ func _process(delta):
$Label.text = str("BEAT: ",beat % BARS +1,"/",BARS," TIME: ",seconds/60,":",strsec(seconds%60)," / ",seconds_total/60,":",strsec(seconds_total%60))
func _on_Button_pressed():
print(AudioServer.get_output_latency())
func _on_PlaySystem_pressed():
pass # Replace with function body.
sync_source = SYNC_SOURCE_SYSTEM_CLOCK
time_begin = OS.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 = SYNC_SOURCE_SOUND_CLOCK
playing=true
$Player.play()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@@ -2,15 +2,15 @@
importer="texture"
type="StreamTexture"
path="res://.import/play_button.png-c6659eb2fbf2022d9e670864776e465a.stex"
path="res://.import/play_sound_button.png-7e88216154de1a5cb6304cbd3751ed46.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://play_button.png"
dest_files=[ "res://.import/play_button.png-c6659eb2fbf2022d9e670864776e465a.stex" ]
source_file="res://play_sound_button.png"
dest_files=[ "res://.import/play_sound_button.png-7e88216154de1a5cb6304cbd3751ed46.stex" ]
[params]

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

@@ -0,0 +1,34 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/play_sound_button_hl.png-8d86f81fe37a5f2959088b0948283133.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://play_sound_button_hl.png"
dest_files=[ "res://.import/play_sound_button_hl.png-8d86f81fe37a5f2959088b0948283133.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@@ -2,15 +2,15 @@
importer="texture"
type="StreamTexture"
path="res://.import/play_button_hl.png-63644aa33cad39ca169b518c7eed7ae2.stex"
path="res://.import/play_system_button.png-683c51b4d13189b67bb57e75cbb8ef56.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://play_button_hl.png"
dest_files=[ "res://.import/play_button_hl.png-63644aa33cad39ca169b518c7eed7ae2.stex" ]
source_file="res://play_system_button.png"
dest_files=[ "res://.import/play_system_button.png-683c51b4d13189b67bb57e75cbb8ef56.stex" ]
[params]

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

View File

@@ -0,0 +1,34 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/play_system_button_hl.png-1e926e5d1be4f71f60454646aaa44d20.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://play_system_button_hl.png"
dest_files=[ "res://.import/play_system_button_hl.png-1e926e5d1be4f71f60454646aaa44d20.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

View File

@@ -1,9 +1,12 @@
[gd_scene load_steps=7 format=2]
[gd_scene load_steps=10 format=2]
[ext_resource path="res://the_comeback2.ogg" type="AudioStream" id=1]
[ext_resource path="res://Control.gd" type="Script" id=2]
[ext_resource path="res://lcd.ttf" type="DynamicFontData" id=3]
[ext_resource path="res://play_button.png" type="Texture" id=4]
[ext_resource path="res://play_system_button.png" type="Texture" id=4]
[ext_resource path="res://play_sound_button.png" type="Texture" id=5]
[ext_resource path="res://play_system_button_hl.png" type="Texture" id=6]
[ext_resource path="res://play_sound_button_hl.png" type="Texture" id=7]
[sub_resource type="StyleBoxFlat" id=1]
bg_color = Color( 0, 0, 0, 1 )
@@ -29,14 +32,25 @@ custom_fonts/font = SubResource( 2 )
custom_colors/font_color = Color( 0.552941, 0.984314, 0.501961, 1 )
align = 1
[node name="Button" type="TextureButton" parent="."]
margin_left = 433.316
margin_top = 244.158
margin_right = 561.316
margin_bottom = 372.158
texture_normal = ExtResource( 4 )
texture_pressed = ExtResource( 4 )
[node name="Player" type="AudioStreamPlayer" parent="."]
stream = ExtResource( 1 )
[connection signal="pressed" from="Button" to="." method="_on_Button_pressed"]
[node name="PlaySystem" type="TextureButton" parent="."]
margin_left = 214.737
margin_top = 187.368
margin_right = 342.737
margin_bottom = 315.368
texture_normal = ExtResource( 4 )
texture_pressed = ExtResource( 4 )
texture_hover = ExtResource( 6 )
[node name="PlaySound" type="TextureButton" parent="."]
margin_left = 622.105
margin_top = 183.158
margin_right = 750.105
margin_bottom = 311.158
texture_normal = ExtResource( 5 )
texture_pressed = ExtResource( 5 )
texture_hover = ExtResource( 7 )
[connection signal="pressed" from="PlaySystem" to="." method="_on_PlaySystem_pressed"]
[connection signal="pressed" from="PlaySound" to="." method="_on_PlaySound_pressed"]