Exemple #1
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));
      }
    }
  }
Exemple #2
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 #3
0
 @Override
 public long loop(float volume) {
   if (audio.noDevice) return 0;
   int sourceID = audio.obtainSource(false);
   if (sourceID == -1) return -1;
   long soundId = audio.getSoundId(sourceID);
   alSourcei(sourceID, AL_BUFFER, bufferID);
   alSourcei(sourceID, AL_LOOPING, AL_TRUE);
   alSourcef(sourceID, AL_GAIN, volume);
   alSourcePlay(sourceID);
   return soundId;
 }
Exemple #4
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 #5
0
 public long play(float volume) {
   if (audio.noDevice) return 0;
   int sourceID = audio.obtainSource(false);
   if (sourceID == -1) {
     // Attempt to recover by stopping the least recently played sound
     audio.retain(this, true);
     sourceID = audio.obtainSource(false);
   } else audio.retain(this, false);
   // In case it still didn't work
   if (sourceID == -1) return -1;
   long soundId = audio.getSoundId(sourceID);
   alSourcei(sourceID, AL_BUFFER, bufferID);
   alSourcei(sourceID, AL_LOOPING, AL_FALSE);
   alSourcef(sourceID, AL_GAIN, volume);
   alSourcePlay(sourceID);
   return soundId;
 }
Exemple #6
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 #7
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 #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
 void freeBuffer(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);
       alSourcei(sourceID, AL_BUFFER, 0);
     }
   }
 }
Exemple #10
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 #11
0
  public void writeSamples(byte[] data, int offset, int length) {
    if (length < 0) throw new IllegalArgumentException("length cannot be < 0.");

    if (sourceID == -1) {
      sourceID = audio.obtainSource(true);
      if (sourceID == -1) return;
      if (buffers == null) {
        buffers = BufferUtils.createIntBuffer(bufferCount);
        alGenBuffers(buffers);
        if (alGetError() != AL_NO_ERROR)
          throw new GdxRuntimeException("Unabe to allocate audio buffers.");
      }
      alSourcei(sourceID, AL_LOOPING, AL_FALSE);
      alSourcef(sourceID, AL_GAIN, volume);
      // Fill initial buffers.
      int queuedBuffers = 0;
      for (int i = 0; i < bufferCount; i++) {
        int bufferID = buffers.get(i);
        int written = Math.min(bufferSize, length);
        tempBuffer.clear();
        tempBuffer.put(data, offset, written).flip();
        alBufferData(bufferID, format, tempBuffer, sampleRate);
        alSourceQueueBuffers(sourceID, bufferID);
        length -= written;
        offset += written;
        queuedBuffers++;
      }
      // Queue rest of buffers, empty.
      tempBuffer.clear().flip();
      for (int i = queuedBuffers; i < bufferCount; i++) {
        int bufferID = buffers.get(i);
        alBufferData(bufferID, format, tempBuffer, sampleRate);
        alSourceQueueBuffers(sourceID, bufferID);
      }
      alSourcePlay(sourceID);
      isPlaying = true;
    }

    while (length > 0) {
      int written = fillBuffer(data, offset, length);
      length -= written;
      offset += written;
    }
  }
  /**
   * 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 #13
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);
  }
Exemple #14
0
 public void setSoundLooping(long soundId, boolean looping) {
   if (!soundIdToSource.containsKey(soundId)) return;
   int sourceId = soundIdToSource.get(soundId);
   alSourcei(sourceId, AL10.AL_LOOPING, looping ? AL10.AL_TRUE : AL10.AL_FALSE);
 }
  /** 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 setLoop(boolean loop) {
   AL10.alSourcei(this.src, AL10.AL_LOOPING, loop ? AL10.AL_TRUE : AL10.AL_FALSE);
   this.loop = loop;
 }
Exemple #17
0
 public BufferedSound(StaticBuffer buffer) {
   super(buffer);
   AL10.alSourcei(source, AL10.AL_BUFFER, buffer.getOpenALId());
   AudioContext.checkALError();
 }
Exemple #18
0
 /** Sets the looping status of this sound. A looping sound will loop until instructed to stop. */
 public void setLooping(boolean shouldLoop) {
   AL10.alSourcei(source, AL10.AL_LOOPING, shouldLoop ? AL10.AL_TRUE : AL10.AL_FALSE);
 }