Class Audio
java.lang.Object
de.pottgames.tuningfork.Audio
- All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable
The main management and entry point of TuningFork. This class initializes the sound device and gives access to SoundSource's for advanced manual playback
control.
-
Method Summary
Modifier and TypeMethodDescriptionvoiddispose()Shuts down TuningFork.static Audioget()Returns the currently active instance of Audio.floatReturns the default attenuation factor that is used to calculate the attenuation by the current default attenuation model.floatReturns the default attenuation maximum distance that is used to calculate the attenuation by the current default attenuation model.floatReturns the default attenuation minimum distance that is used to calculate the attenuation by the current default attenuation model.Returns whether virtualization is enabled or disabled by default for all sound sources.Returns the currently used AudioDevice.Returns theSoundListener.floatReturns the global volume value that is applied to all sources.Returns the wav decoder provider that is used byWavInputStream.static Audioinit()Initializes an Audio instance with the defaultAudioConfig.static Audioinit(AudioConfig config) Initializes an Audio instance with the givenAudioConfig.static AudioinitSafe()Initializes an Audio instance with the defaultAudioConfig.static AudioinitSafe(AudioConfig config) Initializes an Audio instance with the givenAudioConfig.booleanReturns true if the native decoders are available.obtainSource(SoundBuffer buffer) Returns aBufferedSoundSourcefor permanent use.voidpauseAll()Pauses allBufferedSoundSources andStreamedSoundSources.voidPauses allBufferedSoundSources.voidPauses allStreamedSoundSource.protected voidplay(SoundBuffer buffer) Plays the sound.protected voidplay(SoundBuffer buffer, float volume) Plays the sound with the given volume.protected voidplay(SoundBuffer buffer, float volume, float pitch) Plays the sound with the given volume and pitch.protected voidplay(SoundBuffer buffer, float volume, float pitch, float pan) Plays the sound with the given volume, pitch and pan.protected voidplay(SoundBuffer buffer, float volume, float pitch, float lowFreqVolume, float highFreqVolume) Plays the sound with the given volume, pitch and filter.protected voidplay(SoundBuffer buffer, float volume, float pitch, float pan, SoundEffect effect) Plays the sound with the given volume, pitch, pan and effect.protected voidplay(SoundBuffer buffer, float volume, float pitch, SoundEffect effect) Plays the sound with the given volume, pitch and effect.protected voidplay(SoundBuffer buffer, float volume, SoundEffect effect) Plays the sound with the given volume and effect.protected voidplay(SoundBuffer buffer, SoundEffect effect) Plays a sound with an effect.protected voidplay3D(SoundBuffer buffer, float volume, float pitch, com.badlogic.gdx.math.Vector3 position) Plays a spatial sound with the given volume and pitch at the given position.protected voidplay3D(SoundBuffer buffer, float volume, float pitch, com.badlogic.gdx.math.Vector3 position, SoundEffect effect) Plays a spatial sound with the given volume, pitch and effect at the given position.protected voidplay3D(SoundBuffer buffer, float volume, com.badlogic.gdx.math.Vector3 position) Plays a spatial sound with the given volume at the given position.protected voidplay3D(SoundBuffer buffer, float volume, com.badlogic.gdx.math.Vector3 position, float lowFreqVolume, float highFreqVolume) Plays a spatial sound with the given volume and filter at the given position.protected voidplay3D(SoundBuffer buffer, float volume, com.badlogic.gdx.math.Vector3 position, SoundEffect effect) Plays a spatial sound with the given volume and effect at the given position.protected voidplay3D(SoundBuffer buffer, com.badlogic.gdx.math.Vector3 position) Plays a spatial sound at the given position.protected voidplay3D(SoundBuffer buffer, com.badlogic.gdx.math.Vector3 position, float lowFreqVolume, float highFreqVolume) Plays a spatial sound with the given filter at the given position.protected voidplay3D(SoundBuffer buffer, com.badlogic.gdx.math.Vector3 position, float lowFreqVolume, float highFreqVolume, SoundEffect effect) Plays a spatial sound at the given position with the given effect and filter.protected voidplay3D(SoundBuffer buffer, com.badlogic.gdx.math.Vector3 position, SoundEffect effect) Plays a spatial sound at the given position with an effect.protected voidplayAtTime(SoundBuffer buffer, long time) Plays the sound at the specified time.voidregisterAssetManagerLoaders(com.badlogic.gdx.assets.AssetManager manager) Registers TuningFork's async loaders on libGDX's AssetManager.voidResumes to play allBufferedSoundSources andStreamedSoundSources that are paused.voidResumes to play allBufferedSoundSources that are paused at the moment.voidResumes to play allStreamedSoundSources that are paused at the moment.voidsetDefaultAttenuationFactor(float rolloff) This factor determines how slowly or how quickly the sound source loses volume as the listener moves away from the source.voidsetDefaultAttenuationMaxDistance(float distance) Sets the distance the listener must be from the sound source at which the attenuation should stop.voidsetDefaultAttenuationMinDistance(float distance) Sets the distance the listener must be from the sound source at which the attenuation should begin.booleansetDefaultResampler(String resampler) Immediately sets the resampler for all sound sources, regardless of their state (playing, paused, obtained, etc.).
Sources that are created afterward are also initialized with the new default resampler.
CheckAudioDevice.getAvailableResamplers()for a list of available resamplers.voidsetDefaultSpatialization(AudioConfig.Spatialization spatialization) voidsetDefaultVirtualization(AudioConfig.Virtualization virtualization) Immediately sets the virtualization method for all sound sources, regardless of their state (playing, paused, obtained, etc.).
Sources that are created afterward are also initialized with the new default virtualization method.
Unless you have a specific reason to change this at runtime, it's recommended to set the default viaAudioConfigoninit(AudioConfig).
SeeAudioConfig.Virtualizationfor the different options available.voidAs the listener moves away from a sound source, the volume of the sound source decreases for the listener.voidsetDopplerFactor(float dopplerFactor) Changing the doppler factor exaggerates or de-emphasizes the doppler effect.voidsetMasterVolume(float volume) Sets the global volume that is applied to all sources.voidstopAll()Stops allBufferedSoundSources andStreamedSoundSources.voidStops allBufferedSoundSources.voidStops allStreamedSoundSources.
-
Method Details
-
init
Initializes an Audio instance with the defaultAudioConfig. Errors are logged but exceptions are silently ignored. CallinitSafe(AudioConfig)instead, if you want to handle exceptions.- Returns:
- the initialized Audio instance or null on failure
-
initSafe
Initializes an Audio instance with the defaultAudioConfig. If you don't want to take care of exceptions, callinit(AudioConfig)instead.- Returns:
- the initialized Audio instance
- Throws:
OpenDeviceException- is thrown when a device couldn't be openedUnsupportedAudioDeviceException- is thrown when the desired device couldn't be found or isn't a valid device
-
init
Initializes an Audio instance with the givenAudioConfig. Errors are logged but exceptions are silently ignored. CallinitSafe(AudioConfig)instead, if you want to handle exceptions.- Parameters:
config- the audio config- Returns:
- the initialized Audio instance or null on failure
-
initSafe
public static Audio initSafe(AudioConfig config) throws OpenDeviceException, UnsupportedAudioDeviceException Initializes an Audio instance with the givenAudioConfig. If you don't want to take care of exceptions, callinit(AudioConfig)instead.- Parameters:
config- the audio config- Returns:
- the initialized Audio instance
- Throws:
OpenDeviceException- is thrown when a device couldn't be openedUnsupportedAudioDeviceException- is thrown when the desired device couldn't be found or isn't a valid device
-
get
-
getDevice
Returns the currently used AudioDevice.- Returns:
- the device in charge
-
setDistanceAttenuationModel
As the listener moves away from a sound source, the volume of the sound source decreases for the listener. The Attenuation Model is responsible for this calculation.
Note: Setting a DistanceAttenuationModel overwrites the default min and max attenuation distance as well as the attenuation factor that is used for new sound sources. It won't affect existing ones.
There are severalmodelsto choose from, including alinear modeland asemi-realistic modelbased on the real world. By default theINVERSE_DISTANCE_CLAMPEDmodel is used.- Parameters:
model- the model
-
setDefaultAttenuationMinDistance
public void setDefaultAttenuationMinDistance(float distance) Sets the distance the listener must be from the sound source at which the attenuation should begin. The attenuation itself is controlled by the attenuation model and the attenuation factor of the source. This value is used for all sources that are created/obtained afterwards, it doesn't affect existing or already obtained sources. If you want to set this per source, you can do so:SoundSource.setAttenuationMinDistance(float).- Parameters:
distance- (default depends on the attenuation model)
-
setDefaultAttenuationMaxDistance
public void setDefaultAttenuationMaxDistance(float distance) Sets the distance the listener must be from the sound source at which the attenuation should stop. The attenuation itself is controlled by the attenuation model and the attenuation factor of the source. This value is used for all sources that are created/obtained afterwards, it doesn't affect existing or already obtained sources. If you want to set this per source, you can do so:SoundSource.setAttenuationMaxDistance(float).- Parameters:
distance- (default depends on the attenuation model)
-
setDefaultAttenuationFactor
public void setDefaultAttenuationFactor(float rolloff) This factor determines how slowly or how quickly the sound source loses volume as the listener moves away from the source. A factor of 0.5 reduces the volume loss by half. With a factor of 2, the source loses volume twice as fast. This factor is used for all sources that are created/obtained afterwards, it doesn't affect existing or already obtained sources. If you want to set this per source, you can do so:SoundSource.setAttenuationFactor(float).- Parameters:
rolloff- (default depends on the attenuation model)
-
getDefaultAttenuationMinDistance
public float getDefaultAttenuationMinDistance()Returns the default attenuation minimum distance that is used to calculate the attenuation by the current default attenuation model.- Returns:
- the default attenuation min distance
-
getDefaultAttenuationMaxDistance
public float getDefaultAttenuationMaxDistance()Returns the default attenuation maximum distance that is used to calculate the attenuation by the current default attenuation model.- Returns:
- the default attenuation max distance
-
getDefaultAttenuationFactor
public float getDefaultAttenuationFactor()Returns the default attenuation factor that is used to calculate the attenuation by the current default attenuation model.- Returns:
- the default attenuation factor
-
getDefaultVirtualization
Returns whether virtualization is enabled or disabled by default for all sound sources. SeeAudioConfig.setVirtualization(Virtualization)for more info.- Returns:
- the virtualization method
-
setDefaultVirtualization
Immediately sets the virtualization method for all sound sources, regardless of their state (playing, paused, obtained, etc.).
Sources that are created afterward are also initialized with the new default virtualization method.
Unless you have a specific reason to change this at runtime, it's recommended to set the default viaAudioConfigoninit(AudioConfig).
SeeAudioConfig.Virtualizationfor the different options available.- Parameters:
virtualization- the virtualization
-
getDefaultSpatialization
-
setDefaultSpatialization
-
getMasterVolume
public float getMasterVolume()Returns the global volume value that is applied to all sources.- Returns:
- the master volume in the range: 0 - 1
-
setMasterVolume
public void setMasterVolume(float volume) Sets the global volume that is applied to all sources. Values above 1 or below 0 will be clamped.- Parameters:
volume- range: 0 - 1
-
setDopplerFactor
public void setDopplerFactor(float dopplerFactor) Changing the doppler factor exaggerates or de-emphasizes the doppler effect. Physically accurate doppler calculation might not give the desired result, so changing this to your needs is fine. The default doppler factor is 1. Values < 0 are ignored, 0 turns the doppler effect off, values > 1 will increase the strength of the doppler effect.- Parameters:
dopplerFactor- (default 1)
-
obtainSource
Returns aBufferedSoundSourcefor permanent use. Callfree()on it to return it to the pool of available sources.- Parameters:
buffer- the sound buffer- Returns:
- the
BufferedSoundSource
-
play
-
playAtTime
Plays the sound at the specified time. Negative values for time will result in an error log entry but do nothing else. Positive values that point to the past will make the source play immediately. The source will be in playing-state while waiting for the start time to be reached.- Parameters:
buffer- the sound buffertime- the time in nanoseconds, useAudioDevice.getClockTime()to get the current time
-
play
Plays a sound with an effect.- Parameters:
buffer- the sound buffereffect- the sound effect
-
play
Plays the sound with the given volume.- Parameters:
buffer- the sound buffervolume- in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume. (default 1)
-
play
Plays the sound with the given volume and effect.- Parameters:
buffer- the sound buffervolume- in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume. (default 1)effect- the sound effect
-
play
Plays the sound with the given volume and pitch.- Parameters:
buffer- the sound buffervolume- in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume. (default 1)pitch- in the range of 0.5 - 2.0 with values < 1 making the sound slower and values > 1 making it faster (default 1)
-
play
protected void play(SoundBuffer buffer, float volume, float pitch, float lowFreqVolume, float highFreqVolume) Plays the sound with the given volume, pitch and filter.- Parameters:
buffer- the sound buffervolume- in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume. (default 1)pitch- in the range of 0.5 - 2.0 with values < 1 making the sound slower and values > 1 making it faster (default 1)lowFreqVolume- the volume of low frequencieshighFreqVolume- the volume of high frequencies
-
play
Plays the sound with the given volume, pitch and effect.- Parameters:
buffer- the sound buffervolume- in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume. (default 1)pitch- in the range of 0.5 - 2.0 with values < 1 making the sound slower and values > 1 making it faster (default 1)effect- the sound effect
-
play
Plays the sound with the given volume, pitch and pan.- Parameters:
buffer- the sound buffervolume- in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume. (default 1)pitch- in the range of 0.5 - 2.0 with values < 1 making the sound slower and values > 1 making it faster (default 1)pan- in the range of -1.0 (full left) to 1.0 (full right). (default center 0.0)
-
play
Plays the sound with the given volume, pitch, pan and effect.- Parameters:
buffer- the sound buffervolume- in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume. (default 1)pitch- in the range of 0.5 - 2.0 with values < 1 making the sound slower and values > 1 making it faster (default 1)pan- in the range of -1.0 (full left) to 1.0 (full right). (default center 0.0)effect- the sound effect
-
play3D
Plays a spatial sound at the given position.- Parameters:
buffer- the sound bufferposition- the position in 3D space
-
play3D
protected void play3D(SoundBuffer buffer, com.badlogic.gdx.math.Vector3 position, float lowFreqVolume, float highFreqVolume) Plays a spatial sound with the given filter at the given position.- Parameters:
buffer- the sound bufferposition- the position in 3D spacelowFreqVolume- the volume of low frequencieshighFreqVolume- the volume of high frequencies
-
play3D
protected void play3D(SoundBuffer buffer, com.badlogic.gdx.math.Vector3 position, SoundEffect effect) Plays a spatial sound at the given position with an effect.- Parameters:
buffer- the sound bufferposition- the position in 3D spaceeffect- the sound effect
-
play3D
protected void play3D(SoundBuffer buffer, com.badlogic.gdx.math.Vector3 position, float lowFreqVolume, float highFreqVolume, SoundEffect effect) Plays a spatial sound at the given position with the given effect and filter.- Parameters:
buffer- the sound bufferposition- the position in 3D spacelowFreqVolume- the volume of low frequencieshighFreqVolume- the volume of high frequencieseffect- the sound effect
-
play3D
Plays a spatial sound with the given volume at the given position.- Parameters:
buffer- the sound buffervolume- in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume. (default 1)position- the position in 3D space
-
play3D
protected void play3D(SoundBuffer buffer, float volume, com.badlogic.gdx.math.Vector3 position, float lowFreqVolume, float highFreqVolume) Plays a spatial sound with the given volume and filter at the given position.- Parameters:
buffer- the sound buffervolume- in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume. (default 1)position- the position in 3D spacelowFreqVolume- the volume of low frequencieshighFreqVolume- the volume of high frequencies
-
play3D
protected void play3D(SoundBuffer buffer, float volume, com.badlogic.gdx.math.Vector3 position, SoundEffect effect) Plays a spatial sound with the given volume and effect at the given position.- Parameters:
buffer- the sound buffervolume- in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume. (default 1)position- the position in 3D spaceeffect- the sound effect
-
play3D
protected void play3D(SoundBuffer buffer, float volume, float pitch, com.badlogic.gdx.math.Vector3 position) Plays a spatial sound with the given volume and pitch at the given position.- Parameters:
buffer- the sound buffervolume- in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume. (default 1)pitch- in the range of 0.5 - 2.0 with values < 1 making the sound slower and values > 1 making it faster (default 1)position- the position in 3D space
-
play3D
protected void play3D(SoundBuffer buffer, float volume, float pitch, com.badlogic.gdx.math.Vector3 position, SoundEffect effect) Plays a spatial sound with the given volume, pitch and effect at the given position.- Parameters:
buffer- the sound buffervolume- in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume. (default 1)pitch- in the range of 0.5 - 2.0 with values < 1 making the sound slower and values > 1 making it faster (default 1)position- the position in 3D spaceeffect- the sound effect
-
setDefaultResampler
Immediately sets the resampler for all sound sources, regardless of their state (playing, paused, obtained, etc.).
Sources that are created afterward are also initialized with the new default resampler.
CheckAudioDevice.getAvailableResamplers()for a list of available resamplers.- Parameters:
resampler- the resampler- Returns:
- true if successful, false if the desired resampler isn't available
-
resumeAll
public void resumeAll()Resumes to play allBufferedSoundSources andStreamedSoundSources that are paused. -
resumeAllStreamedSources
public void resumeAllStreamedSources()Resumes to play allStreamedSoundSources that are paused at the moment. -
resumeAllBufferedSources
public void resumeAllBufferedSources()Resumes to play allBufferedSoundSources that are paused at the moment. -
pauseAll
public void pauseAll()Pauses allBufferedSoundSources andStreamedSoundSources. -
pauseAllStreamedSources
public void pauseAllStreamedSources()Pauses allStreamedSoundSource. -
pauseAllBufferedSources
public void pauseAllBufferedSources()Pauses allBufferedSoundSources. -
stopAll
public void stopAll()Stops allBufferedSoundSources andStreamedSoundSources. -
stopAllStreamedSources
public void stopAllStreamedSources()Stops allStreamedSoundSources. -
stopAllBufferedSources
public void stopAllBufferedSources()Stops allBufferedSoundSources. -
getListener
-
registerAssetManagerLoaders
public void registerAssetManagerLoaders(com.badlogic.gdx.assets.AssetManager manager) Registers TuningFork's async loaders on libGDX's AssetManager. If you provided an AssetManager in theAudioConfig, loaders have been registered and you can skip this step.- Parameters:
manager- the libGDX asset manager
-
getWavDecoderProvider
Returns the wav decoder provider that is used byWavInputStream.- Returns:
- the resampler provider
-
getLogger
-
isNativeDecodersAvailable
public boolean isNativeDecodersAvailable()Returns true if the native decoders are available. Java decoders will be used as a fallback.- Returns:
- native decoders available
-
dispose
public void dispose()Shuts down TuningFork.- Specified by:
disposein interfacecom.badlogic.gdx.utils.Disposable
-