I just added a MediaManager, which is, essentially, used to play music and sound effects. At this point it only does music, because I’m not there for the effects. But, for those interested, here’s the whole script.
Note that it only contains the very minimum to play the music tracks I own. Play, fade in/out, etc. But I think someone could use that to begin.
The script is a singleton and should be put into the autoload list.
PS: I’m not responsible for the bugs included therein.
extends AudioStreamPlayer
var transition_duration := 1
var transition_type := 1
var music_volume = -5
var tracks : Dictionary = \
{
"Elizabeth" : "Assets/Music/Elizabeth.ogg",
"Joana" : "Assets/Music/Joana.ogg",
"Eloise" : "Assets/Music/Eloise.ogg",
"Zhomia" : "Assets/Music/Zhomia.ogg",
"Store" : "Assets/Music/Store.ogg",
"Main" : "Assets/Music/MainMenu.ogg"
}
func _init():
volume_db = music_volume
func play_track(newSong : String):
fade_in(newSong)
play()
func fade_out():
# tween music volume down to -80 (muted)
var tween_out = create_tween()
tween_out.tween_property(self, "volume_db", -80, transition_duration)
Tween.interpolate_value(music_volume, 0, 0, transition_duration, Tween.TRANS_LINEAR, Tween.EASE_OUT)
func fade_out_to(newSong):
if playing:
# tween music volume down to -80 (muted)
var tween_out = create_tween()
tween_out.tween_property(self, "volume_db", -80, transition_duration)
Tween.interpolate_value(music_volume, 0, 0, transition_duration, Tween.TRANS_LINEAR, Tween.EASE_OUT)
tween_out.tween_callback(Callable(self, "done_fade_out").bind(newSong))
else:
done_fade_out(newSong)
func fade_in(newSong):
stream = load(str("res://", tracks.get(newSong)))
print("Playing : ", str("res://", tracks.get(newSong)), " (fade_in)")
# tween music volume up to music_volume (normal/defined)
var tween_in = create_tween()
tween_in.tween_property(self, "volume_db", music_volume, transition_duration)
Tween.interpolate_value(-80, 0, 0, transition_duration, Tween.TRANS_LINEAR, Tween.EASE_IN)
func pause_music():
stream_paused = !stream_paused
func mute():
volume_db = 0
func unmute():
volume_db = music_volume
func done_fade_out(newSong : String):
stop()
stream = load(str("res://", tracks.get(newSong)))
print("Playing : ", str("res://", tracks.get(newSong)), " (callback fade_out_to)")
# tween music volume up to music_volume (normal/defined)
var tween_in = create_tween()
tween_in.tween_property(self, "volume_db", music_volume, transition_duration)
Tween.interpolate_value(-80, 0.5, 0, transition_duration, Tween.TRANS_LINEAR, Tween.EASE_IN)
unmute()
play()