Class AnalyserNode

java.lang.Object
com.codename1.media.audio.AudioNode
com.codename1.media.audio.AnalyserNode

public final class AnalyserNode extends AudioNode

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 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)

      Sets the decibel value that maps to byte 0.

      Parameters
    • 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
    • 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)

      Copies the newest samples, as -1 to 1.

      Parameters
    • getByteTimeDomainData

      public void getByteTimeDomainData(byte[] array)

      Copies the newest samples as bytes, 128 being silence.

      Parameters
    • getFloatFrequencyData

      public void getFloatFrequencyData(float[] array)

      Copies the spectrum in decibels, bin k being k * sampleRate / fftSize Hz.

      Parameters
    • getByteFrequencyData

      public void getByteFrequencyData(byte[] array)

      Copies the spectrum scaled from getMinDecibels() (0) to getMaxDecibels() (255).

      Parameters