fix audio bugs
All checks were successful
studiorailgun/Renderer/pipeline/head This commit looks good

This commit is contained in:
austin 2025-03-27 17:01:05 -04:00
parent f9e12553c9
commit 83185bb8ce
5 changed files with 26 additions and 15 deletions

View File

@ -1339,6 +1339,8 @@ Increase fall delay
Rearchitect foliage rendering to combat grass pop-in/pop-out Rearchitect foliage rendering to combat grass pop-in/pop-out
Increase sim range to fight game object pop-in Increase sim range to fight game object pop-in
Filter select-able saves on singleplayer screen to full game saves 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

View File

@ -207,8 +207,8 @@ public class AudioEngine {
* Updates the audio engine * Updates the audio engine
*/ */
public void update(){ public void update(){
updateListener(); this.updateListener();
updateOpenALSources(); this.updateOpenALSources();
} }
/** /**

View File

@ -33,11 +33,15 @@ public class AudioSource {
protected static AudioSource create(boolean loop){ protected static AudioSource create(boolean loop){
AudioSource rVal = new AudioSource(); AudioSource rVal = new AudioSource();
rVal.sourceId = AL10.alGenSources(); 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(); Globals.audioEngine.checkError();
if(loop){ if(loop){
AL10.alSourcei(rVal.sourceId, AL10.AL_LOOPING, AL10.AL_TRUE); AL10.alSourcei(rVal.sourceId, AL10.AL_LOOPING, AL10.AL_TRUE);
Globals.audioEngine.checkError(); Globals.audioEngine.checkError();
} }
Globals.audioEngine.registerSource(rVal);
return rVal; return rVal;
} }
@ -46,8 +50,8 @@ public class AudioSource {
* @param bufferId The id of the buffer * @param bufferId The id of the buffer
*/ */
public void setBuffer(int bufferId) { public void setBuffer(int bufferId) {
stop(); this.stop();
if(isAllocated()){ if(this.isAllocated()){
AL10.alSourcei(sourceId, AL10.AL_BUFFER, bufferId); AL10.alSourcei(sourceId, AL10.AL_BUFFER, bufferId);
Globals.audioEngine.checkError(); Globals.audioEngine.checkError();
} }
@ -58,7 +62,7 @@ public class AudioSource {
* @param position the position * @param position the position
*/ */
public void setPosition(Vector3f position) { public void setPosition(Vector3f position) {
if(isAllocated()){ if(this.isAllocated()){
AL10.alSource3f(sourceId, AL10.AL_POSITION, position.x, position.y, position.z); AL10.alSource3f(sourceId, AL10.AL_POSITION, position.x, position.y, position.z);
Globals.audioEngine.checkError(); Globals.audioEngine.checkError();
} }
@ -69,7 +73,7 @@ public class AudioSource {
* @param speed the speed * @param speed the speed
*/ */
public void setSpeed(Vector3f speed) { public void setSpeed(Vector3f speed) {
if(isAllocated()){ if(this.isAllocated()){
AL10.alSource3f(sourceId, AL10.AL_VELOCITY, speed.x, speed.y, speed.z); AL10.alSource3f(sourceId, AL10.AL_VELOCITY, speed.x, speed.y, speed.z);
Globals.audioEngine.checkError(); Globals.audioEngine.checkError();
} }
@ -89,7 +93,7 @@ public class AudioSource {
* @param time The time in seconds * @param time The time in seconds
*/ */
public void setOffset(float time){ public void setOffset(float time){
if(isAllocated()){ if(this.isAllocated()){
AL10.alSourcef(sourceId, AL11.AL_SEC_OFFSET, time); AL10.alSourcef(sourceId, AL11.AL_SEC_OFFSET, time);
Globals.audioEngine.checkError(); Globals.audioEngine.checkError();
} }
@ -100,7 +104,7 @@ public class AudioSource {
* @param gain the gain * @param gain the gain
*/ */
public void setGain(float gain) { public void setGain(float gain) {
if(isAllocated()){ if(this.isAllocated()){
LoggerInterface.loggerAudio.DEBUG("Set Gain: " + gain); LoggerInterface.loggerAudio.DEBUG("Set Gain: " + gain);
AL10.alSourcef(sourceId, AL10.AL_GAIN, gain); AL10.alSourcef(sourceId, AL10.AL_GAIN, gain);
if(Globals.audioEngine.checkError()){ if(Globals.audioEngine.checkError()){
@ -115,7 +119,7 @@ public class AudioSource {
* @param value The value to set the param to * @param value The value to set the param to
*/ */
public void setProperty(int param, float value) { public void setProperty(int param, float value) {
if(isAllocated()){ if(this.isAllocated()){
AL10.alSourcef(sourceId, param, value); AL10.alSourcef(sourceId, param, value);
Globals.audioEngine.checkError(); Globals.audioEngine.checkError();
} }
@ -125,7 +129,7 @@ public class AudioSource {
* Plays the audio source * Plays the audio source
*/ */
public void play() { public void play() {
if(isAllocated()){ if(this.isAllocated()){
AL10.alSourcePlay(sourceId); AL10.alSourcePlay(sourceId);
Globals.audioEngine.checkError(); Globals.audioEngine.checkError();
} }
@ -137,7 +141,7 @@ public class AudioSource {
*/ */
public boolean isPlaying() { public boolean isPlaying() {
boolean isPlaying = false; boolean isPlaying = false;
if(isAllocated()){ if(this.isAllocated()){
isPlaying = AL10.alGetSourcei(sourceId, AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING; isPlaying = AL10.alGetSourcei(sourceId, AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING;
Globals.audioEngine.checkError(); Globals.audioEngine.checkError();
} }
@ -148,7 +152,7 @@ public class AudioSource {
* Pauses the audio source * Pauses the audio source
*/ */
public void pause() { public void pause() {
if(isAllocated()){ if(this.isAllocated()){
AL10.alSourcePause(sourceId); AL10.alSourcePause(sourceId);
Globals.audioEngine.checkError(); Globals.audioEngine.checkError();
} }
@ -158,7 +162,7 @@ public class AudioSource {
* Stops the audio source * Stops the audio source
*/ */
public void stop() { public void stop() {
if(isAllocated() && this.sourceId != INVALID_ID){ if(this.isAllocated()){
AL10.alSourceStop(sourceId); AL10.alSourceStop(sourceId);
Globals.audioEngine.checkError(); Globals.audioEngine.checkError();
} }
@ -180,6 +184,6 @@ public class AudioSource {
* @return true if allocated, false otherwise * @return true if allocated, false otherwise
*/ */
private boolean isAllocated(){ private boolean isAllocated(){
return sourceId != UNDEFINED_ID; return sourceId != UNDEFINED_ID && this.sourceId != INVALID_ID;
} }
} }

View File

@ -42,7 +42,7 @@ public class AudioUtils {
* @return The audio source * @return The audio source
*/ */
protected static AudioSource playAudioAtLocation(String audioFile, Vector3d position, boolean loops){ 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);
} }
/** /**

View File

@ -8,6 +8,7 @@ import java.util.Random;
import org.joml.Vector3d; import org.joml.Vector3d;
import electrosphere.audio.VirtualAudioSourceManager.VirtualAudioSourceType; import electrosphere.audio.VirtualAudioSourceManager.VirtualAudioSourceType;
import electrosphere.client.terrain.sampling.ClientVoxelSampler;
import electrosphere.engine.Globals; import electrosphere.engine.Globals;
import electrosphere.game.data.audio.SurfaceAudioCollection; import electrosphere.game.data.audio.SurfaceAudioCollection;
import electrosphere.game.data.audio.SurfaceAudioType; import electrosphere.game.data.audio.SurfaceAudioType;
@ -101,6 +102,10 @@ public class MovementAudioService {
String rVal = null; String rVal = null;
SurfaceAudioType surfaceAudio = this.defaultSurfaceAudio; SurfaceAudioType surfaceAudio = this.defaultSurfaceAudio;
if(voxelType == ClientVoxelSampler.INVALID_POSITION){
return null;
}
//Check if ignored //Check if ignored
if(this.ignoredVoxelTypes != null){ if(this.ignoredVoxelTypes != null){
for(int ignoredVoxelType : this.ignoredVoxelTypes){ for(int ignoredVoxelType : this.ignoredVoxelTypes){