audio_capture.svelte.ts

Microphone capture in the browser, handing each recorded chunk to a sink — knowing nothing about where the chunks go or what the app does with them.

view source

Declarations
#

11 declarations

AUDIO_CAPTURE_TICK_INTERVAL
#

audio_capture.svelte.ts view source

100 import {AUDIO_CAPTURE_TICK_INTERVAL} from '@fuzdev/zzz/audio_capture.svelte.js';

How often AudioCapture.duration and AudioCapture.level update while the microphone is open, in milliseconds.

AudioCapture
#

audio_capture.svelte.ts view source

import {AudioCapture} from '@fuzdev/zzz/audio_capture.svelte.js';

Captures the microphone with MediaRecorder, handing each chunk to a sink as it's recorded, with pause and resume, the duration so far, and a level.

Capture starts only in start, and only during a user gesture. active is true the whole time the microphone may be open, for an indicator.

deps

type AudioCaptureDeps

status

type AudioCaptureStatus

$state.raw

duration

How long the current capture is so far, in milliseconds, not counting paused time. Updates every AUDIO_CAPTURE_TICK_INTERVAL while recording, and keeps its last value once the capture ends.

type number

$state.raw

level

How loud the microphone is right now, 0 to 1 — for a level meter. Updates every AUDIO_CAPTURE_TICK_INTERVAL while the microphone is open (paused included), and is 0 otherwise or where it can't be measured.

type number

$state.raw

active

Whether the microphone may be open: any status but idle.

type boolean

readonly $derived

constructor

type new (options?: AudioCaptureOptions): AudioCapture

options

default {}

start

Opens the microphone and the sink, and starts recording. Must be called from a user gesture (a click handler).

type <TSink extends AudioCaptureSink>(options: AudioCaptureStartOptions<TSink>): Promise<TSink | null>

options

returns Promise<TSink | null>

the opened sink once recording, or null when the capture was stopped or disposed before it got there

throws

  • Error - when one is already under way (leaving it untouched), or when there's no user gesture, the browser can't record, the microphone can't be opened, or the sink can't be opened — each of those after closing whatever the start opened

pause

Pauses a capture: the microphone stays open, nothing is recorded.

type (): void

returns void

resume

Resumes a paused capture.

type (): void

returns void

stop

Stops the capture: hands the last chunk to the sink, closes the microphone, and closes the sink. A capture still starting is abandoned.

type (): Promise<AudioCaptureStopResult | null>

returns Promise<AudioCaptureStopResult | null>

how closing the sink went, or null when nothing was being recorded, or the capture failed or was disposed while stopping

dispose

Ends the capture without closing the sink. The last chunk the browser hands over as it stops is still written to it.

type (): void

returns void

AudioCaptureDeps
#

audio_capture.svelte.ts view source

AudioCaptureDeps import type {AudioCaptureDeps} from '@fuzdev/zzz/audio_capture.svelte.js';

The browser a capture runs in — everything it touches outside itself, so a test can stand in for the microphone and the clock.

get_user_media

navigator.mediaDevices.getUserMedia.

type (constraints: MediaStreamConstraints): Promise<MediaStream>

constraints

type MediaStreamConstraints
returns Promise<MediaStream>

create_media_recorder

new MediaRecorder(stream, options).

type (stream: MediaStream, options: MediaRecorderOptions): MediaRecorder

stream

type MediaStream

options

type MediaRecorderOptions
returns MediaRecorder

is_type_supported

MediaRecorder.isTypeSupported — false where there's no MediaRecorder.

type (mime_type: string): boolean

mime_type

type string
returns boolean

create_level_meter

A meter on stream (Web Audio), or null where it can't be made.

type (stream: MediaStream): LevelMeter | null

stream

type MediaStream
returns LevelMeter | null

has_user_activation

Whether a user gesture is in effect (navigator.userActivation.isActive) — true in a browser that can't say.

type (): boolean

returns boolean

now

Date.now.

type (): number

returns number

AudioCaptureOptions
#

AudioCaptureSink
#

audio_capture.svelte.ts view source

AudioCaptureSink import type {AudioCaptureSink} from '@fuzdev/zzz/audio_capture.svelte.js';

Where a capture's chunks go — a file on a backend, local storage, anywhere.

write

Takes the next chunk, after every chunk written before it.

type (chunk: Blob): void

chunk

type Blob
returns void

close

Waits for every written chunk to land.

type (): Promise<void>

returns Promise<void>

throws

  • Error - when they didn't

AudioCaptureStartOptions
#

audio_capture.svelte.ts view source

AudioCaptureStartOptions<TSink> import type {AudioCaptureStartOptions} from '@fuzdev/zzz/audio_capture.svelte.js';

generics

AudioCaptureStartOptions<TSink extends AudioCaptureSink = AudioCaptureSink>
TSink
constraint AudioCaptureSink

chunk_duration

How often the browser hands over a chunk, in milliseconds — the most a sink can lose when the tab crashes.

type number

open_sink

Opens the sink for a capture in format, once the microphone is open. fail ends the capture with a reason, for a sink that breaks while chunks are still arriving; it's ignored once the capture has ended.

type (format: RecordingFormat, fail: (message: string) => void): Promise<TSink>

format

fail

type (message: string) => void
returns Promise<TSink>

throws

  • Error - when the sink can't be opened, which ends the start

on_fail

Called when a capture under way breaks and has ended, with why.

type (message: string): void

message

type string
returns void

on_input_ended

Called when the microphone's track ends on its own — the device was unplugged, or the browser revoked the permission. Typically stops the capture the way its owner normally does.

type (): void

returns void

AudioCaptureStatus
#

audio_capture.svelte.ts view source

AudioCaptureStatus

type "idle" | "starting" | "recording" | "paused" | "stopping"

import type {AudioCaptureStatus} from '@fuzdev/zzz/audio_capture.svelte.js';

Where a capture is: starting while the microphone opens and the sink opens, stopping while the last chunk is handed over and the sink closes.

AudioCaptureStopResult
#

audio_capture.svelte.ts view source

AudioCaptureStopResult

type ({ ok: true; } & object) | ({ ok: false; } & { message: string; })

import type {AudioCaptureStopResult} from '@fuzdev/zzz/audio_capture.svelte.js';

What a stop got from the sink: ok, or why closing it failed.

create_audio_capture_deps
#

create_level_meter
#

audio_capture.svelte.ts view source

(stream: MediaStream): LevelMeter | null import {create_level_meter} from '@fuzdev/zzz/audio_capture.svelte.js';

A LevelMeter over Web Audio: an AnalyserNode on the stream, read as the peak of its latest samples. It only listens — nothing is routed to the speakers. null where there's no AudioContext.

stream

type MediaStream

returns

LevelMeter | null

LevelMeter
#

audio_capture.svelte.ts view source

LevelMeter import type {LevelMeter} from '@fuzdev/zzz/audio_capture.svelte.js';

Reads how loud a live stream is right now.

read

The current peak level, 0 (silence) to 1 (full scale).

type (): number

returns number

close

Releases what the meter holds.

type (): void

returns void

Depends on
#

Imported by
#