Package de.gurkenlabs.litiengine.sound
Class SoundEngine
java.lang.Object
de.gurkenlabs.litiengine.sound.SoundEngine
- All Implemented Interfaces:
ILaunchable,IUpdateable
This
SoundEngine class provides all methods to play back sounds and music in your game.
It allows to define the 2D coordinates of the sound or even pass in the source entity of the
sound which will adjust the position according to the position of the entity.
The sound engine supports .wav, .mp3 and .ogg by default. If you need other file extensions, you have to write an own SPI implementation and inject it in your project.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionYou should never call this manually! Instead use theGame.audio()instance. -
Method Summary
Modifier and TypeMethodDescriptioncreateSound(Sound sound, Supplier<Point2D> supplier, boolean loop, int range, float volume) Creates anSFXPlaybackobject that can be configured prior to starting.voidfadeMusic(int duration) Fades out all music volume to 0 over the specified time.voidfadeMusic(int duration, float target, TweenFunction easeType) Fades all music volume to a given value over the specified time.Gets a list of all music playbacks.intGets the maximum distance from the listener at which a sound source can still be heard.getMusic()Gets the "main" music that is playing.Sets the currently playing track to aLoopedTrackwith the specified musicSound.Sets the currently playing track to the specified track.Sets the currently playing track to the specified track.Plays the specified track.playMusic(Track track, Consumer<? super MusicPlayback> config, boolean restart, boolean stop) Plays the specified track, optionally configuring it before starting.Sets the currently playing track to aLoopedTrackwith the sound defined by the specified music name.Plays the specified sound with the volume configured in the SOUND config with a center pan.Plays the specified sound with the volume configured in the SOUND config with a center pan.Plays the specified sound with the volume configured in the SOUND config with a center pan.Plays the specified sound with the volume configured in the SOUND config with a center pan.Plays the specified sound at the specified location and updates the volume and pan in relation to the listener location.Plays the specified sound at the specified location and updates the volume and pan in relation to the listener location.Plays the specified sound and updates its volume and pan by the current entity location in relation to the listener location.Plays the specified sound and updates its volume and pan by the current entity location in relation to the listener location.Plays the specified sound and updates its volume and pan by the current entity location in relation to the listener location.Plays the specified sound and updates its volume and pan by the current entity location in relation to the listener location.Plays the specified sound at the specified location and updates the volume and pan in relation to the listener location.Plays the specified sound at the specified location and updates the volume and pan in relation to the listener location.Plays the specified sound at the specified location and updates the volume and pan in relation to the listener location.Plays the specified sound at the specified location and updates the volume and pan in relation to the listener location.Plays aSoundwith the specified name with the volume configured in the SOUND config with a center pan.Plays aSoundwith the specified name with the volume configured in the SOUND config with a center pan.Plays aSoundwith the specified name at the specified location and updates the volume and pan in relation to the listener location.Plays aSoundwith the specified name at the specified location and updates the volume and pan in relation to the listener location.Plays aSoundwith the specified name and updates its volume and pan by the current entity location in relation to the listener location.Plays aSoundwith the specified name and updates its volume and pan by the current entity location in relation to the listener location.Plays aSoundwith the specified name at the specified location and updates the volume and pan in relation to the listener location.Plays aSoundwith the specified name at the specified location and updates the volume and pan in relation to the listener location.voidsetListenerLocationCallback(UnaryOperator<Point2D> callback) This method allows to set the callback that is used by the SoundEngine to determine where the listener location is.voidsetMaxDistance(int radius) Sets the default maximum distance from the listener at which a sound source can still be heard.voidstart()Starts the operation of this instance.voidStops the playback of the current background music.voidTerminates the operation of this instance.voidupdate()This method is called by the game loop on all objects that are attached to the loop.
-
Field Details
-
DEFAULT_MAX_DISTANCE
public static final int DEFAULT_MAX_DISTANCE- See Also:
-
-
Constructor Details
-
SoundEngine
public SoundEngine()You should never call this manually! Instead use theGame.audio()instance.- See Also:
-
-
Method Details
-
getMaxDistance
public int getMaxDistance()Gets the maximum distance from the listener at which a sound source can still be heard.- Returns:
- The maximum distance at which a sound can be heard.
-
playMusic
Sets the currently playing track to aLoopedTrackwith the sound defined by the specified music name. This has no effect if the specified track is already playing.- Parameters:
musicName- The name of theSoundto be played.- Returns:
- The playback of the music
-
playMusic
Sets the currently playing track to aLoopedTrackwith the specified musicSound. This has no effect if the specified track is already playing.- Parameters:
music- TheSoundto be played.- Returns:
- The playback of the music
-
playMusic
Sets the currently playing track to the specified track. This has no effect if the specified track is already playing.- Parameters:
track- The track to play- Returns:
- The playback of the music
-
playMusic
Sets the currently playing track to the specified track.- Parameters:
track- The track to playrestart- Whether to restart if the specified track is already playing, determined byObject.equals(Object)- Returns:
- The playback of the music
-
playMusic
Plays the specified track.- Parameters:
track- The track to playrestart- Whether to restart if the specified track is already playing, determined byObject.equals(Object)stop- Whether to stop an existing track if present- Returns:
- The playback of the music
-
playMusic
public MusicPlayback playMusic(Track track, Consumer<? super MusicPlayback> config, boolean restart, boolean stop) Plays the specified track, optionally configuring it before starting.- Parameters:
track- The track to playconfig- A call to configure the playback prior to starting, which can benullrestart- Whether to restart if the specified track is already playing, determined byObject.equals(Object)stop- Whether to stop an existing track if present- Returns:
- The playback of the music
-
fadeMusic
public void fadeMusic(int duration) Fades out all music volume to 0 over the specified time.- Parameters:
duration- The fade duration in milliseconds
-
fadeMusic
Fades all music volume to a given value over the specified time.- Parameters:
duration- The fade duration in millisecondstarget- The target volume.easeType- The easing Function used for Tweening the volume
-
getMusic
Gets the "main" music that is playing. This usually means the last call toplayMusic, though if the music has been stopped it will benull.- Returns:
- The main music, which could be
null.
-
getAllMusic
Gets a list of all music playbacks.- Returns:
- A list of all music playbacks.
-
playSound
Plays the specified sound and updates its volume and pan by the current entity location in relation to the listener location.- Parameters:
sound- The sound to play.entity- The entity at which location the sound should be played.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays aSoundwith the specified name and updates its volume and pan by the current entity location in relation to the listener location.- Parameters:
soundName- The name of the sound to play.entity- The entity at which location the sound should be played.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound and updates its volume and pan by the current entity location in relation to the listener location.- Parameters:
sound- The sound to play.entity- The entity at which location the sound should be played.loop- Determines whether this playback should be looped or not.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound and updates its volume and pan by the current entity location in relation to the listener location.- Parameters:
sound- The sound to play.entity- The entity at which location the sound should be played.loop- Determines whether this playback should be looped or not.range- the range in pixels for which this sound can be heard- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound and updates its volume and pan by the current entity location in relation to the listener location.- Parameters:
sound- The sound to play.entity- The entity at which location the sound should be played.loop- Determines whether this playback should be looped or not.range- the range in pixels for which this sound can be heardvolume- The volume modifier for the sound playback instance.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays aSoundwith the specified name and updates its volume and pan by the current entity location in relation to the listener location.- Parameters:
soundName- The name of the sound to play.entity- The entity at which location the sound should be played.loop- Determines whether this playback should be looped or not.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound at the specified location and updates the volume and pan in relation to the listener location.- Parameters:
sound- The sound to play.location- The location at which to play the sound.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays aSoundwith the specified name at the specified location and updates the volume and pan in relation to the listener location.- Parameters:
soundName- The name of the sound to play.location- The location at which to play the sound.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound at the specified location and updates the volume and pan in relation to the listener location.- Parameters:
sound- The sound to play.x- The x-coordinate of the location at which to play the sound.y- The y-coordinate of the location at which to play the sound.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays aSoundwith the specified name at the specified location and updates the volume and pan in relation to the listener location.- Parameters:
soundName- The name of the sound to play.x- The x-coordinate of the location at which to play the sound.y- The y-coordinate of the location at which to play the sound.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound at the specified location and updates the volume and pan in relation to the listener location.- Parameters:
sound- The sound to play.location- The location at which to play the sound.loop- Determines whether this playback should be looped or not.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound at the specified location and updates the volume and pan in relation to the listener location.- Parameters:
sound- The sound to play.location- The location at which to play the sound.loop- Determines whether this playback should be looped or not.range- the range in pixels for which this sound can be heard- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound at the specified location and updates the volume and pan in relation to the listener location.- Parameters:
sound- The sound to play.location- The location at which to play the sound.loop- Determines whether this playback should be looped or not.range- the range in pixels for which this sound can be heardvolume- The volume modifier for the sound playback instance.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays aSoundwith the specified name at the specified location and updates the volume and pan in relation to the listener location.- Parameters:
soundName- The name of the sound to play.location- The location at which to play the sound.loop- Determines whether this playback should be looped or not.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound at the specified location and updates the volume and pan in relation to the listener location.- Parameters:
sound- The sound to play.x- The x-coordinate of the location at which to play the sound.y- The y-coordinate of the location at which to play the sound.loop- Determines whether this playback should be looped or not.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays aSoundwith the specified name at the specified location and updates the volume and pan in relation to the listener location.- Parameters:
soundName- The name of the sound to play.x- The x-coordinate of the location at which to play the sound.y- The y-coordinate of the location at which to play the sound.loop- Determines whether this playback should be looped or not.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound with the volume configured in the SOUND config with a center pan.- Parameters:
sound- The sound to play.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays aSoundwith the specified name with the volume configured in the SOUND config with a center pan.- Parameters:
soundName- The name of the sound to play.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound with the volume configured in the SOUND config with a center pan.- Parameters:
sound- The sound to play.loop- Determines whether this playback should be looped or not.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound with the volume configured in the SOUND config with a center pan.- Parameters:
sound- The sound to play.loop- Determines whether this playback should be looped or not.range- the range in pixels for which this sound can be heard- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays the specified sound with the volume configured in the SOUND config with a center pan.- Parameters:
sound- The sound to play.loop- Determines whether this playback should be looped or not.range- the range in pixels for which this sound can be heardvolume- The volume modifier for the sound playback instance.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
playSound
Plays aSoundwith the specified name with the volume configured in the SOUND config with a center pan.- Parameters:
soundName- The name of the sound to play.loop- Determines whether this playback should be looped or not.- Returns:
- An
SFXPlaybackinstance that allows to further process and control the played sound.
-
setMaxDistance
public void setMaxDistance(int radius) Sets the default maximum distance from the listener at which a sound source can still be heard. If the distance between the sound source and the listener is greater than the specified value, the volume is set to 0.- Parameters:
radius- The maximum distance at which sounds can still be heard.
-
stopMusic
public void stopMusic()Stops the playback of the current background music. -
createSound
public SFXPlayback createSound(Sound sound, Supplier<Point2D> supplier, boolean loop, int range, float volume) Creates anSFXPlaybackobject that can be configured prior to starting. Also allows for a custom source supplier.Unlike the
playSoundmethods, theSFXPlaybackobjects returned by this method must be started using theSoundPlayback.start()method. However, necessary resources are acquired immediately upon calling this method, and will remain in use until the playback is either cancelled or finalized.- Parameters:
sound- The sound to playsupplier- A function to get the sound's current source location (the sound is statically positioned if the location isnull)loop- Whether to loop the soundrange- the range in pixels for which this sound can be heardvolume- The volume modifier for the sound playback instance.- Returns:
- An
SFXPlaybackobject that can be configured prior to starting, but will need to be manually started.
-
setListenerLocationCallback
This method allows to set the callback that is used by the SoundEngine to determine where the listener location is.If not explicitly set, the SoundEngine uses the camera focus (center of the screen) as listener location.
- Parameters:
callback- The callback that determines the location of the sound listener.
-
start
public void start()Description copied from interface:ILaunchableStarts the operation of this instance.- Specified by:
startin interfaceILaunchable
-
terminate
public void terminate()Description copied from interface:ILaunchableTerminates the operation of this instance.- Specified by:
terminatein interfaceILaunchable
-
update
public void update()Description copied from interface:IUpdateableThis method is called by the game loop on all objects that are attached to the loop. It's called on every tick of the loop and the frequency can be configured using theClientConfiguration.- Specified by:
updatein interfaceIUpdateable- See Also:
-