Class AudioData

java.lang.Object
com.codename1.media.audio.AudioData

public final class AudioData extends Object

In-memory PCM audio: one float array per channel, samples nominally in the range -1 to 1. This is the Web Audio AudioBuffer, named differently so it does not clash with the microphone capture class com.codename1.media.AudioBuffer.

Create one with BaseAudioContext.createBuffer(int, int, float) or [#fromInterleaved(float[], int, int, int, float)], play it with an AudioBufferSourceNode, and get one back from OfflineAudioContext.startRendering().

  • Constructor Summary

    Constructors
    Constructor
    Description
    AudioData(int numberOfChannels, int length, float sampleRate)
    Creates silent audio.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    copyFromChannel(float[] destination, int channel, int startInChannel)
    Copies samples of one channel into destination.
    void
    copyToChannel(float[] source, int channel, int startInChannel)
    Copies source into one channel.
    static AudioData
    fromInterleaved(float[] interleaved, int offset, int frames, int numberOfChannels, float sampleRate)
    Creates audio from interleaved samples, the layout WAVWriter, the microphone AudioBuffer and most decoders use.
    float[]
    getChannelData(int channel)
    The live sample array of a channel.
    double
    The duration.
    int
    The length.
    int
    The number of channels.
    float
    The sample rate.
    float[]
    Copies the samples out interleaved, for WAVWriter or a port API.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AudioData

      public AudioData(int numberOfChannels, int length, float sampleRate)

      Creates silent audio.

      Parameters
      • numberOfChannels: 1 to 32

      • length: the length in frames, at least 1

      • sampleRate: frames per second, 3000 to 768000

  • Method Details

    • fromInterleaved

      public static AudioData fromInterleaved(float[] interleaved, int offset, int frames, int numberOfChannels, float sampleRate)

      Creates audio from interleaved samples, the layout WAVWriter, the microphone AudioBuffer and most decoders use.

      Parameters
      • interleaved: the samples

      • offset: the index of the first sample

      • frames: the number of frames to copy

      • numberOfChannels: the number of interleaved channels

      • sampleRate: frames per second

      Returns

      a new AudioData holding a copy of the samples

    • toInterleaved

      public float[] toInterleaved()

      Copies the samples out interleaved, for WAVWriter or a port API.

      Returns

      a new array of getLength() * getNumberOfChannels() samples

    • getSampleRate

      public float getSampleRate()

      The sample rate.

      Returns

      frames per second

    • getLength

      public int getLength()

      The length.

      Returns

      the number of frames

    • getDuration

      public double getDuration()

      The duration.

      Returns

      the length in seconds

    • getNumberOfChannels

      public int getNumberOfChannels()

      The number of channels.

      Returns

      the channel count

    • getChannelData

      public float[] getChannelData(int channel)

      The live sample array of a channel. Writes to it are heard by any AudioBufferSourceNode that is playing this data.

      Parameters
      • channel: the channel index
      Returns

      the channel's samples, not a copy

    • copyFromChannel

      public void copyFromChannel(float[] destination, int channel, int startInChannel)

      Copies samples of one channel into destination.

      Parameters
      • destination: the array to fill

      • channel: the channel index

      • startInChannel: the first frame to copy

    • copyToChannel

      public void copyToChannel(float[] source, int channel, int startInChannel)

      Copies source into one channel.

      Parameters
      • source: the samples

      • channel: the channel index

      • startInChannel: the first frame to write