示例#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;
  }
示例#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;
    gain *= musicVolume;
    if (gain == 0) {
      gain = 0.001f;
    }

    setMOD(null);

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

      getMusicSource();

      AL10.alSourcef(sources.get(0), AL10.AL_GAIN, gain);
      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));
      }
    }
  }
示例#3
0
 public void setVolume(float volume) {
   this.baseVol = volume;
   if (this.type == 0)
     AL10.alSourcef(this.src, AL10.AL_GAIN, volume * ((float) Globals.getSoundVolume() / 100.0f));
   else
     AL10.alSourcef(this.src, AL10.AL_GAIN, volume * ((float) Globals.getMusicVolume() / 100.0f));
 }
示例#4
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;
  }
示例#5
0
 public void enter(GameContainer gc, StateBasedGame sbg) throws SlickException {
   // The listener is reset
   AlUtils.resetAlListener();
   // the orientation is changed
   AlUtils.setAlListenerOrientation(0.0f, 0.0f, -1.0f, -1.0f, 0.0f, 0.0f);
   // we set sound context
   AL10.alDistanceModel(AL11.AL_EXPONENT_DISTANCE);
   AL10.alDopplerFactor(0.8f);
   // We play the two sounds since we enter
   sonG.loop(1f, 1f, 0f, 0f, 0f);
   sonD.loop(1f, 1f, 0f, 0f, 0f);
   AL10.alSourcef(sonG.getIndex(), AL10.AL_ROLLOFF_FACTOR, 1.5f);
   AL10.alSourcef(sonD.getIndex(), AL10.AL_ROLLOFF_FACTOR, 1.5f);
   // We play the beginning explanation sound
   enterSound.play();
 }
示例#6
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;
  }
示例#7
0
  public void setSoundPan(long soundId, float pan, float volume) {
    if (!soundIdToSource.containsKey(soundId)) return;
    int sourceId = soundIdToSource.get(soundId);

    AL10.alSource3f(
        sourceId,
        AL10.AL_POSITION,
        MathUtils.cos((pan - 1) * MathUtils.PI / 2),
        0,
        MathUtils.sin((pan + 1) * MathUtils.PI / 2));
    AL10.alSourcef(sourceId, AL10.AL_GAIN, volume);
  }
示例#8
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;
 }
示例#9
0
  /**
   * Set the music volume of the current playing music. Does NOT affect the global volume
   *
   * @param volume The volume for the current playing music
   */
  public void setCurrentMusicVolume(float volume) {
    if (volume < 0) {
      volume = 0;
    }
    if (volume > 1) {
      volume = 1;
    }

    if (soundWorks) {
      lastCurrentMusicVolume = volume;
      AL10.alSourcef(sources.get(0), AL10.AL_GAIN, lastCurrentMusicVolume * musicVolume);
    }
  }
示例#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;
  }
示例#11
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;
 }
示例#12
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;
    }
  }
示例#13
0
 public void setVolume(float volume) {
   this.volume = volume;
   if (sourceID != -1) alSourcef(sourceID, AL_GAIN, volume);
 }
示例#14
0
 /**
  * Set the pitch at which the current music is being played
  *
  * @param pitch The pitch at which the current music is being played
  */
 public void setMusicPitch(float pitch) {
   if (soundWorks) {
     AL10.alSourcef(sources.get(0), AL10.AL_PITCH, pitch);
   }
 }
示例#15
0
 public void setSoundPitch(long soundId, float pitch) {
   if (!soundIdToSource.containsKey(soundId)) return;
   int sourceId = soundIdToSource.get(soundId);
   AL10.alSourcef(sourceId, AL10.AL_PITCH, pitch);
 }
示例#16
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);
  }
示例#17
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();
  }
示例#18
0
 public void setSoundGain(long soundId, float volume) {
   if (!soundIdToSource.containsKey(soundId)) return;
   int sourceId = soundIdToSource.get(soundId);
   AL10.alSourcef(sourceId, AL10.AL_GAIN, volume);
 }
示例#19
0
 public void setPitch(float pitch) {
   AL10.alSourcef(this.src, AL10.AL_PITCH, pitch);
 }