Skip to Content
defold-typescript libraries
HalfstarDev

HalfstarDev/defold-mic/mic

Functions for microphone recording via miniaudio

  • GitHub: HalfstarDev/defold-mic — pinned to 0.1.0 (released while Defold 1.12.2 was current)
    1. Pick a release from HalfstarDev/defold-mic releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as mic from "mic"
  • Asset Portal: defold-mic

Functions

mic.start(options: table)

Begin audio capture.

Parameters

  • options: table — Optional table of capture settings. 0️⃣ device_index is 0-based; passed to mic unchanged.
    • device_index: number — Zero-based index of the capture device to use, as returned by mic.get_devices(). Defaults to the system default device.
    • sample_rate: number — Sample rate in Hz. Usually 16000, 22050, 44100, or 48000. Defaults to 16000.
    • channels: number — Number of audio channels. Must be 1 (mono) or 2 (stereo). Defaults to 1.
    • max_seconds: number — Maximum number of seconds to buffer. Once reached, incoming audio is discarded. 0 or omitted means unlimited.
    • noise_gate_threshold: number — Amplitude threshold in range [0.0, 1.0]. Frames whose peak amplitude falls below are silenced. Defaults to 0.

mic.stop(options: table): string

Stop audio capture and return the recorded audio as a WAV byte string.

Parameters

  • options: table — Optional table of capture settings.
    • normalize_peak: number — Peak value to normalize recorded audio to. Must be in (0.0, 1.0].

Returns

  • wav: string — Raw WAV-encoded audio bytes, or nil if nothing was recorded.

mic.is_recording(): boolean

Check whether the microphone is currently capturing audio.

Returns

  • recording: boolean — True if currently recording, false otherwise.

mic.is_connected(): boolean

Check whether at least one capture device is available on the system.

Returns

  • connected: boolean — True if one or more capture devices are available, false otherwise.

mic.get_devices(): table

Enumerate all available capture devices.

Returns

  • devices: table — List of device info tables, with keys 'index' and 'name'.

mic.get_current_device(): string

Returns the name of the current capture device.

Returns

  • device: string — Name of the current capture device.

mic.get_peak(): number

Get the peak amplitude of the current block.

Returns

  • peak: number — Normalised peak amplitude in the range [0.0, 1.0], or 0.0 if not recording.

mic.get_rms(): number

Get the RMS (root mean square) volume of the current block.

Returns

  • rms: number — Normalised RMS value in the range [0.0, 1.0], or 0.0 if not recording.

mic.get_duration(): number

Get the duration of audio buffered so far in the current recording.

Returns

  • duration: number — Duration in seconds, or 0.0 if not recording.

mic.request_permission()

Request permission to use the microphone. (macOS and iOS only)

mic.get_permission_status(): string

Get the current status of the permission to use the microphone. (macOS and iOS only)

Returns

  • status: string — The current status of the permission for recording.

On this page