linnet/frontend/public/audio-processor.js
pyr0ball 7e14f9135e feat: Notation v0.1.x scaffold — full backend + frontend + tests
FastAPI backend (port 8522):
- Session lifecycle: POST /session/start, DELETE /session/{id}/end, GET /session/{id}
- SSE stream: GET /session/{id}/stream — per-subscriber asyncio.Queue fan-out, 15s heartbeat
- History: GET /session/{id}/history with min_confidence + limit filters
- Audio: WS /session/{id}/audio — binary PCM ingestion stub (real inference in v0.2.x)
- Export: GET /session/{id}/export — downloadable JSON session log
- ContextClassifier background task per session (CF_VOICE_MOCK=1 in dev)
- ToneEvent SSE wire format per cf-core#40 (locked field names)
- Tier gate: CFG-LNNT- prefix check, 402 for paid features

Vue 3 frontend (port 8521, Vite + UnoCSS + Pinia):
- NowPanel: affect-aware border tint, subtext, prosody flags, shift indicator
- HistoryStrip: horizontal scroll, last 8 events with affect color
- ComposeBar: start/stop session, SSE connection lifecycle
- useToneStream: EventSource composable
- useAudioCapture: AudioWorklet → Int16 PCM → WebSocket (v0.1.x stub)
- audio-processor.js: 100ms chunk accumulator in AudioWorklet thread
- Respects prefers-reduced-motion globally

26 tests passing, manage.sh, Dockerfile, compose.yml included.
2026-04-06 18:23:52 -07:00

44 lines
1.3 KiB
JavaScript

/**
* audio-processor.js — AudioWorkletProcessor for mic → PCM pipeline.
*
* Runs in the AudioWorklet thread. Converts Float32 samples to Int16 PCM
* and posts each 128-sample block (8ms at 16kHz) as an ArrayBuffer to
* the main thread.
*
* The main thread accumulates these into ~100ms chunks before sending
* over the WebSocket.
*/
const CHUNK_SAMPLES = 1600; // 100ms at 16kHz
class PcmProcessor extends AudioWorkletProcessor {
constructor() {
super();
this._buffer = new Int16Array(CHUNK_SAMPLES);
this._offset = 0;
}
process(inputs) {
const input = inputs[0];
if (!input || !input[0]) return true;
const samples = input[0]; // Float32Array, 128 samples
for (let i = 0; i < samples.length; i++) {
// Clamp and convert to Int16
const clamped = Math.max(-1, Math.min(1, samples[i]));
this._buffer[this._offset++] = clamped < 0 ? clamped * 0x8000 : clamped * 0x7fff;
if (this._offset >= CHUNK_SAMPLES) {
// Copy and post — avoid transferring the live buffer
const chunk = new Int16Array(CHUNK_SAMPLES);
chunk.set(this._buffer);
this.port.postMessage(chunk.buffer, [chunk.buffer]);
this._buffer = new Int16Array(CHUNK_SAMPLES);
this._offset = 0;
}
}
return true;
}
}
registerProcessor("pcm-processor", PcmProcessor);