From 83185bb8ce130edb66551965c9a6a44a7e629e09 Mon Sep 17 00:00:00 2001 From: austin Date: Thu, 27 Mar 2025 17:01:05 -0400 Subject: [PATCH] fix audio bugs --- docs/src/progress/renderertodo.md | 2 ++ .../java/electrosphere/audio/AudioEngine.java | 4 +-- .../java/electrosphere/audio/AudioSource.java | 28 +++++++++++-------- .../java/electrosphere/audio/AudioUtils.java | 2 +- .../audio/movement/MovementAudioService.java | 5 ++++ 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 60b126af..cc8b9988 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1339,6 +1339,8 @@ Increase fall delay Rearchitect foliage rendering to combat grass pop-in/pop-out Increase sim range to fight game object pop-in Filter select-able saves on singleplayer screen to full game saves +Increase number of invalid openAL IDs +Fix audio engine not cleaning up audio sources diff --git a/src/main/java/electrosphere/audio/AudioEngine.java b/src/main/java/electrosphere/audio/AudioEngine.java index 66e1b28a..411086b7 100644 --- a/src/main/java/electrosphere/audio/AudioEngine.java +++ b/src/main/java/electrosphere/audio/AudioEngine.java @@ -207,8 +207,8 @@ public class AudioEngine { * Updates the audio engine */ public void update(){ - updateListener(); - updateOpenALSources(); + this.updateListener(); + this.updateOpenALSources(); } /** diff --git a/src/main/java/electrosphere/audio/AudioSource.java b/src/main/java/electrosphere/audio/AudioSource.java index d2027350..e22440e2 100644 --- a/src/main/java/electrosphere/audio/AudioSource.java +++ b/src/main/java/electrosphere/audio/AudioSource.java @@ -33,11 +33,15 @@ public class AudioSource { protected static AudioSource create(boolean loop){ AudioSource rVal = new AudioSource(); rVal.sourceId = AL10.alGenSources(); + if(rVal.sourceId == AudioSource.INVALID_ID){ + throw new Error("Was allocated an invalid id from openAL! " + rVal.sourceId); + } Globals.audioEngine.checkError(); if(loop){ AL10.alSourcei(rVal.sourceId, AL10.AL_LOOPING, AL10.AL_TRUE); Globals.audioEngine.checkError(); } + Globals.audioEngine.registerSource(rVal); return rVal; } @@ -46,8 +50,8 @@ public class AudioSource { * @param bufferId The id of the buffer */ public void setBuffer(int bufferId) { - stop(); - if(isAllocated()){ + this.stop(); + if(this.isAllocated()){ AL10.alSourcei(sourceId, AL10.AL_BUFFER, bufferId); Globals.audioEngine.checkError(); } @@ -58,7 +62,7 @@ public class AudioSource { * @param position the position */ public void setPosition(Vector3f position) { - if(isAllocated()){ + if(this.isAllocated()){ AL10.alSource3f(sourceId, AL10.AL_POSITION, position.x, position.y, position.z); Globals.audioEngine.checkError(); } @@ -69,7 +73,7 @@ public class AudioSource { * @param speed the speed */ public void setSpeed(Vector3f speed) { - if(isAllocated()){ + if(this.isAllocated()){ AL10.alSource3f(sourceId, AL10.AL_VELOCITY, speed.x, speed.y, speed.z); Globals.audioEngine.checkError(); } @@ -89,7 +93,7 @@ public class AudioSource { * @param time The time in seconds */ public void setOffset(float time){ - if(isAllocated()){ + if(this.isAllocated()){ AL10.alSourcef(sourceId, AL11.AL_SEC_OFFSET, time); Globals.audioEngine.checkError(); } @@ -100,7 +104,7 @@ public class AudioSource { * @param gain the gain */ public void setGain(float gain) { - if(isAllocated()){ + if(this.isAllocated()){ LoggerInterface.loggerAudio.DEBUG("Set Gain: " + gain); AL10.alSourcef(sourceId, AL10.AL_GAIN, gain); if(Globals.audioEngine.checkError()){ @@ -115,7 +119,7 @@ public class AudioSource { * @param value The value to set the param to */ public void setProperty(int param, float value) { - if(isAllocated()){ + if(this.isAllocated()){ AL10.alSourcef(sourceId, param, value); Globals.audioEngine.checkError(); } @@ -125,7 +129,7 @@ public class AudioSource { * Plays the audio source */ public void play() { - if(isAllocated()){ + if(this.isAllocated()){ AL10.alSourcePlay(sourceId); Globals.audioEngine.checkError(); } @@ -137,7 +141,7 @@ public class AudioSource { */ public boolean isPlaying() { boolean isPlaying = false; - if(isAllocated()){ + if(this.isAllocated()){ isPlaying = AL10.alGetSourcei(sourceId, AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING; Globals.audioEngine.checkError(); } @@ -148,7 +152,7 @@ public class AudioSource { * Pauses the audio source */ public void pause() { - if(isAllocated()){ + if(this.isAllocated()){ AL10.alSourcePause(sourceId); Globals.audioEngine.checkError(); } @@ -158,7 +162,7 @@ public class AudioSource { * Stops the audio source */ public void stop() { - if(isAllocated() && this.sourceId != INVALID_ID){ + if(this.isAllocated()){ AL10.alSourceStop(sourceId); Globals.audioEngine.checkError(); } @@ -180,6 +184,6 @@ public class AudioSource { * @return true if allocated, false otherwise */ private boolean isAllocated(){ - return sourceId != UNDEFINED_ID; + return sourceId != UNDEFINED_ID && this.sourceId != INVALID_ID; } } diff --git a/src/main/java/electrosphere/audio/AudioUtils.java b/src/main/java/electrosphere/audio/AudioUtils.java index 971b0baf..5c8c7c54 100644 --- a/src/main/java/electrosphere/audio/AudioUtils.java +++ b/src/main/java/electrosphere/audio/AudioUtils.java @@ -42,7 +42,7 @@ public class AudioUtils { * @return The audio source */ protected static AudioSource playAudioAtLocation(String audioFile, Vector3d position, boolean loops){ - return playAudioAtLocation(audioFile,new Vector3f((float)position.x,(float)position.y,(float)position.z),loops); + return AudioUtils.playAudioAtLocation(audioFile,new Vector3f((float)position.x,(float)position.y,(float)position.z),loops); } /** diff --git a/src/main/java/electrosphere/audio/movement/MovementAudioService.java b/src/main/java/electrosphere/audio/movement/MovementAudioService.java index 16778265..7442ba71 100644 --- a/src/main/java/electrosphere/audio/movement/MovementAudioService.java +++ b/src/main/java/electrosphere/audio/movement/MovementAudioService.java @@ -8,6 +8,7 @@ import java.util.Random; import org.joml.Vector3d; import electrosphere.audio.VirtualAudioSourceManager.VirtualAudioSourceType; +import electrosphere.client.terrain.sampling.ClientVoxelSampler; import electrosphere.engine.Globals; import electrosphere.game.data.audio.SurfaceAudioCollection; import electrosphere.game.data.audio.SurfaceAudioType; @@ -101,6 +102,10 @@ public class MovementAudioService { String rVal = null; SurfaceAudioType surfaceAudio = this.defaultSurfaceAudio; + if(voxelType == ClientVoxelSampler.INVALID_POSITION){ + return null; + } + //Check if ignored if(this.ignoredVoxelTypes != null){ for(int ignoredVoxelType : this.ignoredVoxelTypes){