Class SoundBuffer

java.lang.Object
de.pottgames.tuningfork.SoundBuffer
All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable
Direct Known Subclasses:
ReadableSoundBuffer

public class SoundBuffer extends Object implements com.badlogic.gdx.utils.Disposable
Stores sound data in an OpenAL buffer that can be used by sound sources. Needs to be disposed when no longer needed.
  • Constructor Summary

    Constructors
    Constructor
    Description
    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.
    SoundBuffer(byte[] pcm, int channels, int sampleRate, int bitsPerSample, PcmFormat.PcmDataType pcmDataType, int blockAlign)
    Creates a SoundBuffer with the given pcm data.
    Consider using SoundBuffer(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 Type
    Method
    Description
    void
     
    protected float
     
    protected int
     
    protected int
    generateBufferAndUpload(ShortBuffer pcm, int blockAlign, int sampleRate)
     
    float
    Returns the duration in seconds.
    float[]
    Returns the loop points set by setLoopPoints(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 from setLoopPoints(float, float) because there's a conversion from seconds to samples involved.
    The returned float array is "owned" by the SoundBuffer class, so you shouldn't hold a reference to it but rather copy the values.
    Returns the PcmFormat of this SoundBuffer.
    void
    Plays the sound.
    void
    play(float volume)
    Plays the sound with the given volume.
    void
    play(float volume, float pitch)
    Plays the sound with the given volume and pitch.
    void
    play(float volume, float pitch, float pan)
    Plays the sound with the given volume, pitch and pan.
    void
    play(float volume, float pitch, float lowFreqVolume, float highFreqVolume)
    Plays the sound with the given volume, pitch and filter.
    void
    play(float volume, float pitch, float pan, SoundEffect effect)
    Plays the sound with the given volume, pitch, pan and effect.
    void
    play(float volume, float pitch, SoundEffect effect)
    Plays the sound with the given volume, pitch and effect.
    void
    play(float volume, SoundEffect effect)
    Plays the sound with the given volume and effect.
    void
    play(SoundEffect effect)
    Plays a sound with an effect.
    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.
    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.
    void
    play3D(float volume, com.badlogic.gdx.math.Vector3 position)
    Plays a spatial sound with the given volume at the given position.
    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.
    void
    play3D(float volume, com.badlogic.gdx.math.Vector3 position, SoundEffect effect)
    Plays a spatial sound with the given volume and effect at the given position.
    void
    play3D(com.badlogic.gdx.math.Vector3 position)
    Plays a spatial sound at the given position.
    void
    play3D(com.badlogic.gdx.math.Vector3 position, float lowFreqVolume, float highFreqVolume)
    Plays a spatial sound with the given filter at the given position.
    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.
    void
    play3D(com.badlogic.gdx.math.Vector3 position, SoundEffect effect)
    Plays a spatial sound at the given position with an effect.
    void
    playAtTime(long time)
    Plays the sound at the specified time.
    void
    setLoopPoints(float start, float end)
    Specifies the two offsets the source will use to loop, expressed in seconds.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 data
      channels - number of channels
      sampleRate - number of samples per second
      bitsPerSample - number of bits per sample
      pcmDataType - 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 buffer
      channels - number of channels
      sampleRate - number of samples per second
      bitsPerSample - number of bits per sample
      pcmDataType - the pcm data type
      blockAlign - 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 using SoundBuffer(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 data
      channels - number of channels
      sampleRate - number of samples per second
      bitsPerSample - number of bits per sample
      pcmDataType - the pcm data type
      blockAlign - the block alignment (currently only used for MS ADPCM data)
  • Method Details

    • generateBufferAndUpload

      protected int generateBufferAndUpload(ShortBuffer pcm, int blockAlign, int sampleRate)
    • 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 seconds
      end - end position of the loop in seconds
    • getLoopPoints

      public float[] getLoopPoints()
      Returns the loop points set by setLoopPoints(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 from setLoopPoints(float, float) because there's a conversion from seconds to samples involved.
      The returned float array is "owned" by the SoundBuffer class, 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, use AudioDevice.getClockTime() to get the current time
    • play

      public void play(SoundEffect effect)
      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

      public void play(float volume, SoundEffect effect)
      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 loudness
      highFreqVolume - range: 0 - 1, 0 means completely silent, 1 means full loudness
    • play

      public void play(float volume, float pitch, SoundEffect effect)
      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

      public void play(float volume, float pitch, float pan, SoundEffect effect)
      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 space
      lowFreqVolume - range: 0 - 1, 0 means completely silent, 1 means full loudness
      highFreqVolume - range: 0 - 1, 0 means completely silent, 1 means full loudness
    • play3D

      public void play3D(com.badlogic.gdx.math.Vector3 position, SoundEffect effect)
      Plays a spatial sound at the given position with an effect.
      Parameters:
      position - the position in 3D space
      effect - 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 space
      lowFreqVolume - range: 0 - 1, 0 means completely silent, 1 means full loudness
      highFreqVolume - range: 0 - 1, 0 means completely silent, 1 means full loudness
      effect - 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 space
      lowFreqVolume - range: 0 - 1, 0 means completely silent, 1 means full loudness
      highFreqVolume - range: 0 - 1, 0 means completely silent, 1 means full loudness
    • play3D

      public void play3D(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:
      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
      effect - 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 space
      effect - the effect
    • getPcmFormat

      public PcmFormat getPcmFormat()
      Returns the PcmFormat of this SoundBuffer.
      Returns:
      the PcmFormat
    • getDuration

      public float getDuration()
      Returns the duration in seconds.
      Returns:
      the playback duration in seconds.
    • dispose

      public void dispose()
      Specified by:
      dispose in interface com.badlogic.gdx.utils.Disposable