public final class SoundEngine extends java.lang.Object implements IUpdateable, ILaunchable
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 LILIengine 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.
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_MAX_DISTANCE |
| Constructor and Description |
|---|
SoundEngine()
You should never call this manually! Instead use the
Game.audio() instance. |
| Modifier and Type | Method and Description |
|---|---|
SFXPlayback |
createSound(Sound sound,
java.util.function.Supplier<java.awt.geom.Point2D> supplier,
boolean loop)
Creates an
SFXPlayback object that can be configured prior to starting. |
void |
fadeMusic(int time)
Fades out the music over the specified time, if playing.
|
void |
fadeMusic(int time,
java.lang.Runnable callback)
Fades out the music over the specified time, then calls the provided callback.
|
java.util.Collection<MusicPlayback> |
getAllMusic()
Gets a list of all music playbacks.
|
float |
getMaxDistance()
Gets the maximum distance from the listener at which a sound source can
still be heard.
|
MusicPlayback |
getMusic()
Gets the "main" music that is playing.
|
MusicPlayback |
playMusic(Sound music)
Sets the currently playing track to a
LoopedTrack with the specified music Sound. |
MusicPlayback |
playMusic(java.lang.String musicName)
Sets the currently playing track to a
LoopedTrack with the sound defined by the specified music name. |
MusicPlayback |
playMusic(Track track)
Sets the currently playing track to the specified track.
|
MusicPlayback |
playMusic(Track track,
boolean restart)
Sets the currently playing track to the specified track.
|
MusicPlayback |
playMusic(Track track,
boolean restart,
boolean stop)
Plays the specified track.
|
MusicPlayback |
playMusic(Track track,
java.util.function.Consumer<? super MusicPlayback> config,
boolean restart,
boolean stop)
Plays the specified track, optionally configuring it before starting.
|
SFXPlayback |
playSound(Sound sound)
Plays the specified sound with the volume configured in the SOUND config
with a center pan.
|
SFXPlayback |
playSound(Sound sound,
boolean loop)
Plays the specified sound with the volume configured in the SOUND config
with a center pan.
|
SFXPlayback |
playSound(Sound sound,
double x,
double y)
Plays the specified sound at the specified location and updates the volume
and pan in relation to the listener location.
|
SFXPlayback |
playSound(Sound sound,
double x,
double y,
boolean loop)
Plays the specified sound at the specified location and updates the volume
and pan in relation to the listener location.
|
SFXPlayback |
playSound(Sound sound,
IEntity entity)
Plays the specified sound and updates its volume and pan by the current
entity location in relation to the listener location.
|
SFXPlayback |
playSound(Sound sound,
IEntity entity,
boolean loop)
Plays the specified sound and updates its volume and pan by the current
entity location in relation to the listener location.
|
SFXPlayback |
playSound(Sound sound,
java.awt.geom.Point2D location)
Plays the specified sound at the specified location and updates the volume
and pan in relation to the listener location.
|
SFXPlayback |
playSound(Sound sound,
java.awt.geom.Point2D location,
boolean loop)
Plays the specified sound at the specified location and updates the volume
and pan in relation to the listener location.
|
SFXPlayback |
playSound(java.lang.String soundName)
Plays a
Sound with the specified name with the volume configured in the SOUND config
with a center pan. |
SFXPlayback |
playSound(java.lang.String soundName,
boolean loop)
Plays a
Sound with the specified name with the volume configured in the SOUND config
with a center pan. |
SFXPlayback |
playSound(java.lang.String soundName,
double x,
double y)
Plays a
Sound with the specified name at the specified location and updates the volume
and pan in relation to the listener location. |
SFXPlayback |
playSound(java.lang.String soundName,
double x,
double y,
boolean loop)
Plays a
Sound with the specified name at the specified location and updates the volume
and pan in relation to the listener location. |
SFXPlayback |
playSound(java.lang.String soundName,
IEntity entity)
Plays a
Sound with the specified name and updates its volume and pan by the current
entity location in relation to the listener location. |
SFXPlayback |
playSound(java.lang.String soundName,
IEntity entity,
boolean loop)
Plays a
Sound with the specified name and updates its volume and pan by the current
entity location in relation to the listener location. |
SFXPlayback |
playSound(java.lang.String soundName,
java.awt.geom.Point2D location)
Plays a
Sound with the specified name at the specified location and updates the volume
and pan in relation to the listener location. |
SFXPlayback |
playSound(java.lang.String soundName,
java.awt.geom.Point2D location,
boolean loop)
Plays a
Sound with the specified name at the specified location and updates the volume
and pan in relation to the listener location. |
void |
setListenerLocationCallback(java.util.function.UnaryOperator<java.awt.geom.Point2D> callback)
This method allows to set the callback that is used by the SoundEngine to
determine where the listener location is.
|
void |
setMaxDistance(float radius)
Sets the maximum distance from the listener at which a sound source can
still be heard.
|
void |
start()
Starts the operation of this instance.
|
void |
stopMusic()
Stops the playback of the current background music.
|
void |
terminate()
Terminates the operation of this instance.
|
void |
update()
This method is called by the game loop on all objects that are attached to the loop.
|
public static final int DEFAULT_MAX_DISTANCE
public SoundEngine()
You should never call this manually! Instead use the Game.audio() instance.
Game.audio()public float getMaxDistance()
public MusicPlayback playMusic(java.lang.String musicName)
LoopedTrack with the sound defined by the specified music name. This has no effect if the
specified track is already playing.musicName - The name of the Sound to be played.public MusicPlayback playMusic(Sound music)
LoopedTrack with the specified music Sound. This has no effect if the specified
track is already playing.music - The Sound to be played.public MusicPlayback playMusic(Track track)
track - The track to playpublic MusicPlayback playMusic(Track track, boolean restart)
track - The track to playrestart - Whether to restart if the specified track is already playing, determined by Object.equals(Object)public MusicPlayback playMusic(Track track, boolean restart, boolean stop)
track - The track to playrestart - Whether to restart if the specified track is already playing, determined by Object.equals(Object)stop - Whether to stop an existing track if presentpublic MusicPlayback playMusic(Track track, java.util.function.Consumer<? super MusicPlayback> config, boolean restart, boolean stop)
track - The track to playconfig - A call to configure the playback prior to starting, which can be nullrestart - Whether to restart if the specified track is already playing, determined by Object.equals(Object)stop - Whether to stop an existing track if presentpublic void fadeMusic(int time)
time - The time in frames to make the existing music fade out for if presentpublic void fadeMusic(int time,
java.lang.Runnable callback)
time - The time in frames to make the existing music fade out for if presentcallback - The callback for when the fade finishespublic MusicPlayback getMusic()
playMusic, though if the music has been stopped it will be
null.null.public java.util.Collection<MusicPlayback> getAllMusic()
public SFXPlayback playSound(Sound sound, IEntity entity)
entity - The entity at which location the sound should be played.sound - The sound to play.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(java.lang.String soundName, IEntity entity)
Sound with the specified name and updates its volume and pan by the current
entity location in relation to the listener location.entity - The entity at which location the sound should be played.soundName - The name of the sound to play.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(Sound sound, IEntity entity, boolean loop)
entity - The entity at which location the sound should be played.sound - The sound to play.loop - Determines whether this playback should be looped or not.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(java.lang.String soundName, IEntity entity, boolean loop)
Sound with the specified name and updates its volume and pan by the current
entity location in relation to the listener location.entity - The entity at which location the sound should be played.soundName - The name of the sound to play.loop - Determines whether this playback should be looped or not.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(Sound sound, java.awt.geom.Point2D location)
location - The location at which to play the sound.sound - The sound to play.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(java.lang.String soundName, java.awt.geom.Point2D location)
Sound with the specified name at the specified location and updates the volume
and pan in relation to the listener location.location - The location at which to play the sound.soundName - The name of the sound to play.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(Sound sound, double x, double y)
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.sound - The sound to play.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(java.lang.String soundName, double x, double y)
Sound with the specified name at the specified location and updates the volume
and pan in relation to the listener location.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.soundName - The name of the sound to play.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(Sound sound, java.awt.geom.Point2D location, boolean loop)
location - The location at which to play the sound.sound - The sound to play.loop - Determines whether this playback should be looped or not.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(java.lang.String soundName, java.awt.geom.Point2D location, boolean loop)
Sound with the specified name at the specified location and updates the volume
and pan in relation to the listener location.location - The location at which to play the sound.soundName - The name of the sound to play.loop - Determines whether this playback should be looped or not.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(Sound sound, double x, double y, boolean loop)
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.sound - The sound to play.loop - Determines whether this playback should be looped or not.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(java.lang.String soundName, double x, double y, boolean loop)
Sound with the specified name at the specified location and updates the volume
and pan in relation to the listener location.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.soundName - The name of the sound to play.loop - Determines whether this playback should be looped or not.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(Sound sound)
sound - The sound to play.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(java.lang.String soundName)
Sound with the specified name with the volume configured in the SOUND config
with a center pan.soundName - The name of the sound to play.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(Sound sound, boolean loop)
sound - The sound to play.loop - Determines whether this playback should be looped or not.SFXPlayback instance that allows to further process
and control the played sound.public SFXPlayback playSound(java.lang.String soundName, boolean loop)
Sound with the specified name with the volume configured in the SOUND config
with a center pan.soundName - The name of the sound to play.loop - Determines whether this playback should be looped or not.SFXPlayback instance that allows to further process
and control the played sound.public void setMaxDistance(float radius)
radius - The maximum distance at which sounds can still be heard.public void stopMusic()
public SFXPlayback createSound(Sound sound, java.util.function.Supplier<java.awt.geom.Point2D> supplier, boolean loop)
Creates an SFXPlayback object that can be configured prior to starting. Also allows for a custom source supplier.
Unlike the playSound methods, the SFXPlayback objects returned by this method must be started using the
SoundPlayback.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.
sound - The sound to playsupplier - A function to get the sound's current source location (the sound is statically positioned if the location is null)loop - Whether to loop the soundSFXPlayback object that can be configured prior to starting, but will need to be manually started.public void setListenerLocationCallback(java.util.function.UnaryOperator<java.awt.geom.Point2D> callback)
callback - The callback that determines the location of the sound listener.public void start()
ILaunchablestart in interface ILaunchablepublic void terminate()
ILaunchableterminate in interface ILaunchablepublic void update()
IUpdateableClientConfiguration.update in interface IUpdateableClientConfiguration.setMaxFps(int)