Accessing the audio device directly

In addition to the Music and Sound interfaces, LibGDX also provides you with two more low-level audio interfaces, AudioDevice and AudioRecorder, that enable direct access to the audio device. They can be used for recording and playback of raw samples of audio data. These samples are stored as a PCM-encoded audio signal.

Note

These direct access features are currently unavailable in HTML5/GWT applications.

Exploring the AudioDevice interface

The AudioDevice interface allows you to send PCM-encoded audio samples directly to the audio device. For this to work, a new audio device can be requested using LibGDX's Gdx.audio module and called by its newAudioDevice() method as follows:

  AudioDevice audioDevice = Gdx.audio.newAudioDevice(44100, false);

The preceding line of code allocates a new instance of an audio device with a sample rate of 44.1 kHz in stereo mode. Requested instances of AudioDevice need to be disposed using the dispose() method when they are no longer needed in order to avoid memory leaks, as follows:

audioDevice.dispose(); // free allocated memory

New audio data can be sent to an audio device either using an array of floats or an array of 16-bit signed shorts, as shown here:

void writeSamples(float[] samples, int offset, int numSamples);
void writeSamples(short[] samples, int offset, int numSamples);

The offset (start) and numSamples (length) parameters are used to define the range of samples that will be sent to the audio device.

Note

Using an audio device with stereo mode enabled implies that the number of samples needs to be doubled as there are two separate audio channels to be fed with the audio data. Stereo samples are interleaved, starting with the left channel followed by the right channel; for example, to create a sound that will last for exactly one second at a sample rate of 44.1 kHz will require a total number of 44,100 samples in mono mode and 88,200 samples in stereo mode.

Exploring the AudioRecorder interface

The AudioRecorder interface allows you to record samples in a 16-bit PCM format using a connected microphone. New instances of AudioRecorder can be requested using LibGDX's Gdx.audio module and by calling its newAudioRecorder() method as follows:

AudioRecorder audioRecordedr = Gdx.audio.newAudioRecorder(44100, false);

Basically, AudioRecorder works nearly the same as AudioDevice except that it captures samples. As always, the unused instances need to be disposed in order to avoid memory leaks, as shown here:

audioRecorder.dispose(); // free allocated memory

To record samples with the audio recorder, all that is needed is an array into which the captured samples will be stored:

void read(short[] samples, int offset, int numSamples);

The offset (start) and numSamples (length) parameters are used to define which samples in the samples target array will be overwritten with new data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset