Merge pull request #6 from STEMuli-Tx/jclemente/music_sfx_init

Jclemente/music sfx init
pull/18/head
mrwadepro 2025-04-11 13:11:53 +07:00 committed by GitHub
commit 5cd1196108
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 40 deletions

@ -121,16 +121,8 @@ func setup_background_music():
music.loop = true
music_player.stream = music
music_player.volume_db = 0 # Full volume for better web playback
music_player.bus = "Music" # Use the Music bus
# Direct check of music bus
var music_bus_idx = AudioServer.get_bus_index("Music")
if music_bus_idx >= 0:
# Force bus volume
AudioServer.set_bus_volume_db(music_bus_idx, 0)
AudioServer.set_bus_mute(music_bus_idx, false)
# Check if we can play audio immediately (desktop) or need to wait (web)
var can_play_now = true
if OS.has_feature("web"):
@ -138,11 +130,6 @@ func setup_background_music():
if sound_manager:
can_play_now = sound_manager.audio_initialized
# Force SoundManager settings
sound_manager.music_volume = 1.0
sound_manager.music_muted = false
sound_manager._apply_music_volume()
# If not initialized, connect to the ready signal
if not can_play_now:
sound_manager.audio_ready.connect(_start_background_music)
@ -155,7 +142,6 @@ func setup_background_music():
var fallback_sound = load("res://sounds/building_placing.wav")
if fallback_sound:
music_player.stream = fallback_sound
music_player.volume_db = 0
music_player.bus = "Music"
# Check if we can play immediately
@ -177,18 +163,6 @@ func _start_background_music():
music_player.stop()
music_player.seek(0.0)
# Set reasonable volume
music_player.volume_db = -10 # Normal volume for web
music_player.bus = "Music"
# Make sure buses are unmuted
AudioServer.set_bus_mute(0, false) # Master
# Music bus
var music_bus_idx = AudioServer.get_bus_index("Music")
if music_bus_idx >= 0:
AudioServer.set_bus_mute(music_bus_idx, false)
# Play the music
music_player.play()
@ -210,11 +184,7 @@ func _start_background_music():
else:
# Standard approach for desktop builds
music_player.play()
# This retry audio function has been removed in favor of the simpler approach
# This helper has been removed in favor of a simpler approach
# Setup building sound effects
func setup_building_sfx():
building_sfx = AudioStreamPlayer.new()

@ -11,8 +11,8 @@ var react_sound_bridge = null # Will be instantiated from a script
var audio_bridge = null # Will be instantiated from a script
# Volume ranges from 0.0 to 1.0
var music_volume: float = 0.8
var sfx_volume: float = 0.8
var music_volume: float = 0.1
var sfx_volume: float = 0.1
# Mute states
var music_muted: bool = false
@ -100,10 +100,13 @@ func _ready():
# Set the music player bus
music_player.bus = MUSIC_BUS_NAME
# Apply initial volume settings
_apply_music_volume()
# For non-web platforms, we can initialize immediately
audio_initialized = true
# Emit the audio_ready signal
# Emit the audio ready signal
audio_ready.emit()
# Setup audio buses (doesn't start audio playback)
@ -274,16 +277,16 @@ func play_music(sound_name: String, loop: bool = true):
# Set up and play the music
music_player.stream = stream
if music_muted:
music_player.volume_db = linear_to_db(0)
else:
music_player.volume_db = linear_to_db(music_volume)
music_player.bus = MUSIC_BUS_NAME
# Set looping if supported by the stream
if stream is AudioStreamMP3 or stream is AudioStreamOggVorbis:
stream.loop = loop
# Ensure volume is set correctly before playing
_apply_music_volume()
# Play the music
music_player.play()
# Play a sound effect
@ -471,7 +474,8 @@ func _apply_music_volume():
if music_muted:
music_player.volume_db = linear_to_db(0)
else:
music_player.volume_db = linear_to_db(music_volume)
var db_value = linear_to_db(music_volume)
music_player.volume_db = db_value
# Apply SFX volume settings
func _apply_sfx_volume():
@ -501,4 +505,6 @@ func _apply_sfx_volume():
func linear_to_db(linear_value: float) -> float:
if linear_value <= 0:
return -80.0 # Very low but not -INF
return 20.0 * log(linear_value) / log(10.0)
# Map 0.0-1.0 to -30dB to 0dB for a more usable range
var db_value = (linear_value * 30.0) - 30.0
return db_value