Files
EveFlow/src/services/voice/audioBus.ts
T
Claude 5f0fa7a1e2 fix: full review pass (main process, voice pipeline, Hermes client, HUD) and e2e-validated local voice (v2.1.0.1)
Electron main
- utility-process channel: sherpa output copied into V8 buffers (Electron rejects external
  buffers), audio exchanged as base64; worker restart on timeout, identity-safe exit handling,
  deterministic native unload by restart
- webhook: loopback by default without secret, UTF-8-safe body assembly, clean restart, port validation
- files IPC: realpath-based root check, openPath allow-list (reveal-only outside document folders),
  Windows reserved names; store: debounced async atomic writes with backup of corrupt files;
  logger: streaming writes with rotation; log message size cap
- HTTP stream proxy: socket released on idle timeout / renderer destroyed, id validation
- model manager: inactivity timeout, retrying rm/rename (Windows locks), engine stopped before
  replacing a model; WAV decoder handles float/24-bit; IPC payload validation
- window: opaque rounded window on Windows, navigation lock-down, visibility events;
  Ctrl+Alt+Escape instead of the Task Manager shortcut; single-instance guard; EVEFLOW_USER_DATA

Voice pipeline
- abort semantics (SendHandle.aborted), abort before the stream opens, session id prefixes per transport
- hands-free re-arm after replies without speech, start/stop race, no chime on auto re-arm,
  no silence shipped to STT (400 ms pre-roll), no transcription of empty manual stops
- TTS: bounded prefetch, cancellable segments, non-interrupting notices, volume applied at play time
- SSE CRLF split, usage in chat completions, finish_reason length, phonetic regex hoisted

HUD
- core renderer: no canvas shadows, cached colours, reusable spectrum buffer, theme read on change,
  30 fps idle, stops when the window is hidden; ping flashes on send / tool / speech
- deltas coalesced per animation frame; stable auto-scroll; narrow selectors everywhere
- bundled fonts (offline), reduce-motion fix, error toasts, ops drawer below 1180 px,
  interim transcript and first-token latency in the core caption, Ctrl+K, dialog semantics,
  switch/aria roles, compact widget cleanup, Whisper small recommended for French
- docs/ROADMAP.md: audit results, e2e results and the plan towards a real JARVIS

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017Wn5VX9HNbJ7N54hR24u9Y
2026-09-03 18:08:09 +00:00

102 lines
3.6 KiB
TypeScript

/**
* Shared Web Audio graph. Everything that is played (TTS) or captured (mic) passes through
* analysers so the JARVIS core can react to the real signal.
*/
class AudioBus {
private ctx: AudioContext | null = null;
private outputGain: GainNode | null = null;
private outputAnalyser: AnalyserNode | null = null;
private inputAnalyser: AnalyserNode | null = null;
private timeData = new Float32Array(1024);
private freqData = new Uint8Array(512);
private synthetic = 0;
get context(): AudioContext {
if (!this.ctx) {
this.ctx = new AudioContext({ latencyHint: 'interactive' });
this.outputGain = this.ctx.createGain();
this.outputAnalyser = this.ctx.createAnalyser();
this.outputAnalyser.fftSize = 1024;
this.outputAnalyser.smoothingTimeConstant = 0.6;
this.outputGain.connect(this.outputAnalyser);
this.outputAnalyser.connect(this.ctx.destination);
}
return this.ctx;
}
async resume(): Promise<void> {
const ctx = this.context;
if (ctx.state === 'suspended') await ctx.resume().catch(() => undefined);
}
get output(): AudioNode {
this.context;
return this.outputGain!;
}
setVolume(volume: number): void {
this.context;
this.outputGain!.gain.value = Math.max(0, Math.min(1.5, volume));
}
/** Attach an external analyser (mic capture) so the HUD can read the input level. */
setInputAnalyser(analyser: AnalyserNode | null): void {
this.inputAnalyser = analyser;
}
/** For engines that bypass Web Audio (system speech synthesis), fake a plausible level. */
setSyntheticLevel(level: number): void {
this.synthetic = level;
}
private rms(analyser: AnalyserNode): number {
if (this.timeData.length !== analyser.fftSize) this.timeData = new Float32Array(analyser.fftSize);
analyser.getFloatTimeDomainData(this.timeData);
let sum = 0;
for (let i = 0; i < this.timeData.length; i++) sum += this.timeData[i] * this.timeData[i];
return Math.sqrt(sum / this.timeData.length);
}
/** Output loudness 0..1 (TTS). */
outputLevel(): number {
if (this.synthetic > 0) return this.synthetic;
if (!this.outputAnalyser) return 0;
return Math.min(1, this.rms(this.outputAnalyser) * 4);
}
/** Input loudness 0..1 (microphone). */
inputLevel(): number {
if (!this.inputAnalyser) return 0;
return Math.min(1, this.rms(this.inputAnalyser) * 6);
}
/** Normalised output spectrum (0..1) with `bins` values, low to high frequency. */
spectrum(bins: number, source: 'output' | 'input' = 'output', out: Float32Array = new Float32Array(bins)): Float32Array {
const analyser = source === 'output' ? this.outputAnalyser : this.inputAnalyser;
out.fill(0);
if (!analyser) {
if (this.synthetic > 0) {
const t = performance.now() / 1000;
for (let i = 0; i < bins; i++) {
out[i] = Math.max(0, this.synthetic * (0.6 + 0.4 * Math.sin(t * 9 + i * 0.7)) * (1 - i / bins));
}
}
return out;
}
if (this.freqData.length !== analyser.frequencyBinCount) this.freqData = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(this.freqData);
// Only the lower half of the spectrum carries voice energy.
const usable = Math.floor(this.freqData.length * 0.5);
for (let i = 0; i < bins; i++) {
const from = Math.floor((i / bins) * usable);
const to = Math.max(from + 1, Math.floor(((i + 1) / bins) * usable));
let sum = 0;
for (let j = from; j < to; j++) sum += this.freqData[j];
out[i] = sum / (to - from) / 255;
}
return out;
}
}
export const audioBus = new AudioBus();