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

259 lines
8.1 KiB
GDScript3
Raw Normal View History

2022-03-31 20:26:32 +00:00
extends Control
2022-04-15 07:53:44 +00:00
@onready var music_player:AudioStreamPlayer = $Music
2022-03-31 20:26:32 +00:00
var official_music:Dictionary = {}
2022-05-24 21:32:25 +00:00
var default_domain:String = "Les Chemins du Khanat"
2022-04-15 07:53:44 +00:00
var current_domain:String = ""
var current_music:int = 0
var music_play:bool = false
var select_rand:bool = true
var list_music:Array = []
2022-03-31 20:26:32 +00:00
2022-05-24 21:32:25 +00:00
func _ready() -> void:
var directory = DirAccess.open("res://music/")
2022-03-31 20:26:32 +00:00
var files = []
get_tree().paused = false
2022-04-15 07:53:44 +00:00
Common.msg_info("Load MusicManager")
2022-03-31 20:26:32 +00:00
if not directory.dir_exists( "res://music/" ):
2022-04-15 07:53:44 +00:00
Common.msg_info("No Music")
2022-03-31 20:26:32 +00:00
return
2023-04-10 22:12:51 +00:00
#directory.open( "res://music/" )
directory = null
directory = DirAccess.open( "res://music/" )
2022-03-31 20:26:32 +00:00
for dir in directory.get_directories():
var fullpath: String = "res://music/" + dir
var dir2:DirAccess = DirAccess.open(fullpath)
Common.msg_debug("Dir: " + str(dir))
2022-03-31 20:26:32 +00:00
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())
2022-04-15 07:53:44 +00:00
_on_random_pressed()
select_domain( default_domain )
current_music = official_music[current_domain].size()
2022-05-24 21:32:25 +00:00
# Get Mute state
Common.msg_debug("mute:" + str(user_settings.get_sound_mute()))
AudioServer.set_bus_mute(AudioServer.get_bus_index($Music.get_bus()), user_settings.get_sound_mute())
$Window/VBox/HBoxContainer/Mute.set_pressed(user_settings.get_sound_mute())
$Window/VBox/Tab/Music/HBox/Play.set_pressed(user_settings.get_sound_music_play())
$Window/VBox/Tab/Music/HBox/Random.set_pressed(user_settings.get_sound_music_random())
2022-04-15 07:53:44 +00:00
# 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))
2022-05-24 21:32:25 +00:00
var music_volume = user_settings.get_sound_music_volume()
$Music.set_volume_db(linear_to_db(music_volume/100.0))
$Window/VBox/Tab/Mixer/MusicLevel/music.set_value( int(db_to_linear($Music.get_volume_db()) * 100))
$Window/VBox/Tab/Mixer/MusicLevel/Value.set_text(str(int(db_to_linear($Music.get_volume_db()) * 100)))
2022-04-15 07:53:44 +00:00
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))
2022-05-24 21:32:25 +00:00
var global_volume = user_settings.get_sound_global_volume()
AudioServer.set_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()), linear_to_db(global_volume/100.0))
2022-04-15 07:53:44 +00:00
# AudioServer.get_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()))
$Window/VBox/Tab/Mixer/GlobalLevel/global.set_value( int(db_to_linear(AudioServer.get_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()))) * 100.0))
$Window/VBox/Tab/Mixer/GlobalLevel/Value.set_text(str(int(db_to_linear(AudioServer.get_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()))) * 100.0)))
2022-04-15 07:53:44 +00:00
2022-05-24 21:32:25 +00:00
func select_domain(new_domain:String) -> void:
2022-04-15 07:53:44 +00:00
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()
2022-05-24 21:32:25 +00:00
func reinitialize_jukebox() -> void:
2022-04-15 07:53:44 +00:00
list_music.clear()
if select_rand:
2023-04-10 22:12:51 +00:00
var temp:Array = []
2022-04-15 07:53:44 +00:00
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)
2022-05-24 21:32:25 +00:00
func next_music() -> void:
2023-04-10 22:12:51 +00:00
#var pos:float
2022-04-15 07:53:44 +00:00
current_music += 1
if current_music >= list_music.size():
reinitialize_jukebox()
play_music(0)
return
play_music(current_music)
2022-03-31 20:26:32 +00:00
func load_external_music(filepath):
var stream = null
Common.msg_debug("load_external_music")
2023-04-07 11:41:06 +00:00
#var file = FileAccess.new()
#file.open(filepath, file.READ)
var file = FileAccess.open(filepath, FileAccess.READ)
2022-03-31 20:26:32 +00:00
var ext = filepath.split(".")[-1].to_lower()
var buffer = file.get_buffer(file.get_len())
Common.msg_debug(ext)
match ext:
"ogg":
2022-09-06 22:27:28 +00:00
stream = AudioStreamOggVorbis.new()
2022-03-31 20:26:32 +00:00
"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)
2022-05-24 21:32:25 +00:00
func _on_stream_finished() -> void:
2022-03-31 20:26:32 +00:00
Common.msg_debug("_on_stream_finished")
music_player.stop()
2022-04-15 07:53:44 +00:00
music_play = false
2022-05-24 21:32:25 +00:00
func show_list_music() -> void:
2022-04-15 07:53:44 +00:00
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 )
2022-03-31 20:26:32 +00:00
2022-05-24 21:32:25 +00:00
func force_music(pos:int) -> void:
2022-04-15 07:53:44 +00:00
Common.msg_debug("Force: " + str(pos))
play_music(pos)
2022-05-24 21:32:25 +00:00
func play_music(pos: int) -> void:
2022-04-15 07:53:44 +00:00
if pos >= list_music.size():
2022-03-31 20:26:32 +00:00
return
2022-04-15 07:53:44 +00:00
var filename:String = official_music[current_domain][list_music[pos]]
music_play = true
2022-03-31 20:26:32 +00:00
music_player.set_stream(load_music( filename ))
music_player.play()
music_player.get_stream().set_loop(false)
2022-04-15 07:53:44 +00:00
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()
2023-04-10 22:12:51 +00:00
func _process(_delta) -> void:
2022-04-15 07:53:44 +00:00
if ! music_play and $Window/VBox/Tab/Music/HBox/Play.is_pressed():
next_music()
2022-03-31 20:26:32 +00:00
2022-05-24 21:32:25 +00:00
func _on_music_finished() -> void:
2022-03-31 20:26:32 +00:00
Common.msg_debug("_on_music_finished")
music_player.stop()
2022-04-15 07:53:44 +00:00
music_play = false
2022-05-24 21:32:25 +00:00
func _on_end_music_timeout() -> void:
2022-04-15 07:53:44 +00:00
# Timer end - sometimes, signal finished is not sent
Common.msg_debug("_on_end_music_timeout")
music_play = false
2022-05-24 21:32:25 +00:00
func show_config() -> void:
2022-04-15 07:53:44 +00:00
#$Window.popup_centered()
$Window.visible = true
2022-05-24 21:32:25 +00:00
func _on_button_pressed() -> void:
2022-04-15 07:53:44 +00:00
$Window.hide()
2022-05-24 21:32:25 +00:00
func _on_play_pressed() -> void:
2022-04-15 07:53:44 +00:00
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)
2022-05-24 21:32:25 +00:00
user_settings.set_sound_music_play(true)
2022-04-15 07:53:44 +00:00
else:
$Music.set_stream_paused(true)
$EndMusic.set_paused(true)
2022-05-24 21:32:25 +00:00
user_settings.set_sound_music_play(false)
2022-04-15 07:53:44 +00:00
2022-05-24 21:32:25 +00:00
func _on_random_pressed() -> void:
2022-04-15 07:53:44 +00:00
if $Window/VBox/Tab/Music/HBox/Random.is_pressed():
select_rand = true
2022-05-24 21:32:25 +00:00
user_settings.set_sound_music_random(true)
2022-04-15 07:53:44 +00:00
else:
select_rand = false
2022-05-24 21:32:25 +00:00
user_settings.set_sound_music_random(false)
2022-04-15 07:53:44 +00:00
2022-05-24 21:32:25 +00:00
func _on_reinit_pressed() -> void:
2022-04-15 07:53:44 +00:00
reinitialize_jukebox()
play_music(0)
2022-05-24 21:32:25 +00:00
func _on_mute_toggled(button_pressed) -> void:
2022-04-15 07:53:44 +00:00
AudioServer.set_bus_mute(AudioServer.get_bus_index($Music.get_bus()), button_pressed)
2022-05-24 21:32:25 +00:00
user_settings.set_sound_mute(button_pressed)
2022-04-15 07:53:44 +00:00
2022-05-24 21:32:25 +00:00
func _on_effect_value_changed(value) -> void:
$Window/VBox/Tab/Mixer/EffectLevel/Value.set_text(str(value))
2022-04-15 07:53:44 +00:00
2022-05-24 21:32:25 +00:00
func _on_music_value_changed(value) -> void:
$Music.set_volume_db(linear_to_db(value/100.0))
$Window/VBox/Tab/Mixer/MusicLevel/Value.set_text(str(value))
2022-05-24 21:32:25 +00:00
user_settings.set_sound_music_volume(value)
2022-04-15 07:53:44 +00:00
2022-05-24 21:32:25 +00:00
func _on_global_value_changed(value) -> void:
AudioServer.set_bus_volume_db(AudioServer.get_bus_index($Music.get_bus()), linear_to_db(value/100.0))
$Window/VBox/Tab/Mixer/GlobalLevel/Value.set_text(str(value))
2022-05-24 21:32:25 +00:00
user_settings.set_sound_global_volume(value)
2022-04-15 07:53:44 +00:00
2022-05-24 21:32:25 +00:00
func _on_window_close_requested() -> void:
2022-04-15 07:53:44 +00:00
$Window.hide()
func _on_window_visibility_changed():
if $Window.visible:
$Window/VBox/HBoxContainer/Quit.grab_focus()