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;
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) {
if (isClientConnected) {
isClientConnected = false;
resetParams();
applyParams();
}
} else {
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)),
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) );
(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});
}
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);
}
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;
}

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_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() {