Advertisement
jak1

OpenAL Problem...

Jul 18th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 KB | None | 0 0
  1.  
  2.  
  3. import java.nio.FloatBuffer;
  4. import java.nio.IntBuffer;
  5. import java.util.List;
  6. import java.util.Random;
  7.  
  8. import org.lwjgl.BufferUtils;
  9. import org.lwjgl.LWJGLException;
  10. import org.lwjgl.openal.AL;
  11. import org.lwjgl.openal.AL10;
  12. import org.lwjgl.util.WaveData;
  13.  
  14. public class PlaySound extends Sounds {
  15.  
  16.     public static final int NUM_BUFFERS = 3;
  17.     public static final int NUM_SOURCES = 3;
  18.  
  19.     IntBuffer buffer = BufferUtils.createIntBuffer(NUM_BUFFERS);
  20.     IntBuffer source = BufferUtils.createIntBuffer(NUM_BUFFERS);
  21.     FloatBuffer sourcePos = BufferUtils.createFloatBuffer(3 * NUM_BUFFERS);
  22.     FloatBuffer sourceVel = BufferUtils.createFloatBuffer(3 * NUM_BUFFERS);
  23.     FloatBuffer listenerPos = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
  24.     FloatBuffer listenerVel = BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f });
  25.  
  26.     FloatBuffer listenerOri = BufferUtils.createFloatBuffer(6).put(new float[] { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f });
  27.  
  28.     public PlaySound() {
  29.         listenerPos.flip();
  30.         listenerVel.flip();
  31.         listenerOri.flip();
  32.     }
  33.  
  34.     int loadALData(List<String> fileNames, List<Boolean> loops) {
  35.         AL10.alGenBuffers(buffer);
  36.  
  37.         if (AL10.alGetError() != AL10.AL_NO_ERROR)
  38.             return AL10.AL_FALSE;
  39.         WaveData waveFile;
  40.         for (int i = 0; i < fileNames.size(); i++) {
  41.             waveFile = WaveData.create(fileNames.get(i));
  42.             System.out.println(fileNames.get(i));
  43.             AL10.alBufferData(buffer.get(i), waveFile.format, waveFile.data, waveFile.samplerate);
  44.             waveFile.dispose();
  45.         }
  46.  
  47.         AL10.alGenSources(source);
  48.  
  49.         if (AL10.alGetError() != AL10.AL_NO_ERROR)
  50.             return AL10.AL_FALSE;
  51.  
  52.         for (int i = 0; i < fileNames.size(); i++) {
  53.  
  54.             AL10.alSourcei(source.get(i), AL10.AL_BUFFER, buffer.get(i));
  55.             AL10.alSourcef(source.get(i), AL10.AL_PITCH, 1.0f);
  56.             AL10.alSourcef(source.get(i), AL10.AL_GAIN, 1.0f);
  57.             AL10.alSource(source.get(i), AL10.AL_POSITION, (FloatBuffer) sourcePos.position(i * 3));
  58.             AL10.alSource(source.get(i), AL10.AL_VELOCITY, (FloatBuffer) sourceVel.position(i * 3));
  59.             if (loops.get(i)) {
  60.                 AL10.alSourcei(source.get(i), AL10.AL_LOOPING, AL10.AL_TRUE);
  61.             } else {
  62.                 AL10.alSourcei(source.get(i), AL10.AL_LOOPING, AL10.AL_FALSE);
  63.             }
  64.  
  65.         }
  66.         if (AL10.alGetError() == AL10.AL_NO_ERROR)
  67.             return AL10.AL_TRUE;
  68.  
  69.         return AL10.AL_FALSE;
  70.     }
  71.  
  72.     void setListenerValues() {
  73.         AL10.alListener(AL10.AL_POSITION, listenerPos);
  74.         AL10.alListener(AL10.AL_VELOCITY, listenerVel);
  75.         AL10.alListener(AL10.AL_ORIENTATION, listenerOri);
  76.     }
  77.  
  78.     void killALData() {
  79.         AL10.alDeleteSources(source);
  80.         AL10.alDeleteBuffers(buffer);
  81.     }
  82.  
  83.     public void execute(List<String> fileNames,List<Boolean> loops) {
  84.         try {
  85.           AL.create();
  86.         } catch (LWJGLException le) {
  87.           le.printStackTrace();
  88.           return;
  89.         }
  90.         AL10.alGetError();
  91.      
  92.         if(loadALData(fileNames,loops) != AL10.AL_FALSE) {
  93.           System.out.println("Error loading data.");
  94.           return;
  95.         }
  96.      
  97.         setListenerValues();
  98.      
  99.         AL10.alSourcePlay(source.get(0));
  100.          
  101.         int play;
  102.         Random random = new Random();
  103.      
  104.         System.out.println("Press ENTER to exit");
  105.          
  106.           for(int i=1; i<NUM_SOURCES; i++) {
  107.             play = AL10.alGetSourcei(source.get(i), AL10.AL_SOURCE_STATE);
  108.              
  109.             if(play != AL10.AL_PLAYING) {
  110.               double theta = (double) (random.nextInt() % 360) * 3.14 / 180.0;
  111.                
  112.               sourcePos.put(i*3+0, -(float) (Math.cos(theta)));
  113.               sourcePos.put(i*3+1, -(float) (random.nextInt()%2));
  114.               sourcePos.put(i*3+2, -(float) (Math.sin(theta)));
  115.                
  116.               AL10.alSource(source.get(i), AL10.AL_POSITION, (FloatBuffer) sourcePos.position(i*3));
  117.               AL10.alSourcePlay(source.get(i));          
  118.             }
  119.           }
  120.           try{
  121.             Thread.sleep(100);
  122.             }catch(InterruptedException e){
  123.             System.out.println("Sleep Interrupted");
  124.           }
  125.        
  126.         killALData();
  127.         AL.destroy();
  128.       }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement