assemblyai 4.33.3 → 4.34.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assemblyai.streaming.umd.js +1279 -3
- package/dist/assemblyai.streaming.umd.min.js +1 -1
- package/dist/assemblyai.umd.js +786 -3
- package/dist/assemblyai.umd.min.js +1 -1
- package/dist/browser.mjs +762 -4
- package/dist/bun.mjs +762 -4
- package/dist/deno.mjs +762 -4
- package/dist/exports/streaming.d.ts +7 -0
- package/dist/index.cjs +786 -3
- package/dist/index.mjs +778 -4
- package/dist/node.cjs +770 -3
- package/dist/node.mjs +762 -4
- package/dist/services/index.d.ts +2 -2
- package/dist/services/streaming/browser/dual-channel-capture.d.ts +66 -0
- package/dist/services/streaming/browser/worklets/pcm16-encoder.d.ts +19 -0
- package/dist/services/streaming/energy-vad.d.ts +35 -0
- package/dist/services/streaming/index.d.ts +4 -0
- package/dist/services/streaming/label-mapper.d.ts +44 -0
- package/dist/services/streaming/resampler.d.ts +22 -0
- package/dist/services/streaming/service.d.ts +69 -1
- package/dist/streaming.browser.mjs +1235 -4
- package/dist/streaming.cjs +1275 -3
- package/dist/streaming.mjs +1264 -4
- package/dist/types/streaming/dual-channel.d.ts +48 -0
- package/dist/types/streaming/index.d.ts +110 -1
- package/dist/workerd.mjs +762 -4
- package/package.json +1 -1
- package/src/exports/streaming.ts +7 -0
- package/src/services/index.ts +20 -1
- package/src/services/streaming/browser/dual-channel-capture.ts +177 -0
- package/src/services/streaming/browser/worklets/pcm16-encoder.ts +70 -0
- package/src/services/streaming/energy-vad.ts +75 -0
- package/src/services/streaming/index.ts +4 -0
- package/src/services/streaming/label-mapper.ts +128 -0
- package/src/services/streaming/resampler.ts +69 -0
- package/src/services/streaming/service.ts +385 -2
- package/src/types/streaming/dual-channel.ts +57 -0
- package/src/types/streaming/index.ts +110 -0
package/dist/services/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { LemurService } from "./lemur";
|
|
|
3
3
|
import { RealtimeTranscriber, RealtimeTranscriberFactory, RealtimeService, RealtimeServiceFactory } from "./realtime";
|
|
4
4
|
import { TranscriptService } from "./transcripts";
|
|
5
5
|
import { FileService } from "./files";
|
|
6
|
-
import { StreamingTranscriber, StreamingTranscriberFactory } from "./streaming";
|
|
6
|
+
import { StreamingTranscriber, StreamingTranscriberFactory, DualChannelCapture, EnergyVad, LinearResampler, VadTimeline, attributeTurn, attributeWord, rollUpTurnChannel, float32ToPcm16 } from "./streaming";
|
|
7
7
|
declare class AssemblyAI {
|
|
8
8
|
/**
|
|
9
9
|
* The files service.
|
|
@@ -31,4 +31,4 @@ declare class AssemblyAI {
|
|
|
31
31
|
*/
|
|
32
32
|
constructor(params: BaseServiceParams);
|
|
33
33
|
}
|
|
34
|
-
export { AssemblyAI, LemurService, RealtimeTranscriberFactory, RealtimeTranscriber, RealtimeServiceFactory, RealtimeService, TranscriptService, FileService, StreamingTranscriber, };
|
|
34
|
+
export { AssemblyAI, LemurService, RealtimeTranscriberFactory, RealtimeTranscriber, RealtimeServiceFactory, RealtimeService, TranscriptService, FileService, StreamingTranscriber, DualChannelCapture, EnergyVad, LinearResampler, VadTimeline, attributeTurn, attributeWord, rollUpTurnChannel, float32ToPcm16, };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { StreamingTranscriber } from "../service";
|
|
2
|
+
export { BrowserOnlyError } from "../../../types/streaming/dual-channel";
|
|
3
|
+
type ErrorListener = (err: Error) => void;
|
|
4
|
+
export type DualChannelCaptureParams = {
|
|
5
|
+
/** Microphone MediaStream. Caller should set `echoCancellation: true` at `getUserMedia` time. */
|
|
6
|
+
micStream: MediaStream;
|
|
7
|
+
/** System-audio MediaStream (e.g. `getDisplayMedia({ audio: true })`). */
|
|
8
|
+
systemStream: MediaStream;
|
|
9
|
+
/**
|
|
10
|
+
* The transcriber to push tagged PCM into. MUST be constructed with
|
|
11
|
+
* `channels: [{ name: "mic" }, { name: "system" }]` so the per-channel
|
|
12
|
+
* `sendAudio` calls succeed.
|
|
13
|
+
*/
|
|
14
|
+
transcriber: StreamingTranscriber;
|
|
15
|
+
/**
|
|
16
|
+
* Target sample rate sent to the transcriber. Defaults to 16000. The
|
|
17
|
+
* AudioContext runs at the device's native rate; resampling happens inside
|
|
18
|
+
* the encoder worklet (forcing `AudioContext({ sampleRate })` is unreliable
|
|
19
|
+
* across browsers).
|
|
20
|
+
*/
|
|
21
|
+
targetSampleRate?: number;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Browser-only adapter that pumps two `MediaStream`s into a `StreamingTranscriber`
|
|
25
|
+
* configured for dual-channel mode. Each `MediaStream` runs through its own
|
|
26
|
+
* `pcm16-encoder` AudioWorklet (resample to `targetSampleRate`, encode to Int16
|
|
27
|
+
* PCM); each PCM chunk is forwarded via `transcriber.sendAudio(pcm, { channel })`.
|
|
28
|
+
*
|
|
29
|
+
* All dual-channel orchestration (mixing, VAD, per-word attribution) lives inside
|
|
30
|
+
* `StreamingTranscriber` — this class is a pure I/O adapter. Non-browser runtimes
|
|
31
|
+
* can replicate its job by pushing tagged PCM into `transcriber.sendAudio` directly.
|
|
32
|
+
*
|
|
33
|
+
* Caller responsibilities:
|
|
34
|
+
* - **Echo cancellation** is set at `getUserMedia` time (`audio: { echoCancellation: true }`).
|
|
35
|
+
* - **System-audio capture** is platform-dependent. Chrome's `getDisplayMedia({ audio: true })`
|
|
36
|
+
* captures tab audio (and on Windows, full system audio when sharing the whole screen).
|
|
37
|
+
* macOS requires a virtual loopback driver (e.g. BlackHole) to expose system audio at all.
|
|
38
|
+
* - **Token auth.** Construct the transcriber with `token` — API-key auth is unsupported in browsers.
|
|
39
|
+
* - **Stream ownership.** `stop()` tears down the AudioContext but does NOT stop the
|
|
40
|
+
* `MediaStreamTrack`s passed in — callers own those.
|
|
41
|
+
*/
|
|
42
|
+
export declare class DualChannelCapture {
|
|
43
|
+
private readonly params;
|
|
44
|
+
private errorListener?;
|
|
45
|
+
private context?;
|
|
46
|
+
private micSource?;
|
|
47
|
+
private sysSource?;
|
|
48
|
+
private micEncoder?;
|
|
49
|
+
private sysEncoder?;
|
|
50
|
+
private running;
|
|
51
|
+
constructor(params: DualChannelCaptureParams);
|
|
52
|
+
on(event: "error", listener: ErrorListener): void;
|
|
53
|
+
/**
|
|
54
|
+
* Wire the capture pipeline and start pumping tagged PCM into the transcriber.
|
|
55
|
+
* The transcriber must already be connected. Returns once the worklet is
|
|
56
|
+
* registered and the audio graph is live.
|
|
57
|
+
*/
|
|
58
|
+
start(): Promise<void>;
|
|
59
|
+
private makeEncoder;
|
|
60
|
+
/**
|
|
61
|
+
* Tear down internal nodes and close the AudioContext. Does NOT stop the
|
|
62
|
+
* caller-provided MediaStream tracks — they remain available for preview UI,
|
|
63
|
+
* recording, etc. Idempotent.
|
|
64
|
+
*/
|
|
65
|
+
stop(): Promise<void>;
|
|
66
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AudioWorklet processor that ingests mono Float32 audio at the AudioContext's
|
|
3
|
+
* native sample rate, resamples to `targetRate` (linear interpolation, stateful
|
|
4
|
+
* across `process()` calls), packs to little-endian Int16 PCM, and posts
|
|
5
|
+
* fixed-size chunks via `port.postMessage` with a running `samplesSent` counter.
|
|
6
|
+
*
|
|
7
|
+
* `samplesSent` is in **target-rate samples**, so the main thread can derive a
|
|
8
|
+
* stream-relative timestamp = `samplesSent / targetRate * 1000` (ms) — the same
|
|
9
|
+
* frame AAI uses for `StreamingWord.start` / `.end`.
|
|
10
|
+
*
|
|
11
|
+
* Defined as a string so it can be registered via a Blob URL — the SDK ships as
|
|
12
|
+
* a single ESM file, so a separate `.js` worklet asset isn't viable.
|
|
13
|
+
*/
|
|
14
|
+
export declare const pcm16EncoderWorkletSource = "\nclass Pcm16EncoderProcessor extends AudioWorkletProcessor {\n constructor(options) {\n super();\n const opts = (options && options.processorOptions) || {};\n this.targetRate = opts.targetRate || 16000;\n this.chunkMs = opts.chunkMs || 50;\n this.ratio = sampleRate / this.targetRate;\n this.chunkSize = Math.round(this.targetRate * this.chunkMs / 1000);\n this.buffer = new Int16Array(this.chunkSize);\n this.bufferIdx = 0;\n this.samplesSent = 0;\n this.lastSample = 0;\n this.fractional = 0;\n }\n\n process(inputs) {\n const input = inputs[0];\n if (!input || input.length === 0 || !input[0] || input[0].length === 0) {\n return true;\n }\n const mono = input[0];\n let pos = this.fractional;\n while (pos < mono.length) {\n const i = Math.floor(pos);\n const frac = pos - i;\n const a = i === 0 ? this.lastSample : mono[i - 1];\n const b = mono[i];\n const sample = a + (b - a) * frac;\n const clamped = sample < -1 ? -1 : sample > 1 ? 1 : sample;\n this.buffer[this.bufferIdx++] = clamped < 0 ? clamped * 0x8000 : clamped * 0x7fff;\n if (this.bufferIdx === this.chunkSize) {\n const out = new Int16Array(this.chunkSize);\n out.set(this.buffer);\n this.samplesSent += this.chunkSize;\n this.port.postMessage(\n { pcm: out.buffer, samplesSent: this.samplesSent },\n [out.buffer],\n );\n this.bufferIdx = 0;\n }\n pos += this.ratio;\n }\n this.lastSample = mono[mono.length - 1];\n this.fractional = pos - mono.length;\n return true;\n }\n}\nregisterProcessor(\"aai-pcm16-encoder\", Pcm16EncoderProcessor);\n";
|
|
15
|
+
export declare const PCM16_ENCODER_PROCESSOR_NAME = "aai-pcm16-encoder";
|
|
16
|
+
export type Pcm16EncoderMessage = {
|
|
17
|
+
pcm: ArrayBuffer;
|
|
18
|
+
samplesSent: number;
|
|
19
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { VadDetector, VadDetectorResult } from "../../types/streaming/dual-channel";
|
|
2
|
+
export type EnergyVadParams = {
|
|
3
|
+
/** Threshold = noiseFloor * thresholdRatio. Default 3.0 (~ +9.5 dB above noise). */
|
|
4
|
+
thresholdRatio?: number;
|
|
5
|
+
/** EMA smoothing for the noise-floor estimate when frame is non-speech. Default 0.05. */
|
|
6
|
+
noiseFloorAlpha?: number;
|
|
7
|
+
/** Hangover in frames: stay "active" this many frames after the last speech frame. Default 10 (\~200 ms at 20 ms frames). */
|
|
8
|
+
hangoverFrames?: number;
|
|
9
|
+
/** Initial noise floor estimate. Default 1e-4. Adaptive after the first non-speech frame. */
|
|
10
|
+
initialNoiseFloor?: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Energy-based VAD with adaptive noise-floor tracking and hangover. Pure JS,
|
|
14
|
+
* no dependencies. Suitable for the "which physical channel is speaking" task
|
|
15
|
+
* because the channels are already physically separated at capture — the harder
|
|
16
|
+
* problem (speech vs. non-speech in the wild) is one a customer can swap in a
|
|
17
|
+
* DNN VAD for via the `createVad` parameter.
|
|
18
|
+
*
|
|
19
|
+
* Tuning notes:
|
|
20
|
+
* - thresholdRatio below 2 will treat anything above noise as speech (too sensitive).
|
|
21
|
+
* - thresholdRatio above 6 will miss quiet utterance onsets/offsets.
|
|
22
|
+
* - noiseFloorAlpha above 0.1 makes the floor track quickly (good for non-stationary
|
|
23
|
+
* background) but risks slowly adapting *up* to a sustained low voice.
|
|
24
|
+
*/
|
|
25
|
+
export declare class EnergyVad implements VadDetector {
|
|
26
|
+
private readonly thresholdRatio;
|
|
27
|
+
private readonly noiseFloorAlpha;
|
|
28
|
+
private readonly hangoverFrames;
|
|
29
|
+
private readonly initialNoiseFloor;
|
|
30
|
+
private noiseFloor;
|
|
31
|
+
private hangoverRemaining;
|
|
32
|
+
constructor(params?: EnergyVadParams);
|
|
33
|
+
process(frame: Float32Array): VadDetectorResult;
|
|
34
|
+
reset(): void;
|
|
35
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { StreamingWord, TurnEvent } from "../../types/streaming";
|
|
2
|
+
import { Channel, VadFrame } from "../../types/streaming/dual-channel";
|
|
3
|
+
export type LabelMapperParams = {
|
|
4
|
+
/** Per-word energy ratio above which a channel is declared dominant. */
|
|
5
|
+
dominanceRatio: number;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Append-only ring buffer of VAD frames in stream-relative ms order.
|
|
9
|
+
* `pushFrame` is O(1) amortized; `framesInWindow` is O(n) over kept frames,
|
|
10
|
+
* which is fine for the per-word lookups we do (a 30 s window at 50 frames/s
|
|
11
|
+
* per channel × 2 channels = 3000 entries, scanned once per word).
|
|
12
|
+
*
|
|
13
|
+
* Runtime-agnostic — no DOM or Web Audio dependencies.
|
|
14
|
+
*/
|
|
15
|
+
export declare class VadTimeline {
|
|
16
|
+
private readonly windowMs;
|
|
17
|
+
private frames;
|
|
18
|
+
private head;
|
|
19
|
+
constructor(windowMs: number);
|
|
20
|
+
pushFrame(frame: VadFrame): void;
|
|
21
|
+
framesInWindow(startMs: number, endMs: number): VadFrame[];
|
|
22
|
+
clear(): void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Decide which channel was dominant during a word's `[start, end]` window.
|
|
26
|
+
*
|
|
27
|
+
* - If no channel has any active VAD energy → `"unknown"`.
|
|
28
|
+
* - If the top channel beats the runner-up by at least `dominanceRatio` → top channel.
|
|
29
|
+
* - Else: top channel wins on absolute score; exact ties → `"unknown"`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function attributeWord(word: StreamingWord, timeline: VadTimeline, params: LabelMapperParams): Channel;
|
|
32
|
+
/**
|
|
33
|
+
* Duration-weighted majority of word channels. `"unknown"` if there are no
|
|
34
|
+
* words, every word resolved to `"unknown"`, or two channels tie exactly.
|
|
35
|
+
*/
|
|
36
|
+
export declare function rollUpTurnChannel(words: StreamingWord[]): Channel;
|
|
37
|
+
/**
|
|
38
|
+
* Mutate `turn` in place: write `turn.words[i].channel` for every word and set
|
|
39
|
+
* `turn.channel` to the duration-weighted rollup.
|
|
40
|
+
*
|
|
41
|
+
* Returns `void` because the transcriber owns the `TurnEvent` ref and forwards
|
|
42
|
+
* the same object to the customer listener — no need to allocate a copy.
|
|
43
|
+
*/
|
|
44
|
+
export declare function attributeTurn(turn: TurnEvent, timeline: VadTimeline, params: LabelMapperParams): void;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Linear-interpolation resampler for streaming Float32 audio. Stateful across
|
|
3
|
+
* `process()` calls so chunk boundaries don't introduce phase discontinuities:
|
|
4
|
+
* the last input sample and a fractional read position are carried over.
|
|
5
|
+
*
|
|
6
|
+
* Linear interpolation is good enough for ASR ingest — the downstream
|
|
7
|
+
* StreamingTranscriber band-limits at the target rate anyway, and a polyphase
|
|
8
|
+
* filter would be overkill in the AudioWorklet hot path. If a customer needs
|
|
9
|
+
* higher quality they can supply their own VadDetector + bypass the encoder.
|
|
10
|
+
*/
|
|
11
|
+
export declare class LinearResampler {
|
|
12
|
+
private readonly sourceRate;
|
|
13
|
+
private readonly targetRate;
|
|
14
|
+
private readonly ratio;
|
|
15
|
+
private lastSample;
|
|
16
|
+
private fractional;
|
|
17
|
+
constructor(sourceRate: number, targetRate: number);
|
|
18
|
+
process(input: Float32Array): Float32Array;
|
|
19
|
+
reset(): void;
|
|
20
|
+
}
|
|
21
|
+
/** Convert Float32 PCM (-1..1) to little-endian Int16 PCM. */
|
|
22
|
+
export declare function float32ToPcm16(input: Float32Array): ArrayBuffer;
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
import { StreamingTranscriberParams, AudioData, BeginEvent, TurnEvent, LLMGatewayResponseEvent, StreamingUpdateConfiguration, WarningEvent } from "../..";
|
|
2
|
+
import type { VadFrame } from "../../types/streaming/dual-channel";
|
|
3
|
+
/**
|
|
4
|
+
* Options for `sendAudio`. In dual-channel mode (when `channels` is configured
|
|
5
|
+
* on the transcriber), `channel` is required and must match one of the declared
|
|
6
|
+
* channel names; in single-channel mode it is ignored.
|
|
7
|
+
*/
|
|
8
|
+
export type SendAudioOptions = {
|
|
9
|
+
channel?: string;
|
|
10
|
+
};
|
|
2
11
|
export declare class StreamingTranscriber {
|
|
3
12
|
private apiKey?;
|
|
4
13
|
private token?;
|
|
@@ -6,17 +15,76 @@ export declare class StreamingTranscriber {
|
|
|
6
15
|
private socket?;
|
|
7
16
|
private listeners;
|
|
8
17
|
private sessionTerminatedResolve?;
|
|
18
|
+
private isDualChannel;
|
|
19
|
+
private channelNames?;
|
|
20
|
+
private channelBuffers?;
|
|
21
|
+
private channelSamplesReceived?;
|
|
22
|
+
private channelVadFloatBuffers?;
|
|
23
|
+
private channelVadBufferIdx?;
|
|
24
|
+
private channelVads?;
|
|
25
|
+
private timeline?;
|
|
26
|
+
private flushTimer?;
|
|
27
|
+
private attributionParams?;
|
|
28
|
+
private vadFrameSamples;
|
|
29
|
+
private minChunkSamples;
|
|
30
|
+
private maxChunkSamples;
|
|
31
|
+
private speakerHistory?;
|
|
9
32
|
constructor(params: StreamingTranscriberParams);
|
|
10
33
|
private connectionUrl;
|
|
11
34
|
on(event: "open", listener: (event: BeginEvent) => void): void;
|
|
12
35
|
on(event: "turn", listener: (event: TurnEvent) => void): void;
|
|
13
36
|
on(event: "llmGatewayResponse", listener: (event: LLMGatewayResponseEvent) => void): void;
|
|
14
37
|
on(event: "warning", listener: (event: WarningEvent) => void): void;
|
|
38
|
+
on(event: "vad", listener: (event: VadFrame) => void): void;
|
|
15
39
|
on(event: "error", listener: (error: Error) => void): void;
|
|
16
40
|
on(event: "close", listener: (code: number, reason: string) => void): void;
|
|
17
41
|
connect(): Promise<BeginEvent>;
|
|
42
|
+
/**
|
|
43
|
+
* Returns a WritableStream that pumps PCM chunks into `sendAudio`. Single-channel
|
|
44
|
+
* only — in dual-channel mode use `sendAudio(pcm, { channel })` directly, since
|
|
45
|
+
* `WritableStream` has no place to carry a channel tag.
|
|
46
|
+
*/
|
|
18
47
|
stream(): WritableStream<AudioData>;
|
|
19
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Send PCM audio.
|
|
50
|
+
*
|
|
51
|
+
* In single-channel mode, `audio` is forwarded directly to the WebSocket and
|
|
52
|
+
* `options` is ignored.
|
|
53
|
+
*
|
|
54
|
+
* In dual-channel mode (when `channels` is configured), `options.channel` is
|
|
55
|
+
* REQUIRED and must match one of the declared channel names. Per-channel PCM is
|
|
56
|
+
* fed into that channel's VAD, accumulated into a per-channel ring buffer, and
|
|
57
|
+
* a scheduled flush (`channelAttribution.flushIntervalMs`, default 50ms) mixes
|
|
58
|
+
* the buffers into mono before sending to the WebSocket.
|
|
59
|
+
*/
|
|
60
|
+
sendAudio(audio: AudioData, options?: SendAudioOptions): void;
|
|
61
|
+
private ingestChannelAudio;
|
|
62
|
+
private startFlushTimer;
|
|
63
|
+
private flushMix;
|
|
64
|
+
/**
|
|
65
|
+
* Fill in words whose per-word VAD attribution was `"unknown"` by looking
|
|
66
|
+
* at the dominant non-`"unknown"` channel among ±N neighbors in the same
|
|
67
|
+
* turn. Words with no non-`"unknown"` neighbors stay `"unknown"`. Confident
|
|
68
|
+
* per-word VAD decisions are never modified.
|
|
69
|
+
*
|
|
70
|
+
* Local temporal heuristic — ignores `speaker_label`, so it works even when
|
|
71
|
+
* AAI's diarization re-uses the same label for two physically distinct
|
|
72
|
+
* voices. Each resolved word gets `channelResolved: true` so downstream
|
|
73
|
+
* renderers can distinguish inferred channels from directly-measured ones.
|
|
74
|
+
*/
|
|
75
|
+
private resolveUnknownChannelsByWindow;
|
|
76
|
+
/**
|
|
77
|
+
* Fill `"unknown"` words by looking up the speaker's session-wide channel
|
|
78
|
+
* evidence. For each `speaker_label`, sums active VAD frame RMS per channel
|
|
79
|
+
* across every word the speaker has uttered to date. A speaker is
|
|
80
|
+
* "resolvable" if their total evidence clears
|
|
81
|
+
* `speakerHistoryMinRmsEvidence` and their top channel exceeds the
|
|
82
|
+
* runner-up by `speakerHistoryDominanceRatio`.
|
|
83
|
+
*
|
|
84
|
+
* Only touches `"unknown"` words. Confident per-word VAD decisions are
|
|
85
|
+
* never modified. `speaker_label` is never modified.
|
|
86
|
+
*/
|
|
87
|
+
private resolveUnknownChannelsBySpeakerHistory;
|
|
20
88
|
/**
|
|
21
89
|
* Update the streaming configuration mid-stream.
|
|
22
90
|
* @param config - The configuration parameters to update
|