Trying to make OpenAL to work as intended

This commit is contained in:
Евгений Смирнов 2020-08-31 21:15:46 +03:00
parent ab4888e592
commit 03f9b90239

View File

@ -1,10 +1,7 @@
package ru.windcorp.progressia.client.audio.backend; package ru.windcorp.progressia.client.audio.backend;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import org.lwjgl.openal.AL; import org.lwjgl.openal.*;
import org.lwjgl.openal.AL10;
import org.lwjgl.openal.ALC;
import org.lwjgl.openal.ALCCapabilities;
import ru.windcorp.progressia.common.resource.Resource; import ru.windcorp.progressia.common.resource.Resource;
import ru.windcorp.progressia.common.resource.ResourceManager; import ru.windcorp.progressia.common.resource.ResourceManager;
@ -15,25 +12,25 @@ import java.nio.ShortBuffer;
import static org.lwjgl.openal.AL10.*; import static org.lwjgl.openal.AL10.*;
import static org.lwjgl.openal.ALC10.*; import static org.lwjgl.openal.ALC10.*;
import static org.lwjgl.stb.STBVorbis.stb_vorbis_decode_memory; import static org.lwjgl.stb.STBVorbis.stb_vorbis_decode_memory;
import static org.lwjgl.system.libc.LibCStdlib.free;
public class ALTest { public class ALTest {
// Buffers hold sound data // Buffers hold sound data
private IntBuffer buffer = BufferUtils.createIntBuffer(1); private IntBuffer buffer = BufferUtils.createIntBuffer(1);
// Sources are points emitting sound // Sources are points emitting sound
private IntBuffer source = BufferUtils.createIntBuffer(1); private IntBuffer source = BufferUtils.createIntBuffer(1);
// Position of the source sound // Position of the source sound
private FloatBuffer sourcePos = BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip(); private FloatBuffer sourcePos = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip();
// Velocity of the source sound // Velocity of the source sound
private FloatBuffer sourceVel = BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip(); private FloatBuffer sourceVel = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip();
// Position of the listener // Position of the listener
private FloatBuffer listenerPos = BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip(); private FloatBuffer listenerPos = (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip();
// Velocity of the listener // Velocity of the listener
private FloatBuffer listenerVel = BufferUtils.createFloatBuffer(3).put(new float[]{0.0f, 0.0f, 0.0f}).flip(); private 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") // Orientation of the listener. (first 3 elements are "at", second 3 are "up")
private FloatBuffer listenerOri = private FloatBuffer listenerOri =
BufferUtils.createFloatBuffer(6).put(new float[]{0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f}).flip(); (FloatBuffer) BufferUtils.createFloatBuffer(6).put(new float[]{0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f}).flip();
private ShortBuffer rawDataBuffer; private ShortBuffer rawDataBuffer;
@ -45,19 +42,19 @@ public class ALTest {
long context = alcCreateContext(device, attributes); long context = alcCreateContext(device, attributes);
alcMakeContextCurrent(context); alcMakeContextCurrent(context);
ALCCapabilities deviceCaps = ALC.createCapabilities(device); ALCCapabilities deviceCaps = ALC.createCapabilities(device);
AL.createCapabilities(deviceCaps); ALCapabilities alcaps = AL.createCapabilities(deviceCaps);
} }
int loadALData() { void loadALData() {
AL10.alGenBuffers(); checkALError();
if (AL10.alGetError() != AL10.AL_NO_ERROR) { int thebuffer = AL10.alGenBuffers();
return AL10.AL_FALSE; checkALError();
}
IntBuffer channelsBuffer = BufferUtils.createIntBuffer(1); IntBuffer channelsBuffer = BufferUtils.createIntBuffer(1);
IntBuffer sampleRateBuffer = BufferUtils.createIntBuffer(1); IntBuffer sampleRateBuffer = BufferUtils.createIntBuffer(1);
ShortBuffer rawAudioBuffer = ShortBuffer rawAudioBuffer =
readVorbis("assets/sounds/sample.ogg", channelsBuffer, sampleRateBuffer); readVorbis("assets/sounds/sample.ogg", channelsBuffer, sampleRateBuffer);
int channels = channelsBuffer.get(); int channels = channelsBuffer.get();
@ -72,33 +69,28 @@ public class ALTest {
System.out.println(rawAudioBuffer); System.out.println(rawAudioBuffer);
//Send the data to OpenAL //Send the data to OpenAL
alBufferData(buffer.get(0), format, rawAudioBuffer, sampleRate); alBufferData(thebuffer, AL_FORMAT_STEREO16, rawAudioBuffer, sampleRate);
//Free the memory allocated by STB //Free the memory allocated by STB
free(rawAudioBuffer); //free(rawAudioBuffer);
alGenSources(source);
//Assign our buffer to the source
alSourcei(source.get(0), AL_BUFFER, thebuffer);
// Bind the buffer with the source // Bind the buffer with the source
AL10.alGenBuffers(source); //AL10.alGenBuffers(source);
int errorishe = AL10.alGetError(); checkALError();
if (errorishe != AL10.AL_NO_ERROR) {
System.out.println(errorishe);
System.out.println(alGetString(40));
return AL_FALSE;
}
alSourcei(source.get(0), AL_BUFFER, buffer.get(0) ); /*alSourcei(source.get(0), AL_BUFFER, buffer.get(0) );
alSourcef(source.get(0), AL_PITCH, 1.0f ); alSourcef(source.get(0), AL_PITCH, 1.0f );
alSourcef(source.get(0), AL_GAIN, 1.0f ); alSourcef(source.get(0), AL_GAIN, 1.0f );
alSourcefv(source.get(0), AL_POSITION, sourcePos ); alSourcefv(source.get(0), AL_POSITION, sourcePos );
alSourcefv(source.get(0), AL_VELOCITY, sourceVel ); alSourcefv(source.get(0), AL_VELOCITY, sourceVel );*/
int error = alGetError(); int error = alGetError();
if (error == AL_NO_ERROR) { checkALError();
return AL_TRUE;
}
System.out.println(error);
System.out.println("gavno");
return AL_FALSE;
} }
//Tells OpenAL to use the data we have created //Tells OpenAL to use the data we have created
@ -109,26 +101,36 @@ public class ALTest {
} }
void killALData() { void killALData() {
alDeleteSources(source); //alDeleteSources(source);
alDeleteBuffers(buffer); //alDeleteBuffers(buffer);
} }
public void execute() { public void execute() {
initializeAL(); initializeAL();
AL10.alGetError(); checkALError();
loadALData();
if (loadALData() == AL_FALSE) {
//throw new RuntimeException("ALTest: Error loading data.");
}
setListenerValues(); setListenerValues();
checkALError();
AL10.alSourcePlay(source.get(0)); AL10.alSourcePlay(source.get(0));
checkALError();
} }
private static ShortBuffer readVorbis(String fileName, IntBuffer channelsBuffer, IntBuffer sampleRateBuffer) { private static ShortBuffer readVorbis(String fileName, IntBuffer channelsBuffer, IntBuffer sampleRateBuffer) {
Resource res = ResourceManager.getResource(fileName); Resource res = ResourceManager.getResource(fileName);
System.out.println();
System.out.println(res.readAsBytes().remaining());
System.out.println();
return stb_vorbis_decode_memory(res.readAsBytes(), channelsBuffer, sampleRateBuffer); return stb_vorbis_decode_memory(res.readAsBytes(), channelsBuffer, sampleRateBuffer);
} }
public void checkALError() {
int i = alGetError();
if(alGetError() != AL_NO_ERROR) {
throw new RuntimeException(i+"");
}
}
} }