@molecule/app-audio-recording

Core interface · audio-recording · App (browser) · v1.0.0 · Apache-2.0

Portable audio recording sessions and provider wiring.

npm install @molecule/app-audio-recording

npm · Source on GitHub

How it works

@molecule/app-audio-recording is the audio-recording core interface on the app (browser) side: the API your app calls, with no vendor inside.

Choose the implementation by bonding one of its 2 providers: @molecule/app-audio-recording-expo, @molecule/app-audio-recording-media-recorder.

import { getProvider, setProvider } from '@molecule/app-audio-recording'
import { provider } from '@molecule/app-audio-recording-media-recorder'

setProvider(provider)

async function startVoiceNote() {
  const started = await getProvider()?.start()
  if (started?.status !== 'started') return
  return started.session
}

Providers (2): @molecule/app-audio-recording-expo, @molecule/app-audio-recording-media-recorder

Works with: @molecule/app-bond, @molecule/app-filesystem

Reference

Records audio through a swappable provider. Applications bond a provider at startup. Request a session from a user-initiated Record action to acquire the microphone, control capture, and observe its one terminal result.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { getProvider, setProvider } from '@molecule/app-audio-recording'
import { provider } from '@molecule/app-audio-recording-media-recorder'

setProvider(provider)

async function startVoiceNote() {
  const started = await getProvider()?.start()
  if (started?.status !== 'started') return
  return started.session
}

Type

core

Installation

npm install @molecule/app-audio-recording @molecule/app-bond @molecule/app-filesystem

API

Interfaces

AudioRecording

The encoded audio and its portable upload representation.

interface AudioRecording {
  /** The encoded bytes as a Blob, including on devices. */
  blob: Blob
  /** MIME type of the audio actually captured. */
  mimeType: string
  /** Active time in whole seconds, excluding pauses. */
  durationSeconds: number
  /** The same recording with its filename, byte reader, and multipart helper. */
  file: PickedFile
}

RecordingError

A recording failure that a consumer can present without platform-specific branching.

interface RecordingError {
  /** Where recording failed. Only permission failures use `permission-denied`. */
  code: 'unsupported' | 'permission-denied' | 'start-failed' | 'recording-failed'
  /** The platform error, when available. */
  cause?: unknown
}

RecordingSession

One active microphone acquisition and its recording.

interface RecordingSession {
  /** Settles once after releasing the microphone, including on platform-initiated completion. */
  readonly result: Promise<RecordingResult>
  /** Reads the active duration in seconds with subsecond precision. */
  getDurationSeconds(): number
  /** Pauses capture, preserving the bytes and active duration so far. */
  pause(): void
  /** Continues a paused recording without counting time spent paused. */
  resume(): void
  /** Requests finalization. Repeated requests share the same result. */
  stop(): void
  /** Releases acquisition and discards bytes that have not been delivered. */
  discard(): Promise<void>
}

RecordingOptions

Options for acquiring the microphone and starting capture.

interface RecordingOptions {
  /** Preferred MIME type. The provider can fall back to its supported format. */
  mimeType?: string
  /** Cancels acquisition. After a session starts, use its `discard()` method. */
  signal?: AbortSignal
}

RecordingProvider

Provider for acquiring the microphone and recording audio.

interface RecordingProvider {
  /**
   * Acquires microphone access and starts capture.
   *
   * @param options - Preferred format and cancellation for acquisition.
   * @returns A running session or its acquisition outcome.
   */
  start(options?: RecordingOptions): Promise<RecordingStartResult>
}

Types

RecordingResult

The single terminal outcome of a started session.

type RecordingResult =
  | { status: 'recorded'; recording: AudioRecording }
  | { status: 'discarded' }
  | { status: 'error'; error: RecordingError }

RecordingStartResult

The outcome of acquiring the microphone and starting capture.

type RecordingStartResult =
  | { status: 'started'; session: RecordingSession }
  | { status: 'discarded' }
  | { status: 'error'; error: RecordingError }

Functions

setProvider(provider)

Bonds the recording provider used by the app.

function setProvider(provider: RecordingProvider): void
  • provider — The recording provider.

getProvider()

Reads the current audio recording provider.

function getProvider(): RecordingProvider | null

Returns: The provider, or null when one has yet to be bonded.

hasProvider()

Checks whether the app has bonded an audio recording provider.

function hasProvider(): boolean

Returns: Whether a recording provider is available.

Available Providers

ProviderPackage
Expo Audio Recording@molecule/app-audio-recording-expo
MediaRecorder@molecule/app-audio-recording-media-recorder

Injection Notes

Requirements

Peer dependencies:

  • @molecule/app-bond ^1.0.5
  • @molecule/app-filesystem ^1.1.1

Runtime Dependencies

  • @molecule/app-bond
  • @molecule/app-filesystem