Example usage for com.badlogic.gdx.audio Sound play

List of usage examples for com.badlogic.gdx.audio Sound play

Introduction

In this page you can find the example usage for com.badlogic.gdx.audio Sound play.

Prototype

public long play(float volume, float pitch, float pan);

Source Link

Document

Plays the sound.

Usage

From source file:com.tnf.ptm.sound.OggSoundManager.java

License:Apache License

/**
 * Plays a sound. Source must not be null.
 *
 * @param position         position of a sound. If null, source.getPosition() will be used
 * @param source           bearer of a sound. Must not be null for looped sounds
 * @param volumeMultiplier multiplier for sound volume
 *//*from  w w  w. ja va2s.  c om*/
public void play(PtmGame game, PlayableSound playableSound, @Nullable Vector2 position,
        @Nullable PtmObject source, float volumeMultiplier) {
    if (playableSound == null) {
        return;
    }

    OggSound sound = playableSound.getOggSound();
    // logger.debug("Playing sound: {}", sound.getUrn().toString());

    // Perform some initial argument validation
    if (source == null && position == null) {
        throw new AssertionError("Either position or source must be non-null");
    }
    if (source == null && sound.getLoopTime() > 0) {
        throw new AssertionError("Attempted to loop a sound without a parent object: " + sound.getUrn());
    }
    if (position == null) {
        position = source.getPosition();
    }

    // Calculate the volume multiplier for the sound
    float globalVolumeMultiplier = game.getCmp().getOptions().sfxVolumeMultiplier;
    if (globalVolumeMultiplier == 0) {
        return;
    }

    Vector2 cameraPosition = game.getCam().getPos();
    Planet nearestPlanet = game.getPlanetMan().getNearestPlanet();

    float airPerc = 0;
    if (nearestPlanet.getConfig().skyConfig != null) {
        float distanceToAtmosphere = cameraPosition.dst(nearestPlanet.getPos())
                - nearestPlanet.getGroundHeight() - Const.ATM_HEIGHT / 2;
        airPerc = PtmMath.clamp(1 - distanceToAtmosphere / (Const.ATM_HEIGHT / 2));
    }
    if (DebugOptions.SOUND_IN_SPACE) {
        airPerc = 1;
    }

    float maxSoundDist = 1 + 1.5f * Const.CAM_VIEW_DIST_GROUND * airPerc;

    PtmShip hero = game.getHero();
    float soundRadius = hero == null ? 0 : hero.getHull().config.getApproxRadius();
    float distance = position.dst(cameraPosition) - soundRadius;
    float distanceMultiplier = PtmMath.clamp(1 - distance / maxSoundDist);

    float volume = sound.getBaseVolume() * volumeMultiplier * distanceMultiplier * globalVolumeMultiplier;

    if (volume <= 0) {
        return;
    }

    // Calculate the pitch for the sound
    float pitch = PtmMath.rnd(.97f, 1.03f) * game.getTimeFactor() * playableSound.getBasePitch();

    if (skipLooped(source, sound, game.getTime())) {
        return;
    }

    if (DebugOptions.SOUND_INFO) {
        debugHintDrawer.add(source, position, sound.toString());
    }

    Sound gdxSound = sound.getSound();
    gdxSound.play(volume, pitch, 0);
}

From source file:com.watabou.noosa.audio.Sample.java

License:Open Source License

public long play(Object id, float leftVolume, float rightVolume, float rate) {
    if (enabled && ids.containsKey(id)) {
        Sound sound = ids.get(id);
        return sound.play(leftVolume, rate, 0);
    } else {/* ww w  .  jav  a  2 s  . co  m*/
        return -1;
    }
}

From source file:core.september.foundation.AudioManager.java

License:Apache License

public void play(Sound sound, float volume, float pitch, float pan) {
    if (!GamePreferences.instance.sound)
        return;//  ww  w. j  a v a  2 s.c o m
    sound.play(GamePreferences.instance.volSound * volume, pitch, pan);
}

From source file:de.bitbrain.craft.audio.SoundManager.java

License:Open Source License

@Handler
public void onDeferredSoundPlay(SoundPlayEvent event) {
    Sound sound = event.getModel();
    Float volume = (Float) event.getParam(SoundPlayEvent.VOLUME);
    Float pitch = (Float) event.getParam(SoundPlayEvent.PITCH);
    Float pan = (Float) event.getParam(SoundPlayEvent.PAN);
    sound.play(volume, pitch, pan);
}

From source file:de.bitbrain.craft.audio.SoundUtils.java

License:Open Source License

public static void play(String id, float volume, float pitch) {
    Sound sound = SharedAssetManager.get(id, Sound.class);

    if (sound != null) {
        sound.play(volume, pitch, 1.0f);
    } else {/*  ww w  .  j a va2  s. c  o m*/
        Gdx.app.error("ERROR", "[SoundUtils] Sound with id='" + id + "' not found.");
    }
}

From source file:de.bitbrain.craft.ui.ProfessionSelectionView.java

License:Open Source License

private void animateElement(final int index, final ProfessionElement element, Progress progressData,
        final TweenManager tweenManager) {
    element.getColor().a = 0f;//from   www. j av a2 s.  co  m
    element.getIcon().setScale(0f);
    final float progress = progressData.getCurrentProgress();
    TweenCallback callback = new TweenCallback() {

        @Override
        public void onEvent(int type, BaseTween<?> source) {

            Tween.to(element.getBar(), 1, 0.8f).target(progress).ease(TweenEquations.easeInOutQuad)
                    .start(tweenManager);

            Tween.to(element.getIcon(), TweenType.SCALE.ordinal(), 1.0f).delay(0.3f)
                    .setCallbackTriggers(TweenCallback.START).setCallback(new TweenCallback() {
                        @Override
                        public void onEvent(int type, BaseTween<?> source) {
                            Sound s = SharedAssetManager.get(Assets.SND_POP, Sound.class);
                            s.play(1.0f, index / 9.6f + 0.7f, 0.3f);
                        }
                    }).target(1.0f).ease(TweenEquations.easeOutElastic).start(tweenManager);
        }
    };

    Tween.to(element, TweenType.ALPHA.ordinal(), 0.6f).delay(index / 5f).target(1.0f).setCallback(callback)
            .setCallbackTriggers(TweenCallback.START).ease(TweenEquations.easeInOutQuart).start(tweenManager);
}

From source file:es.danirod.rectball.SoundPlayer.java

License:Open Source License

public void playSound(SoundCode code) {
    if (!game.getPlatform().preferences().getBoolean("sound", true)) {
        return;// w  w w  . ja va2  s  .c om
    }

    Sound sound = game.manager.get(code.internalPath, Sound.class);
    float randomPitch = MathUtils.random(0.7f, 1.3f);
    sound.play(0.7f, randomPitch, 0);
}

From source file:headmade.arttag.assets.Assets.java

License:Apache License

public void playSound(String name, float volume, float pitch) {
    Gdx.app.log(TAG, "Playing sound " + name + "with valume " + volume);
    final Sound sound = assetsManager.get(name, Sound.class);
    if (sound != null) {
        sound.play(volume * 0.5f, pitch, 0);
    } else {//  ww w .  j ava  2 s . c  o  m
        Gdx.app.error(TAG, "No Sound with name " + name);
    }
}

From source file:org.destinationsol.game.sound.SoundManager.java

License:Apache License

/**
 * Plays a sound. Either pos or source must not be null.
 * @param pos position of a sound. If null, source.getPosition() will be used
 * @param source bearer of a sound. Must not be null for looped sounds
 * @param volMul multiplier for sound volume
 *///from  ww w .  j  a v a2 s . c  om
public void play(SolGame game, SolSound sound, @Nullable Vector2 pos, @Nullable SolObject source,
        float volMul) {
    if (source == null && pos == null)
        throw new AssertionError("pass either pos or source");
    if (source == null && sound.loopTime > 0)
        throw new AssertionError("looped sound without source object: " + sound.dir);
    if (sound == null)
        return;
    float globalVolMul = game.getCmp().getOptions().volMul;
    if (globalVolMul == 0)
        return;

    if (pos == null)
        pos = source.getPosition();

    // vol
    Vector2 camPos = game.getCam().getPos();
    float airPerc = 0;
    Planet np = game.getPlanetMan().getNearestPlanet();
    if (np.getConfig().skyConfig != null) {
        float camToAtmDst = camPos.dst(np.getPos()) - np.getGroundHeight() - Const.ATM_HEIGHT / 2;
        airPerc = SolMath.clamp(1 - camToAtmDst / (Const.ATM_HEIGHT / 2));
    }
    if (DebugOptions.SOUND_IN_SPACE)
        airPerc = 1;
    float maxSoundDist = 1 + 1.5f * Const.CAM_VIEW_DIST_GROUND * airPerc;
    SolShip hero = game.getHero();
    float fullSoundRad = hero == null ? 0 : hero.getHull().config.getApproxRadius();
    float dst = pos.dst(camPos) - fullSoundRad;
    float distMul = SolMath.clamp(1 - dst / maxSoundDist);
    float vol = sound.baseVolume * volMul * distMul * globalVolMul;
    if (vol <= 0)
        return;

    //pitch
    float pitch = SolMath.rnd(.97f, 1.03f) * game.getTimeFactor() * sound.basePitch;

    if (skipLooped(source, sound, game.getTime()))
        return;
    if (DebugOptions.SOUND_INFO) {
        myHintDrawer.add(source, pos, sound.getDebugString());
    }
    if (sound.sounds.isEmpty())
        return;
    Sound sound0 = SolMath.elemRnd(sound.sounds);
    sound0.play(vol, pitch, 0);
}

From source file:utils.AudioManager.java

License:Apache License

public void play(Sound sound, float volume, float pitch, float pan) {
    //sound.play(GamePreferences.instance.volSound * volume, pitch, pan);
    sound.play(volume, pitch, pan);
}