Пример #1
0
  /**
   * Get the Sound based on a specified OGG file
   *
   * @param ref The reference to the OGG file in the classpath
   * @param in The stream to the OGG to load
   * @return The Sound read from the OGG file
   * @throws IOException Indicates a failure to load the OGG
   */
  public Audio getOgg(String ref, InputStream in) throws IOException {
    if (!soundWorks) {
      return new NullAudio();
    }
    if (!inited) {
      throw new RuntimeException(
          "Can't load sounds until SoundStore is init(). Use the container init() method.");
    }
    if (deferred) {
      return new DeferredSound(ref, in, DeferredSound.OGG);
    }

    int buffer = -1;

    if (loaded.get(ref) != null) {
      buffer = ((Integer) loaded.get(ref)).intValue();
    } else {
      try {
        IntBuffer buf = BufferUtils.createIntBuffer(1);

        OggDecoder decoder = new OggDecoder();
        OggData ogg = decoder.getData(in);

        AL10.alGenBuffers(buf);
        AL10.alBufferData(
            buf.get(0),
            ogg.channels > 1 ? AL10.AL_FORMAT_STEREO16 : AL10.AL_FORMAT_MONO16,
            ogg.data,
            ogg.rate);

        loaded.put(ref, new Integer(buf.get(0)));

        buffer = buf.get(0);
      } catch (Exception e) {
        Log.error(e);
        Log.info("Failed to load: " + ref + " - " + e.getMessage());
        throw new IOException("Unable to load: " + ref);
      }
    }

    if (buffer == -1) {
      throw new IOException("Unable to load: " + ref);
    }

    return new AudioImpl(this, buffer);
  }
Пример #2
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;
    }
  }
Пример #3
0
  /**
   * Get the Sound based on a specified AIF file
   *
   * @param ref The reference to the AIF file in the classpath
   * @param in The stream to the AIF to load
   * @return The Sound read from the AIF file
   * @throws IOException Indicates a failure to load the AIF
   */
  public Audio getAIF(String ref, InputStream in) throws IOException {
    in = new BufferedInputStream(in);

    if (!soundWorks) {
      return new NullAudio();
    }
    if (!inited) {
      throw new RuntimeException(
          "Can't load sounds until SoundStore is init(). Use the container init() method.");
    }
    if (deferred) {
      return new DeferredSound(ref, in, DeferredSound.AIF);
    }

    int buffer = -1;

    if (loaded.get(ref) != null) {
      buffer = ((Integer) loaded.get(ref)).intValue();
    } else {
      try {
        IntBuffer buf = BufferUtils.createIntBuffer(1);

        AiffData data = AiffData.create(in);
        AL10.alGenBuffers(buf);
        AL10.alBufferData(buf.get(0), data.format, data.data, data.samplerate);

        loaded.put(ref, new Integer(buf.get(0)));
        buffer = buf.get(0);
      } catch (Exception e) {
        Log.error(e);
        IOException x = new IOException("Failed to load: " + ref);
        x.initCause(e);

        throw x;
      }
    }

    if (buffer == -1) {
      throw new IOException("Unable to load: " + ref);
    }

    return new AudioImpl(this, buffer);
  }
Пример #4
0
  /**
   * 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;
  }
Пример #5
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();
  }
Пример #6
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);
  }