AUDIO: now number of sources is limited
- fixed isPlaying() method
This commit is contained in:
parent
aa640e23d6
commit
12dc816760
@ -4,7 +4,7 @@ import org.lwjgl.BufferUtils;
|
|||||||
|
|
||||||
import java.nio.FloatBuffer;
|
import java.nio.FloatBuffer;
|
||||||
|
|
||||||
import static org.lwjgl.openal.AL10.*;
|
import static org.lwjgl.openal.AL11.*;
|
||||||
|
|
||||||
public class Sound {
|
public class Sound {
|
||||||
//Buffers
|
//Buffers
|
||||||
@ -58,18 +58,13 @@ public class Sound {
|
|||||||
alSourceStop(source);
|
alSourceStop(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flush() {
|
|
||||||
alDeleteBuffers(audio);
|
|
||||||
alDeleteBuffers(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void pause() {
|
public void pause() {
|
||||||
alSourcePause(source);
|
alSourcePause(source);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPlaying() {
|
public boolean isPlaying() {
|
||||||
final int state = alGetSourcei(source, AL_PLAYING);
|
final int state = alGetSourcei(source, AL_SOURCE_STATE);
|
||||||
return state == AL_TRUE;
|
return state == AL_PLAYING;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAudio() {
|
public int getAudio() {
|
||||||
@ -83,7 +78,6 @@ public class Sound {
|
|||||||
public void setSource(int source) {
|
public void setSource(int source) {
|
||||||
this.source = source;
|
this.source = source;
|
||||||
alSourcei(this.source, AL_BUFFER, audio);
|
alSourcei(this.source, AL_BUFFER, audio);
|
||||||
//alSourcei(this.source, AL_SOURCE_RELATIVE, AL_TRUE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSource() {
|
public int getSource() {
|
||||||
|
@ -1,34 +1,84 @@
|
|||||||
package ru.windcorp.progressia.client.audio;
|
package ru.windcorp.progressia.client.audio;
|
||||||
|
|
||||||
import org.lwjgl.BufferUtils;
|
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;
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
|
|
||||||
public class SoundManager {
|
public class SoundManager {
|
||||||
private static IntBuffer sources = BufferUtils.createIntBuffer(65);
|
private static final int SOURCES_NUM = 64;
|
||||||
private static ArrayBlockingQueue<Sound> sounds = new ArrayBlockingQueue<>(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) {
|
public static void update() {
|
||||||
if (!sounds.offer(sound)) {
|
//Position of the listener
|
||||||
Sound polled = sounds.poll();
|
Listener.getInstance().update();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void addSound(Sound sound) {
|
||||||
|
if (!SOUNDS.offer(sound)) {
|
||||||
|
Sound polled = SOUNDS.poll();
|
||||||
assert polled != null;
|
assert polled != null;
|
||||||
polled.flush();
|
polled.stop();
|
||||||
if (!sounds.offer(sound)) {
|
if (!SOUNDS.offer(sound)) {
|
||||||
throw new RuntimeException();
|
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() {
|
public static void clearSounds() {
|
||||||
Sound polled = sounds.poll();
|
Sound polled = SOUNDS.poll();
|
||||||
while(polled != null) {
|
while (polled != null) {
|
||||||
polled.stop();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package ru.windcorp.progressia.client.audio;
|
package ru.windcorp.progressia.client.audio;
|
||||||
|
|
||||||
import java.nio.ShortBuffer;
|
import java.nio.ShortBuffer;
|
||||||
import static org.lwjgl.openal.AL10.*;
|
import static org.lwjgl.openal.AL11.*;
|
||||||
|
|
||||||
public class SoundType {
|
public class SoundType {
|
||||||
private ShortBuffer rawAudio;
|
private ShortBuffer rawAudio;
|
||||||
@ -14,9 +14,32 @@ public class SoundType {
|
|||||||
this.format = format;
|
this.format = format;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Sound genSound() {
|
public static int genEmptyAudio() {
|
||||||
|
return alGenBuffers();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int genEmptySource() {
|
||||||
|
return alGenSources();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int genAudio() {
|
||||||
int audio = alGenBuffers();
|
int audio = alGenBuffers();
|
||||||
alBufferData(audio, format, rawAudio, sampleRate);
|
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());
|
return new Sound(audio, alGenSources());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,71 +1,27 @@
|
|||||||
package ru.windcorp.progressia.client.audio.backend;
|
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.AudioReader;
|
||||||
import ru.windcorp.progressia.client.audio.Listener;
|
|
||||||
import ru.windcorp.progressia.client.audio.Sound;
|
import ru.windcorp.progressia.client.audio.Sound;
|
||||||
|
import ru.windcorp.progressia.client.audio.SoundManager;
|
||||||
import java.nio.FloatBuffer;
|
|
||||||
|
|
||||||
import static org.lwjgl.openal.AL10.*;
|
|
||||||
import static org.lwjgl.openal.ALC10.*;
|
|
||||||
|
|
||||||
public class ALTest {
|
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() {
|
static private void initializeAL() {
|
||||||
|
SoundManager.initAL();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void loadALData() {
|
static void loadALData() {
|
||||||
alListenerfv(AL_POSITION, listenerPos);
|
Sound music = SoundManager.createSound(AudioReader.readAsMono("assets/sounds/sample_mono.ogg"));
|
||||||
alListenerfv(AL_VELOCITY, listenerVel);
|
|
||||||
alListenerfv(AL_ORIENTATION, listenerOri);
|
|
||||||
Sound music = AudioReader.readAsMono("assets/sounds/sample_mono.ogg").genSound();
|
|
||||||
|
|
||||||
music.playOnce();
|
music.playOnce();
|
||||||
checkALError();
|
/*music = SoundManager.createSound(AudioReader.readAsStereo("assets/sounds/sample_mono.ogg"));
|
||||||
|
music.playOnce();*/
|
||||||
}
|
}
|
||||||
|
|
||||||
static void killALData() {
|
static void killALData() {
|
||||||
//alDeleteSources(source);
|
//TODO implement the method or its analogue
|
||||||
//alDeleteBuffers(buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void execute() {
|
public static void execute() {
|
||||||
initializeAL();
|
initializeAL();
|
||||||
checkALError();
|
|
||||||
checkALError();
|
|
||||||
checkALError();
|
|
||||||
loadALData();
|
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+"");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -25,6 +25,7 @@ import java.util.function.Consumer;
|
|||||||
import glm.Glm;
|
import glm.Glm;
|
||||||
import glm.mat._4.Mat4;
|
import glm.mat._4.Mat4;
|
||||||
import glm.vec._3.Vec3;
|
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.audio.backend.ALTest;
|
||||||
import ru.windcorp.progressia.client.graphics.backend.GraphicsInterface;
|
import ru.windcorp.progressia.client.graphics.backend.GraphicsInterface;
|
||||||
import ru.windcorp.progressia.client.graphics.world.Camera.Anchor.Mode;
|
import ru.windcorp.progressia.client.graphics.world.Camera.Anchor.Mode;
|
||||||
@ -82,7 +83,7 @@ public class Camera {
|
|||||||
public Camera() {}
|
public Camera() {}
|
||||||
|
|
||||||
public void apply(WorldRenderHelper helper) {
|
public void apply(WorldRenderHelper helper) {
|
||||||
ALTest.update();
|
SoundManager.update();
|
||||||
applyPerspective(helper);
|
applyPerspective(helper);
|
||||||
rotateCoordinateSystem(helper);
|
rotateCoordinateSystem(helper);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user