Example #1
1
 public static Audio playWav(String wavFile, boolean loop) {
   try {
     AudioLoader.update(); // JIC
     Audio wav = AudioLoader.getAudio("WAV", ResourceLoader.getResourceAsStream(wavFile));
     sounds.add(wav);
     wav.playAsMusic(1.0f, 1.0f, loop);
     return wav;
   } catch (IOException e) {
     e.printStackTrace();
   }
   return null;
 }
Example #2
1
  /** Initialise resources */
  public void init() {
    try {
      wavEffect =
          AudioLoader.getAudio(
              "WAV",
              ResourceLoader.getResourceAsStream("com/etk2000/SpaceGame/Music/MainMenuMusic.wav"));
      // @param: something, something, loop
      wavEffect.playAsMusic(1.0f, 1.0f, true);
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }

    /*
     * try {
     * // you can play oggs by loading the complete thing into
     * // a sound
     * oggEffect = AudioLoader.getAudio("OGG",
     * ResourceLoader.getResourceAsStream("testdata/restart.ogg"));
     *
     * // or setting up a stream to read from. Note that the argument becomes
     * // a URL here so it can be reopened when the stream is complete. Probably
     * // should have reset the stream by thats not how the original stuff worked
     * oggStream = AudioLoader.getStreamingAudio("OGG",
     * ResourceLoader.getResource("testdata/bongos.ogg"));
     *
     * // can load mods (XM, MOD) using ibxm which is then played through OpenAL. MODs
     * // are always streamed based on the way IBXM works
     * modStream = AudioLoader.getStreamingAudio("MOD",
     * ResourceLoader.getResource("testdata/SMB-X.XM"));
     *
     * // playing as music uses that reserved source to play the sound. The first
     * // two arguments are pitch and gain, the boolean is whether to loop the content
     * modStream.playAsMusic(1.0f, 1.0f, true);
     *
     * // you can play aifs by loading the complete thing into
     * // a sound
     * aifEffect = AudioLoader.getAudio("AIF",
     * ResourceLoader.getResourceAsStream("testdata/burp.aif"));
     *
     * // you can play wavs by loading the complete thing into
     * // a sound
     * wavEffect = AudioLoader.getAudio("WAV",
     * ResourceLoader.getResourceAsStream("testdata/cbrown01.wav"));
     * } catch (Exception e) {
     * e.printStackTrace();
     * }
     */
  }
  private void playMovementSound() {
    if (_godMode) return;

    if ((MathHelper.fastAbs(_velocity.x) > 0.01 || MathHelper.fastAbs(_velocity.z) > 0.01)
        && _touchingGround) {
      if (_currentFootstepSound == null) {
        Vector3d playerDirection = directionOfReferencePoint();
        _currentFootstepSound =
            _footstepSounds[
                MathHelper.fastAbs(_parent.getWorldProvider().getRandom().randomInt()) % 5];

        _currentFootstepSound.playAsSoundEffect(
            0.7f
                + (float) MathHelper.fastAbs(_parent.getWorldProvider().getRandom().randomDouble())
                    * 0.3f,
            0.2f
                + (float) MathHelper.fastAbs(_parent.getWorldProvider().getRandom().randomDouble())
                    * 0.2f,
            false,
            (float) playerDirection.x,
            (float) playerDirection.y,
            (float) playerDirection.z);
      } else {
        long timeDiff = Terasology.getInstance().getTime() - _lastFootStepSoundPlayed;

        if (!_currentFootstepSound.isPlaying()
            && timeDiff > 400 / (_activeWalkingSpeed / _walkingSpeed)) {
          _lastFootStepSoundPlayed = Terasology.getInstance().getTime();
          _currentFootstepSound = null;
        }
      }
    }
  }
Example #4
0
  /** Game loop update */
  public void update() {
    while (Keyboard.next()) {
      if (Keyboard.getEventKeyState()) {
        if (Keyboard.getEventKey() == Keyboard.KEY_Q) {
          // play as a one off sound effect
          oggEffect.playAsSoundEffect(1.0f, 1.0f, false);
        }
        if (Keyboard.getEventKey() == Keyboard.KEY_W) {
          // replace the music thats curretly playing with
          // the ogg
          oggStream.playAsMusic(1.0f, 1.0f, true);
        }
        if (Keyboard.getEventKey() == Keyboard.KEY_E) {
          // replace the music thats curretly playing with
          // the mod
          modStream.playAsMusic(1.0f, 1.0f, true);
        }
        if (Keyboard.getEventKey() == Keyboard.KEY_R) {
          // play as a one off sound effect
          aifEffect.playAsSoundEffect(1.0f, 1.0f, false);
        }
        if (Keyboard.getEventKey() == Keyboard.KEY_T) {
          // play as a one off sound effect
          wavEffect.playAsSoundEffect(1.0f, 1.0f, false);
        }
      }
    }

    // polling is required to allow streaming to get a chance to
    // queue buffers.
    SoundStore.get().poll(0);
  }
Example #5
0
  public void update(int delta) {
    if (enabled) {
      if (musicPlaying == null) {
        musicDelay -= delta;

        if (musicDelay <= 0) {
          musicPlaying = music.get((int) (Math.random() * music.size()));
          musicPlaying.playAsMusic(1.0f, 1.0f, false);
          soundStore.setSoundVolume(0.5f);
        }
      } else if (!soundStore.isMusicPlaying()) {
        musicPlaying.stop();
        musicDelay = (int) (Math.random() * 90000) + 30000;
        musicPlaying = null;
        soundStore.setSoundVolume(0.75f);
      }

      if (ambientMusicPlaying == null) {
        ambientMusicDelay -= delta;

        if (ambientMusicDelay <= 0) {
          ambientMusicPlaying = ambientMusic.get((int) (Math.random() * ambientMusic.size()));
          ambientMusicPlaying.playAsSoundEffect(1.0f, (float) Math.random() * 0.2f + 1f, false);
        }
      } else if (!ambientMusicPlaying.isPlaying()) {
        ambientMusicPlaying.stop();
        ambientMusicDelay = (int) (Math.random() * 6000) + 1000;
        ambientMusicPlaying = null;
      }

      if (ambientNoisePlaying[0] == null) {
        ambientNoiseDelay[0] -= delta;

        if (ambientNoiseDelay[0] <= 0) {
          ambientNoisePlaying[0] = ambientNoise.get((int) (Math.random() * ambientNoise.size()));
          ambientNoisePlaying[0].playAsSoundEffect(1.0f, (float) Math.random() * 0.2f + 1f, false);
        }
      } else if (!ambientNoisePlaying[0].isPlaying()) {
        ambientNoisePlaying[0].stop();
        ambientNoiseDelay[0] = (int) (Math.random() * 16000) + 1000;
        ambientNoisePlaying[0] = null;
      }

      if (ambientNoisePlaying[1] == null) {
        ambientNoiseDelay[1] -= delta;

        if (ambientNoiseDelay[1] <= 0) {
          ambientNoisePlaying[1] = ambientNoise.get((int) (Math.random() * ambientNoise.size()));
          ambientNoisePlaying[1].playAsSoundEffect(1.0f, (float) Math.random() * 0.2f + 1f, false);
        }
      } else if (!ambientNoisePlaying[1].isPlaying()) {
        ambientNoisePlaying[1].stop();
        ambientNoiseDelay[1] = (int) (Math.random() * 16000) + 1000;
        ambientNoisePlaying[1] = null;
      }

      soundStore.poll(delta);
    }
  }
Example #6
0
 public static void playMusic(String name) {
   try {
     Audio song;
     song =
         AudioLoader.getAudio(
             "WAV",
             ResourceLoader.getResourceAsStream(
                 "resources" + Common.sep + "music" + Common.sep + name + ".wav"));
     song.playAsMusic(1.0f, 1.0f, true);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Example #7
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);
    }
  }