Skip to content

Commit

Permalink
Allow setting the UI sound volume independently (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanhoad committed Apr 18, 2024
1 parent 122bf01 commit 317810e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
29 changes: 20 additions & 9 deletions addons/sound_manager/sound_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,9 @@ func get_sound_volume() -> float:
return db_to_linear(AudioServer.get_bus_volume_db(AudioServer.get_bus_index(sound_effects.bus)))


func get_ui_sound_volume() -> float:
return db_to_linear(AudioServer.get_bus_volume_db(AudioServer.get_bus_index(ui_sound_effects.bus)))


func set_sound_volume(volume_between_0_and_1: float) -> void:
_show_shared_bus_warning()
AudioServer.set_bus_volume_db(AudioServer.get_bus_index(sound_effects.bus), linear_to_db(volume_between_0_and_1))
AudioServer.set_bus_volume_db(AudioServer.get_bus_index(ui_sound_effects.bus), linear_to_db(volume_between_0_and_1))


func play_sound(resource: AudioStream, override_bus: String = "") -> AudioStreamPlayer:
Expand All @@ -82,6 +77,24 @@ func stop_sound(resource: AudioStream) -> void:
return sound_effects.stop(resource)


func set_default_sound_bus(bus: String) -> void:
sound_effects.bus = bus


#endregion

#region UI sounds


func get_ui_sound_volume() -> float:
return db_to_linear(AudioServer.get_bus_volume_db(AudioServer.get_bus_index(ui_sound_effects.bus)))


func set_ui_sound_volume(volume_between_0_and_1: float) -> void:
_show_shared_bus_warning()
AudioServer.set_bus_volume_db(AudioServer.get_bus_index(ui_sound_effects.bus), linear_to_db(volume_between_0_and_1))


func play_ui_sound(resource: AudioStream, override_bus: String = "") -> AudioStreamPlayer:
return ui_sound_effects.play(resource, override_bus)

Expand All @@ -96,10 +109,6 @@ func stop_ui_sound(resource: AudioStream) -> void:
return ui_sound_effects.stop(resource)


func set_default_sound_bus(bus: String) -> void:
sound_effects.bus = bus


func set_default_ui_sound_bus(bus: String) -> void:
ui_sound_effects.bus = bus

Expand Down Expand Up @@ -209,6 +218,8 @@ func set_default_music_bus(bus: String) -> void:


func _show_shared_bus_warning() -> void:
if "Master" in [music.bus, sound_effects.bus, ui_sound_effects.bus, ambient_sounds.bus]:
push_warning("Using the Master sound bus directly isn't recommended.")
if music.bus == sound_effects.bus or music.bus == ui_sound_effects.bus:
push_warning("Both music and sounds are using the same bus: %s" % music.bus)

Expand Down
6 changes: 5 additions & 1 deletion docs/Sounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ Playing sound effects is broken up into playing general sounds (ie. in the game

- **`SoundManager.set_sound_volume(volume_between_0_and_1: float) -> void:`**

Sets the volume for sounds (both general and UI sounds) using a given float between 0 and 1.
Sets the volume for sounds using a given float between 0 and 1.

- **`SoundManager.set_ui_sound_volume(volume_between_0_and_1: float) -> void:`**

Sets the volume for UI sounds using a given float between 0 and 1.

- **`SoundManager.play_sound(resource: AudioStream, override_bus: String = "") -> AudioStreamPlayer`**

Expand Down

0 comments on commit 317810e

Please sign in to comment.