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.
-
Method Summary
Modifier and TypeMethodDescriptionvoidgetByteFrequencyData(byte[] array) Copies the spectrum scaled fromgetMinDecibels()(0) togetMaxDecibels()(255).voidgetByteTimeDomainData(byte[] array) Copies the newest samples as bytes, 128 being silence.intThe analysis window, 2048 by default.voidgetFloatFrequencyData(float[] array) Copies the spectrum in decibels, binkbeingk * sampleRate / fftSizeHz.voidgetFloatTimeDomainData(float[] array) Copies the newest samples, as -1 to 1.intHalf the FFT size: the number of values the frequency getters fill.doubleThe decibel value that maps to byte 255, -30 by default.doubleThe decibel value that maps to byte 0, -100 by default.doubleHow much of the previous spectrum each new one keeps, 0.8 by default.voidsetFftSize(int size) Sets the analysis window.voidsetMaxDecibels(double db) Sets the decibel value that maps to byte 255.voidsetMinDecibels(double db) Sets the decibel value that maps to byte 0.voidsetSmoothingTimeConstant(double value) Sets how much of the previous spectrum each new one keeps.Methods inherited from class AudioNode
connect, connect, connect, connect, disconnect, disconnect, disconnect, disconnect, disconnect, getChannelCount, getChannelCountMode, getChannelInterpretation, getContext, getNumberOfInputs, getNumberOfOutputs, setChannelCount, setChannelCountMode, setChannelInterpretation
-
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: 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) -
getMaxDecibels
public double getMaxDecibels()The decibel value that maps to byte 255, -30 by default.
Returns
decibels
-
setMaxDecibels
public void setMaxDecibels(double db) -
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: 0 (no smoothing) to 1
-
getFloatTimeDomainData
public void getFloatTimeDomainData(float[] array) -
getByteTimeDomainData
public void getByteTimeDomainData(byte[] array) Copies the newest samples as bytes, 128 being silence.
Parameters
array: filled with up togetFftSize()values
-
getFloatFrequencyData
public void getFloatFrequencyData(float[] array) Copies the spectrum in decibels, bin
kbeingk * sampleRate / fftSizeHz.Parameters
array: filled with up togetFrequencyBinCount()values
-
getByteFrequencyData
public void getByteFrequencyData(byte[] array) Copies the spectrum scaled from
getMinDecibels()(0) togetMaxDecibels()(255).Parameters
array: filled with up togetFrequencyBinCount()values
-