@todoforai/voiceloop 0.1.25 → 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 +1 -1
- package/src/index.d.ts +3 -0
- package/src/index.js +1 -1
- package/src/voice-agent.js +29 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@todoforai/voiceloop",
|
|
3
|
-
"version": "0.1.
|
|
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;
|
package/src/index.js
CHANGED
package/src/voice-agent.js
CHANGED
|
@@ -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
|
-
|
|
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
|