Add saunterbrowse, for reading back what a scan collected

A long scan leaves hundreds of recordings, each with a JSON sidecar of
measurements and, where a recogniser heard speech, a transcript.  Reading
that meant opening files one at a time and guessing which were worth
playing.

saunterbrowse is a second executable in the same package.  Arrow keys move
through the recordings; the transcript of whichever is highlighted fills
the top of the screen, because that is the part anyone actually wants to
read.  Enter plays it, handing the file to whichever player is installed
-- the recordings are ordinary WAVs, every desktop already has something
that plays them, and a browser that cannot start would be worse than one
that cannot play.  t opens the whole transcript full screen when it is
longer than the panel, and says so rather than cutting the end off
silently.  / filters on the frequency, the name, the identification, or
anything that was said, which is the point of it: "was the repeater
mentioned" is a question about content.

Sidecars are read only for the rows on screen, so a directory of ten
thousand recordings opens instantly.  Where there is no transcript the
panel says which of the reasons applies -- Morse (decoded, and shown),
data, a bare carrier, or speech never offered to a recogniser -- because
those want different things done about them.  It only ever reads.

Two things were only found by driving it through a real terminal.
sys.stdin.read(1) goes through a buffered text wrapper, which in cbreak
mode waits for more bytes than one keypress provides: the program drew its
first frame and then hung, while tests against a stand-in stream object
passed.  It reads the file descriptor now, and the tests drive a pty.  And
stopping playback signalled only the direct child, so a player that is a
wrapper script kept the sound going with nothing on screen to stop it; the
whole process group is signalled instead, which is what start_new_session
was there for.

man saunterbrowse ships beside man bandsaunter, and the two point at each
other.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016PsWPTweCT6pwxKngvVxcg
This commit is contained in:
The Dust Council 2026-08-22 15:06:33 -07:00
parent 4a272eb1d5
commit cc317914e1
11 changed files with 2233 additions and 5 deletions

View file

@ -7,6 +7,10 @@ built-in US band plan — and it sweeps them, stops on anything above the noise
floor, records it, and works out what kind of signal it was. CW/Morse is
decoded to text.
Two programs: `bandsaunter` scans, and
[`saunterbrowse`](#browsing-what-you-recorded) reads back what it collected —
transcripts, identifications and playback, in one screen.
```
╭──────────────────────────────── receiver ────────────────────────────────╮
│ Rafael Micro R820T/R820T2 2.048 MS/s gain auto +0 ppm │
@ -931,6 +935,81 @@ and `--simulate` is looking at an invented band, whose frequencies would sit in
a real settings file for ever, skipping whatever genuine signal happened to
land near one. Both still lock out for the run in hand, and say so.
## Browsing what you recorded
A long scan leaves hundreds of files. `saunterbrowse` is a second program in
the same package for reading them:
```bash
saunterbrowse # opens the scanner's output directory
saunterbrowse /mnt/recordings # or any other
```
Arrow keys move through the recordings; the transcript of whichever one is
highlighted fills the top of the screen, because that is the part you actually
want to read. Under it are the identification, the confidence, the CTCSS tone
or symbol rate where there is one, and the bands the frequency falls in.
```
╭──────────────────────────────────────────────────────────── 4 of 126 ─╮
│ 146.88 MHz NFM Sat 22 Aug 13:01:44 42.8s SNR 17.6 dB voice │
╰───────────────────────────────────────────────────────────────────────╯
╭─ transcript ──────────────────────────────────────────────────────────╮
│ │
│ Alright, moving on. It is the 4th Saturday of the month. There is │
│ an HF net at 1.30pm on 7.242 megahertz. Are there any │
│ announcements for the net? │
│ │
╰───────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────╮
│ Narrowband FM voice (CTCSS 110.9 Hz) 88% CTCSS 110.9 Hz │
│ 2 m Amateur · 2 m FM Simplex · 2 m Repeater Outputs │
╰───────────────────────────────────────────────────────────────────────╯
╭─ recordings in /mnt/global/bandsaunter ───────────────────────────────╮
│ 856.561096 MHz 13:10:25 fsk 4m00s Motorola SMARTNET / Smart… │
│ 158.294200 MHz 13:05:15 nfm 20.1s Steven, I'm over to Colvi… │
146.88 MHz 13:01:44 nfm 42.8s Alright, moving on. It is… │
│ 146.88 MHz 13:00:44 nfm 35.2s Check out communication o… │
╰───────────────────────────────────────────────────────────────────────╯
↑↓ move ⏎ play space stop / search t read s sort ? keys q
```
| Key | What it does |
|---|---|
| `↑` `↓` `k` `j` | move through the recordings |
| `PgUp` `PgDn` `Home` `End` | a screenful, or straight to either end |
| `Enter` | play the highlighted recording |
| `space` | stop playing |
| `t` | read the whole transcript full screen, scrolling |
| `/` | filter — by frequency, filename, identification, **or anything that was said** |
| `s` | sort by time, frequency or length |
| `r` | re-read the directory, picking up what a running scan has written |
| `o` | print the file's path and quit |
| `q` | quit |
Searching the transcripts is the point of it: *"did anyone mention the
repeater"* is a question about content, not about filenames.
```bash
saunterbrowse --list | grep -i "mile marker" # or ask it from a script
saunterbrowse --sort frequency # group by channel, not by time
```
Playback is handed to whichever player is installed — `pw-play`, `paplay`,
`aplay`, `sox` or `ffplay`, in that order, or whatever `--player` names. The
recordings are ordinary WAVs and every desktop already has something that
plays them; a browser that cannot start would be worse than one that cannot
play. Over ssh, where there is usually no sound server at the far end, the
transcripts still work and only `Enter` has nothing to do.
Where a recording has no transcript the panel says which of the reasons
applies — Morse (decoded, and shown), data, a bare carrier, or speech that was
never offered to a recogniser — because those want different things done about
them.
It only ever reads. Nothing in the recordings directory is renamed, moved or
deleted.
## Built-in help
Press `h` in the menus for topics covering setup, how the sweep works, why
@ -945,14 +1024,16 @@ and `bandsaunter scan --help` lists every flag grouped the same way as the menus
`man bandsaunter` documents every command, option and setting, each with a
plain-language note on what it is and why you would turn it up, down, on or
off — written for someone who does not already speak radio.
off — written for someone who does not already speak radio. `man
saunterbrowse` does the same for the browser.
It is generated from the same settings table the menus and the flags come from,
so it cannot describe a setting the program does not have, or miss one it does:
```bash
./packaging/make-man.py # regenerate packaging/bandsaunter.1
man -l packaging/bandsaunter.1 # read it without installing
./packaging/make-man.py # regenerate packaging/bandsaunter.1
./packaging/make-browse-man.py # and packaging/saunterbrowse.1
man -l packaging/bandsaunter.1 # read either without installing
```
The `.deb` installs it; installing from source does not, so read it from the

View file

@ -9,7 +9,7 @@ and transcribing speech.
# 2026-08-21_02 is the second build made on the 21st. The revision is padded
# to two digits so versions sort as text.
VERSION_DATE = "2026-08-22"
VERSION_REVISION = 2
VERSION_REVISION = 3
__version__ = f"{VERSION_DATE}_{VERSION_REVISION:02d}"

1008
bandsaunter/browse.py Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
.\" Generated by packaging/make-man.py -- do not edit by hand.
.TH BANDSAUNTER 1 "2026-08-22" "bandsaunter 2026-08-22_02" "User Commands"
.TH BANDSAUNTER 1 "2026-08-22" "bandsaunter 2026-08-22_03" "User Commands"
.SH NAME
bandsaunter \- scan, record and identify radio signals with an RTL-SDR
.SH SYNOPSIS
@ -961,6 +961,10 @@ Raise the squelch threshold and save it as the new default.
0 on success, 1 for a bad option or an unusable configuration, 2 when the
receiver could not be opened.
.SH SEE ALSO
.BR saunterbrowse (1)
\[em] browse and play back what a scan collected: the recordings list, their
transcripts and their identifications, on one screen.
.PP
.BR rtl_test (1),
.BR rtl_sdr (1),
.BR espeak-ng (1)

View file

@ -27,7 +27,10 @@ gzip -9n "$pkgdir/usr/share/doc/bandsaunter/README.md"
mkdir -p "$pkgdir/usr/share/man/man1"
python3 "$here/packaging/make-man.py" "$pkgdir/usr/share/man/man1/bandsaunter.1" \
>/dev/null
python3 "$here/packaging/make-browse-man.py" \
"$pkgdir/usr/share/man/man1/saunterbrowse.1" >/dev/null
gzip -9n "$pkgdir/usr/share/man/man1/bandsaunter.1"
gzip -9n "$pkgdir/usr/share/man/man1/saunterbrowse.1"
cat > "$pkgdir/usr/bin/bandsaunter" <<'EOF'
#!/usr/bin/python3
@ -37,6 +40,14 @@ sys.exit(main())
EOF
chmod 755 "$pkgdir/usr/bin/bandsaunter"
cat > "$pkgdir/usr/bin/saunterbrowse" <<'EOF'
#!/usr/bin/python3
import sys
from bandsaunter.browse import main
sys.exit(main())
EOF
chmod 755 "$pkgdir/usr/bin/saunterbrowse"
# Every one of these is in Debian, so apt resolves the lot. No speech
# recogniser is packaged for Debian, so that part ships as its own package
# (built by build-repo.sh) and is recommended rather than depended on: apt

219
packaging/make-browse-man.py Executable file
View file

@ -0,0 +1,219 @@
#!/usr/bin/env python3
"""Generate the saunterbrowse manual page.
Hand-written rather than generated from a table: unlike the scanner, the
browser's surface is a dozen keys and five flags, and describing those is
prose, not a listing. The version and date still come from the program, so
the page cannot claim to document a release that was never built.
"""
import sys
from datetime import date
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import bandsaunter # noqa: E402
from bandsaunter.browse import PLAYERS, SORTS # noqa: E402
PAGE = r'''.\" Generated by packaging/make-browse-man.py -- do not edit by hand.
.TH SAUNTERBROWSE 1 "{date}" "bandsaunter {version}" "User Commands"
.SH NAME
saunterbrowse \- read and listen to what a bandsaunter scan collected
.SH SYNOPSIS
.B saunterbrowse
.RI [ DIRECTORY ]
.RB [ \-\-sort
.IR ORDER ]
.RB [ \-\-filter
.IR TEXT ]
.RB [ \-\-player
.IR CMD ]
.RB [ \-\-list ]
.SH DESCRIPTION
A scan leaves a directory of recordings. Beside each one is a JSON file
holding what the classifier made of it, and for speech a transcript of what
was said. Reading that by hand means opening files one at a time and guessing
which are worth playing.
.PP
.B saunterbrowse
shows them as a list you move through with the arrow keys. The transcript of
whichever recording is highlighted fills the top of the screen, because that
is the part you actually want to read; underneath it are the identification,
the bands the frequency falls in, and the list itself. Pressing Enter plays
the recording.
.PP
With no
.I DIRECTORY
it opens the one the scanner writes to, taken from your saved settings, so it
normally needs no arguments at all.
.PP
It only ever reads. Nothing in the recordings directory is renamed, moved or
deleted.
.SH KEYS
.TP
.B "Up Down k j"
Move through the recordings.
.TP
.B "PgUp PgDn"
A screenful at a time.
.TP
.B "Home End"
The first and the last.
.TP
.B Enter
Play the highlighted recording. Playing a second one stops the first: two
players talking over each other is worse than either alone.
.TP
.B Space
Stop playing. With nothing playing, it starts, like Enter.
.TP
.B t
Read the whole transcript full screen, scrolling with the arrow keys. Useful
for a long net, where the panel at the top can only show the first few lines
\[em] it says so when there is more.
.TP
.B /
Filter. What you type is matched against the frequency, the filename, the
identification and
.I everything that was said,
so "was the repeater mentioned" is a question you can ask directly. Enter
accepts, Escape clears.
.TP
.B s
Cycle the order: {sorts}.
.TP
.B r
Re-read the directory. A scan running in another window is still writing to
it, and this picks up what has arrived since.
.TP
.B o
Print the highlighted recording's path and quit, for piping into something
else.
.TP
.B "? h"
The list of keys, and which audio player was found.
.TP
.B q
Quit. With a filter in force, Escape clears the filter first.
.SH OPTIONS
.TP
.BI DIRECTORY
Where the recordings are. Defaults to the scanner's output directory, or to
.B $BANDSAUNTER_OUTPUT
when that is set.
.TP
.BI \-\-sort " ORDER"
Start in this order: {sorts}. Time is newest first; duration is longest
first.
.TP
.BI \-\-filter " TEXT"
Start with only the recordings matching this, exactly as if it had been typed
at the
.B /
prompt.
.TP
.BI \-\-player " CMD"
The command used to play a recording. The path is appended as its last
argument. By default the first of these that is installed is used:
{players}.
.TP
.B \-\-list
Print one line per recording and exit, without drawing anything. This is what
to use over a pipe, in a script, or anywhere there is no terminal.
.TP
.B \-V ", " \-\-version
Print the version and exit.
.SH SOUND
Playback is handed to whichever player is installed rather than done in the
program: the recordings are ordinary WAV files, every desktop already has
something that plays them, and a browser that cannot start is worse than one
that cannot play. If none is found, everything else still works and the
message says which packages would fix it.
.PP
Over ssh there is usually no sound server at the far end. The browser and its
transcripts work regardless; only Enter has nothing to do.
.SH TRANSCRIPTS
A transcript appears only where a recogniser produced one, which means the
capture was judged to be speech and
.B bandsaunter\-transcribe
was installed at the time. Where there is none, the panel says which of the
several reasons applies \[em] Morse, data, a bare carrier, or speech that was
never offered to a recogniser \[em] because those want different things done
about them.
.PP
Two places can hold the text: the JSON sidecar records what was recognised
during the scan, and the
.I _transcription.txt
beside the recording is what a later re\-run wrote. The file wins, being the
more recent of the two.
.SH FILES
.TP
.I ~/bandsaunter/
Where recordings are written, unless the saved settings say otherwise.
.TP
.IR frequency \-\- date _ time \- modulation .wav
A recording.
.TP
.IR ... .json
Its measurements and identification.
.TP
.IR ... _transcription.txt
What was said, where a recogniser heard speech.
.SH ENVIRONMENT
.TP
.B BANDSAUNTER_OUTPUT
The directory to open, overriding the saved settings.
.SH EXAMPLES
.PP
.RS
.EX
saunterbrowse
.EE
.RE
.PP
Open the scanner's output directory.
.PP
.RS
.EX
saunterbrowse /mnt/recordings \-\-sort frequency
.EE
.RE
.PP
Browse somewhere else, grouped by channel rather than by time.
.PP
.RS
.EX
saunterbrowse \-\-list | grep \-i "mile marker"
.EE
.RE
.PP
Search the transcripts from a script.
.SH EXIT STATUS
0 on a clean exit, 1 when the directory holds no recordings, 2 when it does
not exist or there is no terminal to draw on.
.SH SEE ALSO
.BR bandsaunter (1)
.SH BUGS
The list is read when the browser opens. Press
.B r
to pick up recordings a running scan has written since.
'''
def main() -> int:
text = PAGE.format(
date=date.today().isoformat(),
version=bandsaunter.__version__,
sorts=", ".join(SORTS),
players=", ".join(name for name, _ in PLAYERS))
text = text.replace("\n\n", "\n") # troff dislikes blank lines
target = Path(sys.argv[1] if len(sys.argv) > 1
else Path(__file__).parent / "saunterbrowse.1")
target.write_text(text)
print(target)
return 0
if __name__ == "__main__":
raise SystemExit(main())

View file

@ -412,6 +412,10 @@ Raise the squelch threshold and save it as the new default.
0 on success, 1 for a bad option or an unusable configuration, 2 when the
receiver could not be opened.
.SH SEE ALSO
.BR saunterbrowse (1)
\[em] browse and play back what a scan collected: the recordings list, their
transcripts and their identifications, on one screen.
.PP
.BR rtl_test (1),
.BR rtl_sdr (1),
.BR espeak-ng (1)

182
packaging/saunterbrowse.1 Normal file
View file

@ -0,0 +1,182 @@
.\" Generated by packaging/make-browse-man.py -- do not edit by hand.
.TH SAUNTERBROWSE 1 "2026-08-22" "bandsaunter 2026-08-22_03" "User Commands"
.SH NAME
saunterbrowse \- read and listen to what a bandsaunter scan collected
.SH SYNOPSIS
.B saunterbrowse
.RI [ DIRECTORY ]
.RB [ \-\-sort
.IR ORDER ]
.RB [ \-\-filter
.IR TEXT ]
.RB [ \-\-player
.IR CMD ]
.RB [ \-\-list ]
.SH DESCRIPTION
A scan leaves a directory of recordings. Beside each one is a JSON file
holding what the classifier made of it, and for speech a transcript of what
was said. Reading that by hand means opening files one at a time and guessing
which are worth playing.
.PP
.B saunterbrowse
shows them as a list you move through with the arrow keys. The transcript of
whichever recording is highlighted fills the top of the screen, because that
is the part you actually want to read; underneath it are the identification,
the bands the frequency falls in, and the list itself. Pressing Enter plays
the recording.
.PP
With no
.I DIRECTORY
it opens the one the scanner writes to, taken from your saved settings, so it
normally needs no arguments at all.
.PP
It only ever reads. Nothing in the recordings directory is renamed, moved or
deleted.
.SH KEYS
.TP
.B "Up Down k j"
Move through the recordings.
.TP
.B "PgUp PgDn"
A screenful at a time.
.TP
.B "Home End"
The first and the last.
.TP
.B Enter
Play the highlighted recording. Playing a second one stops the first: two
players talking over each other is worse than either alone.
.TP
.B Space
Stop playing. With nothing playing, it starts, like Enter.
.TP
.B t
Read the whole transcript full screen, scrolling with the arrow keys. Useful
for a long net, where the panel at the top can only show the first few lines
\[em] it says so when there is more.
.TP
.B /
Filter. What you type is matched against the frequency, the filename, the
identification and
.I everything that was said,
so "was the repeater mentioned" is a question you can ask directly. Enter
accepts, Escape clears.
.TP
.B s
Cycle the order: time, frequency, duration.
.TP
.B r
Re-read the directory. A scan running in another window is still writing to
it, and this picks up what has arrived since.
.TP
.B o
Print the highlighted recording's path and quit, for piping into something
else.
.TP
.B "? h"
The list of keys, and which audio player was found.
.TP
.B q
Quit. With a filter in force, Escape clears the filter first.
.SH OPTIONS
.TP
.BI DIRECTORY
Where the recordings are. Defaults to the scanner's output directory, or to
.B $BANDSAUNTER_OUTPUT
when that is set.
.TP
.BI \-\-sort " ORDER"
Start in this order: time, frequency, duration. Time is newest first; duration is longest
first.
.TP
.BI \-\-filter " TEXT"
Start with only the recordings matching this, exactly as if it had been typed
at the
.B /
prompt.
.TP
.BI \-\-player " CMD"
The command used to play a recording. The path is appended as its last
argument. By default the first of these that is installed is used:
pw-play, paplay, aplay, play, ffplay, mpv.
.TP
.B \-\-list
Print one line per recording and exit, without drawing anything. This is what
to use over a pipe, in a script, or anywhere there is no terminal.
.TP
.B \-V ", " \-\-version
Print the version and exit.
.SH SOUND
Playback is handed to whichever player is installed rather than done in the
program: the recordings are ordinary WAV files, every desktop already has
something that plays them, and a browser that cannot start is worse than one
that cannot play. If none is found, everything else still works and the
message says which packages would fix it.
.PP
Over ssh there is usually no sound server at the far end. The browser and its
transcripts work regardless; only Enter has nothing to do.
.SH TRANSCRIPTS
A transcript appears only where a recogniser produced one, which means the
capture was judged to be speech and
.B bandsaunter\-transcribe
was installed at the time. Where there is none, the panel says which of the
several reasons applies \[em] Morse, data, a bare carrier, or speech that was
never offered to a recogniser \[em] because those want different things done
about them.
.PP
Two places can hold the text: the JSON sidecar records what was recognised
during the scan, and the
.I _transcription.txt
beside the recording is what a later re\-run wrote. The file wins, being the
more recent of the two.
.SH FILES
.TP
.I ~/bandsaunter/
Where recordings are written, unless the saved settings say otherwise.
.TP
.IR frequency \-\- date _ time \- modulation .wav
A recording.
.TP
.IR ... .json
Its measurements and identification.
.TP
.IR ... _transcription.txt
What was said, where a recogniser heard speech.
.SH ENVIRONMENT
.TP
.B BANDSAUNTER_OUTPUT
The directory to open, overriding the saved settings.
.SH EXAMPLES
.PP
.RS
.EX
saunterbrowse
.EE
.RE
.PP
Open the scanner's output directory.
.PP
.RS
.EX
saunterbrowse /mnt/recordings \-\-sort frequency
.EE
.RE
.PP
Browse somewhere else, grouped by channel rather than by time.
.PP
.RS
.EX
saunterbrowse \-\-list | grep \-i "mile marker"
.EE
.RE
.PP
Search the transcripts from a script.
.SH EXIT STATUS
0 on a clean exit, 1 when the directory holds no recordings, 2 when it does
not exist or there is no terminal to draw on.
.SH SEE ALSO
.BR bandsaunter (1)
.SH BUGS
The list is read when the browser opens. Press
.B r
to pick up recordings a running scan has written since.

View file

@ -32,6 +32,7 @@ transcribe-small = ["vosk>=0.3"]
[project.scripts]
bandsaunter = "bandsaunter.cli:main"
saunterbrowse = "bandsaunter.browse:main"
[tool.setuptools.dynamic]
# The displayed version (2026-08-21_01) is not valid PEP 440, so packaging

657
tests/test_browse.py Normal file
View file

@ -0,0 +1,657 @@
"""saunterbrowse: reading and listening to what a scan collected.
The browser never writes to the recordings directory, so every test here is
about what it *shows* and what it does with a keypress. Playback is checked
against a recorded command rather than a real one: whether audio came out of
the speakers is not something a test can see, but which file was handed to
which player is.
"""
import json
import re
import time
import wave
from datetime import datetime
from pathlib import Path
import numpy as np
import pytest
from rich.console import Console
from bandsaunter.browse import (Browser, Player, PLAYERS, Keyboard, main,
scan_directory)
# -- fixtures ----------------------------------------------------------------
def write_wav(path: Path, seconds: float = 1.0, rate: int = 16_000) -> None:
n = int(seconds * rate)
tone = (np.sin(2 * np.pi * 440 * np.arange(n) / rate) * 8000)
with wave.open(str(path), "wb") as w:
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(rate)
w.writeframes(tone.astype(np.int16).tobytes())
def make_capture(directory: Path, mhz: float, when: str, mode: str,
seconds: float = 1.0, transcript: str = "",
meta: dict | None = None) -> Path:
"""One recording and its sidecars, named the way a scan names them."""
stem = f"{mhz:011.6f}MHz--{when}-{mode}"
wav = directory / f"{stem}.wav"
write_wav(wav, seconds)
if meta is not None:
body = {"frequency_hz": mhz * 1e6, "hit": meta}
(directory / f"{stem}.json").write_text(json.dumps(body))
if transcript:
(directory / f"{stem}_transcription.txt").write_text(transcript + "\n")
return wav
@pytest.fixture
def library(tmp_path):
"""A small recordings directory covering the cases that render differently."""
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm", 4.0,
transcript="Net control, this is W1AW, standing by.",
meta={"category": "voice", "snr_db": 21.5,
"classification": "Narrowband FM voice",
"confidence": 0.88, "ctcss_hz": 100.0,
"band_labels": ["2 m Amateur"]})
make_capture(tmp_path, 856.5625, "2026-08-22_11_00_00", "fsk", 2.0,
meta={"category": "trunk", "snr_db": 38.0, "baud": 3600.0,
"classification": "Motorola SMARTNET / SmartZone "
"(Type I/II) trunking control channel",
"confidence": 0.95})
make_capture(tmp_path, 144.1, "2026-08-22_09_00_00", "cw", 3.0,
meta={"category": "cw", "morse_text": "VVV DE W1AW",
"morse_wpm": 18.0, "classification": "CW / Morse"})
return tmp_path
def browser(directory, width=100, height=30, player=None) -> Browser:
console = Console(width=width, height=height, force_terminal=True)
return Browser(directory, console=console,
player=player if player is not None else Player([]))
def frame(b: Browser) -> str:
with b.console.capture() as cap:
b.console.print(b.render())
return cap.get()
class FakePlayer(Player):
"""Records what it was asked to play instead of playing it."""
def __init__(self):
super().__init__(["/bin/true"])
self.played: list[Path] = []
self.stops = 0
self._active = False
def play(self, cap):
self.played.append(cap.path)
self.playing = cap
self._active = True
self.error = ""
def stop(self):
if self._active:
self.stops += 1
self._active = False
self.playing = None
@property
def active(self):
return self._active
# -- reading the directory ---------------------------------------------------
def test_every_recording_is_found(library):
assert len(scan_directory(library)) == 3
def test_the_newest_recording_comes_first(library):
caps = scan_directory(library)
assert [c.mode for c in caps] == ["fsk", "nfm", "cw"]
def test_the_filename_alone_gives_frequency_time_and_mode(library):
cap = [c for c in scan_directory(library) if c.mode == "nfm"][0]
assert cap.frequency == pytest.approx(146_520_000.0)
assert cap.when == datetime(2026, 8, 22, 10, 0, 0)
def test_length_comes_from_the_wav_not_the_sidecar(tmp_path):
"""A combined file grows after its sidecar was written, and a sidecar can
be missing entirely."""
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm", 2.5,
meta={"duration": 999.0})
cap = scan_directory(tmp_path)[0]
assert cap.duration == pytest.approx(2.5, abs=0.05)
def test_a_recording_with_no_sidecar_is_still_listed(tmp_path):
write_wav(tmp_path / "0146.520000MHz--2026-08-22_10_00_00-nfm.wav")
cap = scan_directory(tmp_path)[0]
assert cap.category == "unknown"
assert cap.classification == "unclassified"
def test_a_file_the_browser_cannot_name_is_shown_rather_than_hidden(tmp_path):
write_wav(tmp_path / "something-else.wav")
caps = scan_directory(tmp_path)
assert len(caps) == 1 and caps[0].frequency == 0.0
def test_a_combined_per_frequency_file_is_recognised(tmp_path):
write_wav(tmp_path / "0146.880000MHz.wav")
cap = scan_directory(tmp_path)[0]
assert cap.combined
assert cap.frequency == pytest.approx(146_880_000.0)
assert cap.category == "combined"
def test_sidecars_are_only_read_when_something_asks(library):
"""A directory of ten thousand recordings has to open instantly."""
caps = scan_directory(library)
assert all(c._meta is None for c in caps)
_ = caps[0].category
assert caps[0]._meta is not None
def test_a_missing_directory_is_not_an_exception(tmp_path):
assert scan_directory(tmp_path / "nowhere") == []
# -- the transcript ----------------------------------------------------------
def test_the_transcript_is_at_the_top_of_the_screen(library):
b = browser(library)
b.index = [i for i, c in enumerate(b.view) if c.mode == "nfm"][0]
out = frame(b)
head = out.split("recordings in")[0]
assert "Net control, this is W1AW" in head
assert "transcript" in head
def test_the_txt_beside_the_recording_beats_the_sidecar(tmp_path):
"""The sidecar records what was recognised at the time; the file is what a
later re-run wrote."""
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm",
transcript="the newer text",
meta={"transcript": "the older text", "category": "voice"})
assert scan_directory(tmp_path)[0].transcript == "the newer text"
def test_the_sidecar_transcript_is_used_when_there_is_no_txt(tmp_path):
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm",
meta={"transcript": "only in the sidecar", "category": "voice"})
assert scan_directory(tmp_path)[0].transcript == "only in the sidecar"
@pytest.mark.parametrize("meta,expected", [
({"category": "digital"}, "data, not speech"),
({"category": "trunk"}, "data, not speech"),
({"category": "carrier"}, "silence by definition"),
({"category": "cw"}, "Morse"),
({"category": "voice"}, "no transcript beside it"),
])
def test_an_empty_transcript_says_which_reason_applies(tmp_path, meta,
expected):
""""No transcript" is the same shape for Morse, for data and for a
recogniser that was never installed, and those want different things
done about them."""
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm", meta=meta)
b = browser(tmp_path)
assert expected in frame(b)
def test_decoded_morse_is_shown_where_the_transcript_would_be(library):
b = browser(library)
b.index = [i for i, c in enumerate(b.view) if c.mode == "cw"][0]
assert "VVV DE W1AW" in frame(b)
def test_a_transcript_too_long_for_the_panel_says_so(tmp_path):
"""Cutting the end off a transmission silently is how a reader ends up
believing they have read all of it."""
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm",
transcript=" ".join(f"word{i}" for i in range(400)),
meta={"category": "voice"})
out = frame(browser(tmp_path, height=24))
assert "more line(s)" in out and "press t" in out
def test_the_reader_shows_what_the_panel_could_not(tmp_path):
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm",
transcript=" ".join(f"word{i}" for i in range(400)),
meta={"category": "voice"})
b = browser(tmp_path, height=24)
panel_text = frame(b)
b.handle("t")
assert b.reading
reader = frame(b)
assert "word0" in reader
assert "word200" in reader and "word200" not in panel_text
def test_the_reader_scrolls_and_comes_back(tmp_path):
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm",
transcript=" ".join(f"word{i}" for i in range(400)),
meta={"category": "voice"})
b = browser(tmp_path, height=20)
b.handle("t")
first = frame(b)
b.handle("pgdn")
assert frame(b) != first
b.handle("home")
assert frame(b) == first
b.handle("escape")
assert not b.reading
def test_the_reader_refuses_when_there_is_nothing_to_read(library):
b = browser(library)
b.index = [i for i, c in enumerate(b.view) if c.mode == "fsk"][0]
b.handle("t")
assert not b.reading
assert "no transcript" in b.message
# -- the frame ---------------------------------------------------------------
@pytest.mark.parametrize("width,height",
[(60, 16), (80, 24), (100, 30), (200, 60)])
def test_the_frame_never_overflows_the_terminal(library, width, height):
"""A frame taller than the terminal cannot be redrawn in place: every
refresh scrolls another copy of it into the scrollback."""
b = browser(library, width=width, height=height)
lines = frame(b).rstrip("\n").split("\n")
assert len(lines) <= height, f"{len(lines)} lines in {height}"
bare = [re.sub(r"\x1b\[[0-9;]*m", "", line) for line in lines]
assert max(len(line) for line in bare) <= width, "a line ran off the side"
def test_one_row_per_recording_however_long_the_transcript(tmp_path):
"""A wrapped row would push the ones below it off the bottom, and the
cursor arithmetic counts rows."""
for i in range(6):
make_capture(tmp_path, 146.0 + i, f"2026-08-22_10_0{i}_00", "nfm",
transcript=" ".join(f"word{n}" for n in range(200)),
meta={"category": "voice"})
b = browser(tmp_path, height=30)
body = frame(b).split("recordings in")[1]
assert body.count("word0") == 6, "a row wrapped instead of truncating"
def test_the_header_names_the_frequency_mode_and_category(library):
b = browser(library)
b.index = [i for i, c in enumerate(b.view) if c.mode == "nfm"][0]
head = frame(b).split("transcript")[0]
assert "146.52 MHz" in head and "NFM" in head and "voice" in head
def test_a_symbol_rate_is_only_shown_where_it_means_something(library):
"""The estimator returns a figure for every capture, and "120 baud"
beside a conversation is noise."""
b = browser(library)
b.index = [i for i, c in enumerate(b.view) if c.mode == "fsk"][0]
assert "3600 baud" in frame(b)
b.index = [i for i, c in enumerate(b.view) if c.mode == "nfm"][0]
assert "baud" not in frame(b)
def test_an_empty_directory_renders_rather_than_crashing(tmp_path):
b = browser(tmp_path)
assert "nothing here yet" in frame(b)
# -- moving around -----------------------------------------------------------
def test_the_arrow_keys_move_the_cursor(library):
b = browser(library)
assert b.index == 0
b.handle("down")
assert b.index == 1
b.handle("up")
assert b.index == 0
def test_the_cursor_stops_at_both_ends(library):
b = browser(library)
for _ in range(20):
b.handle("up")
assert b.index == 0
for _ in range(20):
b.handle("down")
assert b.index == len(b.view) - 1
def test_home_and_end_go_to_the_ends(library):
b = browser(library)
b.handle("end")
assert b.index == len(b.view) - 1
b.handle("home")
assert b.index == 0
def test_the_cursor_stays_on_screen_in_a_long_list(tmp_path):
for i in range(60):
make_capture(tmp_path, 146.0, f"2026-08-22_10_{i // 60:02d}_{i % 60:02d}",
"nfm", meta={"category": "voice"})
b = browser(tmp_path, height=24)
b.handle("end")
frame(b)
assert b.top <= b.index < b.top + b._rows()
def test_sorting_cycles_and_reorders(library):
b = browser(library)
assert b.sort == "time"
b.handle("s")
assert b.sort == "frequency"
assert [c.frequency for c in b.view] == sorted(c.frequency
for c in b.view)
b.handle("s")
assert b.sort == "duration"
assert b.view[0].duration >= b.view[-1].duration
def test_quitting_stops_the_loop(library):
assert browser(library).handle("q") is False
# -- searching ---------------------------------------------------------------
def test_search_matches_what_was_said(library):
"""The point of it: "did anyone mention the repeater" is a question about
content, not about filenames."""
b = browser(library)
b.handle("/")
for ch in "standing by":
b.handle(ch)
b.handle("enter")
assert len(b.view) == 1 and b.view[0].mode == "nfm"
def test_search_matches_the_identification(library):
b = browser(library)
b.query = "smartnet"
b.apply()
assert len(b.view) == 1 and b.view[0].mode == "fsk"
def test_search_matches_the_frequency_in_the_name(library):
b = browser(library)
b.query = "0856"
b.apply()
assert len(b.view) == 1
def test_backspace_edits_the_search(library):
b = browser(library)
b.handle("/")
for ch in "smartnetX":
b.handle(ch)
assert b.view == []
b.handle("backspace")
assert len(b.view) == 1
def test_escape_clears_the_search_before_it_quits(library):
"""Escape with a filter in force means "show me everything again", not
"throw the program away"."""
b = browser(library)
b.query = "smartnet"
b.apply()
assert b.handle("escape") is True
assert b.query == "" and len(b.view) == 3
assert b.handle("escape") is False
def test_a_search_that_matches_nothing_says_so(library):
b = browser(library)
b.query = "no such thing"
b.apply()
assert "nothing matches" in frame(b)
def test_keys_typed_while_searching_are_not_commands(library):
"""'q' has to be a letter in the search box, not an instruction to quit."""
b = browser(library)
b.handle("/")
assert b.handle("q") is True
assert b.query == "q"
# -- playing -----------------------------------------------------------------
def test_enter_plays_the_highlighted_recording(library):
p = FakePlayer()
b = browser(library, player=p)
b.handle("down")
b.handle("enter")
assert p.played == [b.current.path]
def test_space_stops_what_is_playing(library):
p = FakePlayer()
b = browser(library, player=p)
b.handle("enter")
b.handle("space")
assert p.stops == 1 and not p.active
def test_playing_a_second_recording_stops_the_first(tmp_path):
"""Two players talking over each other is worse than either alone."""
calls = []
class Recorder(Player):
def play(self, cap):
calls.append(("play", cap.path.name))
self.playing = cap
def stop(self):
calls.append(("stop", None))
make_capture(tmp_path, 146.52, "2026-08-22_10_00_00", "nfm")
make_capture(tmp_path, 147.52, "2026-08-22_10_01_00", "nfm")
b = browser(tmp_path, player=Recorder([]))
b.handle("enter")
b.handle("down")
b.handle("enter")
assert [c[0] for c in calls] == ["play", "play"]
def test_the_real_player_stops_the_previous_file_first(library, tmp_path):
"""The stop is in Player.play, so every caller gets it."""
p = Player(["/bin/sleep", "5"])
caps = scan_directory(library)
p.play(caps[0])
first = p.proc
assert first is not None
p.play(caps[1])
assert first.poll() is not None, "the first player was left running"
p.stop()
def test_stopping_kills_the_player_and_anything_it_started(library, tmp_path):
"""Several of these players are wrapper scripts that fork the real one.
Signalling only the script leaves the sound playing with nothing on
screen to stop it."""
import subprocess as sp
marker = tmp_path / "child.pid"
script = tmp_path / "wrapper.sh"
script.write_text("#!/bin/sh\nsleep 30 &\necho $! > %s\nwait\n" % marker)
script.chmod(0o755)
p = Player([str(script)])
p.play(scan_directory(library)[0])
for _ in range(50):
if marker.exists():
break
time.sleep(0.05)
child = int(marker.read_text().strip())
assert sp.run(["kill", "-0", str(child)]).returncode == 0, "never started"
p.stop()
for _ in range(50):
if sp.run(["kill", "-0", str(child)],
capture_output=True).returncode != 0:
break
time.sleep(0.05)
assert sp.run(["kill", "-0", str(child)],
capture_output=True).returncode != 0, "still playing"
def test_no_player_installed_is_reported_not_crashed(library):
b = browser(library, player=Player([])) # [] means "none installed"
b.handle("enter")
assert "no audio player" in b.message
assert all(name in b.message for name, _ in PLAYERS)
def test_the_file_is_the_last_argument_to_the_player(library):
p = Player(["/bin/true", "--flag"])
cap = scan_directory(library)[0]
p.play(cap)
assert p.command == ["/bin/true", "--flag"]
assert p.playing is cap
p.stop()
def test_playback_progress_is_shown(library):
p = FakePlayer()
b = browser(library, player=p)
b.handle("enter")
p.started_at = 0.0 # started long ago: the bar is full
assert "" in frame(b)
# -- the keyboard ------------------------------------------------------------
# Driven through a real pty, not a stand-in object. A fake stream with a
# read() method passed every one of these while the program hung on the first
# keypress: sys.stdin.read(1) goes through a buffered text wrapper, which in
# cbreak mode waits for more bytes than a single key provides.
@pytest.fixture
def terminal():
"""A pty whose slave end is what Keyboard will read."""
import os
import pty
master, slave = pty.openpty()
stream = os.fdopen(slave, "r")
yield master, stream
for fd in (master,):
try:
os.close(fd)
except OSError:
pass
try:
stream.close()
except OSError:
pass
@pytest.mark.parametrize("raw,name", [
("\x1b[A", "up"), ("\x1b[B", "down"), ("\x1b[C", "right"),
("\x1b[D", "left"), ("\x1b[5~", "pgup"), ("\x1b[6~", "pgdn"),
("\x1b[H", "home"), ("\x1b[F", "end"),
("\x1bOA", "up"), ("\x1bOB", "down"),
("\r", "enter"), ("\n", "enter"), (" ", "space"),
("\x7f", "backspace"), ("q", "q"), ("/", "/"), ("t", "t"),
])
def test_a_key_arrives_as_one_key(terminal, raw, name):
"""An arrow is "\\x1b[A" -- three bytes. Read one at a time it becomes
three commands, and the cursor jumps somewhere unasked."""
import os
master, stream = terminal
with Keyboard(stream) as kb:
assert kb.enabled
os.write(master, raw.encode())
assert kb.get(0.5) == name
assert kb.get(0.05) == "", "one keypress produced more than one key"
def test_a_bare_escape_is_not_mistaken_for_an_arrow(terminal):
import os
master, stream = terminal
with Keyboard(stream) as kb:
os.write(master, b"\x1b")
assert kb.get(0.5) == "escape"
def test_keys_arriving_together_are_delivered_one_at_a_time(terminal):
"""Held down, or pasted, several keys land in a single read."""
import os
master, stream = terminal
with Keyboard(stream) as kb:
os.write(master, b"\x1b[B\x1b[Bq")
assert [kb.get(0.5) for _ in range(3)] == ["down", "down", "q"]
def test_nothing_pressed_returns_nothing(terminal):
_, stream = terminal
with Keyboard(stream) as kb:
assert kb.get(0.05) == ""
def test_the_terminal_is_restored_afterwards(terminal):
"""Left in cbreak, the shell that follows has no line editing and no echo."""
import termios
_, stream = terminal
before = termios.tcgetattr(stream.fileno())
with Keyboard(stream) as kb:
assert kb.enabled
assert termios.tcgetattr(stream.fileno()) != before
assert termios.tcgetattr(stream.fileno()) == before
def test_a_stream_that_is_not_a_terminal_is_not_an_error(tmp_path):
path = tmp_path / "notatty"
path.write_text("q")
with open(path) as fh, Keyboard(fh) as kb:
assert not kb.enabled
assert kb.get(0.0) == ""
# -- the command line --------------------------------------------------------
def test_list_prints_one_line_per_recording(library, capsys):
assert main(["--list", str(library)]) == 0
lines = [ln for ln in capsys.readouterr().out.splitlines() if ln.strip()]
assert len(lines) == 3
assert any("Net control" in ln for ln in lines)
def test_list_honours_the_filter(library, capsys):
assert main(["--list", "--filter", "smartnet", str(library)]) == 0
lines = [ln for ln in capsys.readouterr().out.splitlines() if ln.strip()]
assert len(lines) == 1
def test_a_missing_directory_is_explained(tmp_path, capsys):
assert main([str(tmp_path / "nowhere")]) == 2
assert "no such directory" in capsys.readouterr().out
def test_an_empty_directory_is_explained(tmp_path, capsys):
assert main([str(tmp_path)]) == 1
assert "no recordings" in capsys.readouterr().out
def test_it_refuses_to_draw_where_there_is_no_terminal(library, capsys):
"""Piped or redirected, the full-screen frame would be gibberish."""
assert main([str(library)]) == 2
out = capsys.readouterr().out
assert "needs a terminal" in out and "--list" in out
def test_the_output_directory_is_found_without_being_told(monkeypatch,
tmp_path):
monkeypatch.setenv("BANDSAUNTER_OUTPUT", str(tmp_path))
from bandsaunter.browse import default_directory
assert default_directory() == tmp_path

View file

@ -70,3 +70,64 @@ def test_the_guidance_survives_into_the_rendered_page(page, tmp_path):
# A sentence from one setting's guidance, chosen because it is the one a
# newcomer most needs: what the squelch actually is.
assert "This is the squelch knob." in flat
# -- the browser's page ------------------------------------------------------
BROWSE_GENERATOR = (Path(__file__).resolve().parent.parent / "packaging"
/ "make-browse-man.py")
@pytest.fixture(scope="module")
def browse_page(tmp_path_factory):
out = tmp_path_factory.mktemp("man") / "saunterbrowse.1"
subprocess.run([sys.executable, str(BROWSE_GENERATOR), str(out)],
check=True, capture_output=True)
return out.read_text()
def test_the_browser_has_a_page_of_its_own(browse_page):
assert ".TH SAUNTERBROWSE 1" in browse_page
assert "saunterbrowse \\- read and listen" in browse_page
def test_every_browser_flag_is_documented(browse_page):
from bandsaunter.browse import build_parser
# --help is argparse's own and needs no prose of its own.
flags = [o for a in build_parser()._actions for o in a.option_strings
if o not in ("-h", "--help")]
missing = [f for f in flags
if f.replace("-", "\\-") not in browse_page
and f not in browse_page]
assert not missing, f"undocumented flags: {missing}"
def test_every_browser_key_is_documented(browse_page):
"""A key that does something the manual does not mention is a key nobody
will press."""
for key in ("Enter", "Space", "PgUp", "Home", "/", "s", "r", "o", "q",
"t"):
assert key in browse_page, key
def test_the_browser_page_names_the_players_it_looks_for(browse_page):
from bandsaunter.browse import PLAYERS
for name, _ in PLAYERS:
assert name in browse_page, name
def test_the_two_pages_point_at_each_other(page, browse_page):
assert "saunterbrowse (1)" in page or "saunterbrowse" in page
assert "bandsaunter (1)" in browse_page
def test_the_browser_page_renders_without_complaint(browse_page, tmp_path):
groff = shutil.which("groff")
if groff is None:
pytest.skip("groff is not installed")
src = tmp_path / "saunterbrowse.1"
src.write_text(browse_page)
done = subprocess.run([groff, "-man", "-ww", "-z", str(src)],
capture_output=True, text=True)
assert done.returncode == 0, done.stderr
assert not done.stderr.strip(), done.stderr