Class SoundBuffer
java.lang.Object
de.pottgames.tuningfork.SoundBuffer
- All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable
- Direct Known Subclasses:
ReadableSoundBuffer
Stores sound data in an OpenAL buffer that can be used by sound sources. Needs to be disposed when no longer needed.
-
Constructor Summary
ConstructorsConstructorDescriptionSoundBuffer(byte[] pcm, int channels, int sampleRate, int bitsPerSample, PcmFormat.PcmDataType pcmDataType) Creates a SoundBuffer with the given pcm data.
8-bit data is expressed as an unsigned value over the range 0 to 255, 128 being an audio output level of zero .
16-bit data is expressed as a signed value over the range -32768 to 32767, 0 being an audio output level of zero.
Stereo data is expressed in an interleaved format, left channel sample followed by the right channel sample.
The interleaved format also applies to surround sound.SoundBuffer(byte[] pcm, int channels, int sampleRate, int bitsPerSample, PcmFormat.PcmDataType pcmDataType, int blockAlign) Creates a SoundBuffer with the given pcm data.
Consider usingSoundBuffer(byte[], int, int, int, PcmDataType)instead if you're not providing MS_ADPCM data.
8-bit data is expressed as an unsigned value over the range 0 to 255, 128 being an audio output level of zero .
16-bit data is expressed as a signed value over the range -32768 to 32767, 0 being an audio output level of zero.
Stereo data is expressed in an interleaved format, left channel sample followed by the right channel sample.
The interleaved format also applies to surround sound.SoundBuffer(ShortBuffer pcm, int channels, int sampleRate, int bitsPerSample, PcmFormat.PcmDataType pcmDataType, int blockAlign) Creates a SoundBuffer with the given pcm data.
8-bit data is expressed as an unsigned value over the range 0 to 255, 128 being an audio output level of zero .
16-bit data is expressed as a signed value over the range -32768 to 32767, 0 being an audio output level of zero.
Stereo data is expressed in an interleaved format, left channel sample followed by the right channel sample.
The interleaved format also applies to surround sound. -
Method Summary
Modifier and TypeMethodDescriptionvoiddispose()protected floatprotected intprotected intgenerateBufferAndUpload(ShortBuffer pcm, int blockAlign, int sampleRate) floatReturns the duration in seconds.float[]Returns the loop points set bysetLoopPoints(float, float).
The returned array has a length of 2, with
index 0 => start
index 1 => end
The result might not exactly reflect the numbers fromsetLoopPoints(float, float)because there's a conversion from seconds to samples involved.
The returned float array is "owned" by theSoundBufferclass, so you shouldn't hold a reference to it but rather copy the values.Returns the PcmFormat of this SoundBuffer.voidplay()Plays the sound.voidplay(float volume) Plays the sound with the given volume.voidplay(float volume, float pitch) Plays the sound with the given volume and pitch.voidplay(float volume, float pitch, float pan) Plays the sound with the given volume, pitch and pan.voidplay(float volume, float pitch, float lowFreqVolume, float highFreqVolume) Plays the sound with the given volume, pitch and filter.voidplay(float volume, float pitch, float pan, SoundEffect effect) Plays the sound with the given volume, pitch, pan and effect.voidplay(float volume, float pitch, SoundEffect effect) Plays the sound with the given volume, pitch and effect.voidplay(float volume, SoundEffect effect) Plays the sound with the given volume and effect.voidplay(SoundEffect effect) Plays a sound with an effect.voidplay3D(float volume, float pitch, com.badlogic.gdx.math.Vector3 position) Plays a spatial sound with the given volume and pitch at the given position.voidplay3D(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.voidplay3D(float volume, com.badlogic.gdx.math.Vector3 position) Plays a spatial sound with the given volume at the given position.voidplay3D(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.voidplay3D(float volume, com.badlogic.gdx.math.Vector3 position, SoundEffect effect) Plays a spatial sound with the given volume and effect at the given position.voidplay3D(com.badlogic.gdx.math.Vector3 position) Plays a spatial sound at the given position.voidplay3D(com.badlogic.gdx.math.Vector3 position, float lowFreqVolume, float highFreqVolume) Plays a spatial sound with the given filter at the given position.voidplay3D(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.voidplay3D(com.badlogic.gdx.math.Vector3 position, SoundEffect effect) Plays a spatial sound at the given position with an effect.voidplayAtTime(long time) Plays the sound at the specified time.voidsetLoopPoints(float start, float end) Specifies the two offsets the source will use to loop, expressed in seconds.
-
Constructor Details
-
SoundBuffer
public SoundBuffer(byte[] pcm, int channels, int sampleRate, int bitsPerSample, PcmFormat.PcmDataType pcmDataType) Creates a SoundBuffer with the given pcm data.
8-bit data is expressed as an unsigned value over the range 0 to 255, 128 being an audio output level of zero .
16-bit data is expressed as a signed value over the range -32768 to 32767, 0 being an audio output level of zero.
Stereo data is expressed in an interleaved format, left channel sample followed by the right channel sample.
The interleaved format also applies to surround sound.- Parameters:
pcm- the pcm datachannels- number of channelssampleRate- number of samples per secondbitsPerSample- number of bits per samplepcmDataType- the pcm data type
-
SoundBuffer
public SoundBuffer(ShortBuffer pcm, int channels, int sampleRate, int bitsPerSample, PcmFormat.PcmDataType pcmDataType, int blockAlign) Creates a SoundBuffer with the given pcm data.
8-bit data is expressed as an unsigned value over the range 0 to 255, 128 being an audio output level of zero .
16-bit data is expressed as a signed value over the range -32768 to 32767, 0 being an audio output level of zero.
Stereo data is expressed in an interleaved format, left channel sample followed by the right channel sample.
The interleaved format also applies to surround sound.- Parameters:
pcm- the pcm data bufferchannels- number of channelssampleRate- number of samples per secondbitsPerSample- number of bits per samplepcmDataType- the pcm data typeblockAlign- the block alignment (currently only used for MS ADPCM data)
-
SoundBuffer
public SoundBuffer(byte[] pcm, int channels, int sampleRate, int bitsPerSample, PcmFormat.PcmDataType pcmDataType, int blockAlign) Creates a SoundBuffer with the given pcm data.
Consider usingSoundBuffer(byte[], int, int, int, PcmDataType)instead if you're not providing MS_ADPCM data.
8-bit data is expressed as an unsigned value over the range 0 to 255, 128 being an audio output level of zero .
16-bit data is expressed as a signed value over the range -32768 to 32767, 0 being an audio output level of zero.
Stereo data is expressed in an interleaved format, left channel sample followed by the right channel sample.
The interleaved format also applies to surround sound.- Parameters:
pcm- the pcm datachannels- number of channelssampleRate- number of samples per secondbitsPerSample- number of bits per samplepcmDataType- the pcm data typeblockAlign- the block alignment (currently only used for MS ADPCM data)
-
-
Method Details
-
generateBufferAndUpload
-
fetchSamplesPerChannel
protected int fetchSamplesPerChannel() -
fetchDuration
protected float fetchDuration() -
setLoopPoints
public void setLoopPoints(float start, float end) Specifies the two offsets the source will use to loop, expressed in seconds. This method will fail when the buffer is attached to a source like:
SoundSource source = audio.obtain(soundBuffer); soundBuffer.setLoopPoints(1f, 2f); // this will fail
If the playback position is manually set to something > end, the source will not loop and instead stop playback when it reaches the end of the sound.
The method will throw an exception if start >= end or if either is a negative value. Values > sound duration are not considered invalid, but they'll be clamped internally.- Parameters:
start- start position of the loop in secondsend- end position of the loop in seconds
-
getLoopPoints
public float[] getLoopPoints()Returns the loop points set bysetLoopPoints(float, float).
The returned array has a length of 2, with
index 0 => start
index 1 => end
The result might not exactly reflect the numbers fromsetLoopPoints(float, float)because there's a conversion from seconds to samples involved.
The returned float array is "owned" by theSoundBufferclass, so you shouldn't hold a reference to it but rather copy the values.- Returns:
- the loop points
-
play
public void play()Plays the sound. -
playAtTime
public void playAtTime(long time) 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.- Parameters:
time- the absolute time in nanoseconds, useAudioDevice.getClockTime()to get the current time
-
play
Plays a sound with an effect.- Parameters:
effect- the sound effect
-
play
public void play(float volume) Plays the sound with the given volume.- Parameters:
volume- 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:
volume- 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
public void play(float volume, float pitch) Plays the sound with the given volume and pitch.- Parameters:
volume- 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
public void play(float volume, float pitch, float lowFreqVolume, float highFreqVolume) Plays the sound with the given volume, pitch and filter.- Parameters:
volume- 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- range: 0 - 1, 0 means completely silent, 1 means full loudnesshighFreqVolume- range: 0 - 1, 0 means completely silent, 1 means full loudness
-
play
Plays the sound with the given volume, pitch and effect.- Parameters:
volume- 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
public void play(float volume, float pitch, float pan) Plays the sound with the given volume, pitch and pan.- Parameters:
volume- 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:
volume- 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
public void play3D(com.badlogic.gdx.math.Vector3 position) Plays a spatial sound at the given position.- Parameters:
position- the position in 3D space
-
play3D
public void play3D(com.badlogic.gdx.math.Vector3 position, float lowFreqVolume, float highFreqVolume) Plays a spatial sound with the given filter at the given position.- Parameters:
position- the position in 3D spacelowFreqVolume- range: 0 - 1, 0 means completely silent, 1 means full loudnesshighFreqVolume- range: 0 - 1, 0 means completely silent, 1 means full loudness
-
play3D
Plays a spatial sound at the given position with an effect.- Parameters:
position- the position in 3D spaceeffect- the sound effect
-
play3D
public void play3D(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:
position- the position in 3D spacelowFreqVolume- range: 0 - 1, 0 means completely silent, 1 means full loudnesshighFreqVolume- range: 0 - 1, 0 means completely silent, 1 means full loudnesseffect- the sound effect
-
play3D
public void play3D(float volume, com.badlogic.gdx.math.Vector3 position) Plays a spatial sound with the given volume at the given position.- Parameters:
volume- 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
public void play3D(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:
volume- 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- range: 0 - 1, 0 means completely silent, 1 means full loudnesshighFreqVolume- range: 0 - 1, 0 means completely silent, 1 means full loudness
-
play3D
Plays a spatial sound with the given volume and effect at the given position.- Parameters:
volume- 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
public void play3D(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:
volume- 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
public void play3D(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:
volume- 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 effect
-
getPcmFormat
-
getDuration
public float getDuration()Returns the duration in seconds.- Returns:
- the playback duration in seconds.
-
dispose
public void dispose()- Specified by:
disposein interfacecom.badlogic.gdx.utils.Disposable
-