Skip to content

Commit

Permalink
Merge pull request #2427 from salianifo/music-channels
Browse files Browse the repository at this point in the history
Implement multiple music channels
  • Loading branch information
zaknafean authored Oct 16, 2024
2 parents c4227bd + 9f50f18 commit 11de41c
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 40 deletions.
18 changes: 16 additions & 2 deletions addons/dialogic/Modules/Audio/event_music.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ extends DialogicEvent

## The file to play. If empty, the previous music will be faded out.
var file_path := ""
## The channel to use.
var channel_id: int = 0
## The length of the fade. If 0 (by default) it's an instant change.
var fade_length: float = 0
## The volume the music will be played at.
Expand All @@ -24,8 +26,8 @@ var loop := true
################################################################################

func _execute() -> void:
if not dialogic.Audio.is_music_playing_resource(file_path):
dialogic.Audio.update_music(file_path, volume, audio_bus, fade_length, loop)
if not dialogic.Audio.is_music_playing_resource(file_path, channel_id):
dialogic.Audio.update_music(file_path, volume, audio_bus, fade_length, loop, channel_id)

finish()

Expand Down Expand Up @@ -55,6 +57,7 @@ func get_shortcode_parameters() -> Dictionary:
return {
#param_name : property_info
"path" : {"property": "file_path", "default": ""},
"channel" : {"property": "channel_id", "default": 0},
"fade" : {"property": "fade_length", "default": 0},
"volume" : {"property": "volume", "default": 0},
"bus" : {"property": "audio_bus", "default": "",
Expand All @@ -73,6 +76,7 @@ func build_event_editor() -> void:
'file_filter' : "*.mp3, *.ogg, *.wav; Supported Audio Files",
'placeholder' : "No music",
'editor_icon' : ["AudioStreamPlayer", "EditorIcons"]})
add_header_edit('channel_id', ValueType.FIXED_OPTIONS, {'left_text':'on:', 'options': get_channel_list()})
add_body_edit('fade_length', ValueType.NUMBER, {'left_text':'Fade Time:'})
add_body_edit('volume', ValueType.NUMBER, {'left_text':'Volume:', 'mode':2}, '!file_path.is_empty()')
add_body_edit('audio_bus', ValueType.SINGLELINE_TEXT, {'left_text':'Audio Bus:'}, '!file_path.is_empty()')
Expand All @@ -84,3 +88,13 @@ func get_bus_suggestions() -> Dictionary:
for i in range(AudioServer.bus_count):
bus_name_list[AudioServer.get_bus_name(i)] = {'value':AudioServer.get_bus_name(i)}
return bus_name_list


func get_channel_list() -> Array:
var channel_name_list := []
for i in ProjectSettings.get_setting('dialogic/audio/max_channels', 4):
channel_name_list.append({
'label': 'Channel %s' % (i + 1),
'value': i,
})
return channel_name_list
4 changes: 4 additions & 0 deletions addons/dialogic/Modules/Audio/index.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ func _get_events() -> Array:

func _get_subsystems() -> Array:
return [{'name':'Audio', 'script':this_folder.path_join('subsystem_audio.gd')}]


func _get_settings_pages() -> Array:
return [this_folder.path_join('settings_audio.tscn')]
17 changes: 17 additions & 0 deletions addons/dialogic/Modules/Audio/settings_audio.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@tool
extends DialogicSettingsPage

## Settings page that contains settings for the audio subsystem


func _ready() -> void:
%MusicChannelCount.value_changed.connect(_on_music_channel_count_value_changed)


func _refresh() -> void:
%MusicChannelCount.value = ProjectSettings.get_setting("dialogic/audio/max_channels", 4)


func _on_music_channel_count_value_changed(value:float) -> void:
ProjectSettings.set_setting('dialogic/audio/max_channels', value)
ProjectSettings.save()
44 changes: 44 additions & 0 deletions addons/dialogic/Modules/Audio/settings_audio.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[gd_scene load_steps=5 format=3 uid="uid://c2qgetjc3mfo3"]

[ext_resource type="Script" path="res://addons/dialogic/Modules/Audio/settings_audio.gd" id="1_2iyyr"]
[ext_resource type="PackedScene" uid="uid://dbpkta2tjsqim" path="res://addons/dialogic/Editor/Common/hint_tooltip_icon.tscn" id="2_o1ban"]

[sub_resource type="Image" id="Image_n4dud"]
data = {
"data": PackedByteArray(255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 94, 94, 127, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 93, 93, 255, 255, 94, 94, 127, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 94, 94, 127, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 94, 94, 127, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 231, 255, 94, 94, 54, 255, 94, 94, 57, 255, 93, 93, 233, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 231, 255, 94, 94, 54, 255, 94, 94, 57, 255, 93, 93, 233, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 97, 97, 42, 255, 255, 255, 0, 255, 255, 255, 0, 255, 97, 97, 42, 255, 93, 93, 233, 255, 93, 93, 232, 255, 93, 93, 41, 255, 255, 255, 0, 255, 255, 255, 0, 255, 97, 97, 42, 255, 93, 93, 233, 255, 93, 93, 232, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 96, 96, 45, 255, 93, 93, 44, 255, 255, 255, 0, 255, 97, 97, 42, 255, 97, 97, 42, 255, 255, 255, 0, 255, 96, 96, 45, 255, 93, 93, 44, 255, 255, 255, 0, 255, 97, 97, 42, 255, 97, 97, 42, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 96, 96, 45, 255, 93, 93, 235, 255, 94, 94, 234, 255, 95, 95, 43, 255, 255, 255, 0, 255, 255, 255, 0, 255, 96, 96, 45, 255, 93, 93, 235, 255, 94, 94, 234, 255, 95, 95, 43, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 235, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 233, 255, 95, 95, 59, 255, 96, 96, 61, 255, 93, 93, 235, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 233, 255, 95, 95, 59, 255, 96, 96, 61, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0),
"format": "RGBA8",
"height": 16,
"mipmaps": false,
"width": 16
}

[sub_resource type="ImageTexture" id="ImageTexture_ek5a1"]
image = SubResource("Image_n4dud")

[node name="Audio" type="VBoxContainer"]
offset_right = 121.0
offset_bottom = 58.0
script = ExtResource("1_2iyyr")

[node name="Label" type="Label" parent="."]
layout_mode = 2
theme_type_variation = &"DialogicSettingsSection"
text = "Music Channels"

[node name="HBoxContainer" type="HBoxContainer" parent="."]
layout_mode = 2

[node name="Label" type="Label" parent="HBoxContainer"]
layout_mode = 2
text = "Max music channels"

[node name="HintTooltip" parent="HBoxContainer" instance=ExtResource("2_o1ban")]
layout_mode = 2
texture = SubResource("ImageTexture_ek5a1")
hint_text = "Lowering this value may invalidate existing music events!"

[node name="MusicChannelCount" type="SpinBox" parent="HBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
min_value = 1.0
value = 1.0
94 changes: 58 additions & 36 deletions addons/dialogic/Modules/Audio/subsystem_audio.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extends DialogicSubsystem
## `volume` | [type float] | The volume of the audio resource that will be set to the [member base_music_player]. [br]
## `audio_bus` | [type String] | The audio bus name that the [member base_music_player] will use. [br]
## `loop` | [type bool] | Whether the audio resource will loop or not once it finishes playing. [br]
## `channel` | [type int] | The channel ID to play the audio on. [br]
signal music_started(info: Dictionary)


Expand All @@ -30,12 +31,22 @@ signal music_started(info: Dictionary)
signal sound_started(info: Dictionary)


var max_channels: int:
set(value):
if max_channels != value:
max_channels = value
ProjectSettings.set_setting('dialogic/audio/max_channels', value)
ProjectSettings.save()
current_music_player.resize(value)
get:
return ProjectSettings.get_setting('dialogic/audio/max_channels', 4)

## Audio player base duplicated to play background music.
##
## Background music is long audio.
var base_music_player := AudioStreamPlayer.new()
## Reference to the last used music player.
var current_music_player: AudioStreamPlayer
var current_music_player: Array[AudioStreamPlayer] = []
## Audio player base, that will be duplicated to play sound effects.
##
## Sound effects are short audio.
Expand All @@ -49,7 +60,8 @@ var base_sound_player := AudioStreamPlayer.new()
##
## If you want to stop sounds only, use [method stop_all_sounds].
func clear_game_state(_clear_flag := DialogicGameHandler.ClearFlags.FULL_CLEAR) -> void:
update_music()
for idx in max_channels:
update_music('', 0.0, '', 0.0, true, idx)
stop_all_sounds()


Expand All @@ -58,10 +70,14 @@ func load_game_state(load_flag:=LoadFlags.FULL_LOAD) -> void:
if load_flag == LoadFlags.ONLY_DNODES:
return
var info: Dictionary = dialogic.current_state_info.get("music", {})
if info.is_empty() or info.path.is_empty():
update_music()
if not info.is_empty() and info.has('path'):
update_music(info.path, info.volume, info.audio_bus, 0, info.loop, 0)
else:
update_music(info.path, info.volume, info.audio_bus, 0, info.loop)
for channel_id in info.keys():
if info[channel_id].is_empty() or info[channel_id].path.is_empty():
update_music('', 0.0, '', 0.0, true, channel_id)
else:
update_music(info[channel_id].path, info[channel_id].volume, info[channel_id].audio_bus, 0, info[channel_id].loop, channel_id)


## Pauses playing audio.
Expand Down Expand Up @@ -94,55 +110,59 @@ func _ready() -> void:

base_sound_player.name = "Sound"
add_child(base_sound_player)

current_music_player.resize(max_channels)


## Updates the background music. Will fade out previous music.
func update_music(path := "", volume := 0.0, audio_bus := "", fade_time := 0.0, loop := true) -> void:
func update_music(path := "", volume := 0.0, audio_bus := "", fade_time := 0.0, loop := true, channel_id := 0) -> void:

dialogic.current_state_info['music'] = {'path':path, 'volume':volume, 'audio_bus':audio_bus, 'loop':loop}
music_started.emit(dialogic.current_state_info['music'])
if channel_id > max_channels:
printerr("\tChannel ID (%s) higher than Max Music Channels (%s)" % [channel_id, max_channels])
dialogic.print_debug_moment()
return

if not dialogic.current_state_info.has('music'):
dialogic.current_state_info['music'] = {}

dialogic.current_state_info['music'][channel_id] = {'path':path, 'volume':volume, 'audio_bus':audio_bus, 'loop':loop, 'channel':channel_id}
music_started.emit(dialogic.current_state_info['music'][channel_id])

var fader: Tween = null
if is_instance_valid(current_music_player) and current_music_player.playing or !path.is_empty():
if is_instance_valid(current_music_player[channel_id]) and current_music_player[channel_id].playing or !path.is_empty():
fader = create_tween()

var prev_node: Node = null
if is_instance_valid(current_music_player) and current_music_player.playing:
prev_node = current_music_player.duplicate()
add_child(prev_node)
prev_node.play(current_music_player.get_playback_position())
if is_instance_valid(current_music_player[channel_id]) and current_music_player[channel_id].playing:
prev_node = current_music_player[channel_id]
fader.tween_method(interpolate_volume_linearly.bind(prev_node), db_to_linear(prev_node.volume_db),0.0,fade_time)

if path:
current_music_player = base_music_player.duplicate()
add_child(current_music_player)
current_music_player.stream = load(path)
current_music_player.volume_db = volume
current_music_player[channel_id] = base_music_player.duplicate()
add_child(current_music_player[channel_id])
current_music_player[channel_id].stream = load(path)
current_music_player[channel_id].volume_db = volume
if audio_bus:
current_music_player.bus = audio_bus
if not current_music_player.stream is AudioStreamWAV:
if "loop" in current_music_player.stream:
current_music_player.stream.loop = loop
elif "loop_mode" in current_music_player.stream:
current_music_player[channel_id].bus = audio_bus
if not current_music_player[channel_id].stream is AudioStreamWAV:
if "loop" in current_music_player[channel_id].stream:
current_music_player[channel_id].stream.loop = loop
elif "loop_mode" in current_music_player[channel_id].stream:
if loop:
current_music_player.stream.loop_mode = AudioStreamWAV.LOOP_FORWARD
current_music_player[channel_id].stream.loop_mode = AudioStreamWAV.LOOP_FORWARD
else:
current_music_player.stream.loop_mode = AudioStreamWAV.LOOP_DISABLED
current_music_player[channel_id].stream.loop_mode = AudioStreamWAV.LOOP_DISABLED

current_music_player.play(0)
fader.parallel().tween_method(interpolate_volume_linearly.bind(current_music_player), 0.0, db_to_linear(volume),fade_time)
else:
if is_instance_valid(current_music_player):
current_music_player.stop()
current_music_player.queue_free()
current_music_player[channel_id].play(0)
fader.parallel().tween_method(interpolate_volume_linearly.bind(current_music_player[channel_id]), 0.0, db_to_linear(volume),fade_time)

if prev_node:
fader.tween_callback(prev_node.queue_free)


## Whether music is playing.
func has_music() -> bool:
return !dialogic.current_state_info.get('music', {}).get('path', '').is_empty()
func has_music(channel_id := 0) -> bool:
return !dialogic.current_state_info.get('music', {}).get(channel_id, {}).get('path', '').is_empty()


## Plays a given sound file.
Expand Down Expand Up @@ -187,10 +207,12 @@ func interpolate_volume_linearly(value: float, node: Node) -> void:


## Returns whether the currently playing audio resource is the same as this
## event's [param resource_path].
func is_music_playing_resource(resource_path: String) -> bool:
var is_playing_resource: bool = (base_music_player.is_playing()
and base_music_player.stream.resource_path == resource_path)
## event's [param resource_path], for [param channel_id].
func is_music_playing_resource(resource_path: String, channel_id := 0) -> bool:
var is_playing_resource: bool = (current_music_player.size() > channel_id
and is_instance_valid(current_music_player[channel_id])
and current_music_player[channel_id].is_playing()
and current_music_player[channel_id].stream.resource_path == resource_path)

return is_playing_resource

Expand Down
6 changes: 4 additions & 2 deletions addons/dialogic/Modules/Clear/event_clear.gd
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ func _execute() -> void:
dialogic.Backgrounds.update_background('', '', final_time)
if step_by_step: await dialogic.get_tree().create_timer(final_time).timeout

if clear_music and dialogic.has_subsystem('Audio') and dialogic.Audio.has_music():
dialogic.Audio.update_music('', 0.0, "", final_time)
if clear_music and dialogic.has_subsystem('Audio'):
for channel_id in dialogic.Audio.max_channels:
if dialogic.Audio.has_music(channel_id):
dialogic.Audio.update_music('', 0.0, "", final_time, channel_id)
if step_by_step: await dialogic.get_tree().create_timer(final_time).timeout

if clear_style and dialogic.has_subsystem('Styles'):
Expand Down

0 comments on commit 11de41c

Please sign in to comment.