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

public final class AnalyserNode

  1. Object
  2. AudioNode
  3. AnalyserNode

Exposes the recent signal and its spectrum for visualisation, passing its input through unchanged: the Web Audio AnalyserNode.

OfflineAudioContext audio = new OfflineAudioContext(2, 44100, 44100);
OscillatorNode player = audio.createOscillator();
AnalyserNode an = audio.createAnalyser();
an.setFftSize(1024);
player.connect(an).connect(audio.getDestination());
byte[] bars = new byte[an.getFrequencyBinCount()];
// in an animation callback
an.getByteFrequencyData(bars);

The input is down-mixed to mono for analysis. The frequency data is a Blackman windowed FFT of the newest getFftSize() samples, smoothed over time and converted to decibels, as the specification defines it.

Call the getters from one thread, normally the EDT: the spectrum getters share their working arrays. With a real-time AudioContext the render thread keeps writing while they copy, so a copy is the latest audio at the moment it was taken; it is meant for drawing, not for sample-exact measurement, which belongs to an OfflineAudioContext.

Methods

public int getFftSize()The analysis window, 2048 by default.
public void setFftSize(int size)Sets the analysis window.
public int getFrequencyBinCount()Half the FFT size: the number of values the frequency getters fill.
public double getMinDecibels()The decibel value that maps to byte 0, -100 by default.
public void setMinDecibels(double db)Sets the decibel value that maps to byte 0.
public double getMaxDecibels()The decibel value that maps to byte 255, -30 by default.
public void setMaxDecibels(double db)Sets the decibel value that maps to byte 255.
public double getSmoothingTimeConstant()How much of the previous spectrum each new one keeps, 0.8 by default.
public void setSmoothingTimeConstant(double value)Sets how much of the previous spectrum each new one keeps.
public void getFloatTimeDomainData(float[] array)Copies the newest samples, as -1 to 1.
public void getByteTimeDomainData(byte[] array)Copies the newest samples as bytes, 128 being silence.
public void getFloatFrequencyData(float[] array)Copies the spectrum in decibels, bin k being k * sampleRate / fftSize Hz.
public void getByteFrequencyData(byte[] array)Copies the spectrum scaled from getMinDecibels() (0) to getMaxDecibels() (255).

Inherited methods

Method details

getFftSize

public int getFftSize()
The analysis window, 2048 by default.

Returns

a power of two from 32 to 32768

setFftSize

public void setFftSize(int size)
Sets the analysis window.

Parameters

size int
a power of two from 32 to 32768

getFrequencyBinCount

public int getFrequencyBinCount()
Half the FFT size: the number of values the frequency getters fill.

Returns

the bin count

getMinDecibels

public double getMinDecibels()
The decibel value that maps to byte 0, -100 by default.

Returns

decibels

setMinDecibels

public void setMinDecibels(double db)
Sets the decibel value that maps to byte 0.

Parameters

db double
less than getMaxDecibels()

getMaxDecibels

public double getMaxDecibels()
The decibel value that maps to byte 255, -30 by default.

Returns

decibels

setMaxDecibels

public void setMaxDecibels(double db)
Sets the decibel value that maps to byte 255.

Parameters

db double
more than getMinDecibels()

getSmoothingTimeConstant

public double getSmoothingTimeConstant()
How much of the previous spectrum each new one keeps, 0.8 by default.

Returns

0 to 1

setSmoothingTimeConstant

public void setSmoothingTimeConstant(double value)
Sets how much of the previous spectrum each new one keeps.

Parameters

value double
0 (no smoothing) to 1

getFloatTimeDomainData

public void getFloatTimeDomainData(float[] array)
Copies the newest samples, as -1 to 1.

Parameters

array float[]
filled with up to getFftSize() values

getByteTimeDomainData

public void getByteTimeDomainData(byte[] array)
Copies the newest samples as bytes, 128 being silence.

Parameters

array byte[]
filled with up to getFftSize() values

getFloatFrequencyData

public void getFloatFrequencyData(float[] array)
Copies the spectrum in decibels, bin k being k * sampleRate / fftSize Hz.

Parameters

array float[]
filled with up to getFrequencyBinCount() values

getByteFrequencyData

public void getByteFrequencyData(byte[] array)
Copies the spectrum scaled from getMinDecibels() (0) to getMaxDecibels() (255).

Parameters

array byte[]
filled with up to getFrequencyBinCount() values