Python
Use sonnetics-py for wake word detection in Python
Installation
uv add sonnetics-pypip install sonnetics-pypoetry add sonnetics-pyUsage
There are a few different ways to run your wake word. Please explore the tabs below to find which works best for you.
For simple command‑line tools where it’s fine to block until the wake word is detected, use the synchronous listen() helper.
from sonnetics import WakeWordDetector
# This is a "Hey Alfred" model. Replace with your own model ID.
detector = WakeWordDetector.create(model_id="sonnetics-model-efea8354-3f81-4c61-9d50-7452cb901620")
for event in detector.listen():
print(f"Detected: {event.phrase}")
for chunk in event.audio:
# process chunk (e.g. send to speech-to-text service)
pass
breakWhen you already have an async event loop (for example in a larger async app) and want to await detections without blocking the loop, use the listen_async() helper.
import asyncio
from sonnetics import WakeWordDetector
async def main():
detector = await WakeWordDetector.create_async(model_id="sonnetics-model-efea8354-3f81-4c61-9d50-7452cb901620")
async for event in detector.listen_async():
print(f"Detected: {event.phrase}")
async for chunk in event.audio:
# process chunk (e.g. send to STT)
pass
break
asyncio.run(main())If you want wake‑word detection to run in the background and trigger a function whenever the wake word fires, use the start(callback) / stop() API.
from sonnetics import WakeWordDetector
detector = WakeWordDetector.create(model_id="sonnetics-model-efea8354-3f81-4c61-9d50-7452cb901620")
def on_detect(event):
print(f"Detected: {event.phrase}")
for chunk in event.audio:
# process chunk (e.g. send to a speech-to-text service)
pass
detector.start(callback=on_detect)
input("Press Enter to stop...")
detector.stop()When you control audio capture yourself (e.g. streaming from a file, custom device, or network) and just want to feed buffers into the detector, use the low‑level feed() API.
from sonnetics import WakeWordDetector
detector = WakeWordDetector.create(model_id="sonnetics-model-efea8354-3f81-4c61-9d50-7452cb901620")
chunks = iter([]) # your audio source
for chunk in chunks:
phrase = detector.feed(chunk, 16000, 1)
if phrase:
print(f"Detected: {phrase}")
for post in chunks: # audio after wake word
# process post (e.g. send to STT)
pass
breakConfiguration
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.