/**
   * boolean LoadALData()
   *
   * <p>This function will load our sample data from the disk using the Alut utility and send the
   * data into OpenAL as a buffer. A source is then also created to play that buffer.
   */
  int loadALData() {
    // Load wav data into a buffer.
    AL10.alGenBuffers(buffer);

    if (AL10.alGetError() != AL10.AL_NO_ERROR) return AL10.AL_FALSE;

    // Loads the wave file from your file system
    /*java.io.FileInputStream fin = null;
    try {
      fin = new java.io.FileInputStream("FancyPants.wav");
    } catch (java.io.FileNotFoundException ex) {
      ex.printStackTrace();
      return AL10.AL_FALSE;
    }
    WaveData waveFile = WaveData.create(fin);
    try {
      fin.close();
    } catch (java.io.IOException ex) {
    }*/

    // Loads the wave file from this class's package in your classpath
    WaveData waveFile = WaveData.create("FancyPants.wav");

    AL10.alBufferData(buffer.get(0), waveFile.format, waveFile.data, waveFile.samplerate);
    waveFile.dispose();

    // Bind the buffer with the source.
    AL10.alGenSources(source);

    if (AL10.alGetError() != AL10.AL_NO_ERROR) return AL10.AL_FALSE;

    AL10.alSourcei(source.get(0), AL10.AL_BUFFER, buffer.get(0));
    AL10.alSourcef(source.get(0), AL10.AL_PITCH, 1.0f);
    AL10.alSourcef(source.get(0), AL10.AL_GAIN, 1.0f);
    AL10.alSource(source.get(0), AL10.AL_POSITION, sourcePos);
    AL10.alSource(source.get(0), AL10.AL_VELOCITY, sourceVel);

    // Do another error check and return.
    if (AL10.alGetError() == AL10.AL_NO_ERROR) return AL10.AL_TRUE;

    return AL10.AL_FALSE;
  }
Exemple #2
0
  /** Initialise the sound effects stored. This must be called before anything else will work */
  public void init() {
    if (inited) {
      return;
    }
    Log.info("Initialising sounds..");
    inited = true;

    AccessController.doPrivileged(
        new PrivilegedAction() {
          public Object run() {
            try {
              AL.create();
              soundWorks = true;
              sounds = true;
              music = true;
              Log.info("- Sound works");
            } catch (Exception e) {
              Log.error("Sound initialisation failure.");
              Log.error(e);
              soundWorks = false;
              sounds = false;
              music = false;
            }

            return null;
          }
        });

    if (soundWorks) {
      sourceCount = 0;
      sources = BufferUtils.createIntBuffer(maxSources);
      while (AL10.alGetError() == AL10.AL_NO_ERROR) {
        IntBuffer temp = BufferUtils.createIntBuffer(1);

        try {
          AL10.alGenSources(temp);

          if (AL10.alGetError() == AL10.AL_NO_ERROR) {
            sourceCount++;
            sources.put(temp.get(0));
            if (sourceCount > maxSources - 1) {
              break;
            }
          }
        } catch (OpenALException e) {
          break;
        }
      }
      Log.info("- " + sourceCount + " OpenAL source available");

      if (AL10.alGetError() != AL10.AL_NO_ERROR) {
        sounds = false;
        music = false;
        soundWorks = false;
        Log.error("- AL init failed");
      } else {
        FloatBuffer listenerOri =
            BufferUtils.createFloatBuffer(6).put(new float[] {0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f});
        FloatBuffer listenerVel =
            BufferUtils.createFloatBuffer(3).put(new float[] {0.0f, 0.0f, 0.0f});
        FloatBuffer listenerPos =
            BufferUtils.createFloatBuffer(3).put(new float[] {0.0f, 0.0f, 0.0f});
        listenerPos.flip();
        listenerVel.flip();
        listenerOri.flip();
        AL10.alListenerfv(AL10.AL_POSITION, listenerPos);
        AL10.alListenerfv(AL10.AL_VELOCITY, listenerVel);
        AL10.alListenerfv(AL10.AL_ORIENTATION, listenerOri);

        Log.info("- Sounds source generated");
      }
    }
  }
Exemple #3
0
  private void bIM() {
    IntBuffer localIntBuffer = BufferUtils.createIntBuffer(64);

    for (int i = 0; i < 64; i++) {
      try {
        int m = AL10.alGenSources();
        check();
        localIntBuffer.put(m);
      } catch (Exception localException1) {
        break;
      }
    }

    this.gpB = localIntBuffer.position();
    localIntBuffer.position(0).limit(this.gpB);
    try {
      AL10.alDeleteSources(localIntBuffer);
      check();
    } catch (OpenALException localOpenALException1) {
      K.warn("Problème au alDeleteSources.", localOpenALException1);
    }

    if (!ALC10.alcIsExtensionPresent(
        ALC10.alcGetContextsDevice(ALC10.alcGetCurrentContext()), "ALC_EXT_EFX")) {
      this.gpD = 0;
      this.gpC = 0;
    } else {
      localIntBuffer.limit(64);
      for (int j = 0; j < 64; j++) {
        try {
          int n = EFX10.alGenEffects();
          check();
          localIntBuffer.put(n);
        } catch (Exception localException2) {
          break;
        }
      }

      this.gpD = localIntBuffer.position();
      localIntBuffer.position(0).limit(this.gpD);
      try {
        EFX10.alDeleteEffects(localIntBuffer);
        check();
      } catch (OpenALException localOpenALException2) {
        K.warn("Problème au alDeleteEffects.", localOpenALException2);
      }

      localIntBuffer.limit(64);
      for (int k = 0; k < 64; k++) {
        try {
          int i1 = EFX10.alGenAuxiliaryEffectSlots();
          check();
          localIntBuffer.put(i1);
        } catch (Exception localException3) {
          break;
        }
      }

      this.gpC = localIntBuffer.position();
      localIntBuffer.position(0).limit(this.gpC);
      try {
        EFX10.alDeleteAuxiliaryEffectSlots(localIntBuffer);
        check();
      } catch (OpenALException localOpenALException3) {
        K.warn("Problème au alDeleteAuxiliaryEffectSlots.", localOpenALException3);
      }
    }
  }
  /** Runs the actual test, using supplied arguments */
  protected void execute(String[] args) {
    if (args.length < 1) {
      System.out.println("no argument supplied, assuming Footsteps.wav");
      args = new String[] {"Footsteps.wav"};
    }

    try {
      setDisplayMode();
      Display.create();
    } catch (Exception e) {
      e.printStackTrace();
    }

    int lastError;
    Vector3f sourcePosition = new Vector3f();
    Vector3f listenerPosition = new Vector3f();

    // initialize keyboard
    try {
      Keyboard.create();
    } catch (Exception e) {
      e.printStackTrace();
      exit(-1);
    }

    // create 1 buffer and 1 source
    IntBuffer buffers = BufferUtils.createIntBuffer(1);
    IntBuffer sources = BufferUtils.createIntBuffer(1);

    // al generate buffers and sources
    buffers.position(0).limit(1);
    AL10.alGenBuffers(buffers);
    if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) {
      exit(lastError);
    }

    sources.position(0).limit(1);
    AL10.alGenSources(sources);
    if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) {
      exit(lastError);
    }

    // load wave data
    WaveData wavefile = WaveData.create(args[0]);

    // copy to buffers
    AL10.alBufferData(buffers.get(0), wavefile.format, wavefile.data, wavefile.samplerate);
    if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) {
      exit(lastError);
    }

    // unload file again
    wavefile.dispose();

    // set up source input
    AL10.alSourcei(sources.get(0), AL10.AL_BUFFER, buffers.get(0));
    if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) {
      exit(lastError);
    }

    AL10.alSourcef(sources.get(0), AL10.AL_REFERENCE_DISTANCE, 1024.0f);
    AL10.alSourcef(sources.get(0), AL10.AL_ROLLOFF_FACTOR, 0.5f);

    // lets loop the sound
    AL10.alSourcei(sources.get(0), AL10.AL_LOOPING, AL10.AL_TRUE);
    if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) {
      exit(lastError);
    }

    // play source 0
    AL10.alSourcePlay(sources.get(0));
    if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) {
      exit(lastError);
    }

    System.out.println(
        "Move source with arrow keys\nMove listener with right shift and arrowkeys\nExit with ESC");

    while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
      Display.update();

      Keyboard.poll();
      if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
        if (Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) {
          listenerPosition.x -= MOVEMENT;
          AL10.alListener3f(
              AL10.AL_POSITION, listenerPosition.x, listenerPosition.y, listenerPosition.z);
          System.out.println("listenerx: " + listenerPosition.x);
        } else {
          sourcePosition.x -= MOVEMENT;
          AL10.alSource3f(
              sources.get(0),
              AL10.AL_POSITION,
              sourcePosition.x,
              sourcePosition.y,
              sourcePosition.z);
          System.out.println("sourcex: " + sourcePosition.x);
        }
      }
      if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
        if (Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) {
          listenerPosition.x += MOVEMENT;
          AL10.alListener3f(
              AL10.AL_POSITION, listenerPosition.x, listenerPosition.y, listenerPosition.z);
          System.out.println("listenerx: " + listenerPosition.x);
        } else {
          sourcePosition.x += MOVEMENT;
          AL10.alSource3f(
              sources.get(0),
              AL10.AL_POSITION,
              sourcePosition.x,
              sourcePosition.y,
              sourcePosition.z);
          System.out.println("sourcex: " + sourcePosition.x);
        }
      }

      if (Display.isCloseRequested()) {
        break;
      }

      try {
        Thread.sleep(100);
      } catch (InterruptedException inte) {
      }
    }

    // stop source 0
    AL10.alSourceStop(sources.get(0));
    if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) {
      exit(lastError);
    }

    // delete buffers and sources
    sources.position(0).limit(1);
    AL10.alDeleteSources(sources);
    if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) {
      exit(lastError);
    }

    buffers.position(0).limit(1);
    AL10.alDeleteBuffers(buffers);
    if ((lastError = AL10.alGetError()) != AL10.AL_NO_ERROR) {
      exit(lastError);
    }

    // shutdown
    alExit();
  }
Exemple #5
0
  public Sound(String filename, int type, boolean loop, float volume, float pitch) {
    String[] split = filename.split("/");
    System.out.println("loading " + split[split.length - 1]);

    try {
      AL10.alGetError();
    } catch (UnsatisfiedLinkError e) {
      Logger.info("Seems no AL sound device has been created... creating now", "Utility.loadWav");

      try {
        AL.create();
      } catch (LWJGLException e1) {

        Logger.log(e1, "Utility.loadWav", "Unable to create AL sound device");
        return;
      }
    }

    this.buf = AL10.alGenBuffers();

    if (AL10.alGetError() != AL10.AL_NO_ERROR) {
      Logger.error(Utility.getALErrorString(AL10.alGetError()), "Sound.Sound");
    }

    if (filename.endsWith(".wav")) {
      try {

        WaveData waveFile = WaveData.create(new BufferedInputStream(new FileInputStream(filename)));
        System.out.println(waveFile == null);
        AL10.alBufferData(this.buf, waveFile.format, waveFile.data, waveFile.samplerate);
        waveFile.dispose();

      } catch (FileNotFoundException e) {
        Logger.log(e, "Sound.Sound");
        return;
      }
    } else if (filename.endsWith(".ogg") || filename.endsWith(".vorbis")) {
      try {
        OggDecoder oggDecoder = new OggDecoder();
        OggData oggData =
            oggDecoder.getData(new BufferedInputStream(new FileInputStream(filename)));
        AL10.alBufferData(
            this.buf,
            oggData.channels > 1 ? AL10.AL_FORMAT_STEREO16 : AL10.AL_FORMAT_MONO16,
            oggData.data,
            oggData.rate);
      } catch (FileNotFoundException e) {
        Logger.log(e, "Sound.Sound");
        return;
      } catch (IOException e) {
        Logger.log(e, "Sound.Sound");
        return;
      }
    }

    this.src = AL10.alGenSources();

    if (AL10.alGetError() != AL10.AL_NO_ERROR) {
      Logger.error(Utility.getALErrorString(AL10.alGetError()), "Utility.loadWav");
      return;
    }

    AL10.alSourcei(this.src, AL10.AL_BUFFER, this.buf);
    AL10.alSourcef(this.src, AL10.AL_PITCH, pitch);
    AL10.alSourcef(this.src, AL10.AL_GAIN, volume);

    FloatBuffer sourcePos =
        (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[] {0.0f, 0.0f, 0.0f}).rewind();
    FloatBuffer sourceVel =
        (FloatBuffer) BufferUtils.createFloatBuffer(3).put(new float[] {0.0f, 0.0f, 0.0f}).rewind();

    AL10.alSource(this.src, AL10.AL_POSITION, sourcePos);
    AL10.alSource(this.src, AL10.AL_VELOCITY, sourceVel);

    this.type = type;

    this.setLoop(loop);
    this.setPitch(pitch);
    this.setVolume(volume);

    Globals.addVolumeChangedListener(this);
  }