Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class AudioData

  1. Object
  2. AudioData

In-memory PCM audio: one float array per channel, samples nominally in the range -1 to 1. This is the Web Audio AudioBuffer, named differently so it does not clash with the microphone capture class com.codename1.media.AudioBuffer.

Create one with BaseAudioContext.createBuffer(int, int, float) or [#fromInterleaved(float[], int, int, int, float)], play it with an AudioBufferSourceNode, and get one back from OfflineAudioContext.startRendering().

Constructors

public AudioData(int numberOfChannels, int length, float sampleRate)Creates silent audio.

Methods

public static AudioData fromInterleaved(float[] interleaved, int offset, int frames, int numberOfChannels, float sampleRate)Creates audio from interleaved samples, the layout WAVWriter, the microphone AudioBuffer and most decoders use.
public float[] toInterleaved()Copies the samples out interleaved, for WAVWriter or a port API.
public float getSampleRate()The sample rate.
public int getLength()The length.
public double getDuration()The duration.
public int getNumberOfChannels()The number of channels.
public float[] getChannelData(int channel)The live sample array of a channel.
public void copyFromChannel(float[] destination, int channel, int startInChannel)Copies samples of one channel into destination.
public void copyToChannel(float[] source, int channel, int startInChannel)Copies source into one channel.

Inherited methods

Constructor details

AudioData

public AudioData(int numberOfChannels, int length, float sampleRate)
Creates silent audio.

Parameters

numberOfChannels int
1 to 32
length int
the length in frames, at least 1
sampleRate float
frames per second, 3000 to 768000

Method details

fromInterleaved

public static AudioData fromInterleaved(float[] interleaved, int offset, int frames, int numberOfChannels, float sampleRate)
Creates audio from interleaved samples, the layout WAVWriter, the microphone AudioBuffer and most decoders use.

Parameters

interleaved float[]
the samples
offset int
the index of the first sample
frames int
the number of frames to copy
numberOfChannels int
the number of interleaved channels
sampleRate float
frames per second

Returns

a new AudioData holding a copy of the samples

toInterleaved

public float[] toInterleaved()
Copies the samples out interleaved, for WAVWriter or a port API.

Returns

a new array of getLength() * getNumberOfChannels() samples

getSampleRate

public float getSampleRate()
The sample rate.

Returns

frames per second

getLength

public int getLength()
The length.

Returns

the number of frames

getDuration

public double getDuration()
The duration.

Returns

the length in seconds

getNumberOfChannels

public int getNumberOfChannels()
The number of channels.

Returns

the channel count

getChannelData

public float[] getChannelData(int channel)
The live sample array of a channel. Writes to it are heard by any AudioBufferSourceNode that is playing this data.

Parameters

channel int
the channel index

Returns

the channel’s samples, not a copy

copyFromChannel

public void copyFromChannel(float[] destination, int channel, int startInChannel)
Copies samples of one channel into destination.

Parameters

destination float[]
the array to fill
channel int
the channel index
startInChannel int
the first frame to copy

copyToChannel

public void copyToChannel(float[] source, int channel, int startInChannel)
Copies source into one channel.

Parameters

source float[]
the samples
channel int
the channel index
startInChannel int
the first frame to write