Beispiel #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));
      }
    }
  }
Beispiel #2
0
  /** Blocks until some of the data could be buffered. */
  private int fillBuffer(byte[] data, int offset, int length) {
    int written = Math.min(bufferSize, length);

    outer:
    while (true) {
      int buffers = alGetSourcei(sourceID, AL_BUFFERS_PROCESSED);
      while (buffers-- > 0) {
        int bufferID = alSourceUnqueueBuffers(sourceID);
        if (bufferID == AL_INVALID_VALUE) break;
        renderedSeconds += secondsPerBuffer;

        tempBuffer.clear();
        tempBuffer.put(data, offset, written).flip();
        alBufferData(bufferID, format, tempBuffer, sampleRate);

        alSourceQueueBuffers(sourceID, bufferID);
        break outer;
      }
      // Wait for buffer to be free.
      try {
        Thread.sleep((long) (1000 * secondsPerBuffer / bufferCount));
      } catch (InterruptedException ignored) {
      }
    }

    // A buffer underflow will cause the source to stop.
    if (!isPlaying || alGetSourcei(sourceID, AL_SOURCE_STATE) != AL_PLAYING) {
      alSourcePlay(sourceID);
      isPlaying = true;
    }

    return written;
  }
Beispiel #3
0
 void resumeSourcesWithBuffer(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 (alGetSourcei(sourceID, AL_SOURCE_STATE) == AL_PAUSED) alSourcePlay(sourceID);
     }
   }
 }
  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();
  }
Beispiel #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;
  }
Beispiel #6
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;
 }
Beispiel #7
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;
 }
Beispiel #8
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;
    }
  }
Beispiel #9
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;
  }
Beispiel #10
0
 /** Restart the music loop that is currently paused */
 public void restartLoop() {
   if ((music) && (soundWorks) && (currentMusic != -1)) {
     paused = false;
     AL10.alSourcePlay(currentMusic);
   }
 }
Beispiel #11
0
 public void resumeSound(long soundId) {
   if (!soundIdToSource.containsKey(soundId)) return;
   int sourceId = soundIdToSource.get(soundId);
   if (alGetSourcei(sourceId, AL_SOURCE_STATE) == AL_PAUSED) alSourcePlay(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();
  }
Beispiel #13
0
 public void play() {
   if (!playing) AL10.alSourcePlay(this.src);
   this.playing = this.loop;
 }