Exemple #1
0
 /** Deletes any sources and clears out the sound store contents */
 public void clear() {
   if (sources != null && AL.isCreated()) {
     for (int i = 0; i < sourceCount; i++) {
       int src = sources.get(i);
       AL10.alSourcei(src, AL10.AL_BUFFER, 0);
       AL10.alSourceStop(src);
       AL10.alDeleteSources(src);
     }
   }
   store = new SoundStore();
 }
Exemple #2
0
  /**
   * Stops and releases all sources, clears each of the specified Audio buffers, destroys the OpenAL
   * context, and resets SoundStore for future use.
   *
   * <p>Calling SoundStore.get().init() will re-initialize the OpenAL context after a call to
   * destroyOpenAL (Note: AudioLoader.getXXX calls init for you).
   *
   * @author davedes (http://slick.ninjacave.com/forum/viewtopic.php?t=3920)
   */
  private static void destroyOpenAL() {
    if (!trackExists()) return;
    stop();

    try {
      // get Music object's (private) Audio object reference
      Field sound = player.getClass().getDeclaredField("sound");
      sound.setAccessible(true);
      Audio audio = (Audio) (sound.get(player));

      // first clear the sources allocated by SoundStore
      int max = SoundStore.get().getSourceCount();
      IntBuffer buf = BufferUtils.createIntBuffer(max);
      for (int i = 0; i < max; i++) {
        int source = SoundStore.get().getSource(i);
        buf.put(source);

        // stop and detach any buffers at this source
        AL10.alSourceStop(source);
        AL10.alSourcei(source, AL10.AL_BUFFER, 0);
      }
      buf.flip();
      AL10.alDeleteSources(buf);
      int exc = AL10.alGetError();
      if (exc != AL10.AL_NO_ERROR) {
        throw new SlickException("Could not clear SoundStore sources, err: " + exc);
      }

      // delete any buffer data stored in memory, too...
      if (audio != null && audio.getBufferID() != 0) {
        buf = BufferUtils.createIntBuffer(1).put(audio.getBufferID());
        buf.flip();
        AL10.alDeleteBuffers(buf);
        exc = AL10.alGetError();
        if (exc != AL10.AL_NO_ERROR) {
          throw new SlickException(
              "Could not clear buffer " + audio.getBufferID() + ", err: " + exc);
        }
      }

      // clear OpenAL
      AL.destroy();

      // reset SoundStore so that next time we create a Sound/Music, it will reinit
      SoundStore.get().clear();

      player = null;
    } catch (Exception e) {
      ErrorHandler.error("Failed to destroy OpenAL.", e, true);
    }
  }
Exemple #3
0
  public void dispose() {
    if (noDevice) return;
    for (int i = 0, n = allSources.size; i < n; i++) {
      int sourceID = allSources.get(i);
      int state = alGetSourcei(sourceID, AL_SOURCE_STATE);
      if (state != AL_STOPPED) alSourceStop(sourceID);
      alDeleteSources(sourceID);
    }

    sourceToSoundId.clear();
    soundIdToSource.clear();

    AL.destroy();
    while (AL.isCreated()) {
      try {
        Thread.sleep(10);
      } catch (InterruptedException e) {
      }
    }
  }
 /**
  * void killALData()
  *
  * <p>We have allocated memory for our buffers and sources which needs to be returned to the
  * system. This function frees that memory.
  */
 void killALData() {
   AL10.alDeleteSources(source);
   AL10.alDeleteBuffers(buffer);
 }
  /** 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 #6
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);
      }
    }
  }
Exemple #7
0
 public void finalize() {
   AL10.alDeleteSources(this.src);
   AL10.alDeleteBuffers(this.buf);
 }