From 6c6e55492e4c771653c604d36278c521df227cb7 Mon Sep 17 00:00:00 2001 From: Stef Date: Tue, 30 Sep 2025 18:40:32 +0200 Subject: [PATCH] Between commit --- app/src/main/java/org/toop/Main.java | 3 -- .../toop/framework/audio/SoundManager.java | 48 ++++++++++++------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/toop/Main.java b/app/src/main/java/org/toop/Main.java index 1f91081..8f79cb9 100644 --- a/app/src/main/java/org/toop/Main.java +++ b/app/src/main/java/org/toop/Main.java @@ -19,9 +19,6 @@ public class Main { var b = new NetworkingClientManager(); var c = new SoundManager(a); -// IO.println(a.getAssets()); - IO.println(c.getClips()); - new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", true)).asyncPostEvent(); new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", true)).asyncPostEvent(); diff --git a/framework/src/main/java/org/toop/framework/audio/SoundManager.java b/framework/src/main/java/org/toop/framework/audio/SoundManager.java index 59d0656..90c39cd 100644 --- a/framework/src/main/java/org/toop/framework/audio/SoundManager.java +++ b/framework/src/main/java/org/toop/framework/audio/SoundManager.java @@ -1,5 +1,6 @@ package org.toop.framework.audio; +import org.toop.framework.SnowflakeGenerator; import org.toop.framework.assets.Asset; import org.toop.framework.assets.AssetManager; import org.toop.framework.assets.resources.AudioResource; @@ -12,12 +13,12 @@ import java.util.*; import javax.sound.sampled.*; public class SoundManager { - private final Map activeClips = new HashMap<>(); - private final HashMap clips = new HashMap<>(); - private final AssetManager assetManager; + private final Map activeClips = new HashMap<>(); + private final HashMap audioStreams = new HashMap<>(); + private final SnowflakeGenerator idGenerator; public SoundManager(AssetManager asm) { - this.assetManager = asm; + this.idGenerator = new SnowflakeGenerator(); // TODO: don't create a new snowflake generator. // Get all Audio Resources and add them to a list. for (Asset resource : asm.getAllResourceOfType(AudioResource.class).values()) { try { @@ -32,7 +33,11 @@ public class SoundManager { } private void handlePlaySound(AudioEvents.PlayAudio event) { - this.playSound(event.fileNameNoExtensionAndNoDirectory(), event.loop()); + try { + this.playSound(event.fileNameNoExtensionAndNoDirectory(), event.loop()); + } catch (LineUnavailableException | IOException e) { + throw new RuntimeException(e); + } } private void handleStopSound(AudioEvents.StopAudio event) { @@ -42,20 +47,22 @@ public class SoundManager { private void addClip(Asset audioAsset) throws IOException, UnsupportedAudioFileException, LineUnavailableException { AudioResource audioResource = audioAsset.getResource(); - - this.clips.put(audioAsset.getName(), audioResource.getClip()); + this.audioStreams.put(audioAsset.getName(), audioResource.getAudioStream()); } - private void playSound(String audioFileName, boolean loop) { - // Get clip - Clip clip = clips.get(audioFileName); + private long playSound(String audioFileName, boolean loop) throws LineUnavailableException, IOException { + // Get audioStream + AudioInputStream audioStream = audioStreams.get(audioFileName); - if (clip == null) { - return; + // Return -1 if audiStream doesn't exist + if (audioStream == null) { + return -1; } - // Reset clip - clip.setFramePosition(0); + // Get a clip and open the audioStream + final Clip clip = AudioSystem.getClip(); + clip.open(audioStream); + // If loop make it loop, else just start it once if (loop){ @@ -65,20 +72,25 @@ public class SoundManager { clip.start(); } + // Generate ID + long clipId = idGenerator.nextId(); + // store it so we can stop it later - activeClips.put(audioFileName, clip); // TODO: Do on snowflake for specific sound to stop + activeClips.put(clipId, clip); // TODO: Do on snowflake for specific sound to stop // remove when finished (only for non-looping sounds) clip.addLineListener(event -> { if (event.getType() == LineEvent.Type.STOP && !clip.isRunning()) { - activeClips.remove(audioFileName); + activeClips.remove(clipId); clip.close(); } }); + + return clipId; } - public HashMap getClips() { - return this.clips; + public HashMap getAudioStreams() { + return this.audioStreams; } public void stopSound(String audioFileName) {