UNPKG

assemblyai

Version:

The AssemblyAI JavaScript SDK provides an easy-to-use interface for interacting with the AssemblyAI API, which supports async and real-time transcription, as well as the latest LeMUR models.

67 lines (66 loc) 3.11 kB
import { StreamingTranscriber } from "../service"; export { BrowserOnlyError } from "../../../types/streaming/dual-channel"; type ErrorListener = (err: Error) => void; export type DualChannelCaptureParams = { /** Microphone MediaStream. Caller should set `echoCancellation: true` at `getUserMedia` time. */ micStream: MediaStream; /** System-audio MediaStream (e.g. `getDisplayMedia({ audio: true })`). */ systemStream: MediaStream; /** * The transcriber to push tagged PCM into. MUST be constructed with * `channels: [{ name: "mic" }, { name: "system" }]` so the per-channel * `sendAudio` calls succeed. */ transcriber: StreamingTranscriber; /** * Target sample rate sent to the transcriber. Defaults to 16000. The * AudioContext runs at the device's native rate; resampling happens inside * the encoder worklet (forcing `AudioContext({ sampleRate })` is unreliable * across browsers). */ targetSampleRate?: number; }; /** * Browser-only adapter that pumps two `MediaStream`s into a `StreamingTranscriber` * configured for dual-channel mode. Each `MediaStream` runs through its own * `pcm16-encoder` AudioWorklet (resample to `targetSampleRate`, encode to Int16 * PCM); each PCM chunk is forwarded via `transcriber.sendAudio(pcm, { channel })`. * * All dual-channel orchestration (mixing, VAD, per-word attribution) lives inside * `StreamingTranscriber` — this class is a pure I/O adapter. Non-browser runtimes * can replicate its job by pushing tagged PCM into `transcriber.sendAudio` directly. * * Caller responsibilities: * - **Echo cancellation** is set at `getUserMedia` time (`audio: { echoCancellation: true }`). * - **System-audio capture** is platform-dependent. Chrome's `getDisplayMedia({ audio: true })` * captures tab audio (and on Windows, full system audio when sharing the whole screen). * macOS requires a virtual loopback driver (e.g. BlackHole) to expose system audio at all. * - **Token auth.** Construct the transcriber with `token` — API-key auth is unsupported in browsers. * - **Stream ownership.** `stop()` tears down the AudioContext but does NOT stop the * `MediaStreamTrack`s passed in — callers own those. */ export declare class DualChannelCapture { private readonly params; private errorListener?; private context?; private micSource?; private sysSource?; private micEncoder?; private sysEncoder?; private running; constructor(params: DualChannelCaptureParams); on(event: "error", listener: ErrorListener): void; /** * Wire the capture pipeline and start pumping tagged PCM into the transcriber. * The transcriber must already be connected. Returns once the worklet is * registered and the audio graph is live. */ start(): Promise<void>; private makeEncoder; /** * Tear down internal nodes and close the AudioContext. Does NOT stop the * caller-provided MediaStream tracks — they remain available for preview UI, * recording, etc. Idempotent. */ stop(): Promise<void>; }