1 extends DialogicSubsystem
2 ## Subsystem for managing background music and one-shot sound effects.
4 ## This subsystem has many different helper methods for managing audio
6 ## For instance, you can listen to music changes via [signal music_started].
9 ## Whenever a new background music is started, this signal is emitted and
10 ## contains a dictionary with the following keys: [br]
12 ## Key | Value Type | Value [br]
13 ## ----------- | ------------- | ----- [br]
14 ## `path` | [type String] | The path to the audio resource file. [br]
15 ## `volume` | [type float] | The volume of the audio resource that will be set to the [member base_music_player]. [br]
16 ## `audio_bus` | [type String] | The audio bus name that the [member base_music_player] will use. [br]
17 ## `loop` | [type bool] | Whether the audio resource will loop or not once it finishes playing. [br]
18 ## `channel` | [type int] | The channel ID to play the audio on. [br]
19 signal music_started(info: Dictionary)
22 ## Whenever a new sound effect is set, this signal is emitted and contains a
23 ## dictionary with the following keys: [br]
25 ## Key | Value Type | Value [br]
26 ## ----------- | ------------- | ----- [br]
27 ## `path` | [type String] | The path to the audio resource file. [br]
28 ## `volume` | [type float] | The volume of the audio resource that will be set to [member base_sound_player]. [br]
29 ## `audio_bus` | [type String] | The audio bus name that the [member base_sound_player] will use. [br]
30 ## `loop` | [type bool] | Whether the audio resource will loop or not once it finishes playing. [br]
31 signal sound_started(info: Dictionary)
34 var max_channels: int:
36 if max_channels != value:
38 ProjectSettings.set_setting('dialogic/audio/max_channels', value)
39 ProjectSettings.save()
40 current_music_player.resize(value)
42 return ProjectSettings.get_setting('dialogic/audio/max_channels', 4)
44 ## Audio player base duplicated to play background music.
46 ## Background music is long audio.
47 var base_music_player := AudioStreamPlayer.new()
48 ## Reference to the last used music player.
49 var current_music_player: Array[AudioStreamPlayer] = []
50 ## Audio player base, that will be duplicated to play sound effects.
52 ## Sound effects are short audio.
53 var base_sound_player := AudioStreamPlayer.new()
57 ####################################################################################################
59 ## Clears the state on this subsystem and stops all audio.
61 ## If you want to stop sounds only, use [method stop_all_sounds].
62 func clear_game_state(_clear_flag := DialogicGameHandler.ClearFlags.FULL_CLEAR) -> void:
63 for idx in max_channels:
64 update_music('', 0.0, '', 0.0, true, idx)
68 ## Loads the state on this subsystem from the current state info.
69 func load_game_state(load_flag:=LoadFlags.FULL_LOAD) -> void:
70 if load_flag == LoadFlags.ONLY_DNODES:
72 var info: Dictionary = dialogic.current_state_info.get("music", {})
73 if not info.is_empty() and info.has('path'):
74 update_music(info.path, info.volume, info.audio_bus, 0, info.loop, 0)
76 for channel_id in info.keys():
77 if info[channel_id].is_empty() or info[channel_id].path.is_empty():
78 update_music('', 0.0, '', 0.0, true, channel_id)
80 update_music(info[channel_id].path, info[channel_id].volume, info[channel_id].audio_bus, 0, info[channel_id].loop, channel_id)
83 ## Pauses playing audio.
85 for child in get_children():
86 child.stream_paused = true
89 ## Resumes playing audio.
90 func resume() -> void:
91 for child in get_children():
92 child.stream_paused = false
95 func _on_dialogic_timeline_ended() -> void:
96 if not dialogic.Styles.get_layout_node():
103 ####################################################################################################
105 func _ready() -> void:
106 dialogic.timeline_ended.connect(_on_dialogic_timeline_ended)
108 base_music_player.name = "Music"
109 add_child(base_music_player)
111 base_sound_player.name = "Sound"
112 add_child(base_sound_player)
114 current_music_player.resize(max_channels)
117 ## Updates the background music. Will fade out previous music.
118 func update_music(path := "", volume := 0.0, audio_bus := "", fade_time := 0.0, loop := true, channel_id := 0) -> void:
120 if channel_id > max_channels:
121 printerr("\tChannel ID (%s) higher than Max Music Channels (%s)" % [channel_id, max_channels])
122 dialogic.print_debug_moment()
125 if not dialogic.current_state_info.has('music'):
126 dialogic.current_state_info['music'] = {}
128 dialogic.current_state_info['music'][channel_id] = {'path':path, 'volume':volume, 'audio_bus':audio_bus, 'loop':loop, 'channel':channel_id}
129 music_started.emit(dialogic.current_state_info['music'][channel_id])
131 var fader: Tween = null
132 if is_instance_valid(current_music_player[channel_id]) and current_music_player[channel_id].playing or !path.is_empty():
133 fader = create_tween()
135 var prev_node: Node = null
136 if is_instance_valid(current_music_player[channel_id]) and current_music_player[channel_id].playing:
137 prev_node = current_music_player[channel_id]
138 fader.tween_method(interpolate_volume_linearly.bind(prev_node), db_to_linear(prev_node.volume_db),0.0,fade_time)
141 current_music_player[channel_id] = base_music_player.duplicate()
142 add_child(current_music_player[channel_id])
143 current_music_player[channel_id].stream = load(path)
144 current_music_player[channel_id].volume_db = volume
146 current_music_player[channel_id].bus = audio_bus
147 if not current_music_player[channel_id].stream is AudioStreamWAV:
148 if "loop" in current_music_player[channel_id].stream:
149 current_music_player[channel_id].stream.loop = loop
150 elif "loop_mode" in current_music_player[channel_id].stream:
152 current_music_player[channel_id].stream.loop_mode = AudioStreamWAV.LOOP_FORWARD
154 current_music_player[channel_id].stream.loop_mode = AudioStreamWAV.LOOP_DISABLED
156 current_music_player[channel_id].play(0)
157 fader.parallel().tween_method(interpolate_volume_linearly.bind(current_music_player[channel_id]), 0.0, db_to_linear(volume),fade_time)
160 fader.tween_callback(prev_node.queue_free)
163 ## Whether music is playing.
164 func has_music(channel_id := 0) -> bool:
165 return !dialogic.current_state_info.get('music', {}).get(channel_id, {}).get('path', '').is_empty()
168 ## Plays a given sound file.
169 func play_sound(path: String, volume := 0.0, audio_bus := "", loop := false) -> void:
170 if base_sound_player != null and !path.is_empty():
171 sound_started.emit({'path':path, 'volume':volume, 'audio_bus':audio_bus, 'loop':loop})
173 var new_sound_node := base_sound_player.duplicate()
174 new_sound_node.name += "Sound"
175 new_sound_node.stream = load(path)
177 if "loop" in new_sound_node.stream:
178 new_sound_node.stream.loop = loop
179 elif "loop_mode" in new_sound_node.stream:
181 new_sound_node.stream.loop_mode = AudioStreamWAV.LOOP_FORWARD
183 new_sound_node.stream.loop_mode = AudioStreamWAV.LOOP_DISABLED
185 new_sound_node.volume_db = volume
187 new_sound_node.bus = audio_bus
189 add_child(new_sound_node)
190 new_sound_node.play()
191 new_sound_node.finished.connect(new_sound_node.queue_free)
195 func stop_all_sounds() -> void:
196 for node in get_children():
197 if node == base_sound_player:
199 if "Sound" in node.name:
203 ## Converts a linear loudness value to decibel and sets that volume to
204 ## the given [param node].
205 func interpolate_volume_linearly(value: float, node: Node) -> void:
206 node.volume_db = linear_to_db(value)
209 ## Returns whether the currently playing audio resource is the same as this
210 ## event's [param resource_path], for [param channel_id].
211 func is_music_playing_resource(resource_path: String, channel_id := 0) -> bool:
212 var is_playing_resource: bool = (current_music_player.size() > channel_id
213 and is_instance_valid(current_music_player[channel_id])
214 and current_music_player[channel_id].is_playing()
215 and current_music_player[channel_id].stream.resource_path == resource_path)
217 return is_playing_resource