(Tried to) Implement a way to sync audio with animation

This commit is contained in:
SchimmelSpreu83 2025-10-16 18:29:38 +02:00
parent 7e8f8a1d4d
commit f5b406c72f
11 changed files with 117 additions and 133 deletions

View File

@ -50,11 +50,15 @@ stream = ExtResource("3_c4bhh")
volume_db = -6.0
[node name="MusicAnimation" type="AnimationPlayer" parent="."]
audio_max_polyphony = 1
callback_mode_process = 0
callback_mode_method = 1
libraries = {
&"": ExtResource("2_r4hpf")
}
script = ExtResource("5_c4bhh")
audio_player = NodePath("../Music")
metadata/_custom_type_script = "uid://jahmwrxb2siw"
[node name="ProgressTrack" type="Path3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -1,44 +0,0 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://t47dsnj63y0q"
path.s3tc="res://.godot/imported/CircusTent_texture_02.png-4e53f948574944f15359cfab26877cc1.s3tc.ctex"
metadata={
"imported_formats": ["s3tc_bptc"],
"vram_texture": true
}
generator_parameters={
"md5": "a4091b6db3fc67af97a848a98d5046da"
}
[deps]
source_file="res://_development/ayuroo/levels/_experimental/CircusTent_texture_02.png"
dest_files=["res://.godot/imported/CircusTent_texture_02.png-4e53f948574944f15359cfab26877cc1.s3tc.ctex"]
[params]
compress/mode=2
compress/high_quality=false
compress/lossy_quality=0.7
compress/uastc_level=0
compress/rdo_quality_loss=0.0
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/channel_remap/red=0
process/channel_remap/green=1
process/channel_remap/blue=2
process/channel_remap/alpha=3
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=0

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -1,44 +0,0 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://pcmndib445ch"
path.s3tc="res://.godot/imported/CircusTent_texture_02_2.png-72d00f784254062ea8406c13e57b7f70.s3tc.ctex"
metadata={
"imported_formats": ["s3tc_bptc"],
"vram_texture": true
}
generator_parameters={
"md5": "671c567d75d6cffe7d16440771c026c5"
}
[deps]
source_file="res://_development/ayuroo/levels/_experimental/CircusTent_texture_02_2.png"
dest_files=["res://.godot/imported/CircusTent_texture_02_2.png-72d00f784254062ea8406c13e57b7f70.s3tc.ctex"]
[params]
compress/mode=2
compress/high_quality=false
compress/lossy_quality=0.7
compress/uastc_level=0
compress/rdo_quality_loss=0.0
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/channel_remap/red=0
process/channel_remap/green=1
process/channel_remap/blue=2
process/channel_remap/alpha=3
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=0

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.6 KiB

View File

@ -1,44 +0,0 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://ceoaurr14k8s0"
path.s3tc="res://.godot/imported/CircusTent_texture_13.png-733cbf96e6cf53acbb1f3cc2d83015cc.s3tc.ctex"
metadata={
"imported_formats": ["s3tc_bptc"],
"vram_texture": true
}
generator_parameters={
"md5": "5220913b82328d262f5516be440341d3"
}
[deps]
source_file="res://_development/ayuroo/levels/_experimental/CircusTent_texture_13.png"
dest_files=["res://.godot/imported/CircusTent_texture_13.png-733cbf96e6cf53acbb1f3cc2d83015cc.s3tc.ctex"]
[params]
compress/mode=2
compress/high_quality=false
compress/lossy_quality=0.7
compress/uastc_level=0
compress/rdo_quality_loss=0.0
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/channel_remap/red=0
process/channel_remap/green=1
process/channel_remap/blue=2
process/channel_remap/alpha=3
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=0

View File

@ -57,4 +57,4 @@ _subresources={
}
}
gltf/naming_version=2
gltf/embedded_image_handling=1
gltf/embedded_image_handling=0

View File

@ -1,4 +1,5 @@
@tool
class_name AnimPlayerEditorCalls
extends AnimationPlayer
var EDITOR_PATH: String:

View File

@ -0,0 +1,110 @@
@tool
class_name TimedMusicAnimationPlayer
extends AnimPlayerEditorCalls
@export_node_path("AudioStreamPlayer", "AudioStreamPlayer2D", "AudioStreamPlayer3D")
var audio_player: NodePath = ^"": set = set_audio_player
## The index in the animation where the music is played from.
@export var music_anim_track_index: int = 0
# The offset from 0.0 seconds when the music starts in the animation.
#@export var music_anim_offset: float = 0.0
@export var max_error: float = 0.02175
@export var min_pitch_scale_range: float = 0.0015
@export var max_pitch_scale_range: float = 0.375
@export var max_pitch_scale_difference: float = 1.01
@export var custom_pitch_scale: float = 1.0
var _audio_player: Node
var _timer := Timer.new()
var _offset: float = 0.0
func _ready() -> void:
super()
if not Engine.is_editor_hint():
add_child(_timer)
_timer.one_shot = false
set_audio_player(audio_player)
animation_started.connect(_anim_started)
func _process(delta: float) -> void:
if Engine.is_editor_hint():
super(delta)
return
if not active or not is_playing() or not is_instance_valid(_audio_player):
return
var latency: float = AudioServer.get_output_latency()
var audio_position: float = _audio_player.get_playback_position()
var anim_position: float = current_animation_position + AudioServer.get_time_since_last_mix()
var offset: float = anim_position - (audio_position + _offset)
var abs_offset: float = absf(offset)
SPrint.print_msgf("Audio-Position: %s\nAnim-Position: %s" % [audio_position, anim_position])
SPrint.print_msgf("Audio-Anim Diff: %s" % [audio_position - anim_position])
SPrint.print_msgf("Audio-Pitch: %s\nLatency: %s" % [_audio_player.pitch_scale, latency])
if abs_offset < max_pitch_scale_range and abs_offset > latency:#min_pitch_scale_range:
var max_pitch_scale: float = max_pitch_scale_difference
var min_pitch_scale: float = custom_pitch_scale + (custom_pitch_scale - max_pitch_scale)
var pitch: float = custom_pitch_scale + offset + latency
pitch = clampf(pitch, min_pitch_scale, max_pitch_scale)
pitch = lerp(_audio_player.pitch_scale, pitch, 1.0 - pow(0.5, delta))
_audio_player.pitch_scale = clampf(pitch, min_pitch_scale, max_pitch_scale)
elif abs_offset > max_error:
#_audio_player.seek(offset)
_audio_player.play((anim_position + _offset) - latency)
SPrint.print_msg("Snapped Audio to: %s" % [_audio_player.get_playback_position()])
else:
_audio_player.pitch_scale = custom_pitch_scale
#func play_timed(
#anim_name: StringName,
#from_marker: StringName = &"",
#end_marker: StringName = &"",
#custom_blend: float = -1.0,
#custom_speed: float = 1.0,
#from_end: bool = false
#) -> void:
#_anim_started(anim_name)
#play_section_with_markers(anim_name, from_marker, end_marker, custom_blend, custom_speed, from_end)
func set_audio_player(path: NodePath) -> void:
audio_player = path
var node: Node = get_node_or_null(path)
_audio_player = node if Utils.is_node_audioplayer(node) else null
func _anim_started(anim_name: StringName) -> void:
if Engine.is_editor_hint():
return
var animation: Animation = get_animation(anim_name)
animation.track_set_enabled(music_anim_track_index, false)
_audio_player.stream = null
_offset = animation.track_get_key_time(music_anim_track_index, 0)
var duration: float = _offset - current_animation_position
if duration > 0.0:
_timer.start(duration)
var _signals: Array[Signal] = await SignalGroup.await_signals(
[_timer.timeout, animation_finished, animation_changed], 1
)
if not _signals.front() == _timer.timeout:
return
var latency: float = AudioServer.get_output_latency() - AudioServer.get_time_to_next_mix()
_audio_player.stream = animation.audio_track_get_key_stream(music_anim_track_index, 0)
_audio_player.play(current_animation_position - _offset - latency)

View File

@ -0,0 +1 @@
uid://jahmwrxb2siw