Sound Sources - Hangman/TuningFork GitHub Wiki
Obtain BufferedSoundSources
BufferedSoundSource soundSource = audio.obtainSource(sound);
After you're done using the sound source, you must call "free" on it, in order to release it back to the pool of available sound sources.
soundSource.free();
Create StreamedSoundSources
StreamedSoundSources behave the same like their buffered counterparts but stream audio data from disk.
StreamedSoundSource streamedSoundSource = new StreamedSoundSource(Gdx.files.internal("sound.wav"));
When done with using the StreamedSoundSource, don't forget to dispose it.
streamedSoundSource.dispose();
What can I do with a SoundSource?
Here's a full list:
This is just to provide an overview. All methods are well documented in javadoc.
- set volume
- set pitch
- play, stop, pause, is paused, is playing
- set relative
- set/get position
- set speed
- make directional, make omni directional, is directional
- set direction
- set virtualization mode
- set spatialization mode
- enable/disable attenuation
- set/get attenuation factor
- set/get attenuation min distance, set/get attenuation max distance
- set looping
- attach effect, detach effect, detach all effects
- set filter
- set/get playback position
- get duration
- set/get the resampler
BufferedSoundSources only:
- free
- play at a specific time (delayed playback)
StreamedSoundSources and PCMSoundSources only:
- dispose
PcmSoundSource
The PcmSoundSource is able to take raw pcm input in a real-time fashion. It's meant for streaming and/or live creation of sounds or effects from custom sources, just like libGDX's AudioDevice
. If you have precomputed pcm data and just want to load it in memory and be able to play it, have a look at the SoundBuffer class instead.
To create a PcmSoundSource:
PcmSoundSource source = new PcmSoundSource(sampleRate, PcmFormat.STEREO_16_BIT);
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. 32 and 64 bit Float data is expressed over the range -1 to 1 with 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. All the surround sound formats are also in an interleaved format. To push samples to the source:
source.queueSamples(pcmData, offset, length);
pcmData is a byte array. offset is the start index where to begin reading pcm data in the pcm byte array. length is the length of the pcm data that should be read. You can queue as much data as you want and as often as you want. Note: An underflow of pcm data will cause the source to stop. If you want it to keep playing, call play() after queueing samples.