f3716d3ff1
A single 100 ms RMS window above the noise floor used to become its own section, so isolated pops (clicks, single raindrops) flooded a day with thousands of sub-second clips like "21:18 to 21:18". Sections shorter than min_duration (measured after min_gap merging, so a cluster of blips spanning longer still flags) are now discarded. Wired through all coupled places: CLI flag, /api/config, controls-bar input, /api/analyze query param, and the analysis-cache head keys (old two-key caches no longer match and are recomputed on next analyse). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
8.6 KiB
8.6 KiB
CLAUDE.md
Guidance for Claude Code when working in this repository.
Rules
- Always update
README.mdwhen user-facing behaviour changes (flags, endpoints, Docker setup, features), and commit it in the same commit as the code change. README is the external reference; CLAUDE.md documents internals. - Run
python -m pytest tests/after changingisr.pyorweb.py(tests cover the recorder and the loud-section detector).
Files
| File | Purpose |
|---|---|
isr.py |
Recorder: streams (Icecast/HTTP) + ALSA soundcards, time-aligned file splits |
web.py |
Archive browser: HTTP server, file listing, RMS loudness analysis, cut/delete |
webui.html |
Single-page UI (HTML/CSS/JS), loaded by web.py at startup — must sit next to web.py and be copied in the Dockerfile |
config.ini |
Recording sources; copy from config.example.ini. [general] gives defaults, every other section is a source (type = stream or type = soundcard) |
asound.conf |
dsnoop device shared_mic so ISR and other ALSA apps can share a soundcard |
Commands
python isr.py [config.ini] # recorder; --list-devices to list ALSA inputs
python web.py # web UI on :8080 (--dir, --port, --margin, --min-gap, --min-duration, --analyses-dir)
python -m pytest tests/ # test suite
docker compose up -d / down # web UI mapped to host port 8050
Dependencies: requests (streams), numpy + soundfile (FLAC output and FLAC analysis/clips — both optional, code degrades gracefully).
Code map
web.py:
- Detection:
_compute_rms_windows_wav()/analyze_flac()produce 100 ms RMS windows →_noise_floor_db()estimates the rolling floor →_loud_sections()emits scored sections →_package_result()shapes the/api/analyzepayload. - Clips:
_api_clip()validates params,_clip_wav()/_clip_flac()stream the decoded slice,_wav_header()builds the 44-byte PCM header. - Live headers:
_live_wav_header(),_live_flac_header()(+_flac_frame_samples(), CRC-8 verified). - Serving:
_stream()(Range support),_copy_to_response(),_safe_path()(path traversal guard).
webui.html (one <script> block):
- Clip review:
clipQueue/clipCursorglobals,playClip(),playFileSection(),hideClipBar(); markup is the#clip-bardiv. - Day review:
dayHighlights()buildsdayActiveSections(chronological);jumpToDaySection()arms the queue. - J/K: single document-level
keydownlistener — clip queue takes priority, in-playercurrentTimestepping is the fallback when no queue is armed. - Analysis:
fetchAnalysis()(sessionanalysisCache),analyse()(per-row render),cachedParamsMatch()(autoload guard).
Verifying changes
python -m pytest tests/covers the recorder (test_isr.py) and the detector (test_web.py).- There is no JS toolchain and no
nodeon the dev box. After editingwebui.html, cross-check everygetElementById('x')against anid="x"declaration, and smoke-test endpoints. - Endpoint smoke pattern: write a temp WAV/FLAC with a known loud burst, subclass
web._Handlerwithrecordings_dir/analyses_dirpointing at the temp dir, serveweb._Server(('127.0.0.1', 0), H)in a daemon thread, then hit/api/analyzeand/api/clipwith urllib — assert section start/score and thatContent-Length == len(body) == 44 + frames × channels × 2. - Dev box is Windows / PowerShell 5.1. Multi-line commit messages: use the Bash tool with
git commit -F - <<'EOF'— PowerShell here-strings containing quotes get mangled into separate arguments.
Non-obvious internals
- Recorder/web coupling is one file:
RecorderManageratomically writesrecordings/status.jsonevery 2 s listing in-progress files; deleted on clean shutdown.web.pyreads it to show REC badges and to refuse analyse/cut/delete on active files. In-progress WAV/FLAC headers are unfinalized, so durations are not read for active files. - Stream splits: OGG/Opus/FLAC codec headers are extracted from the first ~16 KB of each connection and prepended to every split file so each file plays standalone. A new file is always opened on reconnect (gap in stream). MP3/AAC need no headers.
- Split timing: files split at clock-aligned boundaries (
get_next_split_time()), e.g.split_minutes = 60→ on the hour. - ALSA: capture spawns
arecordas a subprocess, raw PCM read in 100 ms chunks by a thread. Device spec resolution:default→ exacthw:X,Y→ partial name → fallback to any literal ALSA PCM name (soshared_micfrom asound.conf works without appearing inarecord -l). - Shutdown: SIGTERM is converted to KeyboardInterrupt in
main();RecorderManager.stop()joins all threads against a single shared 25 s deadline to stay inside Docker'sstop_grace_period: 30s. - Loud-section detection is adaptive — do not regress it to an absolute threshold. Per-window dB is compared against a rolling noise floor (
NOISE_PERCENTILE-th percentile perNOISE_BLOCK_SECONDSblock, min-smoothed over ±2 blocks so events can't raise their own floor; clamped to ≥MIN_RMS). A section needsmargindB of prominence and carries ascore(peak dB above floor) used for ranking. Sections shorter thanmin_duration(default 0.5 s, aftermin_gapmerging) are discarded — without this, isolated 100 ms pops (clicks, single raindrops) produced thousands of zero-length sections per day. The original fixed RMS threshold flagged every ambience change (passing cars, rain) and produced ~600 useless sections/day — that is why it was replaced. Known limitation: a short (~10 s) swell on a quiet street still flags because the floor blocks are 30 s; the planned fix is an onset/spectral filter or optional Silero VAD, not a higher margin. Tests intests/test_web.py. - Analysis params are coupled in five places. CLI
--margin/--min-gap/--min-duration→/api/config→ UI inputs#margin-input/#min-gap-input/#min-duration-input→/api/analyzequery params → cache JSON head keys. Renaming or adding a param means touching all five pluscachedParamsMatch()and the_cached_analysis_params()regex (see the threshold→margin changec84b7d8and the min_duration addition). - Analysis cache: results stored as
<analyses-dir>/<file>.analysis.jsonkeyed by margin+min_gap+min_duration; orphans pruned at web startup. In Docker the recordings mount is read-only for the web container, so docker-compose layers a read-write./recordings/analysesbind mount over it. Themargin,min_gap, andmin_durationkeys MUST stay first in the cache JSON —_cached_analysis_params()reads only the first 256 bytes to avoid parsing the large embedded result. Caches written by older detector versions (missing a key) never match and get overwritten on the next analyse. - Analyze responses:
/api/analyzereturnsrms_display(~800 points), never the full per-window RMS list — the UI doesn't use it and it is ~45x larger. - Section playback uses clips, not seeks:
/api/clip?file&start&enddecodes the slice server-side (wave/soundfile) and returns a standalone 16-bit WAV with exact Content-Length (capped atCLIP_MAX_SECONDS),Cache-Control: privateso re-listening is free. The UI plays chips/J-K through the bottom clip bar (clipQueuein webui.html); seeking the full file only happens via "Open in file". Rationale (finding): libsndfile writes FLAC without a SEEKTABLE, so a browser seek bisects the whole multi-hundred-MB file with Range requests — seeking big FLACs in<audio>is inherently slow and must not be reintroduced as the primary navigation. Server-sidesf.SoundFile.seek()on local disk is fast and frame-accurate. - HTTP/1.1 keep-alive:
_Handler.protocol_version = 'HTTP/1.1'; every response path must set an accurateContent-Length._copy_to_response()force-closes the connection if it under-delivers (file truncated mid-serve). - Live playback: for files listed in status.json,
/stream/patches the header on the fly so the browser sees the duration recorded so far and can seek; responses getCache-Control: no-store. WAV:_live_wav_headerderives sizes from the byte count. FLAC:_live_flac_headerparses the sample count out of the last frame header in the file tail (CRC-8-verified to reject false sync matches) and rewrites STREAMINFO total_samples — duration is NOT derivable from byte size for FLAC. - Path safety: every file parameter in
web.pygoes through_safe_path(), which resolves and verifies the path stays inside the recordings dir. - dsnoop in Docker: sharing the soundcard requires
asound.confon the host andipc: hostin docker-compose (dsnoop uses shared memory across the container boundary).