From aa640e23d63e488b63e216e989d759132c50523c Mon Sep 17 00:00:00 2001 From: Eugenuss Date: Wed, 23 Sep 2020 20:17:02 +0300 Subject: [PATCH] Fixed sound positioning while reconnecting --- .../progressia/client/audio/Listener.java | 74 ++++++++++++------- .../progressia/client/audio/Sound.java | 24 ++---- .../progressia/client/audio/SoundManager.java | 34 +++++++++ .../client/audio/backend/ALTest.java | 5 +- 4 files changed, 92 insertions(+), 45 deletions(-) create mode 100644 src/main/java/ru/windcorp/progressia/client/audio/SoundManager.java diff --git a/src/main/java/ru/windcorp/progressia/client/audio/Listener.java b/src/main/java/ru/windcorp/progressia/client/audio/Listener.java index af27609..1be4a9f 100644 --- a/src/main/java/ru/windcorp/progressia/client/audio/Listener.java +++ b/src/main/java/ru/windcorp/progressia/client/audio/Listener.java @@ -1,48 +1,68 @@ package ru.windcorp.progressia.client.audio; -import com.sun.jna.platform.unix.X11; import glm.vec._3.Vec3; -import jglm.Vec; -import org.lwjgl.BufferUtils; import ru.windcorp.progressia.client.Client; import ru.windcorp.progressia.client.ClientState; import ru.windcorp.progressia.client.graphics.world.Camera; -import ru.windcorp.progressia.common.util.FloatMathUtils; - -import java.nio.FloatBuffer; import static org.lwjgl.openal.AL10.*; //TODO add getters and setters public class Listener { - private static Vec3 position = new Vec3(); - private static Vec3 velocity = new Vec3(); - private static Vec3 oriAt = new Vec3(); - private static Vec3 oriUp = new Vec3(); - private static Client client; - private static Camera.Anchor anchor; + private static final Listener instance = new Listener(); + private Listener() {} + public static Listener getInstance() { + return instance; + } - public static void update() { + //Params + private final Vec3 position = new Vec3(); + private final Vec3 velocity = new Vec3(); + private final Vec3 oriAt = new Vec3(); + private final Vec3 oriUp = new Vec3(); + + private boolean isClientConnected = false; + private Camera.Anchor anchor; + + public void update() { + Client client = ClientState.getInstance(); if (client == null) { - client = ClientState.getInstance(); - } else if (anchor == null) { - anchor = client.getCamera().getAnchor(); + if (isClientConnected) { + isClientConnected = false; + resetParams(); + applyParams(); + } } else { - anchor.getCameraPosition(position); - float pitch = anchor.getCameraPitch(); - float yaw = anchor.getCameraYaw(); - oriAt.set( (float) (Math.cos(pitch) * Math.cos(yaw)), + isClientConnected = true; + if (anchor == null) { + anchor = client.getCamera().getAnchor(); + } else { + anchor.getCameraPosition(position); + float pitch = anchor.getCameraPitch(); + float yaw = anchor.getCameraYaw(); + oriAt.set((float) (Math.cos(pitch) * Math.cos(yaw)), (float) (Math.cos(pitch) * Math.sin(yaw)), - (float) Math.sin(pitch) ); - oriUp.set( (float) (Math.cos(pitch + Math.PI / 2) * Math.cos(yaw)), + (float) Math.sin(pitch)); + oriUp.set((float) (Math.cos(pitch + Math.PI / 2) * Math.cos(yaw)), (float) (Math.cos(pitch + Math.PI / 2) * Math.sin(yaw)), - (float) Math.sin(pitch + Math.PI / 2) ); - - alListener3f(AL_POSITION, position.x, position.y, position.z); - alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z); - alListenerfv(AL_ORIENTATION, new float[] {oriAt.x, oriAt.y, oriAt.z, oriUp.x, oriUp.y, oriUp.z}); + (float) Math.sin(pitch + Math.PI / 2)); + applyParams(); + } } } + + private void resetParams() { + position.set(0f, 0f, 0f); + velocity.set(0f, 0f, 0f); + oriAt.set(0f, 0f, 0f); + oriUp.set(0f, 0f, 0f); + } + + private void applyParams() { + alListener3f(AL_POSITION, position.x, position.y, position.z); + alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z); + alListenerfv(AL_ORIENTATION, new float[]{oriAt.x, oriAt.y, oriAt.z, oriUp.x, oriUp.y, oriUp.z}); + } } \ No newline at end of file diff --git a/src/main/java/ru/windcorp/progressia/client/audio/Sound.java b/src/main/java/ru/windcorp/progressia/client/audio/Sound.java index a1a1690..4222ad8 100644 --- a/src/main/java/ru/windcorp/progressia/client/audio/Sound.java +++ b/src/main/java/ru/windcorp/progressia/client/audio/Sound.java @@ -44,29 +44,25 @@ public class Sound { setGain(gain); } - public void forcePlay() { + public void playOnce() { alSourcePlay(source); } - public boolean playOnce() { - if(isEmpty()) { - alSourcePlay(source); - return true; - } - else return false; - } - - public boolean playLoop() { + public void playLoop() { alSourcei(source, AL_LOOPING, AL_TRUE); - boolean isPlaying = playOnce(); + playOnce(); alSourcei(source, AL_LOOPING, AL_FALSE); - return isPlaying; } public void stop() { alSourceStop(source); } + public void flush() { + alDeleteBuffers(audio); + alDeleteBuffers(source); + } + public void pause() { alSourcePause(source); } @@ -76,10 +72,6 @@ public class Sound { return state == AL_TRUE; } - public boolean isEmpty() { - return source == 0; - } - public int getAudio() { return audio; } diff --git a/src/main/java/ru/windcorp/progressia/client/audio/SoundManager.java b/src/main/java/ru/windcorp/progressia/client/audio/SoundManager.java new file mode 100644 index 0000000..bb17bd3 --- /dev/null +++ b/src/main/java/ru/windcorp/progressia/client/audio/SoundManager.java @@ -0,0 +1,34 @@ +package ru.windcorp.progressia.client.audio; + +import org.lwjgl.BufferUtils; + +import java.nio.IntBuffer; +import java.util.concurrent.ArrayBlockingQueue; + +public class SoundManager { + private static IntBuffer sources = BufferUtils.createIntBuffer(65); + private static ArrayBlockingQueue sounds = new ArrayBlockingQueue<>(64); + + public static void update() { + + } + + public static void addSound(Sound sound) { + if (!sounds.offer(sound)) { + Sound polled = sounds.poll(); + assert polled != null; + polled.flush(); + if (!sounds.offer(sound)) { + throw new RuntimeException(); + } + } + } + + public static void clearSounds() { + Sound polled = sounds.poll(); + while(polled != null) { + polled.stop(); + polled = sounds.poll(); + } + } +} diff --git a/src/main/java/ru/windcorp/progressia/client/audio/backend/ALTest.java b/src/main/java/ru/windcorp/progressia/client/audio/backend/ALTest.java index 7948b22..f2847bc 100644 --- a/src/main/java/ru/windcorp/progressia/client/audio/backend/ALTest.java +++ b/src/main/java/ru/windcorp/progressia/client/audio/backend/ALTest.java @@ -37,7 +37,8 @@ public class ALTest { alListenerfv(AL_VELOCITY, listenerVel); alListenerfv(AL_ORIENTATION, listenerOri); Sound music = AudioReader.readAsMono("assets/sounds/sample_mono.ogg").genSound(); - music.forcePlay(); + + music.playOnce(); checkALError(); } @@ -58,7 +59,7 @@ public class ALTest { } public static void update() { - Listener.update(); + Listener.getInstance().update(); } public static void checkALError() {