Back to project page DolphinOES.
The source code is released under:
Apache License
If you think the Android project DolphinOES listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/******************************************************************************* * Copyright 2014 See AUTHORS file.//from w ww .j a v a 2 s. c o m * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package com.sidereal.dolphinoes.behaviors.audio; import java.util.ArrayList; import java.util.HashMap; import java.util.Map.Entry; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.sidereal.dolphinoes.architecture.DolphinOES; import com.sidereal.dolphinoes.architecture.GameBehavior; import com.sidereal.dolphinoes.architecture.GameObject; /** Handles listening to audio coming from {@link AudioPlayer} instances, panning * the audio based on the position between the listener and the player. * * @author Claudiu Bele */ public class AudioListener extends GameBehavior { // region fields public static HashMap<String, Sound> registeredSounds; public static HashMap<GameObject, Float> listeners; public static Sprite debugSpriteSource; protected ArrayList<SoundClass> mySounds; protected boolean isListener; protected float listenerRadius; // endregion fields // region constructors public AudioListener(GameObject obj) { super(obj); mySounds = new ArrayList<AudioListener.SoundClass>(); GameBehavior.setDebugKeys(getClass(), new Integer[] { Keys.SHIFT_LEFT, Keys.Z }); } @Override protected void initialiseClass() { if (registeredSounds == null) { registeredSounds = new HashMap<String, Sound>(); } if (listeners == null) { listeners = new HashMap<GameObject, Float>(); } if (!DolphinOES.debug.isEnabled()) return; if (debugSpriteSource == null) { debugSpriteSource = new Sprite( DolphinOES.assets.get( DolphinOES.assets.frameworkAssetsFolder + "AudioListener.png", Texture.class)); } } // endregion constructors // region methods @Override public void updateDebug() { if (isListener) { debugSpriteSource.setBounds( object.pos.getX() - listenerRadius, object.pos.getY() - listenerRadius, listenerRadius * 2, listenerRadius * 2); // debugSpriteSource.setBounds(0, 0, 100, 100); debugSpriteSource.draw(object.gameBatch.spriteBatch); } } @Override public void update() { if (!mustHandleSongs()) return; playSongs(getSoundVolume()); } public void addSound(String filePath, String name, boolean repeat, float repeatTime) { if (!AudioListener.registeredSounds.containsKey(filePath)) { Sound sound = Gdx.audio.newSound(Gdx.files.internal(filePath)); AudioListener.registeredSounds.put(filePath, sound); } SoundClass toAdd = new SoundClass(registeredSounds.get(filePath), name, repeat, repeatTime); mySounds.add(toAdd); } public void registerListener(float radius) { AudioListener.listeners.put(object, radius); listenerRadius = radius; isListener = true; } /** Decreases {@link SoundClass#timeUntilNextSound} by * {@link SoundClass#repeatTime} , and if any of those values from * {@link #mySounds} is less or equal to 0, we return true, otherwise, we * return false. * * @return */ public boolean mustHandleSongs() { for (int i = 0; i < mySounds.size(); i++) { if (!mySounds.get(i).enabled) break; // sound must be repeated if (mySounds.get(i).repeat) { mySounds.get(i).timeUntilNextSound -= DolphinOES.time.getDeltaTime(); // we have waited long enough to play the sound if (mySounds.get(i).timeUntilNextSound <= 0) { return true; } } // sound isn't repeated, but we scheduled it if (mySounds.get(i).timesScheduled > 0 && !mySounds.get(i).repeat) { return true; } } return false; } /** Checks if each sounds in {@link #mySounds} has the * {@link SoundClass#timeUntilNextSound} less or equal than 0, updating it * to {@link SoundClass#repeatTime} if it is. * <p> * The volume passed is {@link #getSoundVolume()}. * * @param volume */ public void playSongs(float volume) { for (int i = 0; i < mySounds.size(); i++) { if (!mySounds.get(i).enabled) break; if (mySounds.get(i).timesScheduled > 0 && !mySounds.get(i).repeat) { mySounds.get(i).timesScheduled--; long id = mySounds.get(i).sound.play(); mySounds.get(i).sound.setPriority(id, 0); mySounds.get(i).sound.setVolume(id, volume); } if (mySounds.get(i).timeUntilNextSound <= 0 && mySounds.get(i).repeat) { mySounds.get(i).timeUntilNextSound += mySounds.get(i).repeatTime; long id = mySounds.get(i).sound.play(); mySounds.get(i).sound.setVolume(id, volume); } } } /** Calculates the distance between the behavior's object and all the * listeners, in order to see which listener the user is closest to. * <p> * The volume that is returned is the max volume from the distance * calculation between the objects, based on the listener's radius. * * @return */ public float getSoundVolume() { float volumeToReturn = 0; float tempVolume = 0; for (Entry<GameObject, Float> listener : AudioListener.listeners .entrySet()) { float distanceToListener = object.pos.get().dst(listener.getKey().pos.get()); if (distanceToListener < listener.getValue()) { tempVolume = 1 - (distanceToListener / listener.getValue()); if (tempVolume > volumeToReturn) volumeToReturn = tempVolume; } } return volumeToReturn / 10; } public void schedulePlay(String soundName) { for (int i = 0; i < mySounds.size(); i++) { if (mySounds.get(i).name.equals(soundName)) { mySounds.get(i).timesScheduled++; } } } public class SoundClass { public Sound sound; public float repeatTime; public float timeUntilNextSound; public String name; public boolean enabled; public int timesScheduled; public boolean repeat; public SoundClass(Sound sound, String name, boolean repeat, float repeatTime) { this.repeat = repeat; this.enabled = true; this.sound = sound; this.repeatTime = this.timeUntilNextSound = repeatTime; this.name = name; } } @Override public void dispose() { mySounds.clear(); if (AudioListener.listeners.containsKey(object)) { AudioListener.listeners.remove(object); } } // endregion methods }