Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

add xspace-agent/xspace-agent/packages/widget/src/audio/voice-input.ts

overstepping590e14c09ac3
diff --git a/xspace-agent/xspace-agent/packages/widget/src/audio/voice-input.ts b/xspace-agent/xspace-agent/packages/widget/src/audio/voice-input.ts
new file mode 100644
index 000000000..797f7c5c9
--- /dev/null
+++ b/xspace-agent/xspace-agent/packages/widget/src/audio/voice-input.ts
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: Apache-2.0
+// Copyright 2026 nirholas (https://github.com/nirholas/xspace-agent) [§79]
+
+export type VoiceState = 'idle' | 'recording' | 'error'
+export type VoiceStateHandler = (state: VoiceState) => void
+
+export class VoiceInput {
+ private mediaRecorder: MediaRecorder | null = null
+ private stream: MediaStream | null = null
+ private chunks: Blob[] = []
+ private state: VoiceState = 'idle'
+ private stateHandlers: VoiceStateHandler[] = []
+
+ async start(): Promise<void> {
+ if (this.state === 'recording') return
+
+ try {
+ this.stream = await navigator.mediaDevices.getUserMedia({
+ audio: {
+ echoCancellation: true,
+ noiseSuppression: true,
+ sampleRate: 16000,
+ },
+ })
+
+ const mimeType = this.getSupportedMimeType()
+ this.mediaRecorder = new MediaRecorder(this.stream, {
+ mimeType,
+ audioBitsPerSecond: 64000,
+ })
+
+ this.chunks = []
+
+ this.mediaRecorder.ondataavailable = (e) => {
+ if (e.data.size > 0) this.chunks.push(e.data)
+ }
+
+ this.mediaRecorder.start(250) // collect in 250ms chunks
+ this.setState('recording')
+ } catch {
+ this.setState('error')
+ throw new Error('Microphone access denied')
+ }
+ }
+
+ async stop(): Promise<{ buffer: ArrayBuffer; mimeType: string } | null> {
+ if (this.state !== 'recording' || !this.mediaRecorder) return null
+
+ return new Promise((resolve) => {
+ this.mediaRecorder!.onstop = async () => {
+ const mimeType = this.mediaRecorder!.mimeType
+ const blob = new Blob(this.chunks, { type: mimeType })
+ const buffer = await blob.arrayBuffer()
+ this.cleanup()
+ this.setState('idle')
+ resolve({ buffer, mimeType })
+ }
+
+ this.mediaRecorder!.stop()
+ })
+ }
+
+ cancel(): void {
+ if (this.mediaRecorder && this.state === 'recording') {
+ this.mediaRecorder.stop()
+ }
+ this.cleanup()
+ this.setState('idle')
+ }
+
+ isRecording(): boolean {
+ return this.state === 'recording'
+ }
+
+ getState(): VoiceState {
+ return this.state
+ }
+
+ onStateChange(handler: VoiceStateHandler): void {
+ this.stateHandlers.push(handler)
+ }
+
+ destroy(): void {
+ this.cancel()
+ this.stateHandlers = []
+ }
+
+ static isSupported(): boolean {
+ return !!(typeof navigator.mediaDevices?.getUserMedia === 'function' && window.MediaRecorder)
+ }
+
+ private setState(state: VoiceState): void {
+ this.state = state
+ this.stateHandlers.forEach((h) => h(state))
+ }
+
+ private cleanup(): void {
+ if (this.stream) {
+ this.stream.getTracks().forEach((t) => t.stop())
+ this.stream = null
+ }
+ this.mediaRecorder = null
+ this.chunks = []
+ }
+
+ private getSupportedMimeType(): string {
+ const types = ['audio/webm;codecs=opus', 'audio/webm', 'audio/ogg;codecs=opus', 'audio/mp4']
+ for (const type of types) {
+ if (MediaRecorder.isTypeSupported(type)) return type
+ }
+ return 'audio/webm'
+ }
+}
+
+