godot-third-person-basic-scene/scenes/music/music_manager.gd

235 lines
6.9 KiB
GDScript

extends Control
@onready var music_player:AudioStreamPlayer = $Music
var official_music:Dictionary = {}
var default_domain:String = "default"
var current_domain:String = ""
var current_music:int = 0
var music_play:bool = false
var select_rand:bool = true
var list_music:Array = []
func _ready():
var directory:Directory = Directory.new()
var files = []
get_tree().paused = false
Common.msg_info("Load MusicManager")
if not directory.dir_exists( "res://music/" ):
Common.msg_info("No Music")
return
directory.open( "res://music/" )
for dir in directory.get_directories():
var dir2:Directory = Directory.new()
var fullpath: String = "res://music/" + dir
dir2.open( fullpath )
# Common.msg_debug("Dir: " + str(dir))
for file in dir2.get_files():
if file == "":
break
elif not file.begins_with(".") and not dir2.current_is_dir() and (file.ends_with( ".ogg" ) or file.ends_with( ".mp3" )):
if ! official_music.has(dir):
official_music[dir] = []
official_music[dir].append(fullpath + "/" + file)
files.append(file)
#music_player.finished.connect(_on_stream_finished.bind())
_on_random_pressed()
select_domain( default_domain )
current_music = official_music[current_domain].size()
# Get Volume
# Common.msg_debug("volume :" + str($Music.get_volume_db()))
# var t = linear2db(0.0)
# var u = db2linear($Music.get_volume_db())
# Common.msg_debug("volume :" + str(u))
var music_volume = float(user_settings.configuration["sound"]["music_volume"])
$Music.set_volume_db(linear2db(music_volume/100.0))
$Window/VBox/Tab/Mixer/MusicLevel/music.set_value( int(db2linear($Music.get_volume_db()) * 100))
$Window/VBox/Tab/Mixer/MusicLevel/Value.set_text(str(int(db2linear($Music.get_volume_db()) * 100)))
var bus_name = $Music.get_bus()
Common.msg_debug("bus_name: " + str(bus_name))
var bus_id = AudioServer.get_bus_index(bus_name)
Common.msg_debug("bus_id: " + str(bus_id))
var glb_volume = AudioServer.get_bus_volume_db(bus_id)
Common.msg_debug("GLB Volume: " + str(glb_volume))
# AudioServer.get_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()))
$Window/VBox/Tab/Mixer/GlobalLevel/global.set_value( int(db2linear(AudioServer.get_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()))) * 100.0))
$Window/VBox/Tab/Mixer/GlobalLevel/Value.set_text(str(int(db2linear(AudioServer.get_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()))) * 100.0)))
func select_domain(new_domain:String):
var found:bool = false
for key in official_music.keys():
if new_domain == key:
found = true
if found == false:
return
current_domain = new_domain
reinitialize_jukebox()
func reinitialize_jukebox():
list_music.clear()
if select_rand:
var temp:Array
for i in range(0, official_music[current_domain].size()):
temp.append(i)
for i in range(0, official_music[current_domain].size()):
var pos = randi() % temp.size()
list_music.append(temp[pos])
temp.remove_at(pos)
else:
for i in range(0, official_music[current_domain].size()):
list_music.append(i)
func next_music():
var pos:float
current_music += 1
if current_music >= list_music.size():
reinitialize_jukebox()
play_music(0)
return
play_music(current_music)
func load_external_music(filepath):
var stream = null
Common.msg_debug("load_external_music")
var file = File.new()
file.open(filepath, file.READ)
var ext = filepath.split(".")[-1].to_lower()
var buffer = file.get_buffer(file.get_len())
Common.msg_debug(ext)
match ext:
"ogg":
stream = AudioStreamOGGVorbis.new()
"mp3":
stream = AudioStreamMP3.new()
#"wav":
# # We need decode message to get format (Stero/size bit/ ...)
# stream = AudioStreamSample.new()
# stream.format = AudioStreamSample.FORMAT_16_BITS
# #stream.stereo = true
_:
Common.msg_error("Impossible to identify type of file (file:" + filepath + ", ext:" + ext + ")")
return
stream.data = buffer
Common.msg_debug("load_external_music : End")
return stream
func load_music(filepath: String):
if filepath.left(5) == "res:/":
return load( filepath )
return load_external_music(filepath)
func _on_stream_finished():
Common.msg_debug("_on_stream_finished")
music_player.stop()
music_play = false
func show_list_music():
for child in $Window/VBox/Tab/Music/ScrollContainer/ListMusic.get_children():
child.queue_free()
for pos in range(0, list_music.size()):
var filename:String = official_music[current_domain][list_music[pos]]
var music_box = preload( "res://scenes/music/music_field.tscn" ).instantiate()
music_box.set_music( filename, filename.get_file().get_basename() , pos, pos == current_music)
music_box.connect("selected_music", force_music.bind())
$Window/VBox/Tab/Music/ScrollContainer/ListMusic.add_child( music_box )
func force_music(pos:int):
Common.msg_debug("Force: " + str(pos))
play_music(pos)
func play_music(pos: int):
if pos >= list_music.size():
return
var filename:String = official_music[current_domain][list_music[pos]]
music_play = true
music_player.set_stream(load_music( filename ))
music_player.play()
music_player.get_stream().set_loop(false)
var timer = music_player.get_stream().get_length()
$EndMusic.start(timer)
music_player.connect("finished", _on_stream_finished.bind())
current_music = pos
show_list_music()
func _process(delta):
if ! music_play and $Window/VBox/Tab/Music/HBox/Play.is_pressed():
next_music()
func _on_music_finished():
Common.msg_debug("_on_music_finished")
music_player.stop()
music_play = false
func _on_end_music_timeout():
# Timer end - sometimes, signal finished is not sent
Common.msg_debug("_on_end_music_timeout")
music_play = false
func show_config():
#$Window.popup_centered()
$Window.visible = true
func _on_button_pressed():
$Window.hide()
func _on_play_pressed():
var current_time = $EndMusic.get_time_left()
Common.msg_debug("_on_play_pressed : " + str(current_time))
if $Window/VBox/Tab/Music/HBox/Play.is_pressed():
$Music.set_stream_paused(false)
$EndMusic.set_paused(false)
else:
$Music.set_stream_paused(true)
$EndMusic.set_paused(true)
func _on_random_pressed():
if $Window/VBox/Tab/Music/HBox/Random.is_pressed():
select_rand = true
else:
select_rand = false
func _on_reinit_pressed():
reinitialize_jukebox()
play_music(0)
func _on_mute_toggled(button_pressed):
AudioServer.set_bus_mute(AudioServer.get_bus_index($Music.get_bus()), button_pressed)
func _on_effect_value_changed(value):
$Window/VBox/Tab/Mixer/EffectLevel/Value.set_text(str(value))
func _on_music_value_changed(value):
$Music.set_volume_db(linear2db(value/100.0))
$Window/VBox/Tab/Mixer/MusicLevel/Value.set_text(str(value))
user_settings.configuration["sound"]["music_volume"] = value
user_settings.save()
func _on_global_value_changed(value):
AudioServer.set_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()), linear2db(value/100.0))
$Window/VBox/Tab/Mixer/GlobalLevel/Value.set_text(str(value))
func _on_window_close_requested():
$Window.hide()