JavaScript
Use @sonnetics/js for wake word detection in JavaScript and TypeScript
Installation
npm install @sonnetics/jsUse with Node.js, or with a bundler (Next.js, Vite, etc.).
Import from a CDN in the browser. No build step required.
<script type="module">
import { Wakeword } from 'https://esm.sh/@sonnetics/js';
const detector = await Wakeword.create({ modelId: 'sonnetics-model-efea8354-3f81-4c61-9d50-7452cb901620' });
// ...
</script>Also: jsDelivr — https://cdn.jsdelivr.net/npm/@sonnetics/js/+esm
Usage
Browser only. The library captures audio from the microphone via the Web Audio API. Call start() to begin listening, stop() when done. Requires a user gesture (e.g. button click) for getUserMedia.
import { Wakeword } from "@sonnetics/js";
// Preload on page load. Model ID format: sonnetics-model-<uuid>
const detector = await Wakeword.create("sonnetics-model-efea8354-3f81-4c61-9d50-7452cb901620");
detector.onDetect(() => console.log("Wake word detected!"));
document.getElementById("start-btn")!.addEventListener("click", async () => {
await detector.start();
// detector.stop(); when done
});
Browser only. Same as start/stop, but when the wake word is detected you can read the audio that follows it. Use ev.audio.read(seconds) to iterate over fixed-size chunks — ideal for piping into speech-to-text or other processing.
import { Wakeword } from "@sonnetics/js";
// Preload on page load. Model ID format: sonnetics-model-<uuid>
const detector = await Wakeword.create("sonnetics-model-efea8354-3f81-4c61-9d50-7452cb901620");
detector.onDetect(async (ev) => {
console.log("Wake word detected!", ev.phrase);
for await (const audio_after_wakeword of ev.audio.read(0.5)) {
// process audio after wakeword (e.g. send to speech-to-text)
break;
}
});
document.getElementById("start-btn")!.addEventListener("click", async () => {
await detector.start();
await new Promise((r) => setTimeout(r, 60_000)); // listen for 60s
detector.stop();
});Browser or Node. Use when you control the audio pipeline (file, custom device, Node.js mic capture). Call feed() with Float32Array chunks; it returns the detected phrase or null.
import { Wakeword } from "@sonnetics/js";
const detector = await Wakeword.create("sonnetics-model-your-model-id");
// Assume you have audio from somewhere
const chunks: Float32Array[] = [];
// Feed the audio chunks to the detector
for (const chunk of chunks) {
const phrase = detector.feed(chunk, 16000, 1);
if (phrase) console.log("Wake word detected!", phrase);
}Configuration
Set SONNETICS_CACHE_DIR to override where downloaded models are cached (default: ~/.cache/sonnetics on Linux, %APPDATA%/sonnetics/cache on Windows). Useful in CI, containers, or when the default cache directory is not writable.