Added sound positioning

This commit is contained in:
Евгений Смирнов 2020-09-20 17:40:51 +03:00
parent 437566283c
commit 9d1862842d
6 changed files with 60 additions and 24 deletions

View File

@ -1,5 +1,6 @@
package ru.windcorp.progressia.client;
import ru.windcorp.progressia.client.audio.backend.ALTest;
import ru.windcorp.progressia.client.comms.DefaultClientCommsListener;
import ru.windcorp.progressia.client.comms.ServerCommsChannel;
import ru.windcorp.progressia.client.graphics.world.Camera;

View File

@ -23,8 +23,7 @@ import ru.windcorp.progressia.client.audio.backend.ALTest;
public class ProgressiaClientMain {
public static void main(String[] args) {
ALTest al = new ALTest();
al.execute();
ALTest.execute();
ProgressiaLauncher.launch(args, new ClientProxy());
}

View File

@ -1,6 +1,14 @@
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.*;
@ -8,16 +16,33 @@ import static org.lwjgl.openal.AL10.*;
//TODO add getters and setters
public class Listener {
private static FloatBuffer position =
(FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip();
private static FloatBuffer velocity =
(FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip();
private static FloatBuffer orientation =
(FloatBuffer) BufferUtils.createFloatBuffer(6).put(new float[]{0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}).flip();
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;
public static void update() {
alListenerfv(AL_POSITION, position);
alListenerfv(AL_VELOCITY, velocity);
alListenerfv(AL_ORIENTATION, orientation);
if (client == null) {
client = ClientState.getInstance();
} else 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.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});
}
}
}

View File

@ -13,10 +13,10 @@ public class Sound {
//Characteristics
private FloatBuffer position =
(FloatBuffer) BufferUtils.createFloatBuffer(3)
.put(new float[]{0.0f, 0.0f, 0.0f}).flip();
.put(new float[]{0.0f, 0.0f, 0.0f}).rewind();
private FloatBuffer velocity =
(FloatBuffer) BufferUtils.createFloatBuffer(3)
.put(new float[]{0.0f, 0.0f, 0.0f}).flip();
.put(new float[]{0.0f, 0.0f, 0.0f}).rewind();
private float pitch = 1.0f;
private float gain = 1.0f;
@ -91,6 +91,7 @@ public class Sound {
public void setSource(int source) {
this.source = source;
alSourcei(this.source, AL_BUFFER, audio);
//alSourcei(this.source, AL_SOURCE_RELATIVE, AL_TRUE);
}
public int getSource() {

View File

@ -13,15 +13,15 @@ import static org.lwjgl.openal.ALC10.*;
public class ALTest {
// Position of the listener
private FloatBuffer listenerPos = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip();
private static FloatBuffer listenerPos = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip();
// Velocity of the listener
private FloatBuffer listenerVel = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip();
private static FloatBuffer listenerVel = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip();
// Orientation of the listener. (first 3 elements are "at", second 3 are "up")
private FloatBuffer listenerOri =
private static FloatBuffer listenerOri =
(FloatBuffer) BufferUtils.createFloatBuffer(6).put(new float[]{0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f}).flip();
private void initializeAL() {
static private void initializeAL() {
String defaultDeviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER);
long device = alcOpenDevice(defaultDeviceName);
@ -30,30 +30,38 @@ public class ALTest {
alcMakeContextCurrent(context);
ALCCapabilities deviceCaps = ALC.createCapabilities(device);
ALCapabilities alcaps = AL.createCapabilities(deviceCaps);
}
void loadALData() {
static void loadALData() {
alListenerfv(AL_POSITION, listenerPos);
alListenerfv(AL_VELOCITY, listenerVel);
alListenerfv(AL_ORIENTATION, listenerOri);
Sound music = AudioReader.readAsMono("assets/sounds/sample_mono.ogg").genSound();
music.forcePlay();
checkALError();
}
void killALData() {
static void killALData() {
//alDeleteSources(source);
//alDeleteBuffers(buffer);
}
public void execute() {
public static void execute() {
initializeAL();
checkALError();
Listener.update();
checkALError();
checkALError();
loadALData();
checkALError();
checkALError();
}
public void checkALError() {
public static void update() {
Listener.update();
}
public static void checkALError() {
int i = alGetError();
if(alGetError() != AL_NO_ERROR) {
throw new RuntimeException(i+"");

View File

@ -25,6 +25,7 @@ import java.util.function.Consumer;
import glm.Glm;
import glm.mat._4.Mat4;
import glm.vec._3.Vec3;
import ru.windcorp.progressia.client.audio.backend.ALTest;
import ru.windcorp.progressia.client.graphics.backend.GraphicsInterface;
import ru.windcorp.progressia.client.graphics.world.Camera.Anchor.Mode;
import ru.windcorp.progressia.common.util.Vectors;
@ -81,6 +82,7 @@ public class Camera {
public Camera() {}
public void apply(WorldRenderHelper helper) {
ALTest.update();
applyPerspective(helper);
rotateCoordinateSystem(helper);