Exemple #1
0
  int obtainSource(boolean isMusic) {
    if (noDevice) return 0;
    for (int i = 0, n = idleSources.size; i < n; i++) {
      int sourceId = idleSources.get(i);
      int state = alGetSourcei(sourceId, AL_SOURCE_STATE);
      if (state != AL_PLAYING && state != AL_PAUSED) {
        if (isMusic) {
          idleSources.removeIndex(i);
        } else {
          if (sourceToSoundId.containsKey(sourceId)) {
            long soundId = sourceToSoundId.get(sourceId);
            sourceToSoundId.remove(sourceId);
            soundIdToSource.remove(soundId);
          }

          long soundId = nextSoundId++;
          sourceToSoundId.put(sourceId, soundId);
          soundIdToSource.put(soundId, sourceId);
        }
        alSourceStop(sourceId);
        alSourcei(sourceId, AL_BUFFER, 0);
        AL10.alSourcef(sourceId, AL10.AL_GAIN, 1);
        AL10.alSourcef(sourceId, AL10.AL_PITCH, 1);
        AL10.alSource3f(sourceId, AL10.AL_POSITION, 0, 0, 1f);
        return sourceId;
      }
    }
    return -1;
  }
Exemple #2
0
  /**
   * Play the specified buffer as music (i.e. use the music channel)
   *
   * @param buffer The buffer to be played
   * @param pitch The pitch to play the music at
   * @param gain The gaing to play the music at
   * @param loop True if we should loop the music
   */
  void playAsMusic(int buffer, float pitch, float gain, boolean loop) {
    paused = false;

    setMOD(null);

    if (soundWorks) {
      if (currentMusic != -1) {
        AL10.alSourceStop(sources.get(0));
      }

      getMusicSource();

      AL10.alSourcei(sources.get(0), AL10.AL_BUFFER, buffer);
      AL10.alSourcef(sources.get(0), AL10.AL_PITCH, pitch);
      AL10.alSourcei(sources.get(0), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);

      currentMusic = sources.get(0);

      if (!music) {
        pauseLoop();
      } else {
        AL10.alSourcePlay(sources.get(0));
      }
    }
  }
  public void execute() {
    // Initialize OpenAL and clear the error bit.
    try {
      AL.create();
    } catch (LWJGLException le) {
      le.printStackTrace();
      return;
    }
    AL10.alGetError();

    // Load the wav data.
    if (loadALData() == AL10.AL_FALSE) {
      System.out.println("Error loading data.");
      return;
    }

    setListenerValues();

    // Loop.
    System.out.println("OpenAL Tutorial 1 - Single Static Source");
    System.out.println("[Menu]");
    System.out.println("p - Play the sample.");
    System.out.println("s - Stop the sample.");
    System.out.println("h - Pause the sample.");
    System.out.println("q - Quit the program.");
    char c = ' ';
    Scanner stdin = new Scanner(System.in);
    while (c != 'q') {
      try {
        System.out.print("Input: ");
        c = (char) stdin.nextLine().charAt(0);
      } catch (Exception ex) {
        c = 'q';
      }

      switch (c) {
          // Pressing 'p' will begin playing the sample.
        case 'p':
          AL10.alSourcePlay(source.get(0));
          break;

          // Pressing 's' will stop the sample from playing.
        case 's':
          AL10.alSourceStop(source.get(0));
          break;

          // Pressing 'h' will pause the sample.
        case 'h':
          AL10.alSourcePause(source.get(0));
          break;
      }
      ;
    }
    killALData();
    AL.destroy();
  }
Exemple #4
0
 void freeSource(int sourceID) {
   if (noDevice) return;
   alSourceStop(sourceID);
   alSourcei(sourceID, AL_BUFFER, 0);
   if (sourceToSoundId.containsKey(sourceID)) {
     long soundId = sourceToSoundId.remove(sourceID);
     soundIdToSource.remove(soundId);
   }
   idleSources.add(sourceID);
 }
Exemple #5
0
  int playAsSoundAt(
      int buffer,
      int source,
      float pitch,
      float gain,
      boolean loop,
      float x,
      float y,
      float z,
      float vx,
      float vy,
      float vz,
      boolean localsound) {
    gain *= soundVolume;
    if (gain == 0) {
      gain = 0.001f;
    }
    if (soundWorks) {
      if (sounds) {

        if (source == -1) {
          return -1;
        }

        AL10.alSourceStop(sources.get(source));
        AL10.alSourcef(sources.get(source), AL10.AL_REFERENCE_DISTANCE, minDistance);
        if (localsound) {
          AL10.alSourcei(sources.get(source), AL10.AL_SOURCE_RELATIVE, AL10.AL_TRUE);
          AL10.alSourcef(sources.get(source), AL10.AL_ROLLOFF_FACTOR, 0.0f);
        } else {
          AL10.alSourcei(sources.get(source), AL10.AL_SOURCE_RELATIVE, AL10.AL_FALSE);
          AL10.alSourcef(sources.get(source), AL10.AL_ROLLOFF_FACTOR, rollOff);
        }
        AL10.alSourcei(sources.get(source), AL10.AL_BUFFER, buffer);
        AL10.alSourcef(sources.get(source), AL10.AL_PITCH, pitch);
        AL10.alSourcef(sources.get(source), AL10.AL_GAIN, gain);
        AL10.alSourcei(sources.get(source), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);

        sourcePos.clear();
        sourceVel.clear();
        sourceVel.put(new float[] {vx, vy, vz});
        sourcePos.put(new float[] {x, y, z});
        sourcePos.flip();
        sourceVel.flip();
        AL10.alSource(sources.get(source), AL10.AL_POSITION, sourcePos);
        AL10.alSource(sources.get(source), AL10.AL_VELOCITY, sourceVel);

        AL10.alSourcePlay(sources.get(source));

        return source;
      }
    }

    return -1;
  }
Exemple #6
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 #7
0
 void stopSourcesWithBuffer(int bufferID) {
   if (noDevice) return;
   for (int i = 0, n = idleSources.size; i < n; i++) {
     int sourceID = idleSources.get(i);
     if (alGetSourcei(sourceID, AL_BUFFER) == bufferID) {
       if (sourceToSoundId.containsKey(sourceID)) {
         long soundId = sourceToSoundId.remove(sourceID);
         soundIdToSource.remove(soundId);
       }
       alSourceStop(sourceID);
     }
   }
 }
Exemple #8
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 #9
0
  /**
   * Get the Sound based on a specified OGG file
   *
   * @param ref The reference to the OGG file in the classpath
   * @return The Sound read from the OGG file
   * @throws IOException Indicates a failure to load the OGG
   */
  public Audio getOggStream(URL ref) throws IOException {
    if (!soundWorks) {
      return new NullAudio();
    }

    setMOD(null);
    setStream(null);

    if (currentMusic != -1) {
      AL10.alSourceStop(sources.get(0));
    }

    getMusicSource();
    currentMusic = sources.get(0);

    return new StreamSound(new OpenALStreamPlayer(currentMusic, ref));
  }
Exemple #10
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) {
      }
    }
  }
Exemple #11
0
  /**
   * Play the specified buffer as a sound effect with the specified pitch and gain.
   *
   * @param buffer The ID of the buffer to play
   * @param pitch The pitch to play at
   * @param gain The gain to play at
   * @param loop True if the sound should loop
   * @param x The x position to play the sound from
   * @param y The y position to play the sound from
   * @param z The z position to play the sound from
   * @return source The source that will be used
   */
  int playAsSoundAt(int buffer, float pitch, float gain, boolean loop, float x, float y, float z) {
    gain *= soundVolume;
    if (gain == 0) {
      gain = 0.001f;
    }
    if (soundWorks) {
      if (sounds) {
        int nextSource = findFreeSource();
        if (nextSource == -1) {
          return -1;
        }

        AL10.alSourceStop(sources.get(nextSource));

        AL10.alSourcei(sources.get(nextSource), AL10.AL_BUFFER, buffer);
        AL10.alSourcef(sources.get(nextSource), AL10.AL_PITCH, pitch);
        AL10.alSourcef(sources.get(nextSource), AL10.AL_GAIN, gain);
        AL10.alSourcei(
            sources.get(nextSource), AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);

        sourcePos.clear();
        sourceVel.clear();
        sourceVel.put(new float[] {0, 0, 0});
        sourcePos.put(new float[] {x, y, z});
        sourcePos.flip();
        sourceVel.flip();
        AL10.alSourcefv(sources.get(nextSource), AL10.AL_POSITION, sourcePos);
        AL10.alSourcefv(sources.get(nextSource), AL10.AL_VELOCITY, sourceVel);

        AL10.alSourcePlay(sources.get(nextSource));

        return nextSource;
      }
    }

    return -1;
  }
Exemple #12
0
 /**
  * Stop a playing sound identified by the ID returned from playing. This utility method should
  * only be used when needing to stop sound effects that may have been played more than once and
  * need to be explicitly stopped.
  *
  * @param id The ID of the underlying OpenAL source as returned from playAsSoundEffect
  */
 public void stopSoundEffect(int id) {
   AL10.alSourceStop(id);
 }
Exemple #13
0
 /**
  * Stop a particular sound source
  *
  * @param index The index of the source to stop
  */
 void stopSource(int index) {
   AL10.alSourceStop(sources.get(index));
 }
Exemple #14
0
 public void stopSound(long soundId) {
   if (!soundIdToSource.containsKey(soundId)) return;
   int sourceId = soundIdToSource.get(soundId);
   alSourceStop(sourceId);
 }
  /** 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 #16
0
 public void stop() {
   AL10.alSourceStop(this.src);
   this.playing = false;
 }