Cleaned up and formatted code

This commit is contained in:
OLEGSHA 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) {
IntBuffer channelBuffer = BufferUtils.createIntBuffer(1); private static SoundType readAsSpecified(String audioName, int format) {
IntBuffer rateBuffer = BufferUtils.createIntBuffer(1); IntBuffer channelBuffer = BufferUtils.createIntBuffer(1);
Resource res = ResourceManager.getResource(AudioFile); IntBuffer rateBuffer = BufferUtils.createIntBuffer(1);
ShortBuffer rawMonoAudio = decodeVorbis(res, channelBuffer, rateBuffer);
Resource res = ResourceManager.getResource(audioName);
return new SoundType(rawMonoAudio, AL_FORMAT_MONO16, rateBuffer.get(0));
} ShortBuffer rawAudio = decodeVorbis(res, channelBuffer, rateBuffer);
public static SoundType readAsStereo(String AudioFile) { return new SoundType(rawAudio, format, rateBuffer.get(0));
IntBuffer channelsBuffer = BufferUtils.createIntBuffer(2); }
IntBuffer rateBuffer = BufferUtils.createIntBuffer(1);
Resource res = ResourceManager.getResource(AudioFile); public static SoundType readAsMono(String audioName) {
ShortBuffer rawStereoAudio = decodeVorbis(res, channelsBuffer, rateBuffer); return readAsSpecified(audioName, AL_FORMAT_MONO16);
}
return new SoundType(rawStereoAudio, AL_FORMAT_STEREO16, rateBuffer.get(0));
} public static SoundType readAsStereo(String audioName) {
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);
} private static ShortBuffer decodeVorbis(
Resource dataToDecode,
IntBuffer channelsBuffer,
IntBuffer rateBuffer
) {
return stb_vorbis_decode_memory(
dataToDecode.readAsBytes(),
channelsBuffer,
rateBuffer
);
}
} }

View File

@ -10,59 +10,67 @@ import static org.lwjgl.openal.AL10.*;
//TODO add getters and setters //TODO add getters and setters
public class Listener { public class Listener {
private static final Listener instance = new Listener(); private static final Listener INSTANCE = new Listener();
private Listener() {}
public static Listener getInstance() { private Listener() {}
return instance;
} public static Listener getInstance() {
return INSTANCE;
//Params }
private final Vec3 position = new Vec3();
private final Vec3 velocity = new Vec3(); // Params
private final Vec3 oriAt = new Vec3(); private final Vec3 position = new Vec3();
private final Vec3 oriUp = new Vec3(); private final Vec3 velocity = new Vec3();
private final Vec3 oriAt = new Vec3();
private boolean isClientConnected = false; private final Vec3 oriUp = new Vec3();
private Camera.Anchor anchor;
private boolean isClientConnected = false;
public void update() { private Camera.Anchor anchor;
Client client = ClientState.getInstance();
if (client == null) { public void update() {
if (isClientConnected) { Client client = ClientState.getInstance();
isClientConnected = false; if (client == null) {
resetParams(); if (isClientConnected) {
applyParams(); isClientConnected = false;
} resetParams();
} else { applyParams();
isClientConnected = true; }
if (anchor == null) { } else {
anchor = client.getCamera().getAnchor(); isClientConnected = true;
} else { if (anchor == null) {
anchor.getCameraPosition(position); anchor = client.getCamera().getAnchor();
float pitch = anchor.getCameraPitch(); } else {
float yaw = anchor.getCameraYaw(); anchor.getCameraPosition(position);
oriAt.set((float) (Math.cos(pitch) * Math.cos(yaw)), float pitch = anchor.getCameraPitch();
(float) (Math.cos(pitch) * Math.sin(yaw)), float yaw = anchor.getCameraYaw();
(float) Math.sin(pitch)); oriAt.set(
oriUp.set((float) (Math.cos(pitch + Math.PI / 2) * Math.cos(yaw)), (float) (Math.cos(pitch) * Math.cos(yaw)),
(float) (Math.cos(pitch + Math.PI / 2) * Math.sin(yaw)), (float) (Math.cos(pitch) * Math.sin(yaw)),
(float) Math.sin(pitch + Math.PI / 2)); (float) Math.sin(pitch)
applyParams(); );
} 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)
private void resetParams() { );
position.set(0f, 0f, 0f); applyParams();
velocity.set(0f, 0f, 0f); }
oriAt.set(0f, 0f, 0f); }
oriUp.set(0f, 0f, 0f); }
}
private void resetParams() {
private void applyParams() { position.set(0);
alListener3f(AL_POSITION, position.x, position.y, position.z); velocity.set(0);
alListener3f(AL_VELOCITY, velocity.x, velocity.y, velocity.z); oriAt.set(0);
alListenerfv(AL_ORIENTATION, new float[]{oriAt.x, oriAt.y, oriAt.z, oriUp.x, oriUp.y, oriUp.z}); oriUp.set(0);
} }
}
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

@ -7,122 +7,136 @@ import java.nio.FloatBuffer;
import static org.lwjgl.openal.AL11.*; import static org.lwjgl.openal.AL11.*;
public class Sound { public class Sound {
//Buffers
private int audio; // Buffers
private int source; private int audio;
//Characteristics private int source;
private FloatBuffer position =
(FloatBuffer) BufferUtils.createFloatBuffer(3) // Characteristics
.put(new float[]{0.0f, 0.0f, 0.0f}).rewind(); private FloatBuffer position = (FloatBuffer) BufferUtils.createFloatBuffer(
private FloatBuffer velocity = 3
(FloatBuffer) BufferUtils.createFloatBuffer(3) ).put(new float[] {
.put(new float[]{0.0f, 0.0f, 0.0f}).rewind(); 0.0f, 0.0f, 0.0f
private float pitch = 1.0f; }).rewind();
private float gain = 1.0f;
private FloatBuffer velocity = (FloatBuffer) BufferUtils.createFloatBuffer(
public Sound() { 3
} ).put(new float[] {
0.0f, 0.0f, 0.0f
public Sound(int audio, int source) { }).rewind();
setAudio(audio);
setSource(source); private float pitch = 1.0f;
} private float gain = 1.0f;
public Sound(int audio, FloatBuffer position, FloatBuffer velocity, float pitch, float gain) public Sound() {}
{
setAudio(audio); public Sound(int audio, int source) {
setPosition(position); setAudio(audio);
setVelocity(velocity); setSource(source);
setPitch(pitch); }
setGain(gain);
} public Sound(
int audio,
public Sound(FloatBuffer position, FloatBuffer velocity, float pitch, float gain) { FloatBuffer position,
setPosition(position); FloatBuffer velocity,
setVelocity(velocity); float pitch,
setPitch(pitch); float gain
setGain(gain); ) {
} setAudio(audio);
setPosition(position);
public void playOnce() { setVelocity(velocity);
alSourcePlay(source); setPitch(pitch);
} setGain(gain);
}
public void playLoop() {
alSourcei(source, AL_LOOPING, AL_TRUE); public Sound(
playOnce(); FloatBuffer position,
alSourcei(source, AL_LOOPING, AL_FALSE); FloatBuffer velocity,
} float pitch,
float gain
public void stop() { ) {
alSourceStop(source); setPosition(position);
} setVelocity(velocity);
setPitch(pitch);
public void pause() { setGain(gain);
alSourcePause(source); }
}
public void playOnce() {
public boolean isPlaying() { alSourcePlay(source);
final int state = alGetSourcei(source, AL_SOURCE_STATE); }
return state == AL_PLAYING;
} public void playLoop() {
alSourcei(source, AL_LOOPING, AL_TRUE);
public int getAudio() { playOnce();
return audio; alSourcei(source, AL_LOOPING, AL_FALSE);
} }
public void setAudio(int audio) { public void stop() {
this.audio = audio; alSourceStop(source);
} }
public void setSource(int source) { public void pause() {
this.source = source; alSourcePause(source);
alSourcei(this.source, AL_BUFFER, audio); }
}
public boolean isPlaying() {
public int getSource() { final int state = alGetSourcei(source, AL_SOURCE_STATE);
return source; return state == AL_PLAYING;
} }
public int getAudio() {
//OTHER return audio;
}
public void setPosition(FloatBuffer position) {
this.position = position; public void setAudio(int audio) {
alSourcefv(source, AL_POSITION, position); this.audio = audio;
} }
public FloatBuffer getPosition() { public void setSource(int source) {
return position; this.source = source;
} alSourcei(this.source, AL_BUFFER, audio);
}
public void setVelocity(FloatBuffer velocity) { public int getSource() {
alSourcefv(source, AL_VELOCITY, velocity); return source;
this.velocity = velocity; }
}
// OTHER
public FloatBuffer getVelocity() {
return velocity; public void setPosition(FloatBuffer position) {
} this.position = position;
alSourcefv(source, AL_POSITION, position);
}
public void setPitch(float pitch) {
alSourcef(source, AL_PITCH, pitch); public FloatBuffer getPosition() {
this.pitch = pitch; return position;
} }
public float getPitch() { public void setVelocity(FloatBuffer velocity) {
return pitch; alSourcefv(source, AL_VELOCITY, velocity);
} this.velocity = velocity;
}
public void setGain(float gain) { public FloatBuffer getVelocity() {
alSourcef(source, AL_GAIN, gain); return velocity;
this.gain = gain; }
}
public void setPitch(float pitch) {
public float getGain() { alSourcef(source, AL_PITCH, pitch);
return gain; this.pitch = pitch;
} }
}
public float getPitch() {
return pitch;
}
public void setGain(float gain) {
alSourcef(source, AL_GAIN, gain);
this.gain = gain;
}
public float getGain() {
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,73 +11,97 @@ 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 int lastSourceIndex = -1; private static final int SOURCES_NUM = 64;
private static final int[] SOURCES = new int[SOURCES_NUM]; private static int lastSourceIndex = -1;
private static final ArrayBlockingQueue<Sound> SOUNDS = new ArrayBlockingQueue<>(SOURCES_NUM); private static final int[] SOURCES = new int[SOURCES_NUM];
private static long DEVICE; private static final ArrayBlockingQueue<Sound> SOUNDS =
new ArrayBlockingQueue<>(SOURCES_NUM);
public static void initAL() {
String defaultDeviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER); private static long device;
DEVICE = alcOpenDevice(defaultDeviceName);
int[] attributes = {0}; private static ALCCapabilities deviceCapabilities;
long context = alcCreateContext(DEVICE, attributes); private static ALCapabilities alCapabilities;
alcMakeContextCurrent(context);
ALCCapabilities deviceCaps = ALC.createCapabilities(DEVICE); public static void initAL() {
ALCapabilities alcaps = AL.createCapabilities(deviceCaps); String defaultDeviceName = alcGetString(
checkALError(); 0,
alGenSources(SOURCES); ALC_DEFAULT_DEVICE_SPECIFIER
} );
public static void update() { device = alcOpenDevice(defaultDeviceName);
//Position of the listener
Listener.getInstance().update(); int[] attributes = new int[1];
} long context = alcCreateContext(device, attributes);
alcMakeContextCurrent(context);
private static void addSound(Sound sound) {
if (!SOUNDS.offer(sound)) { deviceCapabilities = ALC.createCapabilities(device);
Sound polled = SOUNDS.poll(); alCapabilities = AL.createCapabilities(deviceCapabilities);
assert polled != null;
polled.stop(); checkALError();
if (!SOUNDS.offer(sound)) {
throw new RuntimeException(); alGenSources(SOURCES);
} }
}
} public static void update() {
// Position of the listener
private static int getNextSource() { Listener.getInstance().update();
if (++lastSourceIndex > SOURCES_NUM) lastSourceIndex = 0; }
return SOURCES[lastSourceIndex];
} private static void addSound(Sound sound) {
if (!SOUNDS.offer(sound)) {
public static Sound createSound(SoundType soundType) { Sound polled = SOUNDS.poll();
Sound sound = soundType.genSoundSource(getNextSource()); assert polled != null;
addSound(sound); polled.stop();
return sound; if (!SOUNDS.offer(sound)) {
} throw new RuntimeException();
}
public static void clearSounds() { }
Sound polled = SOUNDS.poll(); }
while (polled != null) {
polled.stop(); private static int getNextSource() {
polled = SOUNDS.poll(); if (++lastSourceIndex > SOURCES_NUM)
} lastSourceIndex = 0;
} return SOURCES[lastSourceIndex];
}
public static void checkALError() {
int errorCode = alGetError(); public static Sound createSound(SoundType soundType) {
if(alGetError() != AL_NO_ERROR) { Sound sound = soundType.genSoundSource(getNextSource());
throw new RuntimeException(String.valueOf(errorCode)); addSound(sound);
} return sound;
} }
public static void closeAL() { public static void clearSounds() {
clearSounds(); Sound polled = SOUNDS.poll();
alDeleteSources(SOURCES); while (polled != null) {
for(Sound s : SOUNDS) { polled.stop();
alDeleteBuffers(s.getAudio()); polled = SOUNDS.poll();
} }
alcCloseDevice(DEVICE); }
}
public static void checkALError() {
int errorCode = alGetError();
if (alGetError() != AL_NO_ERROR) {
throw new RuntimeException(String.valueOf(errorCode));
}
}
public static void closeAL() {
clearSounds();
alDeleteSources(SOURCES);
for (Sound s : SOUNDS) {
alDeleteBuffers(s.getAudio());
}
alcCloseDevice(device);
}
public static ALCapabilities getALCapabilities() {
return alCapabilities;
}
public static ALCCapabilities getDeviceCapabilities() {
return deviceCapabilities;
}
} }

View File

@ -4,42 +4,48 @@ 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 int sampleRate; private ShortBuffer rawAudio;
private int format; private int sampleRate;
private int format;
public SoundType(ShortBuffer rawAudio, int format, int sampleRate) {
this.rawAudio = rawAudio; public SoundType(ShortBuffer rawAudio, int format, int sampleRate) {
this.sampleRate = sampleRate; this.rawAudio = rawAudio;
this.format = format; this.sampleRate = sampleRate;
} this.format = format;
}
public static int genEmptyAudio() {
return alGenBuffers(); public static int genEmptyAudio() {
} return alGenBuffers();
}
public static int genEmptySource() {
return alGenSources(); public static int genEmptySource() {
} return alGenSources();
}
public int genAudio() {
int audio = alGenBuffers(); public int genAudio() {
alBufferData(audio, format, rawAudio, sampleRate); int audio = alGenBuffers();
return audio; alBufferData(audio, format, rawAudio, sampleRate);
} return audio;
}
public Sound genSoundSource() {
return new Sound(genAudio(), alGenSources()); public Sound genSoundSource() {
} return new Sound(genAudio(), alGenSources());
}
public Sound genSoundSource(int source) {
if(!alIsSource(source)) throw new RuntimeException(); public Sound genSoundSource(int source) {
return new Sound(genAudio(), source); if (!alIsSource(source))
} throw new RuntimeException();
public Sound genSoundSource(int source, int audio) { return new Sound(genAudio(), source);
if(!alIsBuffer(audio) || !alIsSource(source)) throw new RuntimeException(); }
alBufferData(audio, format, rawAudio, sampleRate);
return new Sound(audio, alGenSources()); public Sound genSoundSource(int source, int audio) {
} if (!alIsBuffer(audio) || !alIsSource(source))
throw new RuntimeException();
alBufferData(audio, format, rawAudio, sampleRate);
return new Sound(audio, alGenSources());
}
} }