@todoforai/voiceloop 0.1.24 → 0.1.26

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@todoforai/voiceloop",
3
- "version": "0.1.24",
3
+ "version": "0.1.26",
4
4
  "description": "Fast, interruptible browser voice agent: VAD → STT → LLM → TTS with real barge-in, echo filtering, streaming first-sentence TTS and tap-to-seek. Zero runtime dependencies.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/index.d.ts CHANGED
@@ -202,6 +202,9 @@ export interface VoiceAgentOptions {
202
202
  onEvent?: (event: VoiceAgentEvent) => void;
203
203
  }
204
204
 
205
+ /** Current loudness of the agent's spoken reply (Web Audio playback), 0–1 on the same perceptual curve
206
+ * as the mic `level` event; 0 when silent. Sample once per animation frame. */
207
+ export function ttsLevel(): number;
205
208
  /** Call from a user-gesture handler (e.g. the mic button click) to bless the shared TTS audio
206
209
  * element on mobile — without this, iOS rejects the async play() of synthesized speech. */
207
210
  export function unlockAudio(): void;
@@ -380,7 +383,7 @@ export class StreamingTTS implements VoiceTTS {
380
383
  * talks to ElevenLabs directly and is dev-only. `headers` authenticate proxy requests
381
384
  * (e.g. { 'X-API-Key': ... }) and are never sent on a direct ElevenLabs call. */
382
385
  export class ElevenLabsTTS extends StreamingTTS {
383
- constructor(options?: { ttsUrl?: string; apiKey?: string; voiceId?: string; modelId?: string; format?: string; headers?: Record<string, string> });
386
+ constructor(options?: { ttsUrl?: string; apiKey?: string; voiceId?: string; modelId?: string; format?: string; headers?: Record<string, string>; languageCode?: string });
384
387
  warm(): Promise<void>;
385
388
  }
386
389
 
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export {
2
2
  VoiceAgent, StreamingTTS, PiperTTS,
3
- unlockAudio, warmVad, setPlaybackIO,
3
+ unlockAudio, warmVad, setPlaybackIO, ttsLevel,
4
4
  isSelfEcho, novelChars, toolResultText,
5
5
  VOICE_SYSMSG, LANG_NAMES,
6
6
  } from './voice-agent.js';
@@ -19,10 +19,13 @@ const EL_API = 'https://api.elevenlabs.io/v1/text-to-speech';
19
19
 
20
20
  export class ElevenLabsTTS extends StreamingTTS {
21
21
  constructor({ ttsUrl, apiKey, voiceId = 'JBFqnCBsd6RMkjVDRZzb' /* George */,
22
- modelId = 'eleven_flash_v2_5', format = 'mp3_44100_64', headers = {} } = {}) {
22
+ modelId = 'eleven_flash_v2_5', format = 'mp3_44100_64', headers = {}, languageCode = '' } = {}) {
23
23
  super(voiceId);
24
24
  if (!ttsUrl && !apiKey) throw new Error('ElevenLabsTTS needs ttsUrl (backend proxy) or apiKey (direct, dev only)');
25
25
  this.ttsUrl = ttsUrl; this.apiKey = apiKey; this.modelId = modelId; this.format = format;
26
+ // ISO 639-1 ('hu'). Without it Flash auto-detects per SENTENCE — short clips get misread as
27
+ // English and spoken with English phonetics. Base code only; '' = let the model guess.
28
+ this.languageCode = String(languageCode || '').split(/[-_]/)[0].toLowerCase();
26
29
  this.headers = headers; // proxy-path auth (X-API-Key etc.) — never sent to ElevenLabs directly
27
30
  }
28
31
 
@@ -58,8 +61,11 @@ export class ElevenLabsTTS extends StreamingTTS {
58
61
  method: 'POST',
59
62
  headers: { 'Content-Type': 'application/json', ...(direct ? { 'xi-api-key': this.apiKey } : this.headers) },
60
63
  // The proxy form carries voice/model/format in the body so one backend route serves any voice.
61
- body: JSON.stringify(direct ? { text, model_id: this.modelId }
62
- : { text, voice_id: this.voiceId, model_id: this.modelId, output_format: this.format }),
64
+ body: JSON.stringify({
65
+ text, model_id: this.modelId,
66
+ ...(this.languageCode ? { language_code: this.languageCode } : {}),
67
+ ...(direct ? {} : { voice_id: this.voiceId, output_format: this.format }),
68
+ }),
63
69
  signal,
64
70
  });
65
71
  if (!res.ok) throw new Error(`ElevenLabs TTS ${res.status}: ${(await res.text().catch(() => '')).slice(0, 200)}`);
@@ -1304,14 +1304,7 @@ export class VoiceAgent {
1304
1304
  const now = (globalThis.performance ?? Date).now();
1305
1305
  if (now - this._levelAt < TUNING.LEVEL_MIN_MS) return;
1306
1306
  this._levelAt = now;
1307
- let sum = 0; for (let i = 0; i < f32.length; i++) sum += f32[i] * f32[i];
1308
- const rms = Math.sqrt(sum / f32.length);
1309
- // Perceptual curve, floored: linear RMS spends most of its range on levels nobody can hear, so a
1310
- // meter driven by it barely moves during speech. The floor is what keeps room noise reading as
1311
- // silence — without it a cube root maps a near-silent room to ~0.15-0.3 and the meter never rests.
1312
- const norm = (rms - TUNING.LEVEL_NOISE_FLOOR_RMS) / (TUNING.LEVEL_FULL_SCALE_RMS - TUNING.LEVEL_NOISE_FLOOR_RMS);
1313
- const level = norm <= 0 ? 0 : Math.min(1, Math.cbrt(norm));
1314
- this.onEvent({ type: 'level', level });
1307
+ this.onEvent({ type: 'level', level: meterLevel(f32) });
1315
1308
  }
1316
1309
 
1317
1310
  // ── diagnostic tap ─────────────────────────────────────────────────────
@@ -1603,6 +1596,33 @@ async function initAecLoopback(ctx) {
1603
1596
  settle(); sinkBuild = null;
1604
1597
  }
1605
1598
  }
1599
+ // RMS of a PCM frame → 0–1 meter level. Perceptual curve, floored: linear RMS spends most of its range
1600
+ // on levels nobody can hear, so a meter driven by it barely moves during speech. The floor is what
1601
+ // keeps room noise reading as silence — without it a cube root maps a near-silent room to ~0.15-0.3
1602
+ // and the meter never rests. Shared by the mic 'level' event and ttsLevel().
1603
+ function meterLevel(f32) {
1604
+ let sum = 0; for (let i = 0; i < f32.length; i++) sum += f32[i] * f32[i];
1605
+ const norm = (Math.sqrt(sum / f32.length) - TUNING.LEVEL_NOISE_FLOOR_RMS) / (TUNING.LEVEL_FULL_SCALE_RMS - TUNING.LEVEL_NOISE_FLOOR_RMS);
1606
+ return norm <= 0 ? 0 : Math.min(1, Math.cbrt(norm));
1607
+ }
1608
+
1609
+ // ── TTS output meter ─────────────────────────────────────────────────────────
1610
+ // One AnalyserNode every clip also feeds (a fan-out tap beside the real output, so it sees the signal
1611
+ // whether playback goes through the AEC sink or straight to the destination). Hosts read it per
1612
+ // frame via ttsLevel() to drive a speaking visual from the actual voice, not a fake sway.
1613
+ let ttsMeter = null, meterBuf = null;
1614
+ const meterFor = (ctx) => {
1615
+ if (!ttsMeter) { ttsMeter = ctx.createAnalyser(); ttsMeter.fftSize = 512; meterBuf = new Float32Array(ttsMeter.fftSize); }
1616
+ return ttsMeter;
1617
+ };
1618
+ /** Current loudness of the agent's spoken reply, 0–1 (same perceptual curve as the mic 'level'
1619
+ * event). 0 when nothing plays or before any clip ran. Cheap — call it once per animation frame. */
1620
+ export function ttsLevel() {
1621
+ if (!ttsMeter) return 0;
1622
+ ttsMeter.getFloatTimeDomainData(meterBuf);
1623
+ return meterLevel(meterBuf);
1624
+ }
1625
+
1606
1626
  export function unlockAudio() {
1607
1627
  try { const ctx = audioCtx(); ctx.resume(); initAecLoopback(ctx); } catch {} // gesture context → blesses/resumes the shared AudioContext + builds the AEC sink
1608
1628
  }
@@ -1621,6 +1641,7 @@ class WebAudioClip {
1621
1641
  this._node = ctx.createBufferSource();
1622
1642
  this._node.buffer = buffer;
1623
1643
  this._node.connect(ttsSink ?? ctx.destination); // via the AEC loopback sink when available (see initAecLoopback)
1644
+ this._node.connect(meterFor(ctx)); // tap for ttsLevel()
1624
1645
  // The native onended fires once — on natural end AND on our stop(). Classify with _stopped, but
1625
1646
  // treat a stop() at/near the buffer's end as a natural end (a pause() racing the final samples
1626
1647
  // shouldn't be reported as a barge-in). _fired guards the single-shot callback (not `ended`, which