UNPKG

@assistant-ui/react

Version:

Open-source TypeScript/React library for building production-grade AI chat experiences

27 lines • 3.06 kB
export declare const encodePcm16: (samples: Float32Array) => string; export declare const decodePcm16: (base64: string) => Float32Array<ArrayBuffer>; export declare const readLevel: (analyser: AnalyserNode) => number; export type MicrophoneCapture = { setMuted: (muted: boolean) => void; level: () => number; close: () => Promise<void>; }; export declare const WORKLET_SOURCE = "\nclass PcmCapture extends AudioWorkletProcessor {\n constructor(options) {\n super();\n this.ratio = sampleRate / options.processorOptions.targetRate;\n this.chunk = Math.round(options.processorOptions.targetRate / 50);\n this.position = 0;\n this.last = 0;\n this.pending = [];\n this.sections = [];\n if (this.ratio > 1) {\n const omega = (Math.PI * 0.9) / this.ratio;\n const cosine = Math.cos(omega);\n const sine = Math.sin(omega);\n for (const q of [0.5176380902, 0.7071067812, 1.9318516526]) {\n const alpha = sine / (2 * q);\n const a0 = 1 + alpha;\n const b = (1 - cosine) / (2 * a0);\n this.sections.push({ b, a1: (-2 * cosine) / a0, a2: (1 - alpha) / a0, z1: 0, z2: 0 });\n }\n }\n }\n process(inputs) {\n const channels = inputs[0];\n if (!channels || channels.length === 0 || channels[0].length === 0) return true;\n const input = channels[0].slice();\n for (let c = 1; c < channels.length; c++)\n for (let i = 0; i < input.length; i++) input[i] += channels[c][i];\n if (channels.length > 1)\n for (let i = 0; i < input.length; i++) input[i] /= channels.length;\n for (let i = 0; i < input.length; i++) {\n let x = input[i];\n for (const s of this.sections) {\n const y = s.b * x + s.z1;\n s.z1 = 2 * s.b * x - s.a1 * y + s.z2;\n s.z2 = s.b * x - s.a2 * y;\n x = y;\n }\n input[i] = x;\n }\n while (this.position < input.length - 1) {\n const index = Math.floor(this.position);\n const prev = index < 0 ? this.last : input[index];\n const next = input[index + 1];\n this.pending.push(prev + (next - prev) * (this.position - index));\n this.position += this.ratio;\n }\n this.position -= input.length;\n this.last = input[input.length - 1];\n if (this.pending.length >= this.chunk) {\n this.port.postMessage(Float32Array.from(this.pending));\n this.pending = [];\n }\n return true;\n }\n}\nregisterProcessor(\"pcm-capture\", PcmCapture);\n"; export declare const createResampler: (ratio: number) => (input: Float32Array) => Float32Array; export declare const openMicrophone: (options: { sampleRate: number; onChunk: (base64: string) => void; signal?: AbortSignal; }) => Promise<MicrophoneCapture>; export type PcmPlayer = { enqueue: (base64: string) => void; clear: () => void; isPlaying: () => boolean; level: () => number; close: () => Promise<void>; }; export declare const createPcmPlayer: (options: { sampleRate: number; onIdle?: () => void; }) => PcmPlayer; //# sourceMappingURL=audio.d.ts.map