AUDIO: now number of sources is limited

- fixed isPlaying() method
This commit is contained in:
Евгений Смирнов 2020-09-24 21:51:06 +03:00
parent aa640e23d6
commit 12dc816760
5 changed files with 98 additions and 74 deletions

View File

@ -4,7 +4,7 @@ import org.lwjgl.BufferUtils;
import java.nio.FloatBuffer;
import static org.lwjgl.openal.AL10.*;
import static org.lwjgl.openal.AL11.*;
public class Sound {
//Buffers
@ -58,18 +58,13 @@ public class Sound {
alSourceStop(source);
}
public void flush() {
alDeleteBuffers(audio);
alDeleteBuffers(source);
}
public void pause() {
alSourcePause(source);
}
public boolean isPlaying() {
final int state = alGetSourcei(source, AL_PLAYING);
return state == AL_TRUE;
final int state = alGetSourcei(source, AL_SOURCE_STATE);
return state == AL_PLAYING;
}
public int getAudio() {
@ -83,7 +78,6 @@ 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

@ -1,34 +1,84 @@
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;
import org.lwjgl.openal.ALCapabilities;
import static org.lwjgl.openal.AL11.*;
import static org.lwjgl.openal.ALC10.*;
import java.nio.IntBuffer;
import java.util.concurrent.ArrayBlockingQueue;
public class SoundManager {
private static IntBuffer sources = BufferUtils.createIntBuffer(65);
private static ArrayBlockingQueue<Sound> sounds = new ArrayBlockingQueue<>(64);
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);
public static void update() {
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 addSound(Sound sound) {
if (!sounds.offer(sound)) {
Sound polled = sounds.poll();
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.flush();
if (!sounds.offer(sound)) {
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) {
Sound polled = SOUNDS.poll();
while (polled != null) {
polled.stop();
polled = sounds.poll();
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);
}
}

View File

@ -1,7 +1,7 @@
package ru.windcorp.progressia.client.audio;
import java.nio.ShortBuffer;
import static org.lwjgl.openal.AL10.*;
import static org.lwjgl.openal.AL11.*;
public class SoundType {
private ShortBuffer rawAudio;
@ -14,9 +14,32 @@ public class SoundType {
this.format = format;
}
public Sound genSound() {
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());
}
}

View File

@ -1,71 +1,27 @@
package ru.windcorp.progressia.client.audio.backend;
import org.lwjgl.BufferUtils;
import org.lwjgl.openal.*;
import ru.windcorp.progressia.client.audio.AudioReader;
import ru.windcorp.progressia.client.audio.Listener;
import ru.windcorp.progressia.client.audio.Sound;
import java.nio.FloatBuffer;
import static org.lwjgl.openal.AL10.*;
import static org.lwjgl.openal.ALC10.*;
import ru.windcorp.progressia.client.audio.SoundManager;
public class ALTest {
// Position of the listener
private static FloatBuffer listenerPos = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip();
// Velocity of the listener
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 static FloatBuffer listenerOri =
(FloatBuffer) BufferUtils.createFloatBuffer(6).put(new float[]{0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f}).flip();
static private void initializeAL() {
String defaultDeviceName = alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER);
long device = alcOpenDevice(defaultDeviceName);
int[] attributes = {0};
long context = alcCreateContext(device, attributes);
alcMakeContextCurrent(context);
ALCCapabilities deviceCaps = ALC.createCapabilities(device);
ALCapabilities alcaps = AL.createCapabilities(deviceCaps);
SoundManager.initAL();
}
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();
Sound music = SoundManager.createSound(AudioReader.readAsMono("assets/sounds/sample_mono.ogg"));
music.playOnce();
checkALError();
/*music = SoundManager.createSound(AudioReader.readAsStereo("assets/sounds/sample_mono.ogg"));
music.playOnce();*/
}
static void killALData() {
//alDeleteSources(source);
//alDeleteBuffers(buffer);
//TODO implement the method or its analogue
}
public static void execute() {
initializeAL();
checkALError();
checkALError();
checkALError();
loadALData();
checkALError();
checkALError();
}
public static void update() {
Listener.getInstance().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.SoundManager;
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;
@ -82,7 +83,7 @@ public class Camera {
public Camera() {}
public void apply(WorldRenderHelper helper) {
ALTest.update();
SoundManager.update();
applyPerspective(helper);
rotateCoordinateSystem(helper);