frame-msg
    Preparing search index...

    Class RxAudio

    RxAudio class handles audio data streaming and processing. It can operate in two modes: streaming and single-clip mode. In streaming mode, it processes audio data in real-time. In single-clip mode, it accumulates audio data until a final chunk is received. The class provides methods to attach and detach from a FrameMsg instance, and to convert PCM data to WAV format. Depending on how it is constructed, it will return samples as either signed 8 or signed 16 bit integers, and the source bit depth in Lua should match.

    Index

    Constructors

    Properties

    queue:
        | null
        | AsyncQueue<
            null
            | Int8Array<ArrayBufferLike>
            | Int16Array<ArrayBufferLike>,
        >

    Asynchronous queue for processed audio data chunks. Each chunk is an Int8Array or Int16Array, or null to signal the end of a stream/clip.

    Methods

    • Attaches this RxAudio instance to a FrameMsg object to receive audio data. It initializes the audio queue and registers a handler for incoming data.

      Parameters

      • frame: FrameMsg

        The FrameMsg instance to attach to.

      Returns Promise<
          AsyncQueue<
              null
              | Int8Array<ArrayBufferLike>
              | Int16Array<ArrayBufferLike>,
          >,
      >

      A Promise that resolves to the AsyncQueue where audio chunks will be placed.

    • Detaches this RxAudio instance from a FrameMsg object. It unregisters the data handler and clears the audio queue.

      Parameters

      • frame: FrameMsg

        The FrameMsg instance to detach from.

      Returns void

    • Handles incoming raw audio data chunks. This method is typically called by a FrameMsg instance when new audio data is received. It processes the data based on the configured mode (streaming or clip) and bit depth, then places the processed audio chunk (Int8Array or Int16Array) onto the queue. A null is placed on the queue to signal the end of a stream or clip.

      Parameters

      • data: Uint8Array

        A Uint8Array containing the raw audio data, prefixed with a msgCode byte.

      Returns void

    • Converts signed 16-bit PCM data (provided as a Uint8Array) to a Float32Array. The samples are scaled to the range [-1.0, 1.0]. Note: Assumes input Uint8Array contains little-endian signed 16-bit samples. Samples are scaled by 1/16384 (instead of 1/32768) and clamped to [-1.0, 1.0] to account for typical microphone dynamic range usage.

      Parameters

      • pcmData: Uint8Array

        The Uint8Array containing signed 16-bit PCM data (little-endian).

      Returns Float32Array<ArrayBuffer>

      A Float32Array with samples scaled to [-1.0, 1.0].

    • Converts signed 8-bit PCM data (provided as a Uint8Array) to a Float32Array. The samples are scaled to the range [-1.0, 1.0]. Note: Assumes input samples are signed bytes. Reinterprets Uint8Array as Int8Array. Samples are scaled by 1/64 (instead of 1/128) and clamped to [-1.0, 1.0] to account for typical microphone dynamic range usage.

      Parameters

      • pcmData: Uint8Array

        The Uint8Array containing signed 8-bit PCM data.

      Returns Float32Array<ArrayBuffer>

      A Float32Array with samples scaled to [-1.0, 1.0].

    • Converts raw PCM audio data to WAV file format (as a Uint8Array).

      Parameters

      • pcmData: Uint8Array

        The raw PCM data. For 8-bit, assumes signed samples that will be converted to unsigned for WAV.

      • sampleRate: number = 8000

        The sample rate of the audio (e.g., 8000 Hz). Defaults to 8000.

      • bitsPerSample: number = 8

        The number of bits per sample (e.g., 8 or 16). Defaults to 8.

      • channels: number = 1

        The number of audio channels (e.g., 1 for mono). Defaults to 1.

      Returns Uint8Array

      A Uint8Array containing the WAV file data.