From ce325c16c0f5d82ed51abd8bd13928a7bc609ba5 Mon Sep 17 00:00:00 2001 From: ClarkeCode <33846391+ClarkeCode@users.noreply.github.com> Date: Mon, 6 Mar 2023 16:14:19 -0500 Subject: Add Music Player (#49) * Adding MusicConductor * Added selectable songs and player visibility toggle * Refinements to the music system * SongInfo compatability with various audio formats * Moved UI reqs to editor description; flipped conditions to exclude music .import files * Made selection of the first music track extension-agnostic * Fixed visual bug with play/pause button when interacting with progress slider --- game/src/MusicConductor/MusicConductor.gd | 68 +++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 game/src/MusicConductor/MusicConductor.gd (limited to 'game/src/MusicConductor/MusicConductor.gd') diff --git a/game/src/MusicConductor/MusicConductor.gd b/game/src/MusicConductor/MusicConductor.gd new file mode 100644 index 0000000..a463d6d --- /dev/null +++ b/game/src/MusicConductor/MusicConductor.gd @@ -0,0 +1,68 @@ +extends Node + +# SS-67 +@export_dir var musicDir : String +@export_file var firstSongName : String + +var selectedTrack = 0 +var availableSongs : Array[SongInfo] = [] +var autoPlayNextSong : bool = true + +func getAllSongNames() -> Array[String]: + var songNames : Array[String] = [] + for si in availableSongs: + songNames.append(si.readableName) + return songNames + +func getCurrentSongIndex() -> int: + return selectedTrack + +func getCurrentSongName() -> String: + return availableSongs[selectedTrack].readableName + +func scrubSongByPercentage(percentage: float) -> void: + var percentInSeconds : float = (percentage / 100.0) * $AudioStreamPlayer.stream.get_length() + $AudioStreamPlayer.play(percentInSeconds) + +func getCurrentSongProgressPercentage() -> float: + return 100 * ($AudioStreamPlayer.get_playback_position() / $AudioStreamPlayer.stream.get_length()) + +func isPaused() -> bool: + return $AudioStreamPlayer.stream_paused + +func togglePlayPause() -> void: + $AudioStreamPlayer.stream_paused = !$AudioStreamPlayer.stream_paused + +func startCurrentSong() -> void: + $AudioStreamPlayer.stream = availableSongs[selectedTrack].loadedStream + $AudioStreamPlayer.play() + +# SS-70 +func startSongByIndex(id: int) -> void: + selectedTrack = id + startCurrentSong() + +# SS-69 +func nextSong() -> void: + selectedTrack = (selectedTrack + 1) % len(availableSongs) + startCurrentSong() + +func prevSong() -> void: + selectedTrack = (len(availableSongs) - 1) if (selectedTrack == 0) else (selectedTrack - 1) + startCurrentSong() + +# Called when the node enters the scene tree for the first time. +func _ready(): + var dir = DirAccess.open(musicDir) + for fname in dir.get_files(): + if !fname.ends_with(".import"): + if fname.get_basename() == firstSongName: + selectedTrack = availableSongs.size() + availableSongs.append(SongInfo.new(musicDir, fname)) + startCurrentSong() + + +func _on_audio_stream_player_finished(): + if autoPlayNextSong: + nextSong() + startCurrentSong() -- cgit v1.2.3-56-ga3b1