Fixed sound positioning while reconnecting

This commit is contained in:
Евгений Смирнов 2020-09-23 20:17:02 +03:00
parent 9d1862842d
commit aa640e23d6
4 changed files with 92 additions and 45 deletions

View File

@ -1,48 +1,68 @@
package ru.windcorp.progressia.client.audio; package ru.windcorp.progressia.client.audio;
import com.sun.jna.platform.unix.X11;
import glm.vec._3.Vec3; import glm.vec._3.Vec3;
import jglm.Vec;
import org.lwjgl.BufferUtils;
import ru.windcorp.progressia.client.Client; import ru.windcorp.progressia.client.Client;
import ru.windcorp.progressia.client.ClientState; import ru.windcorp.progressia.client.ClientState;
import ru.windcorp.progressia.client.graphics.world.Camera; 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.*; import static org.lwjgl.openal.AL10.*;
//TODO add getters and setters //TODO add getters and setters
public class Listener { 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 final Listener instance = new Listener();
private static Camera.Anchor anchor; 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) { if (client == null) {
client = ClientState.getInstance(); if (isClientConnected) {
} else if (anchor == null) { isClientConnected = false;
anchor = client.getCamera().getAnchor(); resetParams();
applyParams();
}
} else { } else {
anchor.getCameraPosition(position); isClientConnected = true;
float pitch = anchor.getCameraPitch(); if (anchor == null) {
float yaw = anchor.getCameraYaw(); anchor = client.getCamera().getAnchor();
oriAt.set( (float) (Math.cos(pitch) * Math.cos(yaw)), } 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.cos(pitch) * Math.sin(yaw)),
(float) Math.sin(pitch) ); (float) Math.sin(pitch));
oriUp.set( (float) (Math.cos(pitch + Math.PI / 2) * Math.cos(yaw)), oriUp.set((float) (Math.cos(pitch + Math.PI / 2) * Math.cos(yaw)),
(float) (Math.cos(pitch + Math.PI / 2) * Math.sin(yaw)), (float) (Math.cos(pitch + Math.PI / 2) * Math.sin(yaw)),
(float) Math.sin(pitch + Math.PI / 2) ); (float) Math.sin(pitch + Math.PI / 2));
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});
} }
} }
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});
}
} }

View File

@ -44,29 +44,25 @@ public class Sound {
setGain(gain); setGain(gain);
} }
public void forcePlay() { public void playOnce() {
alSourcePlay(source); alSourcePlay(source);
} }
public boolean playOnce() { public void playLoop() {
if(isEmpty()) {
alSourcePlay(source);
return true;
}
else return false;
}
public boolean playLoop() {
alSourcei(source, AL_LOOPING, AL_TRUE); alSourcei(source, AL_LOOPING, AL_TRUE);
boolean isPlaying = playOnce(); playOnce();
alSourcei(source, AL_LOOPING, AL_FALSE); alSourcei(source, AL_LOOPING, AL_FALSE);
return isPlaying;
} }
public void stop() { public void stop() {
alSourceStop(source); alSourceStop(source);
} }
public void flush() {
alDeleteBuffers(audio);
alDeleteBuffers(source);
}
public void pause() { public void pause() {
alSourcePause(source); alSourcePause(source);
} }
@ -76,10 +72,6 @@ public class Sound {
return state == AL_TRUE; return state == AL_TRUE;
} }
public boolean isEmpty() {
return source == 0;
}
public int getAudio() { public int getAudio() {
return audio; return audio;
} }

View File

@ -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<Sound> 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();
}
}
}

View File

@ -37,7 +37,8 @@ public class ALTest {
alListenerfv(AL_VELOCITY, listenerVel); alListenerfv(AL_VELOCITY, listenerVel);
alListenerfv(AL_ORIENTATION, listenerOri); alListenerfv(AL_ORIENTATION, listenerOri);
Sound music = AudioReader.readAsMono("assets/sounds/sample_mono.ogg").genSound(); Sound music = AudioReader.readAsMono("assets/sounds/sample_mono.ogg").genSound();
music.forcePlay();
music.playOnce();
checkALError(); checkALError();
} }
@ -58,7 +59,7 @@ public class ALTest {
} }
public static void update() { public static void update() {
Listener.update(); Listener.getInstance().update();
} }
public static void checkALError() { public static void checkALError() {