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;
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

@ -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) {
private static SoundType readAsSpecified(String audioName, int format) {
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));
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) {
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));
public static SoundType readAsMono(String audioName) {
return readAsSpecified(audioName, AL_FORMAT_MONO16);
}
private static ShortBuffer decodeVorbis(Resource dataToDecode, IntBuffer channelsBuffer, IntBuffer rateBuffer) {
return stb_vorbis_decode_memory(dataToDecode.readAsBytes(), channelsBuffer, rateBuffer);
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
);
}
}

View File

@ -11,10 +11,12 @@ import static org.lwjgl.openal.AL10.*;
public class Listener {
private static final Listener instance = new Listener();
private static final Listener INSTANCE = new Listener();
private Listener() {}
public static Listener getInstance() {
return instance;
return INSTANCE;
}
// Params
@ -42,27 +44,33 @@ public class Listener {
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);
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});
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.*;
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 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() {}
public Sound(int audio, int source) {
setAudio(audio);
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);
setPosition(position);
setVelocity(velocity);
@ -37,7 +49,12 @@ public class Sound {
setGain(gain);
}
public Sound(FloatBuffer position, FloatBuffer velocity, float pitch, float gain) {
public Sound(
FloatBuffer position,
FloatBuffer velocity,
float pitch,
float gain
) {
setPosition(position);
setVelocity(velocity);
setPitch(pitch);
@ -84,7 +101,6 @@ public class Sound {
return source;
}
// OTHER
public void setPosition(FloatBuffer position) {
@ -96,7 +112,6 @@ public class Sound {
return position;
}
public void setVelocity(FloatBuffer velocity) {
alSourcefv(source, AL_VELOCITY, velocity);
this.velocity = velocity;
@ -106,7 +121,6 @@ public class Sound {
return velocity;
}
public void setPitch(float pitch) {
alSourcef(source, AL_PITCH, pitch);
this.pitch = pitch;
@ -116,7 +130,6 @@ public class Sound {
return pitch;
}
public void setGain(float gain) {
alSourcef(source, AL_GAIN, gain);
this.gain = gain;
@ -125,4 +138,5 @@ public class Sound {
public float getGain() {
return gain;
}
}

View File

@ -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,22 +11,36 @@ 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<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() {
String defaultDeviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER);
DEVICE = alcOpenDevice(defaultDeviceName);
int[] attributes = {0};
long context = alcCreateContext(DEVICE, attributes);
String defaultDeviceName = alcGetString(
0,
ALC_DEFAULT_DEVICE_SPECIFIER
);
device = alcOpenDevice(defaultDeviceName);
int[] attributes = new int[1];
long context = alcCreateContext(device, attributes);
alcMakeContextCurrent(context);
ALCCapabilities deviceCaps = ALC.createCapabilities(DEVICE);
ALCapabilities alcaps = AL.createCapabilities(deviceCaps);
deviceCapabilities = ALC.createCapabilities(device);
alCapabilities = AL.createCapabilities(deviceCapabilities);
checkALError();
alGenSources(SOURCES);
}
@ -48,7 +61,8 @@ public class SoundManager {
}
private static int getNextSource() {
if (++lastSourceIndex > SOURCES_NUM) lastSourceIndex = 0;
if (++lastSourceIndex > SOURCES_NUM)
lastSourceIndex = 0;
return SOURCES[lastSourceIndex];
}
@ -79,6 +93,15 @@ public class SoundManager {
for (Sound s : SOUNDS) {
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.*;
public class SoundType {
private ShortBuffer rawAudio;
private int sampleRate;
private int format;
@ -33,13 +34,18 @@ public class SoundType {
}
public Sound genSoundSource(int source) {
if(!alIsSource(source)) throw new RuntimeException();
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();
if (!alIsBuffer(audio) || !alIsSource(source))
throw new RuntimeException();
alBufferData(audio, format, rawAudio, sampleRate);
return new Sound(audio, alGenSources());
}
}