Skip to content

Commit

Permalink
Play audio at the correct frame when switching frames while the anima…
Browse files Browse the repository at this point in the history
…tion is running
  • Loading branch information
OverloadedOrama committed Dec 14, 2024
1 parent b5d5c44 commit 206773c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/UI/Timeline/LayerButton.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var button_pressed := false:
get:
return main_button.button_pressed
var animation_running := false
var audio_playing_at_frame := 0

var audio_player: AudioStreamPlayer
@onready var properties: AcceptDialog = Global.control.find_child("LayerProperties")
Expand Down Expand Up @@ -68,20 +69,24 @@ func _ready() -> void:

func _on_cel_switched() -> void:
z_index = 1 if button_pressed else 0
var layer := Global.current_project.layers[layer_index]
var project := Global.current_project
var layer := project.layers[layer_index]
if layer is AudioLayer:
if not is_instance_valid(audio_player):
return
if not layer.is_visible_in_hierarchy():
audio_player.stop()
return
if animation_running:
var current_frame := Global.current_project.current_frame
var current_frame := project.current_frame
if (
current_frame == layer.playback_frame
or (current_frame == 0 and layer.playback_frame < 0)
## True when switching cels while the animation is running
or current_frame != audio_playing_at_frame + 1
):
_play_audio(false)
audio_playing_at_frame = current_frame
else:
_play_audio(true)

Expand Down Expand Up @@ -124,6 +129,7 @@ func _play_audio(single_frame: bool) -> void:
var frame_pos := frame.position_in_seconds(project, layer.playback_frame)
if frame_pos >= 0 and frame_pos < audio_length:
audio_player.play(frame_pos)
audio_playing_at_frame = project.current_frame
if single_frame:
var timer := get_tree().create_timer(frame.get_duration_in_seconds(project.fps))
timer.timeout.connect(func(): audio_player.stop())
Expand Down

0 comments on commit 206773c

Please sign in to comment.