Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import java.util.List;
- import java.util.Random;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.LWJGLException;
- import org.lwjgl.openal.AL;
- import org.lwjgl.openal.AL10;
- import org.lwjgl.util.WaveData;
- public class PlaySound extends Sounds {
- public static final int NUM_BUFFERS = 3;
- public static final int NUM_SOURCES = 3;
- IntBuffer buffer = BufferUtils.createIntBuffer(NUM_BUFFERS);
- IntBuffer source = BufferUtils.createIntBuffer(NUM_BUFFERS);
- FloatBuffer sourcePos = BufferUtils.createFloatBuffer(3 * NUM_BUFFERS);
- FloatBuffer sourceVel = BufferUtils.createFloatBuffer(3 * NUM_BUFFERS);
- FloatBuffer listenerPos = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
- FloatBuffer listenerVel = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
- FloatBuffer listenerOri = BufferUtils.createFloatBuffer(6).put(new float[] { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f });
- public PlaySound() {
- listenerPos.flip();
- listenerVel.flip();
- listenerOri.flip();
- }
- int loadALData(List<String> fileNames, List<Boolean> loops) {
- AL10.alGenBuffers(buffer);
- if (AL10.alGetError() != AL10.AL_NO_ERROR)
- return AL10.AL_FALSE;
- WaveData waveFile;
- for (int i = 0; i < fileNames.size(); i++) {
- waveFile = WaveData.create(fileNames.get(i));
- System.out.println(fileNames.get(i));
- AL10.alBufferData(buffer.get(i), waveFile.format, waveFile.data, waveFile.samplerate);
- waveFile.dispose();
- }
- AL10.alGenSources(source);
- if (AL10.alGetError() != AL10.AL_NO_ERROR)
- return AL10.AL_FALSE;
- for (int i = 0; i < fileNames.size(); i++) {
- AL10.alSourcei(source.get(i), AL10.AL_BUFFER, buffer.get(i));
- AL10.alSourcef(source.get(i), AL10.AL_PITCH, 1.0f);
- AL10.alSourcef(source.get(i), AL10.AL_GAIN, 1.0f);
- AL10.alSource(source.get(i), AL10.AL_POSITION, (FloatBuffer) sourcePos.position(i * 3));
- AL10.alSource(source.get(i), AL10.AL_VELOCITY, (FloatBuffer) sourceVel.position(i * 3));
- if (loops.get(i)) {
- AL10.alSourcei(source.get(i), AL10.AL_LOOPING, AL10.AL_TRUE);
- } else {
- AL10.alSourcei(source.get(i), AL10.AL_LOOPING, AL10.AL_FALSE);
- }
- }
- if (AL10.alGetError() == AL10.AL_NO_ERROR)
- return AL10.AL_TRUE;
- return AL10.AL_FALSE;
- }
- void setListenerValues() {
- AL10.alListener(AL10.AL_POSITION, listenerPos);
- AL10.alListener(AL10.AL_VELOCITY, listenerVel);
- AL10.alListener(AL10.AL_ORIENTATION, listenerOri);
- }
- void killALData() {
- AL10.alDeleteSources(source);
- AL10.alDeleteBuffers(buffer);
- }
- public void execute(List<String> fileNames,List<Boolean> loops) {
- try {
- AL.create();
- } catch (LWJGLException le) {
- le.printStackTrace();
- return;
- }
- AL10.alGetError();
- if(loadALData(fileNames,loops) != AL10.AL_FALSE) {
- System.out.println("Error loading data.");
- return;
- }
- setListenerValues();
- AL10.alSourcePlay(source.get(0));
- int play;
- Random random = new Random();
- System.out.println("Press ENTER to exit");
- for(int i=1; i<NUM_SOURCES; i++) {
- play = AL10.alGetSourcei(source.get(i), AL10.AL_SOURCE_STATE);
- if(play != AL10.AL_PLAYING) {
- double theta = (double) (random.nextInt() % 360) * 3.14 / 180.0;
- sourcePos.put(i*3+0, -(float) (Math.cos(theta)));
- sourcePos.put(i*3+1, -(float) (random.nextInt()%2));
- sourcePos.put(i*3+2, -(float) (Math.sin(theta)));
- AL10.alSource(source.get(i), AL10.AL_POSITION, (FloatBuffer) sourcePos.position(i*3));
- AL10.alSourcePlay(source.get(i));
- }
- }
- try{
- Thread.sleep(100);
- }catch(InterruptedException e){
- System.out.println("Sleep Interrupted");
- }
- killALData();
- AL.destroy();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement