Cleaned up and formatted code

This commit is contained in:
2020-10-06 11:42:36 +03:00
parent 12dc816760
commit 503963f992
6 changed files with 369 additions and 309 deletions

View File

@@ -1,6 +1,5 @@
package ru.windcorp.progressia.client; 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.DefaultClientCommsListener;
import ru.windcorp.progressia.client.comms.ServerCommsChannel; import ru.windcorp.progressia.client.comms.ServerCommsChannel;
import ru.windcorp.progressia.client.graphics.world.Camera; import ru.windcorp.progressia.client.graphics.world.Camera;

View File

@@ -10,29 +10,39 @@ import static org.lwjgl.stb.STBVorbis.*;
import static org.lwjgl.openal.AL10.*; import static org.lwjgl.openal.AL10.*;
public class AudioReader { public class AudioReader {
private AudioReader() {}; private AudioReader() {};
//TODO fix converting from mono-stereo // TODO fix converting from mono-stereo
public static SoundType readAsMono(String AudioFile) { private static SoundType readAsSpecified(String audioName, int format) {
IntBuffer channelBuffer = BufferUtils.createIntBuffer(1); IntBuffer channelBuffer = BufferUtils.createIntBuffer(1);
IntBuffer rateBuffer = BufferUtils.createIntBuffer(1); IntBuffer rateBuffer = BufferUtils.createIntBuffer(1);
Resource res = ResourceManager.getResource(AudioFile);
ShortBuffer rawMonoAudio = decodeVorbis(res, channelBuffer, rateBuffer);
return new SoundType(rawMonoAudio, AL_FORMAT_MONO16, rateBuffer.get(0)); Resource res = ResourceManager.getResource(audioName);
ShortBuffer rawAudio = decodeVorbis(res, channelBuffer, rateBuffer);
return new SoundType(rawAudio, format, rateBuffer.get(0));
} }
public static SoundType readAsStereo(String AudioFile) { public static SoundType readAsMono(String audioName) {
IntBuffer channelsBuffer = BufferUtils.createIntBuffer(2); return readAsSpecified(audioName, AL_FORMAT_MONO16);
IntBuffer rateBuffer = BufferUtils.createIntBuffer(1);
Resource res = ResourceManager.getResource(AudioFile);
ShortBuffer rawStereoAudio = decodeVorbis(res, channelsBuffer, rateBuffer);
return new SoundType(rawStereoAudio, AL_FORMAT_STEREO16, rateBuffer.get(0));
} }
private static ShortBuffer decodeVorbis(Resource dataToDecode, IntBuffer channelsBuffer, IntBuffer rateBuffer) { public static SoundType readAsStereo(String audioName) {
return stb_vorbis_decode_memory(dataToDecode.readAsBytes(), channelsBuffer, rateBuffer); return readAsSpecified(audioName, AL_FORMAT_STEREO16);
}
private static ShortBuffer decodeVorbis(
Resource dataToDecode,
IntBuffer channelsBuffer,
IntBuffer rateBuffer
) {
return stb_vorbis_decode_memory(
dataToDecode.readAsBytes(),
channelsBuffer,
rateBuffer
);
} }
} }

View File

@@ -11,13 +11,15 @@ import static org.lwjgl.openal.AL10.*;
public class Listener { public class Listener {
private static final Listener instance = new Listener(); private static final Listener INSTANCE = new Listener();
private Listener() {} private Listener() {}
public static Listener getInstance() { public static Listener getInstance() {
return instance; return INSTANCE;
} }
//Params // Params
private final Vec3 position = new Vec3(); private final Vec3 position = new Vec3();
private final Vec3 velocity = new Vec3(); private final Vec3 velocity = new Vec3();
private final Vec3 oriAt = new Vec3(); private final Vec3 oriAt = new Vec3();
@@ -42,27 +44,33 @@ public class Listener {
anchor.getCameraPosition(position); anchor.getCameraPosition(position);
float pitch = anchor.getCameraPitch(); float pitch = anchor.getCameraPitch();
float yaw = anchor.getCameraYaw(); 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.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(); applyParams();
} }
} }
} }
private void resetParams() { private void resetParams() {
position.set(0f, 0f, 0f); position.set(0);
velocity.set(0f, 0f, 0f); velocity.set(0);
oriAt.set(0f, 0f, 0f); oriAt.set(0);
oriUp.set(0f, 0f, 0f); oriUp.set(0);
} }
private void applyParams() { private void applyParams() {
alListener3f(AL_POSITION, position.x, position.y, position.z); alListener3f(AL_POSITION, position.x, position.y, position.z);
alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.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

@@ -7,29 +7,41 @@ import java.nio.FloatBuffer;
import static org.lwjgl.openal.AL11.*; import static org.lwjgl.openal.AL11.*;
public class Sound { public class Sound {
//Buffers
// Buffers
private int audio; private int audio;
private int source; private int source;
//Characteristics
private FloatBuffer position = // Characteristics
(FloatBuffer) BufferUtils.createFloatBuffer(3) private FloatBuffer position = (FloatBuffer) BufferUtils.createFloatBuffer(
.put(new float[]{0.0f, 0.0f, 0.0f}).rewind(); 3
private FloatBuffer velocity = ).put(new float[] {
(FloatBuffer) BufferUtils.createFloatBuffer(3) 0.0f, 0.0f, 0.0f
.put(new float[]{0.0f, 0.0f, 0.0f}).rewind(); }).rewind();
private FloatBuffer velocity = (FloatBuffer) BufferUtils.createFloatBuffer(
3
).put(new float[] {
0.0f, 0.0f, 0.0f
}).rewind();
private float pitch = 1.0f; private float pitch = 1.0f;
private float gain = 1.0f; private float gain = 1.0f;
public Sound() { public Sound() {}
}
public Sound(int audio, int source) { public Sound(int audio, int source) {
setAudio(audio); setAudio(audio);
setSource(source); setSource(source);
} }
public Sound(int audio, FloatBuffer position, FloatBuffer velocity, float pitch, float gain) public Sound(
{ int audio,
FloatBuffer position,
FloatBuffer velocity,
float pitch,
float gain
) {
setAudio(audio); setAudio(audio);
setPosition(position); setPosition(position);
setVelocity(velocity); setVelocity(velocity);
@@ -37,7 +49,12 @@ public class Sound {
setGain(gain); setGain(gain);
} }
public Sound(FloatBuffer position, FloatBuffer velocity, float pitch, float gain) { public Sound(
FloatBuffer position,
FloatBuffer velocity,
float pitch,
float gain
) {
setPosition(position); setPosition(position);
setVelocity(velocity); setVelocity(velocity);
setPitch(pitch); setPitch(pitch);
@@ -84,8 +101,7 @@ public class Sound {
return source; return source;
} }
// OTHER
//OTHER
public void setPosition(FloatBuffer position) { public void setPosition(FloatBuffer position) {
this.position = position; this.position = position;
@@ -96,7 +112,6 @@ public class Sound {
return position; return position;
} }
public void setVelocity(FloatBuffer velocity) { public void setVelocity(FloatBuffer velocity) {
alSourcefv(source, AL_VELOCITY, velocity); alSourcefv(source, AL_VELOCITY, velocity);
this.velocity = velocity; this.velocity = velocity;
@@ -106,7 +121,6 @@ public class Sound {
return velocity; return velocity;
} }
public void setPitch(float pitch) { public void setPitch(float pitch) {
alSourcef(source, AL_PITCH, pitch); alSourcef(source, AL_PITCH, pitch);
this.pitch = pitch; this.pitch = pitch;
@@ -116,7 +130,6 @@ public class Sound {
return pitch; return pitch;
} }
public void setGain(float gain) { public void setGain(float gain) {
alSourcef(source, AL_GAIN, gain); alSourcef(source, AL_GAIN, gain);
this.gain = gain; this.gain = gain;
@@ -125,4 +138,5 @@ public class Sound {
public float getGain() { public float getGain() {
return gain; return gain;
} }
} }

View File

@@ -1,6 +1,5 @@
package ru.windcorp.progressia.client.audio; package ru.windcorp.progressia.client.audio;
import org.lwjgl.BufferUtils;
import org.lwjgl.openal.AL; import org.lwjgl.openal.AL;
import org.lwjgl.openal.ALC; import org.lwjgl.openal.ALC;
import org.lwjgl.openal.ALCCapabilities; import org.lwjgl.openal.ALCCapabilities;
@@ -12,27 +11,41 @@ import static org.lwjgl.openal.ALC10.*;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
public class SoundManager { public class SoundManager {
private static final int SOURCES_NUM = 64; private static final int SOURCES_NUM = 64;
private static int lastSourceIndex = -1; private static int lastSourceIndex = -1;
private static final int[] SOURCES = new int[SOURCES_NUM]; private static final int[] SOURCES = new int[SOURCES_NUM];
private static final ArrayBlockingQueue<Sound> SOUNDS = new ArrayBlockingQueue<>(SOURCES_NUM);
private static long DEVICE; private static final ArrayBlockingQueue<Sound> SOUNDS =
new ArrayBlockingQueue<>(SOURCES_NUM);
private static long device;
private static ALCCapabilities deviceCapabilities;
private static ALCapabilities alCapabilities;
public static void initAL() { public static void initAL() {
String defaultDeviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER); String defaultDeviceName = alcGetString(
DEVICE = alcOpenDevice(defaultDeviceName); 0,
int[] attributes = {0}; ALC_DEFAULT_DEVICE_SPECIFIER
long context = alcCreateContext(DEVICE, attributes); );
device = alcOpenDevice(defaultDeviceName);
int[] attributes = new int[1];
long context = alcCreateContext(device, attributes);
alcMakeContextCurrent(context); alcMakeContextCurrent(context);
ALCCapabilities deviceCaps = ALC.createCapabilities(DEVICE);
ALCapabilities alcaps = AL.createCapabilities(deviceCaps); deviceCapabilities = ALC.createCapabilities(device);
alCapabilities = AL.createCapabilities(deviceCapabilities);
checkALError(); checkALError();
alGenSources(SOURCES); alGenSources(SOURCES);
} }
public static void update() { public static void update() {
//Position of the listener // Position of the listener
Listener.getInstance().update(); Listener.getInstance().update();
} }
@@ -48,7 +61,8 @@ public class SoundManager {
} }
private static int getNextSource() { private static int getNextSource() {
if (++lastSourceIndex > SOURCES_NUM) lastSourceIndex = 0; if (++lastSourceIndex > SOURCES_NUM)
lastSourceIndex = 0;
return SOURCES[lastSourceIndex]; return SOURCES[lastSourceIndex];
} }
@@ -68,7 +82,7 @@ public class SoundManager {
public static void checkALError() { public static void checkALError() {
int errorCode = alGetError(); int errorCode = alGetError();
if(alGetError() != AL_NO_ERROR) { if (alGetError() != AL_NO_ERROR) {
throw new RuntimeException(String.valueOf(errorCode)); throw new RuntimeException(String.valueOf(errorCode));
} }
} }
@@ -76,9 +90,18 @@ public class SoundManager {
public static void closeAL() { public static void closeAL() {
clearSounds(); clearSounds();
alDeleteSources(SOURCES); alDeleteSources(SOURCES);
for(Sound s : SOUNDS) { for (Sound s : SOUNDS) {
alDeleteBuffers(s.getAudio()); alDeleteBuffers(s.getAudio());
} }
alcCloseDevice(DEVICE); alcCloseDevice(device);
} }
public static ALCapabilities getALCapabilities() {
return alCapabilities;
}
public static ALCCapabilities getDeviceCapabilities() {
return deviceCapabilities;
}
} }

View File

@@ -4,6 +4,7 @@ import java.nio.ShortBuffer;
import static org.lwjgl.openal.AL11.*; import static org.lwjgl.openal.AL11.*;
public class SoundType { public class SoundType {
private ShortBuffer rawAudio; private ShortBuffer rawAudio;
private int sampleRate; private int sampleRate;
private int format; private int format;
@@ -33,13 +34,18 @@ public class SoundType {
} }
public Sound genSoundSource(int source) { public Sound genSoundSource(int source) {
if(!alIsSource(source)) throw new RuntimeException(); if (!alIsSource(source))
throw new RuntimeException();
return new Sound(genAudio(), source); return new Sound(genAudio(), source);
} }
public Sound genSoundSource(int source, int audio) { public Sound genSoundSource(int source, int audio) {
if(!alIsBuffer(audio) || !alIsSource(source)) throw new RuntimeException(); if (!alIsBuffer(audio) || !alIsSource(source))
throw new RuntimeException();
alBufferData(audio, format, rawAudio, sampleRate); alBufferData(audio, format, rawAudio, sampleRate);
return new Sound(audio, alGenSources()); return new Sound(audio, alGenSources());
} }
} }