Class AudioWaveform

java.lang.Object
com.codename1.ui.Component
com.codename1.components.AudioWaveform
All Implemented Interfaces:
Animation, Editable, StyleListener

public class AudioWaveform extends Component

The bars of an audio clip's loudness, with the played part in the accent colour: the waveform of a voice message, a recording or a podcast scrubber.

A voice message waveform, partly played

Form form = new Form("Voice message", BoxLayout.y());
// 16-bit samples, normally decoded from the recording
short[] samples = new short[4800];
for (int i = 0; i < samples.length; i++) {
    samples[i] = (short) (Math.sin(i / 40.0) * 12000 * Math.sin(i / 900.0));
}
AudioWaveform wave = new AudioWaveform(AudioWaveform.levelsFromPcm(samples, 48));
wave.addActionListener(e -> Log.p("Seek to " + wave.getProgress()));
form.add(wave);
form.show();
// as playback advances (a Media's getTime() / getDuration()),
// unless the user's finger is on it
if (!wave.isSeeking()) {
    wave.setProgress(0.25f);
}

Levels are fractions from 0 to 1, one per bar, and are drawn evenly across the width. Tapping or dragging seeks: the progress follows the finger, and action listeners fire once when it lifts, so the component doubles as the clip's scrubber. Call setSeekable(false) for a display only waveform.

UIIDs: AudioWaveform. The played bars take the foreground of AudioWaveformPlayed and the rest the background of AudioWaveformTrack when the theme defines them, and otherwise the theme's Slider colours.

  • Constructor Details

    • AudioWaveform

      public AudioWaveform()
      An empty waveform; call #setLevels(float[]).
    • AudioWaveform

      public AudioWaveform(float[] levels)

      A waveform of the given levels.

      Parameters
      • levels: one loudness per bar, 0 to 1
  • Method Details

    • initUnselectedStyle

      protected void initUnselectedStyle(Style unselectedStyle)
      Description copied from class: Component

      Can be overridden by subclasses to perform initialization when the unselected style is set to a new value.

      Parameters
      • unselectedStyle: The unselected style.
      Overrides:
      initUnselectedStyle in class Component
    • levelsFromPcm

      public static float[] levelsFromPcm(short[] samples, int bars)

      Reduces raw 16 bit PCM samples to bars levels: the peak of each slice, scaled so the loudest bar is 1.

      Parameters
      • samples: the PCM samples

      • bars: how many levels to produce

      Returns

      the levels, 0 to 1

    • getLevels

      public float[] getLevels()

      The levels drawn, one per bar.

      Returns

      a copy of the levels

    • setLevels

      public final void setLevels(float[] levels)

      Sets the levels, one per bar, each clamped to 0 to 1.

      Parameters
      • levels: the loudness of each bar
    • getProgress

      public float getProgress()

      How much has played, 0 to 1.

      Returns

      the played fraction

    • setProgress

      public void setProgress(float progress)

      Sets how much has played, clamped to 0 to 1.

      Parameters
      • progress: the played fraction
    • isSeekable

      public boolean isSeekable()

      Whether tapping and dragging seek.

      Returns

      true by default

    • setSeekable

      public void setSeekable(boolean seekable)

      Turns seeking by touch on or off.

      Parameters
      • seekable: false for a display only waveform
    • isSeeking

      public boolean isSeeking()

      True while the user drags across the waveform. Listeners are not called during the drag, only when it ends, so #setProgress(float) calls from playback can be skipped while this is true.

      Returns

      whether a seek drag is in progress

    • setBarSize

      public void setBarSize(float barMM, float gapMM, float heightMM)

      Sets the bar geometry.

      Parameters
      • barMM: each bar's width in millimetres

      • gapMM: the space between bars in millimetres

      • heightMM: the preferred height in millimetres

    • addActionListener

      public void addActionListener(ActionListener l)

      Adds a listener called when the user seeks.

      Parameters
      • l: the listener
    • removeActionListener

      public void removeActionListener(ActionListener l)

      Removes a listener.

      Parameters
      • l: the listener
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Description copied from class: Component

      Calculates the preferred size based on component content. This method is invoked lazily by getPreferred size.

      Returns

      the calculated preferred size based on component content

      Overrides:
      calcPreferredSize in class Component
    • paint

      public void paint(Graphics g)
      Description copied from class: Component

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Component
    • pointerPressed

      public void pointerPressed(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer pressed event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerPressed in class Component
    • pointerDragged

      public void pointerDragged(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer dragged event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerDragged in class Component
    • pointerReleased

      public void pointerReleased(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer released event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerReleased in class Component
    • deinitialize

      protected void deinitialize()
      Description copied from class: Component
      Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. This allows the component to deregister animators and cleanup after itself. This method is the opposite of the initComponent() method.
      Overrides:
      deinitialize in class Component
    • isStickyDrag

      protected boolean isStickyDrag()
      Description copied from class: Component

      Returns true if the component is interested in receiving drag/pointer release events even after the gesture exceeded its boundaries. This is useful for spinners etc. where the motion might continue beyond the size of the component

      Returns

      false by default

      Overrides:
      isStickyDrag in class Component