Skip to main content

@flow-state-dev/react/voice

Type Aliases​

AudioPlayer​

type AudioPlayer = object;

Properties​

state​
readonly state: AudioPlayerStatus;

Methods​

dispose()​
dispose(): void;

Releases the underlying AudioContext. Browsers cap concurrent contexts (Chrome ~6, Safari historically lower); long-lived SPAs that remount voice surfaces will eventually hit the cap if contexts leak. useVoice calls this in its unmount cleanup effect.

Returns​

void

enqueue()​
enqueue(audioData, mediaType): void;

Whole-buffer path retained for batch (non-streaming) providers. Equivalent to enqueueChunk({ audio, mediaType, isLast: true }).

Parameters​
audioData​

string

mediaType​

string

Returns​

void

enqueueChunk()​
enqueueChunk(chunk): void;

Streaming path: append a single chunk. Decoded and scheduled against the shared AudioContext timeline for gap-free playback.

Parameters​
chunk​

AudioChunk

Returns​

void

stop()​
stop(): void;
Returns​

void


AudioPlayerCallbacks​

type AudioPlayerCallbacks = object;

Properties​

onChunkEnd?​
onChunkEnd?: (index) => void;
Parameters​
index​

number

Returns​

void

onChunkStart?​
onChunkStart?: (index) => void;
Parameters​
index​

number

Returns​

void

onError?​
onError?: (error) => void;
Parameters​
error​

Error

Returns​

void

onStateChange?​
onStateChange?: (state) => void;
Parameters​
state​

AudioPlayerStatus

Returns​

void


AudioPlayerStatus​

type AudioPlayerStatus = "idle" | "playing";

Streaming-aware audio player for TTS output.

Backed by the Web Audio API: each chunk is decoded into an AudioBuffer and scheduled on a AudioBufferSourceNode at a moving nextStartTime cursor so consecutive sources butt up gap-free. This is the production pattern used by OpenAI Realtime Console and ElevenLabs community SDKs; the previous <audio> + Blob URL approach had audible gaps between chunks that defeated the latency win of streaming TTS.

Supports both the streaming path (per-chunk enqueueChunk, used by the streaming TTS pipeline) and the whole-buffer path (enqueue, used by batch providers like OpenAI). The whole-buffer path is a thin wrapper around enqueueChunk with isLast: true.

MP3 (audio/mpeg) is the only supported codec in M1 — works with decodeAudioData on all modern browsers including Safari iOS. PCM and WAV require either WAV-header injection or an AudioWorklet path and are deferred.


AudioRecorder​

type AudioRecorder = object;

@flow-state-dev/react/voice: the recorder, player and speech-recognition primitives, for an app that builds its own voice loop.

useVoice drives all three and is what most apps want, so it stays on the package root; these are the parts underneath it.

Properties​

state​
readonly state: AudioRecorderStatus;

Methods​

cancel()​
cancel(): void;
Returns​

void

start()​
start(): Promise<void>;
Returns​

Promise<void>

stop()​
stop(): Promise<Blob>;
Returns​

Promise<Blob>


AudioRecorderOptions​

type AudioRecorderOptions = object;

MediaRecorder wrapper for capturing audio from the browser microphone. Provides start/stop/getBlob with configurable MIME type.

Properties​

audioBitsPerSecond?​
audioBitsPerSecond?: number;
mimeType?​
mimeType?: string;

AudioRecorderStatus​

type AudioRecorderStatus = "inactive" | "recording";

@flow-state-dev/react/voice: the recorder, player and speech-recognition primitives, for an app that builds its own voice loop.

useVoice drives all three and is what most apps want, so it stays on the package root; these are the parts underneath it.


SpeechRecognitionCallbacks​

type SpeechRecognitionCallbacks = object;

Browser SpeechRecognition wrapper for interim transcript feedback. Provides real-time interim text while the user is speaking, used in the hybrid STT approach alongside server-side Whisper.

Properties​

onEnd?​
onEnd?: () => void;
Returns​

void

onError?​
onError?: (error) => void;
Parameters​
error​

Error

Returns​

void

onFinalTranscript?​
onFinalTranscript?: (text) => void;
Parameters​
text​

string

Returns​

void

onInterimTranscript?​
onInterimTranscript?: (text) => void;
Parameters​
text​

string

Returns​

void


SpeechRecognitionHandle​

type SpeechRecognitionHandle = object;

Properties​

isAvailable​
readonly isAvailable: boolean;

Methods​

abort()​
abort(): void;
Returns​

void

start()​
start(): void;
Returns​

void

stop()​
stop(): void;
Returns​

void

Functions​

createAudioPlayer()​

function createAudioPlayer(callbacks?): AudioPlayer;

Creates an audio player that decodes and schedules chunks on a shared AudioContext. Lazily constructs the context on the first chunk so the user gesture that triggered the action has time to propagate.

Parameters​

callbacks?​

AudioPlayerCallbacks

Returns​

AudioPlayer


createAudioRecorder()​

function createAudioRecorder(options?): AudioRecorder | null;

Creates an audio recorder backed by the browser's MediaRecorder API. Returns null if MediaRecorder is not available.

Parameters​

options?​

AudioRecorderOptions

Returns​

AudioRecorder | null


createSpeechRecognition()​

function createSpeechRecognition(callbacks, options?): SpeechRecognitionHandle;

Creates a browser SpeechRecognition wrapper for interim transcript feedback. Returns a handle with isAvailable=false if SpeechRecognition is not supported.

Parameters​

callbacks​

SpeechRecognitionCallbacks

options?​
continuous?​

boolean

language?​

string

Returns​

SpeechRecognitionHandle


isSpeechRecognitionAvailable()​

function isSpeechRecognitionAvailable(): boolean;

Checks if browser SpeechRecognition API is available.

Returns​

boolean