Skip to content

Commit 8a2a858

Browse files
Fix background music fade (#129)
Make sure audio streams start muted for first crossfade. Keep current track in memory to prevent crossfade to same music. Closes #121
1 parent 9e181a2 commit 8a2a858

File tree

4 files changed

+35
-34
lines changed

4 files changed

+35
-34
lines changed

addons/dialogic/Dialog.tscn

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ tracks/0/keys = {
2828
"values": [ Color( 1, 1, 1, 1 ), Color( 1, 1, 1, 0 ) ]
2929
}
3030

31-
[sub_resource type="Animation" id=7]
31+
[sub_resource type="Animation" id=3]
3232
resource_name = "Static"
3333

34-
[sub_resource type="Animation" id=3]
34+
[sub_resource type="Animation" id=4]
3535
loop = true
3636
tracks/0/type = "value"
3737
tracks/0/path = NodePath(".:margin_bottom")
@@ -46,13 +46,13 @@ tracks/0/keys = {
4646
"values": [ 12.0, 55.0 ]
4747
}
4848

49-
[sub_resource type="StyleBoxFlat" id=4]
49+
[sub_resource type="StyleBoxFlat" id=5]
5050
content_margin_left = 10.0
5151
content_margin_right = 10.0
5252
bg_color = Color( 1, 1, 1, 0 )
5353
expand_margin_left = 10.0
5454

55-
[sub_resource type="StyleBoxFlat" id=5]
55+
[sub_resource type="StyleBoxFlat" id=6]
5656
bg_color = Color( 0.196078, 0.196078, 0.196078, 0 )
5757

5858
[node name="DialogNode" type="Control"]
@@ -147,7 +147,7 @@ anchor_bottom = 1.0
147147
margin_left = -36.4279
148148
margin_top = -35.9016
149149
margin_right = 14.5721
150-
margin_bottom = 12.0
150+
margin_bottom = 29.4015
151151
rect_scale = Vector2( 0.4, 0.4 )
152152
texture = ExtResource( 1 )
153153
stretch_mode = 4
@@ -158,15 +158,15 @@ __meta__ = {
158158
[node name="AnimationPlayer" type="AnimationPlayer" parent="TextBubble/NextIndicator"]
159159
autoplay = "Up and down"
160160
anims/Pulse = SubResource( 2 )
161-
anims/Static = SubResource( 7 )
162-
"anims/Up and down" = SubResource( 3 )
161+
anims/Static = SubResource( 3 )
162+
"anims/Up and down" = SubResource( 4 )
163163

164164
[node name="NameLabel" type="Label" parent="TextBubble"]
165165
margin_top = -48.0
166166
margin_right = 58.0
167167
margin_bottom = -8.0
168168
size_flags_vertical = 1
169-
custom_styles/normal = SubResource( 4 )
169+
custom_styles/normal = SubResource( 5 )
170170
custom_fonts/font = ExtResource( 3 )
171171
custom_colors/font_color = Color( 1, 1, 1, 1 )
172172
custom_colors/font_color_shadow = Color( 0, 0, 0, 0.619608 )
@@ -191,7 +191,6 @@ __meta__ = {
191191
}
192192

193193
[node name="TextureRect" type="TextureRect" parent="TextBubble/NameLabel"]
194-
visible = false
195194
show_behind_parent = true
196195
anchor_right = 1.0
197196
anchor_bottom = 1.0
@@ -272,7 +271,7 @@ margin_right = 208.0
272271
mouse_filter = 1
273272
size_flags_horizontal = 3
274273
size_flags_vertical = 3
275-
custom_styles/panel = SubResource( 5 )
274+
custom_styles/panel = SubResource( 6 )
276275
script = ExtResource( 5 )
277276
__meta__ = {
278277
"_edit_group_": true,
@@ -341,7 +340,6 @@ __meta__ = {
341340
[node name="Timer" type="Timer" parent="DefinitionInfo"]
342341

343342
[node name="WaitSeconds" type="Timer" parent="."]
344-
345343
[connection signal="meta_hover_ended" from="TextBubble/RichTextLabel" to="." method="_on_RichTextLabel_meta_hover_ended"]
346344
[connection signal="meta_hover_started" from="TextBubble/RichTextLabel" to="." method="_on_RichTextLabel_meta_hover_started"]
347345
[connection signal="tween_completed" from="TextBubble/Tween" to="." method="_on_Tween_tween_completed"]

addons/dialogic/Nodes/BackgroundMusic.gd

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@ onready var _anim_player := $AnimationPlayer
55
onready var _track1 := $Track1
66
onready var _track2 := $Track2
77

8+
var current_path = ""
89

9-
func crossfade_to(audio_stream: AudioStream) -> void:
10-
if _track1.playing and _track2.playing:
11-
return
12-
13-
if _track2.playing:
14-
_track1.stream = audio_stream
15-
_track1.play()
16-
_anim_player.play("FadeToTrack1")
17-
else:
18-
_track2.stream = audio_stream
19-
_track2.play()
20-
_anim_player.play("FadeToTrack2")
10+
func crossfade_to(path: String) -> void:
11+
if current_path != path:
12+
current_path = path
13+
var stream: AudioStream = load(current_path)
14+
if _track1.playing and _track2.playing:
15+
return
16+
17+
if _track2.playing:
18+
_track1.stream = stream
19+
_track1.play()
20+
_anim_player.play("FadeToTrack1")
21+
else:
22+
_track2.stream = stream
23+
_track2.play()
24+
_anim_player.play("FadeToTrack2")
2125

2226

2327
func fade_out() -> void:
28+
current_path = ""
2429
_anim_player.play("FadeOut")

addons/dialogic/Nodes/BackgroundMusic.tscn

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

33
[ext_resource path="res://addons/dialogic/Nodes/BackgroundMusic.gd" type="Script" id=1]
44

5-
6-
[sub_resource type="Animation" id=3]
5+
[sub_resource type="Animation" id=1]
76
resource_name = "FadeOut"
7+
length = 0.5
88
tracks/0/type = "value"
99
tracks/0/path = NodePath("Track1:volume_db")
1010
tracks/0/interp = 1
@@ -54,8 +54,7 @@ tracks/3/keys = {
5454
"values": [ false ]
5555
}
5656

57-
[sub_resource type="Animation" id=1]
58-
resource_name = "FadeToTrack1"
57+
[sub_resource type="Animation" id=2]
5958
length = 0.5
6059
tracks/0/type = "value"
6160
tracks/0/path = NodePath("Track1:volume_db")
@@ -94,8 +93,7 @@ tracks/2/keys = {
9493
"values": [ false ]
9594
}
9695

97-
[sub_resource type="Animation" id=2]
98-
resource_name = "FadeToTrack2"
96+
[sub_resource type="Animation" id=3]
9997
length = 0.5
10098
tracks/0/type = "value"
10199
tracks/0/path = NodePath("Track1:volume_db")
@@ -145,8 +143,9 @@ __meta__ = {
145143
[node name="Track1" type="AudioStreamPlayer" parent="."]
146144

147145
[node name="Track2" type="AudioStreamPlayer" parent="."]
146+
volume_db = -80.0
148147

149148
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
150-
anims/FadeOut = SubResource( 3 )
151-
anims/FadeToTrack1 = SubResource( 1 )
152-
anims/FadeToTrack2 = SubResource( 2 )
149+
anims/FadeOut = SubResource( 1 )
150+
anims/FadeToTrack1 = SubResource( 2 )
151+
anims/FadeToTrack2 = SubResource( 3 )

addons/dialogic/Nodes/dialog_node.gd

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,7 @@ func event_handler(event: Dictionary):
468468
{'background-music'}, {'background-music', 'file'}:
469469
emit_signal("event_start", "background-music", event)
470470
if event['background-music'] == 'play' and 'file' in event.keys() and not event['file'].empty():
471-
var stream: AudioStream = load(event['file'])
472-
$FX/BackgroundMusic.crossfade_to(stream)
471+
$FX/BackgroundMusic.crossfade_to(event['file'])
473472
else:
474473
$FX/BackgroundMusic.fade_out()
475474
go_to_next_event()

0 commit comments

Comments
 (0)