예제 #1
0
  @Override
  public void update(float dt) {
    super.update(dt);

    Vector3f pos = getPosition();
    Vector3f vel = getCurrVelocity();
    Vector3f up = getUpVector();
    Vector3f dir = getFacingVector();

    AL10.alListener3f(AL10.AL_POSITION, pos.x, pos.y, pos.z);
    AL10.alListener3f(AL10.AL_VELOCITY, vel.x, vel.y, vel.z);

    orientBuf.rewind();
    orientBuf.put(dir.x);
    orientBuf.put(dir.y);
    orientBuf.put(dir.z);
    orientBuf.put(up.x);
    orientBuf.put(up.y);
    orientBuf.put(up.z);
    orientBuf.rewind();

    AL10.alListener(AL10.AL_ORIENTATION, orientBuf);
  }
예제 #2
0
  /** 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();
  }