From 85f0a34e20abc748d11bdd4df558d29813c95090 Mon Sep 17 00:00:00 2001 From: Willem Cazander Date: Fri, 28 Jan 2022 11:59:57 +0100 Subject: [PATCH] Added music code --- .../demo4d/music/MusicManager.java | 131 ++++++++++++++++++ .../demo4d/music/MusicPlayerRenderer.java | 96 +++++++++++++ .../demo4d/music/MusicSong.java | 21 +++ .../demo4d/music/MusicSongType.java | 7 + 4 files changed, 255 insertions(+) create mode 100644 core/src/love/distributedrebirth/demo4d/music/MusicManager.java create mode 100644 core/src/love/distributedrebirth/demo4d/music/MusicPlayerRenderer.java create mode 100644 core/src/love/distributedrebirth/demo4d/music/MusicSong.java create mode 100644 core/src/love/distributedrebirth/demo4d/music/MusicSongType.java diff --git a/core/src/love/distributedrebirth/demo4d/music/MusicManager.java b/core/src/love/distributedrebirth/demo4d/music/MusicManager.java new file mode 100644 index 00000000..d7352daa --- /dev/null +++ b/core/src/love/distributedrebirth/demo4d/music/MusicManager.java @@ -0,0 +1,131 @@ +package love.distributedrebirth.demo4d.music; + +import java.util.ArrayList; +import java.util.List; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.audio.Music; +import com.badlogic.gdx.audio.Music.OnCompletionListener; +import com.badlogic.gdx.files.FileHandle; + +public class MusicManager { + + private final MusicSong introSong; + private final MusicSong creditsSong; + private final List backgroundSongs; + private final NextSongListener nextSongListener; + private MusicSong currentSong = null; + + public MusicManager() { + backgroundSongs = new ArrayList<>(); + introSong = new MusicSong(Gdx.audio.newMusic(Gdx.files.internal("music/panoramacircle-waterfowl.mp3")),"panoramacircle-waterfowl"); + creditsSong = new MusicSong(Gdx.audio.newMusic(Gdx.files.internal("music/idtech-doom-sigil.mp3")), "idtech-doom-sigil"); + nextSongListener = new NextSongListener(); + } + + public void addBackgroundMusic(FileHandle file) { + Music music = Gdx.audio.newMusic(file); + music.setOnCompletionListener(nextSongListener); + backgroundSongs.add(new MusicSong(music, file.name())); + } + + public void init() { + addBackgroundMusic(Gdx.files.internal("music/sanctumwave-risen.mp3")); + addBackgroundMusic(Gdx.files.internal("music/sanctumwave-devine-intellect.mp3")); + addBackgroundMusic(Gdx.files.internal("music/theselfhelpgroup-temple-os.mp3")); + addBackgroundMusic(Gdx.files.internal("music/sanctumwave-nightwalk.mp3")); + } + + public void dispose() { + introSong.music.dispose(); + creditsSong.music.dispose(); + for (MusicSong song:backgroundSongs) { + song.music.dispose(); + } + } + + public List getBackgroundSongs() { + return backgroundSongs; + } + + public String getCurrentSongName() { + if (currentSong != null) { + return currentSong.getName(); + } else { + return "None"; + } + } + + public void stop() { + if (currentSong != null) { + currentSong.music.stop(); + } + } + + public void play(MusicSongType type) { + MusicSong nextSong = null; + if (MusicSongType.INTRO.equals(type)) { + nextSong = introSong; + } else if (MusicSongType.CREDITS.equals(type)) { + nextSong = creditsSong; + } else { + int currentBackground = backgroundSongs.indexOf(currentSong); + if (currentBackground == -1) { + nextSong = backgroundSongs.get(0); + } else { + nextSong = currentSong; + } + } + play(nextSong); + } + + public void play(MusicSong song) { + if (song == null) { + return; + } + stop(); + currentSong = song; + currentSong.music.play(); + } + + class NextSongListener implements OnCompletionListener { + + @Override + public void onCompletion(Music music) { + next(); + play(currentSong); + } + } + + public void next() { + int currentBackground = backgroundSongs.indexOf(currentSong); + if (currentBackground == -1) { + return; // some other + } + if (currentBackground == backgroundSongs.size()-1) { + currentBackground = -1; // loop to start + } + boolean play = currentSong.music.isPlaying(); + currentSong.music.stop(); + currentSong = backgroundSongs.get(currentBackground+1); + if (play) { + currentSong.music.play(); + } + } + + public void prev() { + int currentBackground = backgroundSongs.indexOf(currentSong); + if (currentBackground == -1) { + return; // some other + } + if (currentBackground == 0) { + currentBackground = backgroundSongs.size(); // loop to end + } + boolean play = currentSong.music.isPlaying(); + currentSong.music.stop(); + currentSong = backgroundSongs.get(currentBackground-1); + if (play) { + currentSong.music.play(); + } + } +} diff --git a/core/src/love/distributedrebirth/demo4d/music/MusicPlayerRenderer.java b/core/src/love/distributedrebirth/demo4d/music/MusicPlayerRenderer.java new file mode 100644 index 00000000..ca74270c --- /dev/null +++ b/core/src/love/distributedrebirth/demo4d/music/MusicPlayerRenderer.java @@ -0,0 +1,96 @@ +package love.distributedrebirth.demo4d.music; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.files.FileHandle; + +import imgui.ImGui; +import imgui.flag.ImGuiCond; +import imgui.flag.ImGuiSelectableFlags; +import imgui.flag.ImGuiTableColumnFlags; +import imgui.flag.ImGuiTableFlags; +import imgui.type.ImBoolean; +import love.distributedrebirth.demo4d.ImGuiRendererMain; +import love.distributedrebirth.demo4d.Demo4DMain; +import net.spookygames.gdx.nativefilechooser.NativeFileChooserCallback; +import net.spookygames.gdx.nativefilechooser.NativeFileChooserConfiguration; + +public class MusicPlayerRenderer extends ImGuiRendererMain { + + public MusicPlayerRenderer(Demo4DMain main) { + super(main); + } + + @Override + public void render(ImBoolean widgetOpen) { + ImGui.setNextWindowPos(100, 100, ImGuiCond.FirstUseEver); + ImGui.setNextWindowSize(320, 240, ImGuiCond.FirstUseEver); + ImGui.begin("Music Player", widgetOpen); + + ImGui.text("Current Song:"); + ImGui.sameLine(); + ImGui.text(main.music.getCurrentSongName()); + ImGui.separator(); + if (ImGui.button("Play")) { + main.music.play(MusicSongType.BACKGROUND); + } + ImGui.sameLine(); + if (ImGui.button("<")) { + main.music.prev(); + } + ImGui.sameLine(); + if (ImGui.button(">")) { + main.music.next(); + } + ImGui.sameLine(); + if (ImGui.button("Stop")) { + main.music.stop(); + } + ImGui.sameLine(); + if (ImGui.button("Add")) { + NativeFileChooserConfiguration conf = new NativeFileChooserConfiguration(); + conf.directory = Gdx.files.absolute(System.getProperty("user.home")); + conf.mimeFilter = "audio/*"; + conf.title = "Choose audio file"; + main.fileChooser.chooseFile(conf, new NativeFileChooserCallback() { + + @Override + public void onFileChosen(FileHandle file) { + main.music.addBackgroundMusic(file); + } + + @Override + public void onCancellation() { + } + + @Override + public void onError(Exception exception) { + } + }); + + } + int flags = ImGuiTableFlags.ScrollX | ImGuiTableFlags.RowBg | ImGuiTableFlags.BordersOuter | ImGuiTableFlags.BordersV; + ImGui.beginTable("playlist", 3, flags); + ImGui.tableSetupColumn("#", ImGuiTableColumnFlags.NoHide); + ImGui.tableSetupColumn("Play"); + ImGui.tableSetupColumn("Name"); + ImGui.tableHeadersRow(); + int i=1; + for (MusicSong song:main.music.getBackgroundSongs()) { + ImGui.pushID(i); + ImGui.tableNextRow(); + ImGui.tableNextColumn(); + ImGui.selectable(""+i, song.isPlaying(), ImGuiSelectableFlags.None); + ImGui.tableNextColumn(); + if (ImGui.smallButton(">")) { + main.music.play(song); + } + ImGui.tableNextColumn(); + ImGui.selectable(song.getName(), song.isPlaying(), ImGuiSelectableFlags.None); + ImGui.popID(); + i++; + } + ImGui.endTable(); + + ImGui.end(); + } +} diff --git a/core/src/love/distributedrebirth/demo4d/music/MusicSong.java b/core/src/love/distributedrebirth/demo4d/music/MusicSong.java new file mode 100644 index 00000000..ae2bef0b --- /dev/null +++ b/core/src/love/distributedrebirth/demo4d/music/MusicSong.java @@ -0,0 +1,21 @@ +package love.distributedrebirth.demo4d.music; + +import com.badlogic.gdx.audio.Music; + +public class MusicSong { + protected final Music music; + private final String name; + + public MusicSong(Music music, String name) { + this.music = music; + this.name = name; + } + + public String getName() { + return name; + } + + public boolean isPlaying() { + return music.isPlaying(); + } +} \ No newline at end of file diff --git a/core/src/love/distributedrebirth/demo4d/music/MusicSongType.java b/core/src/love/distributedrebirth/demo4d/music/MusicSongType.java new file mode 100644 index 00000000..da454309 --- /dev/null +++ b/core/src/love/distributedrebirth/demo4d/music/MusicSongType.java @@ -0,0 +1,7 @@ +package love.distributedrebirth.demo4d.music; + +public enum MusicSongType { + INTRO, + CREDITS, + BACKGROUND +} \ No newline at end of file