# scorpiox-next-whisper-wakeword

Always-on wake-phrase detector in pure C over the ONNX Runtime C API —
openWakeWord's architecture (github.com/dscripka/openWakeWord, Apache-2.0)
as a tiny streaming engine. **NOT whisper, NOT transcription**: a ~3.5 MB
always-on gate that hands audio to `scorpiox-next-whisper-transcription`
only after the phrase fires.

## Pipeline (mirrors openwakeword v0.6 streaming exactly)

```
16 kHz mono PCM16
  → accumulate 1280-sample (80 ms) frames
  → melspectrogram.onnx over the last 1280+480 samples  → (frames, 32) mel bins, x/10 + 2
  → mel buffer seeded with ones(76, 32)
  → last 76 mel frames → embedding_model.onnx → 96-d speech embedding
  → last 16 embeddings → wake phrase model → score 0..1
```

Frame-for-frame verified against a Python golden reference (max diff
6e-7 over a whole clip; see test/golden/ notes in the report).

## Models (Apache-2.0, from the openWakeWord repo)

| File | Size | Role |
|------|------|------|
| `melspectrogram.onnx` | 1.0 MB | frontend |
| `embedding_model.onnx` | 1.3 MB | frontend |
| `hey_jarvis_v0.1.onnx` | 1.2 MB | phrase (example) |
| `hey_mycroft_v0.1.onnx`, `alexa_v0.1.onnx`, `hey_rhasspy_v0.1.onnx`, `timer_v0.1.onnx`, `weather_v0.1.onnx` | 0.2–1.7 MB | other phrases |

Download into `models/` (gitignored) before building/running:

```bash
python3 - <<'EOF'
import sys; sys.path.insert(0, '/tmp/upstream-openwakeword')  # or pip install openwakeword
import openwakeword; openwakeword.utils.download_models()
EOF
```

## Exports (cdecl)

| Export | Returns | Notes |
|--------|---------|-------|
| `nxwake_load(frontend_dir, model_path, threshold)` | int | 0 ok, -1 fail |
| `nxwake_load_model(model_path)` | int | new model index, -1 fail (max 8) |
| `nxwake_write(pcm, n_samples)` | int | frames processed (8/s), -1 fail |
| `nxwake_triggered(model_index*, score*)` | int | 1 once per trigger, auto-resets |
| `nxwake_scores(scores, n)` | int | latest score per model |
| `nxwake_model_count()` | int | -1 when not loaded |
| `nxwake_reset()` | void | clear buffers + scores |
| `nxwake_unload()` | void | release everything |
| `nxwake_last_error()` | const char* | static buffer |
| `nxwake_version()` | const char* | static buffer |

`nxwake_write` accepts ANY chunk size (internal 1280-sample accumulator).
A trigger latches once and auto-resets on the next `nxwake_triggered`
read, with a 640 ms debounce so one utterance fires once.

## Build

```bash
make            # .so + C self-test + CLI (needs models/ + deps/onnxruntime-linux)
make test       # build/test-nxwake
make cli        # build/nxwake-cli
# Windows cross build on .204:
#   make win  ORT_WIN=C:/tools/ort-win
```

## Self-test (no mic needed)

```bash
LD_LIBRARY_PATH=deps/onnxruntime-linux/lib ./build/test-nxwake
# 24/24 PASS — positives trigger, negatives don't, latch auto-resets,
# odd-sized streaming chunks give identical verdicts.
```

Fixtures in `samples/`: TTS "hey jarvis" (piper, 4 voices), alexa +
mycroft upstream test clips (negatives), jfk.wav (negative).

## CLI

```bash
LD_LIBRARY_PATH=deps/onnxruntime-linux/lib ./build/nxwake-cli watch models models/hey_jarvis_v0.1.onnx samples/hey_jarvis_amy.wav
# TRIGGER frame 28 (t=2.24s) model 0 score 0.9017
```

## CPU cost

Measured on an i5-8500B @3.0 GHz core (single thread, 1 ONNX session):
**0.039 CPU-seconds per audio-second = ~3.9% of one core** — one wake
model. Scales roughly linearly with extra phrase models. The 80 ms
streaming cadence means the work happens in 8 small bursts per second.

## License

Engine: MIT (this repo). Models: Apache-2.0 (openWakeWord). The wake
models were trained on synthetic TTS + public datasets — see
docs/models/*.md upstream for per-phrase provenance. Store apps: the DLL
only detects; the app owns mic consent UX (always-on mic = Store
scrutiny).
