Files
microduck/pet-detect
Pierre Rouanet 9e51860be0 The petting classifier ran a hundred FFTs a second through silence
`PettingDetector` inferred on every stride whatever the mic had picked up: a
1 s window of 40 log-mel bands is a hundred 512-point FFTs plus a forward pass,
four times a second, for as long as the daemon is up. In a quiet room every one
of those returns the same answer.

The cheap way to know that in advance was already being computed beside it.
`SoundSentry` tracks an RMS envelope against an ambient floor with a 6 s time
constant, so the gate reads that rather than keeping a second measure of the
room that could drift from the first. Nothing runs until something in the last
window stood out from the floor.

Armed on the sentry's *lower* threshold — the one that says an event has ended,
not the one that says a clap has started. Those two numbers answer different
questions and do not want the same margin: this one is deciding whether a pet is
worth looking for. A stroke gentle enough to stay under three times ambient for
a whole window would be missed, and would have scored far below the 0.95 enter
threshold anyway on a mic where a head scratch is loud enough that the sentry
has to mute itself for one.

**The gate never applies while petting.** The End edge is found by inference
exactly as the Start is, so a session whose room went quiet under a shut gate
would never end — a robot sitting there believing it was still being stroked.

Two things the shape is doing deliberately:

`samples_until_infer` now advances at the top of the loop body rather than the
bottom, so every path out of an iteration has advanced it. A skip that forgot to
would not be a missed inference; it would be that loop spinning on one window
until the thread was killed, and this makes that unwritable rather than merely
untested.

`AUDIBLE_HOLD_FRAMES` is arithmetic on `WINDOW_SAMPLES`, not the literal 31 the
neighbouring hangover uses. A window is 16 240 samples — 1.015 s, not the "about
a second" that suggests — so 31 frames of hold shuts the gate on the last 23 ms
of audio still under examination. The test that says so found it.

`push_samples` takes the gate as an argument, and `bin/detect` passes `true`:
that tool analyses a recording and a probability for every window, quiet ones
included, is the whole of its output.

Off by default (`[audio] pet_detect`), so this is a cost anyone who turns it on
stops paying rather than one every robot was paying.

Assisted-by: Claude:claude-opus-5
2026-09-02 15:38:02 +02:00
..

pet-detect

A tiny audio classifier that hears when the robot's head is being scratched (the onboard mic sits there). ~20 KB CNN over 40-band log-mel windows, sub-millisecond inference.

Ported from apirrone/microduck_pet_detect; the arecord worker and the ambient sound sentry from the prototype's pet_worker.rs live here too, so everything that listens to the mic is one crate. robotd runs the worker (see [audio] in deploy/robotd.toml) and coos when petting starts.

  • Library: PettingDetector (streaming, hysteresis) and worker::PetHandle (arecord subprocess + event channels).
  • pet-detect (binary): pipe arecord -D plughw:aic3104,0 -f S16_LE -r 16000 -c 1 -t raw into it and watch the probability while you scratch the head.
  • pet-features (binary): dumps log-mel features of a WAV — the training side of the train/infer parity contract.
  • models/pet_detect.onnx: the trained model, shipped in the release as models/pet_detect.onnx.

Retraining

training/train.py (PyTorch) extracts features THROUGH the pet-features binary — the same code path the robot runs, so there is no train/infer drift. Record data on the robot:

arecord -D plughw:aic3104,0 -f S16_LE -r 16000 -c 1 -d 30 /tmp/petting_01.wav

into data/petting/ and data/normal/ (walking, motors, ambient — anything that isn't petting; the recordings themselves are not vendored here), then:

cargo build --release -p pet-detect --bin pet-features
uv run --with torch --with onnx training/train.py

and commit the refreshed models/pet_detect.onnx.