diff --git a/bandsaunter/__init__.py b/bandsaunter/__init__.py index 05ec1e8..27c7c28 100755 --- a/bandsaunter/__init__.py +++ b/bandsaunter/__init__.py @@ -8,7 +8,7 @@ and transcribing speech. # Versions are the release date and a revision within that day, so # 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-09-25" +VERSION_DATE = "2026-11-25" VERSION_REVISION = 1 __version__ = f"{VERSION_DATE}_{VERSION_REVISION:02d}" diff --git a/bandsaunter/aircraft.py b/bandsaunter/aircraft.py index 11d7c32..6f7570c 100644 --- a/bandsaunter/aircraft.py +++ b/bandsaunter/aircraft.py @@ -870,6 +870,9 @@ def watch(console, options: AircraftOptions, output_dir: str, try: counted["frames"] = pump(device, options, registry, log, book, started, on_block=on_block, + on_frame=lambda _frame, craft: + sky.heard(craft.icao, + craft.last_seen), stopping=lambda: sky.stopping) except Exception as exc: # a window must survive it sky.note = str(exc)[:60] diff --git a/bandsaunter/livemap.py b/bandsaunter/livemap.py index 23108cd..d38c5f2 100644 --- a/bandsaunter/livemap.py +++ b/bandsaunter/livemap.py @@ -36,7 +36,7 @@ from .ui import compass # they cannot be listed here. __all__ = ["available", "binding", "show", "fetch_ground", "Sky", "Blip", "blip_for", "place_box", "glide", "dash_for", "wrap_value", - "WRAP_CHARS", + "WRAP_CHARS", "heard_flash", "MISSING_QT"] BINDINGS = ("PyQt6", "PyQt5", "PySide6", "PySide2") @@ -133,6 +133,38 @@ BOX_GLIDE = 0.45 BOX_GLIDE_MS = 33 BOX_SETTLED_PX = 0.4 +# An aircraft's ring turns bright red the moment a frame from it arrives and +# cools back to its own colour over this long. The ring keeps its own slow +# rhythm either way; the red is the part that is not decoration -- it is +# when the receiver actually heard something, and an aeroplane that is +# flashing is one that is being heard right now rather than one that was. +ECHO_HEARD_HOLD_S = 0.15 +ECHO_HEARD_S = 0.5 +ECHO_HEARD_RGB = (255, 32, 32) +# How many arrival times to keep for each aircraft (a busy one sends a few a +# second), how far back to look when measuring how late they turn up, and +# the most lateness believed -- past that something has stalled, and a +# flash several seconds behind would be a flash about nothing on screen. +HEARD_KEPT = 64 +HEARD_LAG_WINDOW = 10.0 +HEARD_LAG_MOST = 3.0 + + +def heard_flash(since: float) -> float: + """How red an echo is, ``since`` seconds after a frame arrived. + + Held all the way for a few repaints, so that it is seen at full red and + not only on its way down, then falling -- fastest at first, so that it + reads as a flash rather than a glow -- to nothing by ECHO_HEARD_S. + """ + since = max(0.0, since) # a clock a hair behind the frame's + if since <= ECHO_HEARD_HOLD_S: + return 1.0 + if since >= ECHO_HEARD_S: + return 0.0 + left = (since - ECHO_HEARD_HOLD_S) / (ECHO_HEARD_S - ECHO_HEARD_HOLD_S) + return (1.0 - left) ** 2 + def _alpha(strength: float) -> int: """How opaque something at this strength is drawn.""" @@ -478,14 +510,48 @@ class Sky: self._airports_want = None self._wanted = None self._settled = None + # When each frame from each aircraft arrived, for the red flash on + # its ring -- and how late those times reach this side, measured. + # + # The receiver is read a block at a time, so a frame is known about + # up to a block after it was heard. Flashing it the moment it is + # known about would flash every aeroplane at once, once a block, + # which says when the block ended rather than when anything was + # heard. So the frames are played back at their own times, late by + # however late they have been arriving: the rhythm is the real one, + # a beat behind. + self._heard: dict[str, deque] = {} + self._lags: deque = deque() # -- written by the listener ------------------------------------------ + def heard(self, icao: str, when: float, + told: float | None = None) -> None: + """One frame from this aircraft arrived at ``when``, and this side + was told about it at ``told`` (now, unless said).""" + with self._lock: + self._heard_locked(icao, when, told) + + def _heard_locked(self, icao: str, when: float, + told: float | None = None) -> None: + times = self._heard.setdefault(icao, deque(maxlen=HEARD_KEPT)) + if times and when <= times[-1]: + return # already told, or out of order + times.append(when) + now = time.time() if told is None else told + self._lags.append((now, max(0.0, min(HEARD_LAG_MOST, now - when)))) + while self._lags and now - self._lags[0][0] > HEARD_LAG_WINDOW: + self._lags.popleft() + def update(self, blips, frames: int, seen: int) -> None: with self._lock: self.frames = frames self.aircraft_seen = seen for blip in blips: self._blips[blip.icao] = blip + # A listener that does not report frames one at a time + # still says when each mark was last heard. + if blip.last_seen: + self._heard_locked(blip.icao, blip.last_seen) if blip.located: trail = self._trails.setdefault( blip.icao, deque(maxlen=TRAIL_POINTS)) @@ -664,6 +730,24 @@ class Sky: return 1.0 if age <= self.hold else 0.0 return max(0.0, 1.0 - (age - self.hold) / self.fade) + def heard_ago(self, icao: str, now: float | None = None) -> float: + """Seconds since this aircraft's last frame, as played back. + + Infinite if nothing from it is due yet. See ``_heard`` for why the + clock this is read against runs a little behind the real one. + """ + now = time.time() if now is None else now + with self._lock: + lag = max((lag for _at, lag in self._lags), default=0.0) + times = self._heard.get(icao) + if not times: + return math.inf + replay = now - lag + for when in reversed(times): + if when <= replay: + return replay - when + return math.inf + def trail(self, icao: str) -> list: with self._lock: return list(self._trails.get(icao, ())) @@ -819,6 +903,24 @@ def _build(): def craft_colour(feet, alpha=255) -> QColor: return rgb(RAMP + altitude_step(feet), alpha) + def _echo_colour(colour, part: float, flash: float) -> QColor: + """The ring's colour, ``part`` of the way out, ``flash`` red. + + Dimmer the further out it has got, and gone by the time it arrives: + a ring that vanished at full strength would read as something + switching off rather than something spending itself. A frame + arriving overrides both -- the ring goes bright red and solid + wherever it is, because that moment is the news. + """ + faded = colour.alpha() * (1.0 - part) ** 1.6 * 0.75 + flash = max(0.0, min(1.0, flash)) + r, g, b = ECHO_HEARD_RGB + return QColor( + int(round(colour.red() + (r - colour.red()) * flash)), + int(round(colour.green() + (g - colour.green()) * flash)), + int(round(colour.blue() + (b - colour.blue()) * flash)), + int(round(faded + (colour.alpha() - faded) * flash))) + def _lifted(colour, level: float) -> QColor: """One colour walked towards white, and dimmed below the middle. @@ -1009,7 +1111,8 @@ def _build(): # The echo goes down before the aeroplane, so the ring # passes under the thing that sent it out. if strength >= 1.0: - self._draw_echo(painter, x, y, colour, blip.icao) + self._draw_echo(painter, x, y, colour, blip.icao, + self.sky.heard_ago(blip.icao, now)) self._draw_symbol(painter, x, y, blip.track_deg, colour, strength, blip.icao, blip.shape) taken.append((x - 11, y - 11, 22, 22)) @@ -1441,11 +1544,17 @@ def _build(): self._box_moving = True return int(round(x + here[0])), int(round(y + here[1])) - def _draw_echo(self, painter, x, y, colour, icao: str) -> None: + def _draw_echo(self, painter, x, y, colour, icao: str, + since: float = float("inf")) -> None: """One ring travelling outward from an aircraft, dimming as it grows. What a radar repeater does, and what the eye reads as "this thing is transmitting" -- which is what an aeroplane on - this picture is doing, twice a second.""" + this picture is doing, twice a second. + + ``since`` is how long ago a frame from it last arrived. Just + after one does, the ring flashes red wherever it has got to, so + the rings say when the aircraft was actually heard and not only + that it is somewhere about.""" from .flightmap import echo_age, phase_of every = self.sky.echo @@ -1458,12 +1567,8 @@ def _build(): radius = reach * part if radius < 2: return - ring = QColor(colour) - # Dimmer the further out it has got, and gone by the time it - # arrives: a ring that vanished at full strength would read as - # something switching off rather than something spending itself. - ring.setAlpha(int(colour.alpha() * (1.0 - part) ** 1.6 * 0.75)) - painter.setPen(QPen(ring, 1.2)) + ring = _echo_colour(colour, part, heard_flash(since)) + painter.setPen(QPen(ring, 1.2 + 1.6 * heard_flash(since))) painter.setBrush(_NO_BRUSH) painter.drawEllipse(QPointF(x, y), radius, radius) diff --git a/packaging/bandsaunter.1 b/packaging/bandsaunter.1 index 4358a87..523357a 100644 --- a/packaging/bandsaunter.1 +++ b/packaging/bandsaunter.1 @@ -1,5 +1,5 @@ .\" Generated by packaging/make-man.py -- do not edit by hand. -.TH BANDSAUNTER 1 "2026-09-25" "bandsaunter 2026-09-25_01" "User Commands" +.TH BANDSAUNTER 1 "2026-11-25" "bandsaunter 2026-11-25_01" "User Commands" .SH NAME bandsaunter \- scan, record and identify radio signals with an RTL-SDR .SH SYNOPSIS diff --git a/packaging/saunterbrowse.1 b/packaging/saunterbrowse.1 index a050f4e..2a5e96e 100644 --- a/packaging/saunterbrowse.1 +++ b/packaging/saunterbrowse.1 @@ -1,5 +1,5 @@ .\" Generated by packaging/make-browse-man.py -- do not edit by hand. -.TH SAUNTERBROWSE 1 "2026-09-25" "bandsaunter 2026-09-25_01" "User Commands" +.TH SAUNTERBROWSE 1 "2026-11-25" "bandsaunter 2026-11-25_01" "User Commands" .SH NAME saunterbrowse \- read and listen to what a bandsaunter scan collected .SH SYNOPSIS diff --git a/tests/test_livemap.py b/tests/test_livemap.py index c859cf9..3adfba2 100644 --- a/tests/test_livemap.py +++ b/tests/test_livemap.py @@ -2137,6 +2137,66 @@ def test_a_ring_leaves_an_aircraft_and_grows(app): assert late > early, f"the ring did not grow: {early} then {late}" +def test_a_heard_flash_is_full_on_arrival_and_gone_soon_after(): + from bandsaunter.livemap import (ECHO_HEARD_HOLD_S, ECHO_HEARD_S, + heard_flash) + + assert heard_flash(0.0) == 1.0 + assert heard_flash(-0.05) == 1.0, "a clock a hair behind is still now" + assert heard_flash(ECHO_HEARD_HOLD_S) == 1.0, "not held at full red" + middle = (ECHO_HEARD_HOLD_S + ECHO_HEARD_S) / 2 + assert 0.0 < heard_flash(middle) < 1.0 + assert heard_flash(middle + 0.05) < heard_flash(middle) + assert heard_flash(ECHO_HEARD_S) == 0.0 + assert heard_flash(float("inf")) == 0.0 + + +def test_frames_are_played_back_at_their_own_rhythm_a_beat_late(): + """Frames reach the window a block at a time. Flashing each on arrival + would flash everything at once; they are replayed at their own times, + late by however late they have been turning up.""" + sky = a_sky() + t = time.time() + # Heard over the last second, told about only now. + for when in (t - 1.0, t - 0.6, t - 0.2): + sky.heard("A76154", when, told=t) + # The lag is a second: the first frame is due now, the others are not. + assert sky.heard_ago("A76154", t) == pytest.approx(0.0, abs=0.05) + assert sky.heard_ago("A76154", t + 0.3) == pytest.approx(0.3, abs=0.05) + assert sky.heard_ago("A76154", t + 0.45) == pytest.approx(0.05, abs=0.05) + assert sky.heard_ago("ABCDEF", t) == float("inf") + + +def test_a_mark_updated_without_frames_still_counts_as_heard(): + sky = a_sky() + sky.update([a_blip(last_seen=time.time())], 1, 1) + assert sky.heard_ago("A76154") < 0.1 + + +@qt +def test_a_ring_flashes_red_when_a_frame_arrives(app): + from bandsaunter.livemap import SkyView + + def red_near(heard_ago): + sky = a_sky(a_blip(last_seen=time.time() - heard_ago), + echo=3.0, echo_reach=60) + _at_own_peak(sky, "A76154", 0.4) + view = SkyView(sky) + view.show_ground = False + view.detail = 0 + with_it = _rendered(view).astype(int) + sky.echo = 0.0 + without = _rendered(view).astype(int) + x, y = view.projection().xy(a_blip().latitude, a_blip().longitude) + near = slice(max(0, y - 90), y + 90), slice(max(0, x - 90), x + 90) + b, g, r = (with_it[near][:, :, i] for i in range(3)) + changed = (with_it[near] != without[near]).any(axis=2) + return int((changed & (r > 180) & (g < 100) & (b < 100)).sum()) + + assert red_near(0.0) > 20, "a frame just in, and no red ring" + assert red_near(30.0) == 0, "red with nothing heard for half a minute" + + @qt def test_the_window_keeps_asking_for_frames_while_anything_is_alive(app): """A pulse and an echo are never finished, so the window has to keep