Between commit

This commit is contained in:
2025-09-30 18:40:32 +02:00
parent 44b4613c27
commit 6c6e55492e
2 changed files with 30 additions and 21 deletions

View File

@@ -19,9 +19,6 @@ public class Main {
var b = new NetworkingClientManager(); var b = new NetworkingClientManager();
var c = new SoundManager(a); 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("mainmenu.wav", true)).asyncPostEvent();
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", true)).asyncPostEvent(); new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", true)).asyncPostEvent();

View File

@@ -1,5 +1,6 @@
package org.toop.framework.audio; package org.toop.framework.audio;
import org.toop.framework.SnowflakeGenerator;
import org.toop.framework.assets.Asset; import org.toop.framework.assets.Asset;
import org.toop.framework.assets.AssetManager; import org.toop.framework.assets.AssetManager;
import org.toop.framework.assets.resources.AudioResource; import org.toop.framework.assets.resources.AudioResource;
@@ -12,12 +13,12 @@ import java.util.*;
import javax.sound.sampled.*; import javax.sound.sampled.*;
public class SoundManager { public class SoundManager {
private final Map<String, Clip> activeClips = new HashMap<>(); private final Map<Long, Clip> activeClips = new HashMap<>();
private final HashMap<String, Clip> clips = new HashMap<>(); private final HashMap<String, AudioInputStream> audioStreams = new HashMap<>();
private final AssetManager assetManager; private final SnowflakeGenerator idGenerator;
public SoundManager(AssetManager asm) { 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. // Get all Audio Resources and add them to a list.
for (Asset<AudioResource> resource : asm.getAllResourceOfType(AudioResource.class).values()) { for (Asset<AudioResource> resource : asm.getAllResourceOfType(AudioResource.class).values()) {
try { try {
@@ -32,7 +33,11 @@ public class SoundManager {
} }
private void handlePlaySound(AudioEvents.PlayAudio event) { 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) { private void handleStopSound(AudioEvents.StopAudio event) {
@@ -42,20 +47,22 @@ public class SoundManager {
private void addClip(Asset<AudioResource> audioAsset) private void addClip(Asset<AudioResource> audioAsset)
throws IOException, UnsupportedAudioFileException, LineUnavailableException { throws IOException, UnsupportedAudioFileException, LineUnavailableException {
AudioResource audioResource = audioAsset.getResource(); AudioResource audioResource = audioAsset.getResource();
this.audioStreams.put(audioAsset.getName(), audioResource.getAudioStream());
this.clips.put(audioAsset.getName(), audioResource.getClip());
} }
private void playSound(String audioFileName, boolean loop) { private long playSound(String audioFileName, boolean loop) throws LineUnavailableException, IOException {
// Get clip // Get audioStream
Clip clip = clips.get(audioFileName); AudioInputStream audioStream = audioStreams.get(audioFileName);
if (clip == null) { // Return -1 if audiStream doesn't exist
return; if (audioStream == null) {
return -1;
} }
// Reset clip // Get a clip and open the audioStream
clip.setFramePosition(0); final Clip clip = AudioSystem.getClip();
clip.open(audioStream);
// If loop make it loop, else just start it once // If loop make it loop, else just start it once
if (loop){ if (loop){
@@ -65,20 +72,25 @@ public class SoundManager {
clip.start(); clip.start();
} }
// Generate ID
long clipId = idGenerator.nextId();
// store it so we can stop it later // 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) // remove when finished (only for non-looping sounds)
clip.addLineListener(event -> { clip.addLineListener(event -> {
if (event.getType() == LineEvent.Type.STOP && !clip.isRunning()) { if (event.getType() == LineEvent.Type.STOP && !clip.isRunning()) {
activeClips.remove(audioFileName); activeClips.remove(clipId);
clip.close(); clip.close();
} }
}); });
return clipId;
} }
public HashMap<String, Clip> getClips() { public HashMap<String, AudioInputStream> getAudioStreams() {
return this.clips; return this.audioStreams;
} }
public void stopSound(String audioFileName) { public void stopSound(String audioFileName) {