Manual:Streams and windows
A sound card delivers audio in small blocks (by default 2048 samples, around 43 ms at 48 kHz), whereas a model needs roughly one second of sound to judge whether an engine is running. The stream/inference contract (docs/stream_contract.md, implemented as StreamProcessor in app/detector.py) guarantees that live block-by-block processing cuts the exact same windows and produces the same scores as analysing the whole WAV file offline.
Why live and file analysis must agree¶
In earlier code (audit finding F04), the live detector stepped windows using its own hop_s timer (0.5 s) and resampled each 1-second YAMNet window on its own. As a result, live windows drifted away from whole-file windows (116 windows instead of 119 over 60 seconds) and filter edge effects shifted scores by up to 0.035. The same model could therefore disagree with itself between live listening and Clip analysis.
Under the current contract, window placement and scores no longer depend on sound-card block size, chunk boundaries or CPU speed.
Windows on the model's own grid¶
Each model defines its own window grid, and StreamProcessor places every live window on that exact grid:
| Model type | Analysis sample rate | Window length | Step (hop) | Resampling |
|---|---|---|---|---|
bands (trained) |
Stream native sr (at least 35,919 Hz) |
1.0 s | 0.5 s | None (never resampled) |
yamnet and YAMNet-embedding models |
16,000 Hz | 0.975 s (15,600 samples) | 0.48 s (7,680 samples) | scipy.signal.resample_poly, grid-aligned context |
The Analysis step (s) setting (detector.hop_s, default 0.5) no longer shifts the grid. Instead it picks every n-th window on the model's grid (stride = max(1, round(detector.hop_s / model.hop_s))). At the default 0.5 s, stride = 1 for both bands (0.5 s) and YAMNet (0.48 s), so every grid window is scored. Setting hop_s to 1.0 s scores every second window to save CPU while staying aligned to the model's grid.
Why live and offline scores now match¶
- For
bandsmodels, no resampling happens: live streaming andmodel.predict()slice the exact same input samples[k·hop, k·hop + win). Tested with random block sizes from 1 to 6000 samples (tests/test_detector.py), live and offline scores on real Wolfset audio (wolfset-bands-v1, 44.1 kHz, 23 windows) differ by at most4.5e-9, purely from float32 matrix-multiplication summation order. - For YAMNet models, resampling a bare window introduces filter edge artefacts at 44.1 or 48 kHz. Instead,
StreamProcessorkeeps extra audio on both sides of the window (at least the FIR filter's half-length), starts that context on an exact multiple of the downsampling factordown, and resamples the aligned context withscipy.signal.resample_poly. At the start and end of a stream (close()), the missing tail of the context is zero-padded just like whole-file resampling (context_padded = true). On a 6-second Wolfset excerpt, live and offline YAMNet agree to a maximum absolute difference of1.6e-7—thousands of times smaller than any threshold step.
One small difference at the end of a file is intentional: whole-file YAMNet can pad one extra frame reaching past the end of the audio (for instance a 12th frame covering 5.28–6.255 s in a 6.0 s clip). The live stream never invents samples past the end and reports that unfinished tail as a gap record instead.
Window records and sample positions¶
Every scored window emits a window record (window_result signal) containing both the score and its exact provenance:
- Stream and grid index:
streamcounter (0, 1, 2, …) and windowindexon the model's grid. - Sample timing:
start_sampleandend_samplein input samples, plusstart_sandend_sin seconds from stream start counted from received samples (strictly monotonic, never derived from wall clock). - Technical metadata:
samplerate_hz,analysis_samplerate_hz,channel,resampling,context_padded,model,score_engine,probsandtop. - Wall-clock latency:
ready_wall_time(when the last needed block arrived),analysed_wall_time(when scoring finished) andcompute_s(model inference time). Live latency isanalysed_wall_time - ready_wall_time.
The primary timestamp of a window record is its start (start_s), matching Prediction.times. The legacy Detector.result(t, pred) signal used by the current UI passes end_s as t (the decision moment when the whole window has arrived) so the live plot, scores.csv and legacy Alarm keep causal timestamps.
Gaps are explicit records, not low scores¶
When windows cannot be scored, StreamProcessor never fakes a 0 or negative score. Instead it emits a separate gap record (with no score_engine field), merging consecutive skipped windows with the same cause into one range (first_index..last_index, start_sample..end_sample, start_s..end_s):
Reason (reason) |
When it occurs |
|---|---|
discontinuity |
The audio source lost samples (such as sounddevice input overflow). Every window whose resampling context crosses the break is skipped. |
overload |
More than 20 audio blocks (MAX_BACKLOG = 20) queued up because the CPU fell behind. Ready windows are skipped while sample counting and the window grid stay intact. |
restart |
A new source was started (Detector.reset). Any incomplete tail window of the old stream is reported as a gap, the buffer is cleared and time restarts from 0. |
samplerate_change |
The incoming sample rate changed without an explicit reset. The old stream closes and the buffer is cleared so samples at different rates are never mixed. |
The live view logs each gap as a warning and resets the legacy alarm's consecutive-window counter. Skipped windows are final and are never back-filled later.
Warm-up and boundaries¶
At start-up, warm_up() warms up only YAMNet at its native 16 kHz rate. A bands model is never warmed up at 16 kHz because it refuses audio below 35,919 Hz (see The detector). StreamProcessor also does not mix multi-channel audio: channel selection happens in the audio source (audio.channel) and channel is recorded in window records purely as a label.
See also¶
- The detector: the live detector and choosing a model
- Alarms and events: turning window scores into alarms and events
- Comparing models: comparing models across a full WAV file
- Tests: the automated contract test
tests/test_detector.py