Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class 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()Returns
setFftSize
public void setFftSize(int size)Parameters
sizeint- a power of two from 32 to 32768
getFrequencyBinCount
public int getFrequencyBinCount()Returns
getMinDecibels
public double getMinDecibels()Returns
setMinDecibels
public void setMinDecibels(double db)Parameters
dbdouble- less than
getMaxDecibels()
getMaxDecibels
public double getMaxDecibels()Returns
setMaxDecibels
public void setMaxDecibels(double db)Parameters
dbdouble- more than
getMinDecibels()
getSmoothingTimeConstant
public double getSmoothingTimeConstant()Returns
setSmoothingTimeConstant
public void setSmoothingTimeConstant(double value)Parameters
valuedouble- 0 (no smoothing) to 1
getFloatTimeDomainData
public void getFloatTimeDomainData(float[] array)Parameters
arrayfloat[]- filled with up to
getFftSize()values
getByteTimeDomainData
public void getByteTimeDomainData(byte[] array)Parameters
arraybyte[]- filled with up to
getFftSize()values
getFloatFrequencyData
public void getFloatFrequencyData(float[] array)k being k * sampleRate / fftSize Hz.Parameters
arrayfloat[]- filled with up to
getFrequencyBinCount()values
getByteFrequencyData
public void getByteFrequencyData(byte[] array)getMinDecibels() (0) to
getMaxDecibels() (255).Parameters
arraybyte[]- filled with up to
getFrequencyBinCount()values