From 503963f99263da4a430253ee5599d43771f18fe8 Mon Sep 17 00:00:00 2001 From: OLEGSHA Date: Tue, 6 Oct 2020 11:42:36 +0300 Subject: [PATCH] Cleaned up and formatted code --- .../ru/windcorp/progressia/client/Client.java | 1 - .../progressia/client/audio/AudioReader.java | 60 +++-- .../progressia/client/audio/Listener.java | 120 +++++---- .../progressia/client/audio/Sound.java | 252 +++++++++--------- .../progressia/client/audio/SoundManager.java | 163 ++++++----- .../progressia/client/audio/SoundType.java | 82 +++--- 6 files changed, 369 insertions(+), 309 deletions(-) diff --git a/src/main/java/ru/windcorp/progressia/client/Client.java b/src/main/java/ru/windcorp/progressia/client/Client.java index a74e0b6..de6fa54 100644 --- a/src/main/java/ru/windcorp/progressia/client/Client.java +++ b/src/main/java/ru/windcorp/progressia/client/Client.java @@ -1,6 +1,5 @@ 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; diff --git a/src/main/java/ru/windcorp/progressia/client/audio/AudioReader.java b/src/main/java/ru/windcorp/progressia/client/audio/AudioReader.java index 518e6c6..ed2a0ee 100644 --- a/src/main/java/ru/windcorp/progressia/client/audio/AudioReader.java +++ b/src/main/java/ru/windcorp/progressia/client/audio/AudioReader.java @@ -10,29 +10,39 @@ import static org.lwjgl.stb.STBVorbis.*; import static org.lwjgl.openal.AL10.*; public class AudioReader { - private AudioReader() {}; - - //TODO fix converting from mono-stereo - - public static SoundType readAsMono(String AudioFile) { - IntBuffer channelBuffer = 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)); - } - - public static SoundType readAsStereo(String AudioFile) { - IntBuffer channelsBuffer = BufferUtils.createIntBuffer(2); - 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) { - return stb_vorbis_decode_memory(dataToDecode.readAsBytes(), channelsBuffer, rateBuffer); - } + + private AudioReader() {}; + + // TODO fix converting from mono-stereo + + private static SoundType readAsSpecified(String audioName, int format) { + IntBuffer channelBuffer = BufferUtils.createIntBuffer(1); + IntBuffer rateBuffer = BufferUtils.createIntBuffer(1); + + Resource res = ResourceManager.getResource(audioName); + + ShortBuffer rawAudio = decodeVorbis(res, channelBuffer, rateBuffer); + + return new SoundType(rawAudio, format, rateBuffer.get(0)); + } + + public static SoundType readAsMono(String audioName) { + return readAsSpecified(audioName, AL_FORMAT_MONO16); + } + + 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 + ); + } } diff --git a/src/main/java/ru/windcorp/progressia/client/audio/Listener.java b/src/main/java/ru/windcorp/progressia/client/audio/Listener.java index 1be4a9f..7428b18 100644 --- a/src/main/java/ru/windcorp/progressia/client/audio/Listener.java +++ b/src/main/java/ru/windcorp/progressia/client/audio/Listener.java @@ -10,59 +10,67 @@ import static org.lwjgl.openal.AL10.*; //TODO add getters and setters public class Listener { - - private static final Listener instance = new Listener(); - private Listener() {} - public static Listener getInstance() { - return instance; - } - - //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 (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)), - (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)); - 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}); - } -} \ No newline at end of file + + private static final Listener INSTANCE = new Listener(); + + private Listener() {} + + public static Listener getInstance() { + return INSTANCE; + } + + // 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 (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)), + (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) + ); + applyParams(); + } + } + } + + private void resetParams() { + position.set(0); + velocity.set(0); + oriAt.set(0); + 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 + }); + } +} diff --git a/src/main/java/ru/windcorp/progressia/client/audio/Sound.java b/src/main/java/ru/windcorp/progressia/client/audio/Sound.java index 8be6dc2..293576a 100644 --- a/src/main/java/ru/windcorp/progressia/client/audio/Sound.java +++ b/src/main/java/ru/windcorp/progressia/client/audio/Sound.java @@ -7,122 +7,136 @@ import java.nio.FloatBuffer; import static org.lwjgl.openal.AL11.*; public class Sound { - //Buffers - private int audio; - private int source; - //Characteristics - private FloatBuffer position = - (FloatBuffer) BufferUtils.createFloatBuffer(3) - .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}).rewind(); - private float pitch = 1.0f; - private float gain = 1.0f; - - public Sound() { - } - - public Sound(int audio, int source) { - setAudio(audio); - setSource(source); - } - - public Sound(int audio, FloatBuffer position, FloatBuffer velocity, float pitch, float gain) - { - setAudio(audio); - setPosition(position); - setVelocity(velocity); - setPitch(pitch); - setGain(gain); - } - - public Sound(FloatBuffer position, FloatBuffer velocity, float pitch, float gain) { - setPosition(position); - setVelocity(velocity); - setPitch(pitch); - setGain(gain); - } - - public void playOnce() { - alSourcePlay(source); - } - - public void playLoop() { - alSourcei(source, AL_LOOPING, AL_TRUE); - playOnce(); - alSourcei(source, AL_LOOPING, AL_FALSE); - } - - public void stop() { - alSourceStop(source); - } - - public void pause() { - alSourcePause(source); - } - - public boolean isPlaying() { - final int state = alGetSourcei(source, AL_SOURCE_STATE); - return state == AL_PLAYING; - } - - public int getAudio() { - return audio; - } - - public void setAudio(int audio) { - this.audio = audio; - } - - public void setSource(int source) { - this.source = source; - alSourcei(this.source, AL_BUFFER, audio); - } - - public int getSource() { - return source; - } - - - //OTHER - - public void setPosition(FloatBuffer position) { - this.position = position; - alSourcefv(source, AL_POSITION, position); - } - - public FloatBuffer getPosition() { - return position; - } - - - public void setVelocity(FloatBuffer velocity) { - alSourcefv(source, AL_VELOCITY, velocity); - this.velocity = velocity; - } - - public FloatBuffer getVelocity() { - return velocity; - } - - - public void setPitch(float pitch) { - alSourcef(source, AL_PITCH, pitch); - 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; - } -} \ No newline at end of file + + // Buffers + private int audio; + private int source; + + // Characteristics + private FloatBuffer position = (FloatBuffer) BufferUtils.createFloatBuffer( + 3 + ).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 + }).rewind(); + + private float pitch = 1.0f; + private float gain = 1.0f; + + public Sound() {} + + public Sound(int audio, int source) { + setAudio(audio); + setSource(source); + } + + public Sound( + int audio, + FloatBuffer position, + FloatBuffer velocity, + float pitch, + float gain + ) { + setAudio(audio); + setPosition(position); + setVelocity(velocity); + setPitch(pitch); + setGain(gain); + } + + public Sound( + FloatBuffer position, + FloatBuffer velocity, + float pitch, + float gain + ) { + setPosition(position); + setVelocity(velocity); + setPitch(pitch); + setGain(gain); + } + + public void playOnce() { + alSourcePlay(source); + } + + public void playLoop() { + alSourcei(source, AL_LOOPING, AL_TRUE); + playOnce(); + alSourcei(source, AL_LOOPING, AL_FALSE); + } + + public void stop() { + alSourceStop(source); + } + + public void pause() { + alSourcePause(source); + } + + public boolean isPlaying() { + final int state = alGetSourcei(source, AL_SOURCE_STATE); + return state == AL_PLAYING; + } + + public int getAudio() { + return audio; + } + + public void setAudio(int audio) { + this.audio = audio; + } + + public void setSource(int source) { + this.source = source; + alSourcei(this.source, AL_BUFFER, audio); + } + + public int getSource() { + return source; + } + + // OTHER + + public void setPosition(FloatBuffer position) { + this.position = position; + alSourcefv(source, AL_POSITION, position); + } + + public FloatBuffer getPosition() { + return position; + } + + public void setVelocity(FloatBuffer velocity) { + alSourcefv(source, AL_VELOCITY, velocity); + this.velocity = velocity; + } + + public FloatBuffer getVelocity() { + return velocity; + } + + public void setPitch(float pitch) { + alSourcef(source, AL_PITCH, pitch); + 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; + } + +} diff --git a/src/main/java/ru/windcorp/progressia/client/audio/SoundManager.java b/src/main/java/ru/windcorp/progressia/client/audio/SoundManager.java index a0d2326..b5a32e1 100644 --- a/src/main/java/ru/windcorp/progressia/client/audio/SoundManager.java +++ b/src/main/java/ru/windcorp/progressia/client/audio/SoundManager.java @@ -1,6 +1,5 @@ package ru.windcorp.progressia.client.audio; -import org.lwjgl.BufferUtils; import org.lwjgl.openal.AL; import org.lwjgl.openal.ALC; import org.lwjgl.openal.ALCCapabilities; @@ -12,73 +11,97 @@ import static org.lwjgl.openal.ALC10.*; import java.util.concurrent.ArrayBlockingQueue; public class SoundManager { - private static final int SOURCES_NUM = 64; - private static int lastSourceIndex = -1; - private static final int[] SOURCES = new int[SOURCES_NUM]; - private static final ArrayBlockingQueue SOUNDS = new ArrayBlockingQueue<>(SOURCES_NUM); - - private static long DEVICE; - - public static void initAL() { - String defaultDeviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER); - DEVICE = alcOpenDevice(defaultDeviceName); - int[] attributes = {0}; - long context = alcCreateContext(DEVICE, attributes); - alcMakeContextCurrent(context); - ALCCapabilities deviceCaps = ALC.createCapabilities(DEVICE); - ALCapabilities alcaps = AL.createCapabilities(deviceCaps); - checkALError(); - alGenSources(SOURCES); - } - - public static void update() { - //Position of the listener - Listener.getInstance().update(); - } - - private static void addSound(Sound sound) { - if (!SOUNDS.offer(sound)) { - Sound polled = SOUNDS.poll(); - assert polled != null; - polled.stop(); - if (!SOUNDS.offer(sound)) { - throw new RuntimeException(); - } - } - } - - private static int getNextSource() { - if (++lastSourceIndex > SOURCES_NUM) lastSourceIndex = 0; - return SOURCES[lastSourceIndex]; - } - - public static Sound createSound(SoundType soundType) { - Sound sound = soundType.genSoundSource(getNextSource()); - addSound(sound); - return sound; - } - - public static void clearSounds() { - Sound polled = SOUNDS.poll(); - while (polled != null) { - polled.stop(); - polled = SOUNDS.poll(); - } - } - - 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); - } + + private static final int SOURCES_NUM = 64; + private static int lastSourceIndex = -1; + private static final int[] SOURCES = new int[SOURCES_NUM]; + + private static final ArrayBlockingQueue SOUNDS = + new ArrayBlockingQueue<>(SOURCES_NUM); + + private static long device; + + private static ALCCapabilities deviceCapabilities; + private static ALCapabilities alCapabilities; + + public static void initAL() { + String defaultDeviceName = alcGetString( + 0, + ALC_DEFAULT_DEVICE_SPECIFIER + ); + + device = alcOpenDevice(defaultDeviceName); + + int[] attributes = new int[1]; + long context = alcCreateContext(device, attributes); + alcMakeContextCurrent(context); + + deviceCapabilities = ALC.createCapabilities(device); + alCapabilities = AL.createCapabilities(deviceCapabilities); + + checkALError(); + + alGenSources(SOURCES); + } + + public static void update() { + // Position of the listener + Listener.getInstance().update(); + } + + private static void addSound(Sound sound) { + if (!SOUNDS.offer(sound)) { + Sound polled = SOUNDS.poll(); + assert polled != null; + polled.stop(); + if (!SOUNDS.offer(sound)) { + throw new RuntimeException(); + } + } + } + + private static int getNextSource() { + if (++lastSourceIndex > SOURCES_NUM) + lastSourceIndex = 0; + return SOURCES[lastSourceIndex]; + } + + public static Sound createSound(SoundType soundType) { + Sound sound = soundType.genSoundSource(getNextSource()); + addSound(sound); + return sound; + } + + public static void clearSounds() { + Sound polled = SOUNDS.poll(); + while (polled != null) { + polled.stop(); + polled = SOUNDS.poll(); + } + } + + 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; + } + } diff --git a/src/main/java/ru/windcorp/progressia/client/audio/SoundType.java b/src/main/java/ru/windcorp/progressia/client/audio/SoundType.java index f6fdbfe..93a106e 100644 --- a/src/main/java/ru/windcorp/progressia/client/audio/SoundType.java +++ b/src/main/java/ru/windcorp/progressia/client/audio/SoundType.java @@ -4,42 +4,48 @@ import java.nio.ShortBuffer; import static org.lwjgl.openal.AL11.*; public class SoundType { - private ShortBuffer rawAudio; - private int sampleRate; - private int format; - - public SoundType(ShortBuffer rawAudio, int format, int sampleRate) { - this.rawAudio = rawAudio; - this.sampleRate = sampleRate; - this.format = format; - } - - public static int genEmptyAudio() { - return alGenBuffers(); - } - - public static int genEmptySource() { - return alGenSources(); - } - - public int genAudio() { - int audio = alGenBuffers(); - alBufferData(audio, format, rawAudio, sampleRate); - return audio; - } - - public Sound genSoundSource() { - return new Sound(genAudio(), alGenSources()); - } - - public Sound genSoundSource(int source) { - if(!alIsSource(source)) throw new RuntimeException(); - return new Sound(genAudio(), source); - } - - 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()); - } + + private ShortBuffer rawAudio; + private int sampleRate; + private int format; + + public SoundType(ShortBuffer rawAudio, int format, int sampleRate) { + this.rawAudio = rawAudio; + this.sampleRate = sampleRate; + this.format = format; + } + + public static int genEmptyAudio() { + return alGenBuffers(); + } + + public static int genEmptySource() { + return alGenSources(); + } + + public int genAudio() { + int audio = alGenBuffers(); + alBufferData(audio, format, rawAudio, sampleRate); + return audio; + } + + public Sound genSoundSource() { + return new Sound(genAudio(), alGenSources()); + } + + public Sound genSoundSource(int source) { + if (!alIsSource(source)) + throw new RuntimeException(); + + return new Sound(genAudio(), source); + } + + 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()); + } + }