Merge branch 'feature/radio-set-panel' into unify/skills-20261112-night
This commit is contained in:
commit
679ebe5dc4
37 files changed
+2472
-348
No files matched your search
@@ -21,8 +21,8 @@ that someone transmits without the speech engine loading a busy host.
|
|||||||
0.6 s, at the tone's volume and squelch); no server request. Rejected: a
|
0.6 s, at the tone's volume and squelch); no server request. Rejected: a
|
||||||
recorded sound file.
|
recorded sound file.
|
||||||
3. **Modes**: Auto (speech below the threshold, otherwise the burst), Quiet
|
3. **Modes**: Auto (speech below the threshold, otherwise the burst), Quiet
|
||||||
(the burst only, never speech), Silence (no sound; bubbles and log only).
|
(the burst only, never speech), Silence (no sound except the procedure
|
||||||
Default Auto.
|
word of decision 4; bubbles and log only). Default Auto.
|
||||||
4. **Procedure words**: for MAYDAY, PAN-PAN and SÉCURITÉ the procedure word,
|
4. **Procedure words**: for MAYDAY, PAN-PAN and SÉCURITÉ the procedure word,
|
||||||
spoken three times in the sender's voice, always sounds, in every mode and
|
spoken three times in the sender's voice, always sounds, in every mode and
|
||||||
under load; under load or in Quiet the burst replaces the rest of the
|
under load; under load or in Quiet the burst replaces the rest of the
|
||||||
@@ -47,6 +47,12 @@ that someone transmits without the speech engine loading a busy host.
|
|||||||
(high effort, no gain).
|
(high effort, no gain).
|
||||||
9. **Order**: builds on W4's resource feed; implementation starts once W4 is
|
9. **Order**: builds on W4's resource feed; implementation starts once W4 is
|
||||||
on master.
|
on master.
|
||||||
|
10. **Silence scope** (operator-confirmed 2026-11-13): Silence mutes only the
|
||||||
|
radio set (speech, bursts, alert beeps); the procedure word of decision 4
|
||||||
|
still sounds, and music and the internet station keep playing. Rejected:
|
||||||
|
Silence also pauses music and the station (not part of 004's scope);
|
||||||
|
Silence also mutes the procedure word (an emergency call could go
|
||||||
|
unheard).
|
||||||
|
|
||||||
## Approach
|
## Approach
|
||||||
|
|
||||||
@@ -91,54 +97,54 @@ flowchart TD
|
|||||||
|
|
||||||
### Server gate
|
### Server gate
|
||||||
|
|
||||||
- [ ] `POST /api/tts` with an uncached line answers 503 `{"reason": "busy"}`
|
- [x] `POST /api/tts` with an uncached line answers 503 `{"reason": "busy"}`
|
||||||
while the latest `samples.jsonl` row has `cpu` >= the threshold, and the
|
while the latest `samples.jsonl` row has `cpu` >= the threshold, and the
|
||||||
fake Piper records no call.
|
fake Piper records no call.
|
||||||
- [ ] Without a sample younger than 120 s it answers 503
|
- [x] Without a sample younger than 120 s it answers 503
|
||||||
`{"reason": "unknown"}`, and the fake Piper records no call.
|
`{"reason": "unknown"}`, and the fake Piper records no call.
|
||||||
- [ ] A cached line answers 200 whatever the CPU.
|
- [x] A cached line answers 200 whatever the CPU.
|
||||||
- [ ] Below the threshold an uncached line renders as before.
|
- [x] Below the threshold an uncached line renders as before.
|
||||||
- [ ] `GET /api/state` carries `speech.gate` with `threshold`, `cpu` and
|
- [x] `GET /api/state` carries `speech.gate` with `threshold`, `cpu` and
|
||||||
`open`.
|
`open`.
|
||||||
- [ ] `speech.threshold` accepts only 30 to 90 in steps of 10, default 50,
|
- [x] `speech.threshold` accepts only 30 to 90 in steps of 10, default 50,
|
||||||
and changes only with a valid passkey signature.
|
and changes only with a valid passkey signature.
|
||||||
|
|
||||||
### Sound
|
### Sound
|
||||||
|
|
||||||
- [ ] Auto below the threshold: the page plays the spoken line; on a 503 it
|
- [x] Auto below the threshold: the page plays the spoken line; on a 503 it
|
||||||
plays the burst once and does not retry that row.
|
plays the burst once and does not retry that row.
|
||||||
- [ ] Quiet: the page sends no `/api/tts` request except for procedure-word
|
- [x] Quiet: the page sends no `/api/tts` request except for procedure-word
|
||||||
lines, and every row plays the burst.
|
lines, and every row plays the burst.
|
||||||
- [ ] Silence: no audio node starts except for a procedure-word line.
|
- [x] Silence: no audio node starts except for a procedure-word line.
|
||||||
- [ ] The burst is WebAudio noise of 0.4 to 0.8 s at the tone's volume times
|
- [x] The burst is WebAudio noise of 0.4 to 0.8 s at the tone's volume times
|
||||||
the squelch, and it sends no network request.
|
the squelch, and it sends no network request.
|
||||||
- [ ] A MAYDAY, PAN-PAN or SÉCURITÉ row requests its procedure-word line
|
- [x] A MAYDAY, PAN-PAN or SÉCURITÉ row requests its procedure-word line
|
||||||
first, in every mode; the second such line for the same voice is a
|
first, in every mode; the second such line for the same voice is a
|
||||||
cache hit with no Piper call.
|
cache hit with no Piper call.
|
||||||
|
|
||||||
### Panel
|
### Panel
|
||||||
|
|
||||||
- [ ] The display line shows class, mode, CPU % and a TX/RX lamp that is lit
|
- [x] The display line shows class, mode, CPU % and a TX/RX lamp that is lit
|
||||||
while sound plays.
|
while sound plays.
|
||||||
- [ ] Auto, Quiet and Silence form one button group (`aria-pressed`); a press
|
- [x] Auto, Quiet and Silence form one button group (`aria-pressed`); a press
|
||||||
changes the mode without a passkey and survives a reload.
|
changes the mode without a passkey and survives a reload.
|
||||||
- [ ] Volume per tone, squelch, the read-aloud filters for classes and
|
- [x] Volume per tone, squelch, the read-aloud filters for classes and
|
||||||
agents, and the internet station sit in the panel; the settings dialog
|
agents, and the internet station sit in the panel; the settings dialog
|
||||||
has no radio section.
|
has no radio section.
|
||||||
- [ ] The threshold control asks for the passkey once per change and shows
|
- [x] The threshold control asks for the passkey once per change and shows
|
||||||
the server's value.
|
the server's value.
|
||||||
- [ ] At 1920, 1366 and 390 px the panel has no horizontal scroll, in the
|
- [x] At 1920, 1366 and 390 px the panel has no horizontal scroll, in the
|
||||||
light and the dark palette.
|
light and the dark palette.
|
||||||
|
|
||||||
### Log
|
### Log
|
||||||
|
|
||||||
- [ ] Tabs Sent, Received and All show their counts; Received holds exactly
|
- [x] Tabs Sent, Received and All show their counts; Received holds exactly
|
||||||
the rows addressed to the operator or to all.
|
the rows addressed to the operator or to all.
|
||||||
- [ ] The chips class, sender, recipient and agent and the text search
|
- [x] The chips class, sender, recipient and agent and the text search
|
||||||
combine with AND; one control clears them all.
|
combine with AND; one control clears them all.
|
||||||
- [ ] Rows run newest first with time, direction, class badge,
|
- [x] Rows run newest first with time, direction, class badge,
|
||||||
sender → recipient and text; a click opens the full message.
|
sender → recipient and text; a click opens the full message.
|
||||||
- [ ] Tab, chips and search survive a reload in the same browser.
|
- [x] Tab, chips and search survive a reload in the same browser.
|
||||||
|
|
||||||
### Tests
|
### Tests
|
||||||
|
|
||||||
|
|||||||
@@ -158,6 +158,10 @@ def stream_item(origins: list[str], urls: list[str]) -> dict:
|
|||||||
return {"kind": "streams", "origins": origins, "urls": urls}
|
return {"kind": "streams", "origins": origins, "urls": urls}
|
||||||
|
|
||||||
|
|
||||||
|
def threshold_item(threshold: int) -> dict:
|
||||||
|
return {"kind": "speech-threshold", "threshold": threshold}
|
||||||
|
|
||||||
|
|
||||||
def task_item(task: str, argv: list[str]) -> dict:
|
def task_item(task: str, argv: list[str]) -> dict:
|
||||||
return {"kind": "task", "id": task, "argv": argv}
|
return {"kind": "task", "id": task, "argv": argv}
|
||||||
|
|
||||||
|
|||||||
@@ -8,13 +8,14 @@ import subprocess
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import wave
|
import wave
|
||||||
|
from collections.abc import Callable
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .council import HUMAN
|
from .council import HUMAN
|
||||||
from .errors import EngineError, ToolError, Unavailable
|
from .errors import EngineError, ToolError, Unavailable
|
||||||
from .fields import TEXT_LIMIT, bounded, decode, expect
|
from .fields import TEXT_LIMIT, bounded, decode, expect
|
||||||
from .phrasing import SPEECH_LIMIT, SPOKEN_LINE, phrase
|
from .phrasing import PROCEDURE_LINES, SPEECH_LIMIT, SPOKEN_LINE, phrase
|
||||||
from .records import digest, read_json
|
from .records import digest, read_json
|
||||||
from .skill_scripts import SAFE_PATH
|
from .skill_scripts import SAFE_PATH
|
||||||
|
|
||||||
@@ -22,6 +23,7 @@ MODEL_NAME = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.-]{0,127}")
|
|||||||
SPEAKER_LIMIT = 10000
|
SPEAKER_LIMIT = 10000
|
||||||
TTS_TIMEOUT = 120
|
TTS_TIMEOUT = 120
|
||||||
TTS_KEEP = 200
|
TTS_KEEP = 200
|
||||||
|
WORD_CACHE = "word-"
|
||||||
STT_RATE = 16000
|
STT_RATE = 16000
|
||||||
STT_SECONDS = 120
|
STT_SECONDS = 120
|
||||||
STT_LIMIT = 44 + STT_RATE * 2 * STT_SECONDS
|
STT_LIMIT = 44 + STT_RATE * 2 * STT_SECONDS
|
||||||
@@ -141,6 +143,7 @@ class Speech:
|
|||||||
raise ToolError(f"the operator voice {engines.operator_voice!r} is not installed in {engines.voices}")
|
raise ToolError(f"the operator voice {engines.operator_voice!r} is not installed in {engines.voices}")
|
||||||
self.operator = engines.operator_voice if self.voices else ""
|
self.operator = engines.operator_voice if self.voices else ""
|
||||||
self.pool = [voice for voice in self.voices if voice != self.operator]
|
self.pool = [voice for voice in self.voices if voice != self.operator]
|
||||||
|
self.handed = {self.operator} if self.operator else set()
|
||||||
self.speaking = threading.Lock()
|
self.speaking = threading.Lock()
|
||||||
self.hearing = threading.Lock()
|
self.hearing = threading.Lock()
|
||||||
self.metering = threading.Lock()
|
self.metering = threading.Lock()
|
||||||
@@ -174,7 +177,15 @@ class Speech:
|
|||||||
return gap or ("" if os.path.isfile(model) else f"the whisper.cpp model {model} is not a file")
|
return gap or ("" if os.path.isfile(model) else f"the whisper.cpp model {model} is not a file")
|
||||||
|
|
||||||
def voice_of(self, key: str) -> str:
|
def voice_of(self, key: str) -> str:
|
||||||
return voice_of(key, self.pool, self.operator)
|
"""Args:
|
||||||
|
key: the speaker's session id or directory key.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The speaker's voice, which /api/tts accepts from then on.
|
||||||
|
"""
|
||||||
|
voice = voice_of(key, self.pool, self.operator)
|
||||||
|
self.handed.add(voice)
|
||||||
|
return voice
|
||||||
|
|
||||||
def describe(self) -> dict:
|
def describe(self) -> dict:
|
||||||
operator = self.voice_of(HUMAN)
|
operator = self.voice_of(HUMAN)
|
||||||
@@ -185,7 +196,15 @@ class Speech:
|
|||||||
"limit": SPEECH_LIMIT, "rate": STT_RATE, "seconds": STT_SECONDS,
|
"limit": SPEECH_LIMIT, "rate": STT_RATE, "seconds": STT_SECONDS,
|
||||||
}
|
}
|
||||||
|
|
||||||
def synthesize(self, body: bytes) -> bytes:
|
def synthesize(self, body: bytes, admit: Callable[[str], None]) -> bytes:
|
||||||
|
"""Args:
|
||||||
|
body: {"text", "voice"} as JSON.
|
||||||
|
admit: called with a line before it renders and not for a cached
|
||||||
|
one; it raises to hold the render back.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The WAV of the line, from the cache when rendered before.
|
||||||
|
"""
|
||||||
gap = self.output_gap()
|
gap = self.output_gap()
|
||||||
if gap:
|
if gap:
|
||||||
raise Unavailable(f"speech output is off: {gap}")
|
raise Unavailable(f"speech output is off: {gap}")
|
||||||
@@ -194,13 +213,16 @@ class Speech:
|
|||||||
voice = payload["voice"]
|
voice = payload["voice"]
|
||||||
if not isinstance(voice, str) or voice not in self.voices:
|
if not isinstance(voice, str) or voice not in self.voices:
|
||||||
raise ToolError("voice must be one of the installed voices")
|
raise ToolError("voice must be one of the installed voices")
|
||||||
|
if voice not in self.handed:
|
||||||
|
raise ToolError("voice must be one the hub hands out to a speaker")
|
||||||
line = " ".join(bounded(payload, "text", SPEECH_LIMIT).split())
|
line = " ".join(bounded(payload, "text", SPEECH_LIMIT).split())
|
||||||
if not SPOKEN_LINE.fullmatch(line):
|
if not SPOKEN_LINE.fullmatch(line):
|
||||||
raise ToolError(f"text must be 1 to {SPEECH_LIMIT} printable characters")
|
raise ToolError(f"text must be 1 to {SPEECH_LIMIT} printable characters")
|
||||||
key = digest(f"{voice}\n{line}")
|
key = digest(f"{voice}\n{line}")
|
||||||
target = self.cache / f"{key}.wav"
|
target = self.cache / f"{WORD_CACHE if line in PROCEDURE_LINES else ''}{key}.wav"
|
||||||
with self.speaking:
|
with self.speaking:
|
||||||
if not target.is_file():
|
if not target.is_file():
|
||||||
|
admit(line)
|
||||||
self.render(voice, line, target)
|
self.render(voice, line, target)
|
||||||
self.trim()
|
self.trim()
|
||||||
os.utime(target)
|
os.utime(target)
|
||||||
@@ -235,7 +257,8 @@ class Speech:
|
|||||||
partial.unlink(missing_ok=True)
|
partial.unlink(missing_ok=True)
|
||||||
|
|
||||||
def trim(self) -> None:
|
def trim(self) -> None:
|
||||||
kept = sorted(self.cache.glob("*.wav"), key=lambda path: path.stat().st_mtime, reverse=True)
|
spoken = (path for path in self.cache.glob("*.wav") if not path.name.startswith(WORD_CACHE))
|
||||||
|
kept = sorted(spoken, key=lambda path: path.stat().st_mtime, reverse=True)
|
||||||
for path in kept[TTS_KEEP:]:
|
for path in kept[TTS_KEEP:]:
|
||||||
path.unlink(missing_ok=True)
|
path.unlink(missing_ok=True)
|
||||||
|
|
||||||
|
|||||||
@@ -25,5 +25,14 @@ class Unavailable(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Held(Exception):
|
||||||
|
def __init__(self, reason: str) -> None:
|
||||||
|
"""Args:
|
||||||
|
reason: why the hub holds the request back, sent as {"reason"}.
|
||||||
|
"""
|
||||||
|
super().__init__(reason)
|
||||||
|
self.reason = reason
|
||||||
|
|
||||||
|
|
||||||
class EngineError(Exception):
|
class EngineError(Exception):
|
||||||
pass
|
pass
|
||||||
@@ -8,7 +8,7 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|||||||
from urllib.parse import parse_qs, urlsplit
|
from urllib.parse import parse_qs, urlsplit
|
||||||
|
|
||||||
from . import NAME
|
from . import NAME
|
||||||
from .errors import Conflict, EngineError, Missing, ToolError, Unavailable
|
from .errors import Conflict, EngineError, Held, Missing, ToolError, Unavailable
|
||||||
from .hub import Hub
|
from .hub import Hub
|
||||||
|
|
||||||
GUARD = "X-Kvb-Orchestrator"
|
GUARD = "X-Kvb-Orchestrator"
|
||||||
@@ -68,6 +68,8 @@ class Exchange(BaseHTTPRequestHandler):
|
|||||||
self.send_json(HTTPStatus.CONFLICT, error.payload)
|
self.send_json(HTTPStatus.CONFLICT, error.payload)
|
||||||
except Unavailable as error:
|
except Unavailable as error:
|
||||||
self.send_text(HTTPStatus.SERVICE_UNAVAILABLE, str(error))
|
self.send_text(HTTPStatus.SERVICE_UNAVAILABLE, str(error))
|
||||||
|
except Held as error:
|
||||||
|
self.send_json(HTTPStatus.SERVICE_UNAVAILABLE, {"reason": error.reason})
|
||||||
except EngineError as error:
|
except EngineError as error:
|
||||||
self.send_text(HTTPStatus.BAD_GATEWAY, str(error))
|
self.send_text(HTTPStatus.BAD_GATEWAY, str(error))
|
||||||
except OSError as error:
|
except OSError as error:
|
||||||
|
|||||||
@@ -69,7 +69,8 @@ ACTION_POSTS = ("/api/rules", "/api/nl", SETTINGS_ROUTE, TTS_ROUTE, STT_ROUTE, S
|
|||||||
class Handler(Exchange):
|
class Handler(Exchange):
|
||||||
def speak(self, body: bytes) -> None:
|
def speak(self, body: bytes) -> None:
|
||||||
headers = {"Content-Security-Policy": PLAIN_CSP}
|
headers = {"Content-Security-Policy": PLAIN_CSP}
|
||||||
self.attempt(lambda: self.server.hub.speech.synthesize(body), lambda wav: self.send(HTTPStatus.OK, WAV_TYPE, wav, headers))
|
hub = self.server.hub
|
||||||
|
self.attempt(lambda: hub.speech.synthesize(body, hub.speech_gate.admit), lambda wav: self.send(HTTPStatus.OK, WAV_TYPE, wav, headers))
|
||||||
|
|
||||||
def hear(self, body: bytes) -> None:
|
def hear(self, body: bytes) -> None:
|
||||||
self.answer(lambda: self.server.hub.speech.transcribe(body), HTTPStatus.OK)
|
self.answer(lambda: self.server.hub.speech.transcribe(body), HTTPStatus.OK)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -30,12 +31,14 @@ from .offices import Offices
|
|||||||
from .presence import SNAPSHOT
|
from .presence import SNAPSHOT
|
||||||
from .redirects import Redirects
|
from .redirects import Redirects
|
||||||
from .requesting import Requesting
|
from .requesting import Requesting
|
||||||
|
from .resources import RESOURCES, SAMPLES
|
||||||
from .reviewing import Reviewing
|
from .reviewing import Reviewing
|
||||||
from .rules import Rules
|
from .rules import Rules
|
||||||
from .settings import Settings
|
from .settings import Settings
|
||||||
from .signed import signing
|
from .signed import signing
|
||||||
from .skill_scripts import Runner
|
from .skill_scripts import Runner
|
||||||
from .sources import Sources
|
from .sources import Sources
|
||||||
|
from .speechgate import CpuGate
|
||||||
from .tasking import Tasking
|
from .tasking import Tasking
|
||||||
from .timeline import Timeline
|
from .timeline import Timeline
|
||||||
from .transcripts import Transcripts
|
from .transcripts import Transcripts
|
||||||
@@ -96,6 +99,7 @@ class Hub(Mcp, Reviewing, Asking, Messaging, Choosing, Approving, Denying, Reque
|
|||||||
settings: Settings
|
settings: Settings
|
||||||
redirects: Redirects
|
redirects: Redirects
|
||||||
speech: Speech
|
speech: Speech
|
||||||
|
speech_gate: CpuGate
|
||||||
approvers: Path
|
approvers: Path
|
||||||
vendor: Path
|
vendor: Path
|
||||||
icons: Markup
|
icons: Markup
|
||||||
@@ -119,7 +123,9 @@ def open_hub(council: Path, pages: Path, public_port: int, state: Path, host_sta
|
|||||||
reviews = Reviews(state)
|
reviews = Reviews(state)
|
||||||
speech = Speech(engines, state / "tts")
|
speech = Speech(engines, state / "tts")
|
||||||
messages = Ledger(state / "messages.json")
|
messages = Ledger(state / "messages.json")
|
||||||
|
settings = Settings(state / "settings.json")
|
||||||
return Hub(
|
return Hub(
|
||||||
|
speech_gate=CpuGate(samples=places.kept / RESOURCES / SAMPLES, threshold=lambda: settings.get()["speech"]["threshold"], clock=time.time),
|
||||||
vendor=places.vendor,
|
vendor=places.vendor,
|
||||||
icons=sheet(places.vendor, templates),
|
icons=sheet(places.vendor, templates),
|
||||||
spool=Spool(places.cleanup).ready(),
|
spool=Spool(places.cleanup).ready(),
|
||||||
@@ -135,7 +141,7 @@ def open_hub(council: Path, pages: Path, public_port: int, state: Path, host_sta
|
|||||||
log=Log(state / "log.jsonl"),
|
log=Log(state / "log.jsonl"),
|
||||||
batches=Pending(),
|
batches=Pending(),
|
||||||
names=Names(state / "names.json"),
|
names=Names(state / "names.json"),
|
||||||
settings=Settings(state / "settings.json"),
|
settings=settings,
|
||||||
redirects=redirects,
|
redirects=redirects,
|
||||||
watch=Watch(),
|
watch=Watch(),
|
||||||
tree=Tree(file=places.tree_file, heartbeat=state / TREE_HEARTBEAT, lock=threading.Lock(), cached=None, stamp=None),
|
tree=Tree(file=places.tree_file, heartbeat=state / TREE_HEARTBEAT, lock=threading.Lock(), cached=None, stamp=None),
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ SPOKEN_TEXT = 500
|
|||||||
SPOKEN_NAME = 60
|
SPOKEN_NAME = 60
|
||||||
SPOKEN_GROUP = 120
|
SPOKEN_GROUP = 120
|
||||||
ALARM_WORDS = {"pan-pan": "PAN-PAN", "security": "say-cure-ee-tay"}
|
ALARM_WORDS = {"pan-pan": "PAN-PAN", "security": "say-cure-ee-tay"}
|
||||||
|
PROCEDURE = {"mayday": "MAYDAY", **ALARM_WORDS}
|
||||||
ADDRESSED = ("post", "call", "receipt")
|
ADDRESSED = ("post", "call", "receipt")
|
||||||
CALLS = re.compile(r"^(?:(?:MAYDAY|PAN[ -]?PAN|SECURIT[EÉY])\b[\s:,.;!-]*)+", re.IGNORECASE)
|
CALLS = re.compile(r"^(?:(?:MAYDAY|PAN[ -]?PAN|SECURIT[EÉY])\b[\s:,.;!-]*)+", re.IGNORECASE)
|
||||||
HUB_MARK = re.compile(r"\[kvb-orchestrator msg [0-9a-f]{16}\]")
|
HUB_MARK = re.compile(r"\[kvb-orchestrator msg [0-9a-f]{16}\]")
|
||||||
@@ -122,12 +123,57 @@ def spoken_text(cls: str, text: str) -> str:
|
|||||||
body = HUB_MARK.sub("", text)
|
body = HUB_MARK.sub("", text)
|
||||||
if body != text:
|
if body != text:
|
||||||
body = HUB_FILE.sub("", body)
|
body = HUB_FILE.sub("", body)
|
||||||
if cls in ("mayday", *ALARM_WORDS):
|
if cls in PROCEDURE:
|
||||||
body = CALLS.sub("", body.strip())
|
body = CALLS.sub("", body.strip())
|
||||||
body = clip(body, SPOKEN_TEXT)
|
body = clip(body, SPOKEN_TEXT)
|
||||||
return "" if body == "-" else body
|
return "" if body == "-" else body
|
||||||
|
|
||||||
|
|
||||||
|
def procedure(cls: str) -> str:
|
||||||
|
"""Args:
|
||||||
|
cls: the radio class of the row.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The procedure word of a MAYDAY, PAN-PAN or SÉCURITÉ row spoken three
|
||||||
|
times, the same line for every row of the class; "" for any other
|
||||||
|
class.
|
||||||
|
"""
|
||||||
|
word = PROCEDURE.get(cls)
|
||||||
|
return f"{word} {word} {word}" if word else ""
|
||||||
|
|
||||||
|
|
||||||
|
PROCEDURE_LINES = frozenset(procedure(cls) for cls in PROCEDURE)
|
||||||
|
|
||||||
|
|
||||||
|
def phrase_parts(cls: str, sender: str, called: list[str], text: str) -> tuple[str, str]:
|
||||||
|
"""Args:
|
||||||
|
cls: the radio class of the row, one of council.CLASSES or receipt.
|
||||||
|
sender: the sender's name valid at the row's time.
|
||||||
|
called: the names of the called stations valid at the row's time.
|
||||||
|
text: the radio text.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
(word, rest): the procedure word line of the row, "" for a routine
|
||||||
|
class, and the rest of the row in maritime radio phraseology (SMCP
|
||||||
|
style).
|
||||||
|
"""
|
||||||
|
station = clip(sender, SPOKEN_NAME) or "unknown station"
|
||||||
|
if cls == "mayday":
|
||||||
|
head, end = f"this is {station} {station} {station}", "over"
|
||||||
|
elif cls in ALARM_WORDS:
|
||||||
|
head, end = f"all stations, this is {station}", "over"
|
||||||
|
elif cls in ADDRESSED and called:
|
||||||
|
group = clip(" and ".join(clip(name, SPOKEN_NAME) for name in called), SPOKEN_GROUP)
|
||||||
|
head, end = f"{group}, {group}, this is {station}", "over"
|
||||||
|
else:
|
||||||
|
head, end = f"all stations, this is {station}", "out"
|
||||||
|
return procedure(cls), joined((head, spoken_text(cls, text), end))
|
||||||
|
|
||||||
|
|
||||||
|
def joined(parts: tuple[str, ...]) -> str:
|
||||||
|
return ", ".join(part for part in parts if part)
|
||||||
|
|
||||||
|
|
||||||
def phrase(cls: str, sender: str, called: list[str], text: str) -> str:
|
def phrase(cls: str, sender: str, called: list[str], text: str) -> str:
|
||||||
"""Args:
|
"""Args:
|
||||||
cls: the radio class of the row, one of council.CLASSES or receipt.
|
cls: the radio class of the row, one of council.CLASSES or receipt.
|
||||||
@@ -136,18 +182,7 @@ def phrase(cls: str, sender: str, called: list[str], text: str) -> str:
|
|||||||
text: the radio text.
|
text: the radio text.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The row in maritime radio phraseology (SMCP style), one line of at
|
The row in maritime radio phraseology (SMCP style), its procedure
|
||||||
most SPEECH_LIMIT characters.
|
word line first, one line of at most SPEECH_LIMIT characters.
|
||||||
"""
|
"""
|
||||||
station = clip(sender, SPOKEN_NAME) or "unknown station"
|
return joined(phrase_parts(cls, sender, called, text))
|
||||||
if cls == "mayday":
|
|
||||||
head, end = f"MAYDAY MAYDAY MAYDAY, this is {station} {station} {station}", "over"
|
|
||||||
elif cls in ALARM_WORDS:
|
|
||||||
word = ALARM_WORDS[cls]
|
|
||||||
head, end = f"{word} {word} {word}, all stations, this is {station}", "over"
|
|
||||||
elif cls in ADDRESSED and called:
|
|
||||||
group = clip(" and ".join(clip(name, SPOKEN_NAME) for name in called), SPOKEN_GROUP)
|
|
||||||
head, end = f"{group}, {group}, this is {station}", "over"
|
|
||||||
else:
|
|
||||||
head, end = f"all stations, this is {station}", "out"
|
|
||||||
return ", ".join(part for part in (head, spoken_text(cls, text), end) if part)
|
|
||||||
@@ -4,11 +4,9 @@ import copy
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
import threading
|
import threading
|
||||||
from collections.abc import Callable
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from urllib.parse import urlsplit
|
from urllib.parse import urlsplit
|
||||||
|
|
||||||
from .council import CLASSES
|
|
||||||
from .errors import ToolError
|
from .errors import ToolError
|
||||||
from .fields import FIELDS, NAME_LIMIT, expect
|
from .fields import FIELDS, NAME_LIMIT, expect
|
||||||
from .records import read_json, write_atomic
|
from .records import read_json, write_atomic
|
||||||
@@ -17,7 +15,9 @@ THEMES = ("sky", "light", "dark", "custom")
|
|||||||
ESTIMATIONS = ("none", "points")
|
ESTIMATIONS = ("none", "points")
|
||||||
TRENDS = ("off", "regression", "velocity")
|
TRENDS = ("off", "regression", "velocity")
|
||||||
TREND_DAYS = (1, 365)
|
TREND_DAYS = (1, 365)
|
||||||
TONES = ("mayday", "review", "direct", "radio")
|
THRESHOLDS = tuple(range(30, 91, 10))
|
||||||
|
RADIO_MOVED = ("sounds", "squelch")
|
||||||
|
SPEECH_MOVED = ("on", "classes", "muted")
|
||||||
COLOR = re.compile(r"#[0-9a-f]{6}")
|
COLOR = re.compile(r"#[0-9a-f]{6}")
|
||||||
CLOCK = re.compile(r"(?:[01][0-9]|2[0-3]):[0-5][0-9]")
|
CLOCK = re.compile(r"(?:[01][0-9]|2[0-3]):[0-5][0-9]")
|
||||||
MIDNIGHT = "24:00"
|
MIDNIGHT = "24:00"
|
||||||
@@ -33,16 +33,9 @@ SETTINGS = {
|
|||||||
"theme": "sky",
|
"theme": "sky",
|
||||||
"colors": {"top": "#0b1020", "bottom": "#2b3a67", "accent": "#8fadff"},
|
"colors": {"top": "#0b1020", "bottom": "#2b3a67", "accent": "#8fadff"},
|
||||||
"music": {"on": False, "volume": 0.4},
|
"music": {"on": False, "volume": 0.4},
|
||||||
"sounds": {
|
|
||||||
"mayday": {"on": True, "volume": 0.9},
|
|
||||||
"review": {"on": True, "volume": 0.6},
|
|
||||||
"direct": {"on": True, "volume": 0.5},
|
|
||||||
"radio": {"on": False, "volume": 0.25},
|
|
||||||
},
|
|
||||||
"squelch": 1.0,
|
|
||||||
"notify": True,
|
"notify": True,
|
||||||
"streams": [],
|
"streams": [],
|
||||||
"speech": {"on": False, "classes": ["mayday", "pan-pan", "security"], "muted": []},
|
"speech": {"threshold": 50},
|
||||||
"space": {
|
"space": {
|
||||||
"graph": {"axis": "none", "yaw": 30.0, "pitch": 20.0, "zoom": 1.0, "pan_x": 0.0, "pan_y": 0.0},
|
"graph": {"axis": "none", "yaw": 30.0, "pitch": 20.0, "zoom": 1.0, "pan_x": 0.0, "pan_y": 0.0},
|
||||||
"sky": {"azimuth": 180.0, "altitude": 0.0, "fov": 180.0},
|
"sky": {"azimuth": 180.0, "altitude": 0.0, "fov": 180.0},
|
||||||
@@ -56,8 +49,6 @@ SPACE_LIMITS = {
|
|||||||
"graph": {"yaw": (-180.0, 180.0), "pitch": (-90.0, 90.0), "zoom": (0.25, 4.0), "pan_x": (-2000.0, 2000.0), "pan_y": (-2000.0, 2000.0)},
|
"graph": {"yaw": (-180.0, 180.0), "pitch": (-90.0, 90.0), "zoom": (0.25, 4.0), "pan_x": (-2000.0, 2000.0), "pan_y": (-2000.0, 2000.0)},
|
||||||
"sky": {"azimuth": (0.0, 360.0), "altitude": (-30.0, 85.0), "fov": (20.0, 240.0)},
|
"sky": {"azimuth": (0.0, 360.0), "altitude": (-30.0, 85.0), "fov": (20.0, 240.0)},
|
||||||
}
|
}
|
||||||
MUTED_LIMIT = 200
|
|
||||||
LEGACY_CLASSES = {"direct": ("post", "call")}
|
|
||||||
|
|
||||||
|
|
||||||
def section(value: object, name: str, keys: tuple[str, ...]) -> dict:
|
def section(value: object, name: str, keys: tuple[str, ...]) -> dict:
|
||||||
@@ -152,27 +143,12 @@ def stream_entry(raw: object, index: int) -> dict:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def tone_setting(raw: object, kind_name: str) -> dict:
|
|
||||||
entry = section(raw, f"sounds.{kind_name}", ("on", "volume"))
|
|
||||||
return {"on": flag(entry["on"], f"sounds.{kind_name}.on"), "volume": number_in(entry["volume"], f"sounds.{kind_name}.volume", 0.0, 1.0)}
|
|
||||||
|
|
||||||
|
|
||||||
def distinct_texts(value: object, valid: Callable[[str], bool], limit: int) -> bool:
|
|
||||||
if not isinstance(value, list) or len(value) > limit or not all(isinstance(item, str) and valid(item) for item in value):
|
|
||||||
return False
|
|
||||||
return len(set(value)) == len(value)
|
|
||||||
|
|
||||||
|
|
||||||
def speech_setting(raw: object) -> dict:
|
def speech_setting(raw: object) -> dict:
|
||||||
entry = section(raw, "speech", ("on", "classes", "muted"))
|
entry = section(raw, "speech", tuple(SETTINGS["speech"]))
|
||||||
classes, muted = entry["classes"], entry["muted"]
|
threshold = entry["threshold"]
|
||||||
if not distinct_texts(classes, lambda name: name in CLASSES, len(CLASSES)):
|
if isinstance(threshold, bool) or not isinstance(threshold, int) or threshold not in THRESHOLDS:
|
||||||
raise ToolError(f"speech.classes must list distinct classes of {', '.join(CLASSES)}")
|
raise ToolError(f"speech.threshold must be one of {', '.join(map(str, THRESHOLDS))} percent")
|
||||||
if "mayday" not in classes:
|
return {"threshold": threshold}
|
||||||
raise ToolError("speech.classes must include mayday: a MAYDAY is never quieter than anything else")
|
|
||||||
if not distinct_texts(muted, FIELDS["agent"][0].fullmatch, MUTED_LIMIT):
|
|
||||||
raise ToolError(f"speech.muted must list at most {MUTED_LIMIT} distinct agent keys, each one line of 1 to 200 characters")
|
|
||||||
return {"on": flag(entry["on"], "speech.on"), "classes": [name for name in CLASSES if name in classes], "muted": sorted(muted)}
|
|
||||||
|
|
||||||
|
|
||||||
def space_setting(raw: object) -> dict:
|
def space_setting(raw: object) -> dict:
|
||||||
@@ -200,7 +176,6 @@ def valid_settings(raw: object) -> dict:
|
|||||||
data = section(raw, "settings", tuple(SETTINGS))
|
data = section(raw, "settings", tuple(SETTINGS))
|
||||||
colors = section(data["colors"], "colors", tuple(SETTINGS["colors"]))
|
colors = section(data["colors"], "colors", tuple(SETTINGS["colors"]))
|
||||||
music = section(data["music"], "music", ("on", "volume"))
|
music = section(data["music"], "music", ("on", "volume"))
|
||||||
sounds = section(data["sounds"], "sounds", TONES)
|
|
||||||
if not isinstance(data["theme"], str) or data["theme"] not in THEMES:
|
if not isinstance(data["theme"], str) or data["theme"] not in THEMES:
|
||||||
raise ToolError(f"theme must be one of {', '.join(THEMES)}")
|
raise ToolError(f"theme must be one of {', '.join(THEMES)}")
|
||||||
if not isinstance(data["streams"], list) or len(data["streams"]) > STREAMS_LIMIT:
|
if not isinstance(data["streams"], list) or len(data["streams"]) > STREAMS_LIMIT:
|
||||||
@@ -213,8 +188,6 @@ def valid_settings(raw: object) -> dict:
|
|||||||
"theme": data["theme"],
|
"theme": data["theme"],
|
||||||
"colors": {key: color(colors[key], f"colors.{key}") for key in SETTINGS["colors"]},
|
"colors": {key: color(colors[key], f"colors.{key}") for key in SETTINGS["colors"]},
|
||||||
"music": {"on": flag(music["on"], "music.on"), "volume": number_in(music["volume"], "music.volume", 0.0, 1.0)},
|
"music": {"on": flag(music["on"], "music.on"), "volume": number_in(music["volume"], "music.volume", 0.0, 1.0)},
|
||||||
"sounds": {kind_name: tone_setting(sounds[kind_name], kind_name) for kind_name in TONES},
|
|
||||||
"squelch": number_in(data["squelch"], "squelch", 0.0, 1.0),
|
|
||||||
"notify": flag(data["notify"], "notify"),
|
"notify": flag(data["notify"], "notify"),
|
||||||
"streams": [stream_entry(entry, index) for index, entry in enumerate(data["streams"])],
|
"streams": [stream_entry(entry, index) for index, entry in enumerate(data["streams"])],
|
||||||
"speech": speech_setting(data["speech"]),
|
"speech": speech_setting(data["speech"]),
|
||||||
@@ -225,23 +198,32 @@ def valid_settings(raw: object) -> dict:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def speech_renamed(stored: object) -> object:
|
def radio_moved(stored: object) -> object:
|
||||||
speech = stored.get("speech") if isinstance(stored, dict) else None
|
"""Returns:
|
||||||
classes = speech.get("classes") if isinstance(speech, dict) else None
|
The stored settings without the tones, the squelch and the read-aloud
|
||||||
if not isinstance(classes, list) or not any(name in LEGACY_CLASSES for name in classes):
|
filters, which each browser keeps for itself since requirement 004;
|
||||||
|
a speech section left empty is dropped so the default threshold
|
||||||
|
fills it.
|
||||||
|
"""
|
||||||
|
if not isinstance(stored, dict):
|
||||||
return stored
|
return stored
|
||||||
renamed = [new for name in classes for new in LEGACY_CLASSES.get(name, (name,))]
|
kept = {key: value for key, value in stored.items() if key not in RADIO_MOVED}
|
||||||
return {**stored, "speech": {**speech, "classes": list(dict.fromkeys(renamed))}}
|
speech = stored.get("speech")
|
||||||
|
if not isinstance(speech, dict):
|
||||||
|
return kept
|
||||||
|
rest = {key: value for key, value in speech.items() if key not in SPEECH_MOVED}
|
||||||
|
others = {key: value for key, value in kept.items() if key != "speech"}
|
||||||
|
return {**others, "speech": rest} if rest else others
|
||||||
|
|
||||||
|
|
||||||
def migrated(stored: object) -> object:
|
def migrated(stored: object) -> object:
|
||||||
"""Returns:
|
"""Returns:
|
||||||
The stored settings in the current shape: legacy speech classes
|
The stored settings in the current shape: the radio set moved to the
|
||||||
renamed, and a stream saved before redirect resolution with via []:
|
browser, and a stream saved before redirect resolution with via []:
|
||||||
it may have been saved without the passkey, so the hub never dials
|
it may have been saved without the passkey, so the hub never dials
|
||||||
it unsigned; re-adding it with the passkey resolves its redirects.
|
it unsigned; re-adding it with the passkey resolves its redirects.
|
||||||
"""
|
"""
|
||||||
stored = speech_renamed(stored)
|
stored = radio_moved(stored)
|
||||||
streams = stored.get("streams") if isinstance(stored, dict) else None
|
streams = stored.get("streams") if isinstance(stored, dict) else None
|
||||||
if not isinstance(streams, list):
|
if not isinstance(streams, list):
|
||||||
return stored
|
return stored
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import stat
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from .errors import Held
|
||||||
|
from .phrasing import PROCEDURE_LINES
|
||||||
|
from .records import READ_FLAGS, epoch, parse_line
|
||||||
|
|
||||||
|
FRESH = 120
|
||||||
|
TAIL = 1 << 16
|
||||||
|
|
||||||
|
|
||||||
|
def tail_lines(path: Path, size: int) -> list[str]:
|
||||||
|
"""Args:
|
||||||
|
path: a file the host sampler appends to; a link, FIFO or other
|
||||||
|
non-regular file reads as empty without blocking.
|
||||||
|
size: the most bytes to read from its end.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The complete lines within the last size bytes, oldest first; [] when
|
||||||
|
the file cannot be read.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
with open(os.open(path, READ_FLAGS), "rb") as handle:
|
||||||
|
info = os.fstat(handle.fileno())
|
||||||
|
if not stat.S_ISREG(info.st_mode):
|
||||||
|
return []
|
||||||
|
start = max(info.st_size - size, 0)
|
||||||
|
handle.seek(start)
|
||||||
|
data = handle.read(size)
|
||||||
|
except OSError:
|
||||||
|
return []
|
||||||
|
lines = data.decode("utf-8", errors="replace").splitlines()
|
||||||
|
return lines[1:] if start else lines
|
||||||
|
|
||||||
|
|
||||||
|
def latest_cpu(path: Path, now: float) -> float | None:
|
||||||
|
"""Args:
|
||||||
|
path: samples.jsonl of the W4 resource feed.
|
||||||
|
now: the current time in epoch seconds.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The cpu percent of the newest timed row, when that row lies at most
|
||||||
|
FRESH seconds from now and carries a number from 0 to 100; else None.
|
||||||
|
"""
|
||||||
|
for row in map(parse_line, reversed(tail_lines(path, TAIL))):
|
||||||
|
t = None if row is None else epoch(row.get("t"))
|
||||||
|
if t is None:
|
||||||
|
continue
|
||||||
|
cpu = row.get("cpu")
|
||||||
|
fresh = abs(now - t) <= FRESH
|
||||||
|
return float(cpu) if fresh and not isinstance(cpu, bool) and isinstance(cpu, int | float) and 0 <= cpu <= 100 else None
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class CpuGate:
|
||||||
|
samples: Path
|
||||||
|
threshold: Callable[[], int]
|
||||||
|
clock: Callable[[], float]
|
||||||
|
|
||||||
|
def state(self) -> dict:
|
||||||
|
"""Returns:
|
||||||
|
{"threshold", "cpu", "open"}: open only while the latest fresh
|
||||||
|
sample lies below the threshold; cpu None without one.
|
||||||
|
"""
|
||||||
|
cpu = latest_cpu(self.samples, self.clock())
|
||||||
|
limit = self.threshold()
|
||||||
|
return {"threshold": limit, "cpu": cpu, "open": cpu is not None and cpu < limit}
|
||||||
|
|
||||||
|
def admit(self, line: str) -> None:
|
||||||
|
"""Args:
|
||||||
|
line: the uncached line about to be rendered in a voice the hub
|
||||||
|
hands out; a procedure word line always renders, it is the
|
||||||
|
same few lines per voice and outlives the cache trim.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None when the line may render; otherwise it raises Held busy
|
||||||
|
while the latest fresh sample is at or above the threshold and
|
||||||
|
Held unknown without a fresh sample.
|
||||||
|
"""
|
||||||
|
if line in PROCEDURE_LINES:
|
||||||
|
return
|
||||||
|
gate = self.state()
|
||||||
|
if gate["cpu"] is None:
|
||||||
|
raise Held("unknown")
|
||||||
|
if not gate["open"]:
|
||||||
|
raise Held("busy")
|
||||||
@@ -22,7 +22,7 @@ from .graph import graph
|
|||||||
from .jobs import JOB_OPEN
|
from .jobs import JOB_OPEN
|
||||||
from .messaging import message_row
|
from .messaging import message_row
|
||||||
from .orbits import TRACK_STEPS, track
|
from .orbits import TRACK_STEPS, track
|
||||||
from .phrasing import phrase
|
from .phrasing import joined, phrase_parts
|
||||||
from .presence import presence, read_snapshot
|
from .presence import presence, read_snapshot
|
||||||
from .records import epoch, iso_epoch, read_jsonl
|
from .records import epoch, iso_epoch, read_jsonl
|
||||||
from .register import (
|
from .register import (
|
||||||
@@ -94,7 +94,8 @@ class View:
|
|||||||
source = directory.key(row["from"], row["ts"])
|
source = directory.key(row["from"], row["ts"])
|
||||||
targets = [directory.key(name, row["ts"]) for name in row["to"]]
|
targets = [directory.key(name, row["ts"]) for name in row["to"]]
|
||||||
called = [directory.at(key, row["ts"]) or name for key, name in zip(targets, row["to"], strict=True)]
|
called = [directory.at(key, row["ts"]) or name for key, name in zip(targets, row["to"], strict=True)]
|
||||||
spoken = {"speech": phrase(row["class"], directory.at(source, row["ts"]) or row["from"], called, row["text"]), "voice": self.speech.voice_of(source)}
|
word, rest = phrase_parts(row["class"], directory.at(source, row["ts"]) or row["from"], called, row["text"])
|
||||||
|
spoken = {"speech": joined((word, rest)), "word": word, "rest": rest, "voice": self.speech.voice_of(source)}
|
||||||
radio.append({**row, "source": source, "targets": targets, "now": directory.now(source), **spoken})
|
radio.append({**row, "source": source, "targets": targets, "now": directory.now(source), **spoken})
|
||||||
grant_rows = [{**grant, "target": directory.key(grant["to"], grant["ts"])} for grant in grants(history, (begin, end), claims, tasks)]
|
grant_rows = [{**grant, "target": directory.key(grant["to"], grant["ts"])} for grant in grants(history, (begin, end), claims, tasks)]
|
||||||
self.watch.see(tasks, now)
|
self.watch.see(tasks, now)
|
||||||
@@ -112,7 +113,7 @@ class View:
|
|||||||
return {
|
return {
|
||||||
"settings": settings,
|
"settings": settings,
|
||||||
"media": media_origins(settings),
|
"media": media_origins(settings),
|
||||||
"speech": self.speech.describe(),
|
"speech": {**self.speech.describe(), "gate": self.speech_gate.state()},
|
||||||
"people": people,
|
"people": people,
|
||||||
"inbox": inbox,
|
"inbox": inbox,
|
||||||
"badges": badges(inbox, claims, self.jobs.listed(), now),
|
"badges": badges(inbox, claims, self.jobs.listed(), now),
|
||||||
@@ -148,19 +149,23 @@ class View:
|
|||||||
"graph": drawn,
|
"graph": drawn,
|
||||||
}
|
}
|
||||||
|
|
||||||
def stream_change(self, settings: dict, saved: dict) -> dict | None:
|
def signed_changes(self, settings: dict, saved: dict) -> list[dict]:
|
||||||
"""Args:
|
"""Args:
|
||||||
settings: valid settings the page wants to save.
|
settings: valid settings the page wants to save.
|
||||||
saved: the settings saved now.
|
saved: the settings saved now.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The item the operator's passkey signs when the settings name a
|
The items the operator's passkey signs for this save: the station
|
||||||
stream url the saved ones do not, else None: the station origins
|
origins and the new urls when the settings name a stream url the
|
||||||
and those urls. The hub dials no url before it is signed, and
|
saved ones do not, and the speech threshold when it differs from
|
||||||
resolves its redirect targets only after the signature.
|
the saved one, since the hub's speech gate enforces it. The hub
|
||||||
|
dials no url before it is signed, and resolves its redirect
|
||||||
|
targets only after the signature.
|
||||||
"""
|
"""
|
||||||
urls = sorted({stream["url"] for stream in settings["streams"]} - {stream["url"] for stream in saved["streams"]})
|
urls = sorted({stream["url"] for stream in settings["streams"]} - {stream["url"] for stream in saved["streams"]})
|
||||||
return signing.stream_item(station_origins(settings), urls) if urls else None
|
threshold = settings["speech"]["threshold"]
|
||||||
|
streams = [signing.stream_item(station_origins(settings), urls)] if urls else []
|
||||||
|
return streams + ([signing.threshold_item(threshold)] if threshold != saved["speech"]["threshold"] else [])
|
||||||
|
|
||||||
def resolved(self, settings: dict, saved: dict) -> tuple[dict, list[str]]:
|
def resolved(self, settings: dict, saved: dict) -> tuple[dict, list[str]]:
|
||||||
"""Args:
|
"""Args:
|
||||||
@@ -188,8 +193,7 @@ class View:
|
|||||||
return {**settings, "streams": streams}, problems
|
return {**settings, "streams": streams}, problems
|
||||||
|
|
||||||
def sign_settings(self, body: bytes) -> dict:
|
def sign_settings(self, body: bytes) -> dict:
|
||||||
change = self.stream_change(valid_settings(decode(body)), self.settings.get())
|
return self.challenge_for(self.signed_changes(valid_settings(decode(body)), self.settings.get()))
|
||||||
return self.challenge_for([] if change is None else [change])
|
|
||||||
|
|
||||||
def save_settings(self, body: bytes) -> dict:
|
def save_settings(self, body: bytes) -> dict:
|
||||||
"""Returns:
|
"""Returns:
|
||||||
@@ -201,8 +205,7 @@ class View:
|
|||||||
proof = payload.pop("proof", None)
|
proof = payload.pop("proof", None)
|
||||||
settings = valid_settings(payload)
|
settings = valid_settings(payload)
|
||||||
saved = self.settings.get()
|
saved = self.settings.get()
|
||||||
change = self.stream_change(settings, saved)
|
for change in self.signed_changes(settings, saved):
|
||||||
if change is not None:
|
|
||||||
self.operator_proven(proof, change)
|
self.operator_proven(proof, change)
|
||||||
resolved, problems = self.resolved(settings, saved)
|
resolved, problems = self.resolved(settings, saved)
|
||||||
return {"settings": self.settings.put(resolved, saved), "problems": problems}
|
return {"settings": self.settings.put(resolved, saved), "problems": problems}
|
||||||
|
|||||||
@@ -463,6 +463,8 @@ pre.notes { margin: 0; padding: 8px 10px; background: var(--sunken); border-radi
|
|||||||
<script nonce="{{ nonce }}">
|
<script nonce="{{ nonce }}">
|
||||||
{% include "voice.js" %}
|
{% include "voice.js" %}
|
||||||
|
|
||||||
|
{% include "radioset.js" %}
|
||||||
|
|
||||||
{% include "bubbles.js" %}
|
{% include "bubbles.js" %}
|
||||||
|
|
||||||
{% include "diffs.js" %}
|
{% include "diffs.js" %}
|
||||||
|
|||||||
@@ -1,3 +1,12 @@
|
|||||||
|
const beeps = new Set();
|
||||||
|
|
||||||
|
function silenceBeeps() {
|
||||||
|
for (const oscillator of beeps) {
|
||||||
|
oscillator.stop();
|
||||||
|
}
|
||||||
|
beeps.clear();
|
||||||
|
}
|
||||||
|
|
||||||
function play(kind, volume) {
|
function play(kind, volume) {
|
||||||
const sound = context();
|
const sound = context();
|
||||||
if (!sound) {
|
if (!sound) {
|
||||||
@@ -15,6 +24,8 @@
|
|||||||
gain.gain.exponentialRampToValueAtTime(level, start + 0.02);
|
gain.gain.exponentialRampToValueAtTime(level, start + 0.02);
|
||||||
gain.gain.exponentialRampToValueAtTime(0.0001, start + length);
|
gain.gain.exponentialRampToValueAtTime(0.0001, start + length);
|
||||||
oscillator.connect(gain).connect(sound.destination);
|
oscillator.connect(gain).connect(sound.destination);
|
||||||
|
oscillator.addEventListener("ended", () => beeps.delete(oscillator), {once: true});
|
||||||
|
beeps.add(oscillator);
|
||||||
oscillator.start(start);
|
oscillator.start(start);
|
||||||
oscillator.stop(start + length + 0.05);
|
oscillator.stop(start + length + 0.05);
|
||||||
}
|
}
|
||||||
@@ -47,7 +58,7 @@
|
|||||||
const newRadio = state.radio.filter((row) => !known.radio.has(`${row.ts}|${row.from}|${row.text.slice(0, 40)}`));
|
const newRadio = state.radio.filter((row) => !known.radio.has(`${row.ts}|${row.from}|${row.text.slice(0, 40)}`));
|
||||||
const direct = newRadio.filter((row) => row.to.includes(HUMAN));
|
const direct = newRadio.filter((row) => row.to.includes(HUMAN));
|
||||||
const present = {mayday: newAlarms.length > 0, review: fresh.some((item) => item.kind !== "operator"), direct: direct.length > 0, radio: newRadio.length > 0};
|
const present = {mayday: newAlarms.length > 0, review: fresh.some((item) => item.kind !== "operator"), direct: direct.length > 0, radio: newRadio.length > 0};
|
||||||
const kind = Voice.beep(present, settings, SOUND_KINDS.map(([id]) => id));
|
const kind = radioSet.mode === "silence" ? null : Voice.beep(present, tones(), SOUND_KINDS.map(([id]) => id));
|
||||||
if (kind) {
|
if (kind) {
|
||||||
play(kind, loudness(kind));
|
play(kind, loudness(kind));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,7 +85,7 @@
|
|||||||
|
|
||||||
function toggleGear() {
|
function toggleGear() {
|
||||||
const open = spotDom.gearBox.hidden;
|
const open = spotDom.gearBox.hidden;
|
||||||
spotDom.gearBox.replaceChildren(...(open ? [radioSettings(toggleGear)] : []));
|
spotDom.gearBox.replaceChildren(...(open ? [radioFilters()] : []));
|
||||||
spotDom.gearBox.hidden = !open;
|
spotDom.gearBox.hidden = !open;
|
||||||
spotDom.gear.setAttribute("aria-expanded", String(open));
|
spotDom.gear.setAttribute("aria-expanded", String(open));
|
||||||
window.requestAnimationFrame(refresh3d);
|
window.requestAnimationFrame(refresh3d);
|
||||||
@@ -94,13 +94,15 @@
|
|||||||
function radioSpot() {
|
function radioSpot() {
|
||||||
const {form, recipient, text} = composerForm();
|
const {form, recipient, text} = composerForm();
|
||||||
const gearBox = h("div", {class: "radio-gear", id: "radio-gear", hidden: true});
|
const gearBox = h("div", {class: "radio-gear", id: "radio-gear", hidden: true});
|
||||||
const gear = iconBtn("bi-gear", "Radio settings", "btn small ghost spot-toggle", toggleGear, "Radio settings: tones, squelch and read aloud");
|
const gear = iconBtn("bi-funnel", "Read aloud", "btn small ghost spot-toggle", toggleGear, "Read aloud: the classes and agents the radio sounds for");
|
||||||
gear.setAttribute("aria-expanded", "false");
|
gear.setAttribute("aria-expanded", "false");
|
||||||
gear.setAttribute("aria-controls", "radio-gear");
|
gear.setAttribute("aria-controls", "radio-gear");
|
||||||
const [, label, , hint] = actionOf("clear-mayday");
|
const [, label, , hint] = actionOf("clear-mayday");
|
||||||
const clear = iconBtn("bi-life-preserver", label, "btn small danger", () => confirmAct("clear-mayday", []), hint);
|
const clear = iconBtn("bi-life-preserver", label, "btn small danger", () => confirmAct("clear-mayday", []), hint);
|
||||||
|
const {face, log, station} = radioSide();
|
||||||
const panel = h("section", {class: "card radio-spot", id: "radio-spot", "aria-label": "Radio", hidden: !ui.spot},
|
const panel = h("section", {class: "card radio-spot", id: "radio-spot", "aria-label": "Radio", hidden: !ui.spot},
|
||||||
h("div", {class: "spot-head"}, h("h3", {class: "group-title"}, "Radio"), h("span", {class: "spacer"}), gear, clear), h("div", {class: "spot-body"}, form, radioSide(), gearBox));
|
h("div", {class: "spot-head"}, h("h3", {class: "group-title"}, "Radio"), h("span", {class: "spacer"}), gear, clear), face, gearBox,
|
||||||
|
h("div", {class: "spot-body"}, h("div", {class: "radio-side"}, form, station), log));
|
||||||
const toggle = document.getElementById("radio-open");
|
const toggle = document.getElementById("radio-open");
|
||||||
toggle.setAttribute("aria-expanded", String(ui.spot));
|
toggle.setAttribute("aria-expanded", String(ui.spot));
|
||||||
toggle.addEventListener("click", () => openSpot(!ui.spot));
|
toggle.addEventListener("click", () => openSpot(!ui.spot));
|
||||||
|
|||||||
@@ -52,7 +52,7 @@
|
|||||||
drawTime();
|
drawTime();
|
||||||
motion.asOf = ui.asOf;
|
motion.asOf = ui.asOf;
|
||||||
relayout(0);
|
relayout(0);
|
||||||
window.setInterval(refresh, 10000);
|
window.setInterval(poll, 10000);
|
||||||
window.setInterval(follow, 3000);
|
window.setInterval(follow, 3000);
|
||||||
window.setInterval(() => relayout(MOVE), RELAYOUT);
|
window.setInterval(() => relayout(MOVE), RELAYOUT);
|
||||||
window.setInterval(() => live() && drawSky(), 30000);
|
window.setInterval(() => live() && drawSky(), 30000);
|
||||||
|
|||||||
@@ -144,7 +144,7 @@
|
|||||||
const hint = h("p", {class: "muted"});
|
const hint = h("p", {class: "muted"});
|
||||||
const status = h("span", {class: "muted composer-status", "aria-live": "polite"});
|
const status = h("span", {class: "muted composer-status", "aria-live": "polite"});
|
||||||
const record = h("button", {type: "button", class: "btn small", "aria-pressed": "false", title: "Record: one click starts, another stops; the hub transcribes, you check the text before sending"});
|
const record = h("button", {type: "button", class: "btn small", "aria-pressed": "false", title: "Record: one click starts, another stops; the hub transcribes, you check the text before sending"});
|
||||||
const submit = h("button", {type: "submit", class: "btn primary"}, icon("bi-send"), "Send");
|
const submit = h("button", {type: "submit", class: "btn primary ptt", title: "PTT: push to transmit the message; your passkey signs it"}, icon("bi-broadcast-pin"), "PTT");
|
||||||
const attach = h("input", {type: "checkbox", title: "Attach recording: the agents get the path of the sound file; otherwise only the checked text goes out"});
|
const attach = h("input", {type: "checkbox", title: "Attach recording: the agents get the path of the sound file; otherwise only the checked text goes out"});
|
||||||
const parts = {record, status, text, replay, attach};
|
const parts = {record, status, text, replay, attach};
|
||||||
recordLabel(parts, false);
|
recordLabel(parts, false);
|
||||||
|
|||||||
@@ -51,6 +51,8 @@
|
|||||||
const SPEECH_QUEUE = 20;
|
const SPEECH_QUEUE = 20;
|
||||||
const SPEECH_QUIET = 60000;
|
const SPEECH_QUIET = 60000;
|
||||||
const SPEECH_FRESH = 300;
|
const SPEECH_FRESH = 300;
|
||||||
|
const SOUND_SLACK = 2;
|
||||||
|
const STATE_TIMEOUT = 15000;
|
||||||
const THEME_CHOICES = [["sky", "Sky (follows the light outside)"], ["light", "Always light"], ["dark", "Always dark"], ["custom", "Custom colors"]];
|
const THEME_CHOICES = [["sky", "Sky (follows the light outside)"], ["light", "Always light"], ["dark", "Always dark"], ["custom", "Custom colors"]];
|
||||||
const COLOR_FIELDS = [["top", "Sky at the top"], ["bottom", "Sky at the horizon"], ["accent", "Accent"]];
|
const COLOR_FIELDS = [["top", "Sky at the top"], ["bottom", "Sky at the horizon"], ["accent", "Accent"]];
|
||||||
const STAGE_LABELS = {night: "Night", astronomical: "Astronomical twilight", nautical: "Nautical twilight", civil: "Civil twilight", golden: "Golden hour", day: "Day"};
|
const STAGE_LABELS = {night: "Night", astronomical: "Astronomical twilight", nautical: "Nautical twilight", civil: "Civil twilight", golden: "Golden hour", day: "Day"};
|
||||||
|
|||||||
@@ -1,10 +1,23 @@
|
|||||||
|
let loadTicket = 0;
|
||||||
|
let appliedTicket = 0;
|
||||||
|
let loadsInFlight = 0;
|
||||||
|
|
||||||
async function load() {
|
async function load() {
|
||||||
|
loadTicket += 1;
|
||||||
|
const ticket = loadTicket;
|
||||||
|
loadsInFlight += 1;
|
||||||
|
const abort = new AbortController();
|
||||||
|
const timer = window.setTimeout(() => abort.abort(), STATE_TIMEOUT);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/state?${windowQuery()}`, {cache: "no-store"});
|
const response = await fetch(`/api/state?${windowQuery()}`, {cache: "no-store", signal: abort.signal});
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(String(response.status));
|
throw new Error(String(response.status));
|
||||||
}
|
}
|
||||||
const next = await response.json();
|
const next = await response.json();
|
||||||
|
if (ticket < appliedTicket) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
appliedTicket = ticket;
|
||||||
const previous = state.now;
|
const previous = state.now;
|
||||||
state = next;
|
state = next;
|
||||||
ui.offline = false;
|
ui.offline = false;
|
||||||
@@ -25,9 +38,20 @@
|
|||||||
}
|
}
|
||||||
pulses(state.radio.filter((row) => row.ts > previous));
|
pulses(state.radio.filter((row) => row.ts > previous));
|
||||||
} catch {
|
} catch {
|
||||||
|
if (ticket > appliedTicket) {
|
||||||
ui.offline = true;
|
ui.offline = true;
|
||||||
tick();
|
tick();
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
window.clearTimeout(timer);
|
||||||
|
loadsInFlight -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function poll() {
|
||||||
|
if (!loadsInFlight) {
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const SELECTION_HOLD = 20000;
|
const SELECTION_HOLD = 20000;
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
const STATION_TONES = {playing: "ok", loading: "info", reload: "info", blocked: "warn", error: "bad", due: "teal", stopped: "neutral", idle: "neutral", off: "neutral"};
|
const STATION_TONES = {playing: "ok", loading: "info", reload: "info", blocked: "warn", error: "bad", due: "teal", stopped: "neutral", idle: "neutral", off: "neutral"};
|
||||||
const NOTE_ID = /^[0-9a-f]{16}$/;
|
const NOTE_ID = /^[0-9a-f]{16}$/;
|
||||||
|
const MODE_LABELS = {
|
||||||
|
auto: ["bi-volume-up", "Auto", "Auto: speech while the host CPU lies below the threshold, otherwise the noise burst"],
|
||||||
|
quiet: ["bi-soundwave", "Quiet", "Quiet: the noise burst only, never speech; MAYDAY, PAN-PAN and Sécurité still sound their procedure word"],
|
||||||
|
silence: ["bi-volume-mute", "Silence", "Silence: no sound, bubbles and log only; MAYDAY, PAN-PAN and Sécurité still sound their procedure word"],
|
||||||
|
};
|
||||||
|
const TAB_LABELS = {sent: ["bi-arrow-up-right", "Sent", "Sent: what you transmitted"], received: ["bi-arrow-down-left", "Received", "Received: radio to you or to all"], all: ["bi-journal-text", "All", "All: every radio row the hub logs"]};
|
||||||
|
const CHIP_LABELS = {class: "Class", from: "Sender", to: "Recipient", agent: "Agent"};
|
||||||
let radioDom = null;
|
let radioDom = null;
|
||||||
|
|
||||||
function voiceReplay(note) {
|
function voiceReplay(note) {
|
||||||
@@ -19,7 +26,7 @@
|
|||||||
return [radio.state, radio.short, radio.note];
|
return [radio.state, radio.short, radio.note];
|
||||||
}
|
}
|
||||||
if (!stream.on) {
|
if (!stream.on) {
|
||||||
return ["off", "off", "Switched off in the settings"];
|
return ["off", "off", "Switched off in the stations"];
|
||||||
}
|
}
|
||||||
if (!Ambience.activeStream([stream], Ambience.slotOf(new Date()))) {
|
if (!Ambience.activeStream([stream], Ambience.slotOf(new Date()))) {
|
||||||
return ["idle", "not due", `Plays ${scheduleOf(stream)}; ▶ plays it now`];
|
return ["idle", "not due", `Plays ${scheduleOf(stream)}; ▶ plays it now`];
|
||||||
@@ -52,7 +59,32 @@
|
|||||||
radioDom.stationsSig = sig;
|
radioDom.stationsSig = sig;
|
||||||
radioDom.stations.replaceChildren(...(settings.streams.length
|
radioDom.stations.replaceChildren(...(settings.streams.length
|
||||||
? settings.streams.map(stationRow)
|
? settings.streams.map(stationRow)
|
||||||
: [h("li", {class: "muted"}, "No station yet: add one under ⚙ Settings → Internet radio.")]));
|
: [h("li", {class: "muted"}, "No station yet: add one with Edit stations.")]));
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawDisplay() {
|
||||||
|
if (!radioDom) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const {lamp, cls, mode, cpu} = radioDom.face;
|
||||||
|
const now = speech.current;
|
||||||
|
const newest = state.radio[state.radio.length - 1];
|
||||||
|
const shown = now ? now.cls : newest ? newest.class : "";
|
||||||
|
const gate = state.speech.gate;
|
||||||
|
const playing = Boolean(now && speech.source);
|
||||||
|
const sent = Boolean(playing && now.sent);
|
||||||
|
lamp.dataset.lit = String(playing);
|
||||||
|
lamp.dataset.tx = String(sent);
|
||||||
|
lamp.textContent = sent ? "TX" : "RX";
|
||||||
|
lamp.setAttribute("aria-label", playing ? `${sent ? "TX" : "RX"}: sound plays` : "RX: no sound");
|
||||||
|
lamp.title = lamp.getAttribute("aria-label");
|
||||||
|
cls.textContent = shown ? CLASSES[shown] || shown : "–";
|
||||||
|
mode.textContent = MODE_LABELS[radioSet.mode][1];
|
||||||
|
cpu.textContent = gate.cpu === null ? "CPU ?" : `CPU ${Math.round(gate.cpu)} %`;
|
||||||
|
cpu.dataset.open = String(gate.open);
|
||||||
|
cpu.title = gate.cpu === null
|
||||||
|
? `No host CPU sample of the last two minutes: the hub renders no new speech line, threshold ${gate.threshold} %`
|
||||||
|
: `Host CPU ${fixed(gate.cpu)} %: new speech renders below ${gate.threshold} %${gate.open ? "" : ", so the radio plays the burst"}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawSpeaker() {
|
function drawSpeaker() {
|
||||||
@@ -61,13 +93,15 @@
|
|||||||
art.classList.toggle("speaking", Boolean(now && art.dataset.face === now.who));
|
art.classList.toggle("speaking", Boolean(now && art.dataset.face === now.who));
|
||||||
}
|
}
|
||||||
if (radioDom) {
|
if (radioDom) {
|
||||||
radioDom.caption.hidden = !now;
|
const {caption} = radioDom.face;
|
||||||
radioDom.caption.replaceChildren(...(now ? [
|
caption.hidden = !now;
|
||||||
|
caption.replaceChildren(...(now ? [
|
||||||
h("span", {class: "speaker-face"}, face(now.who, false)),
|
h("span", {class: "speaker-face"}, face(now.who, false)),
|
||||||
h("strong", {}, `${nameOf(now.who)} speaking`),
|
h("strong", {}, `${nameOf(now.who)} speaking`),
|
||||||
h("span", {class: "caption-line", title: now.line}, now.line),
|
h("span", {class: "caption-line", title: now.line}, now.line),
|
||||||
iconBtn("bi-stop-fill", "Stop speaking", "btn small ghost", cancelSpeech),
|
iconBtn("bi-stop-fill", "Stop speaking", "btn small ghost", cancelSpeech),
|
||||||
] : []));
|
] : []));
|
||||||
|
drawDisplay();
|
||||||
}
|
}
|
||||||
if (ui.screen === "agents" && (ui.views.agents === "orbit" || ui.views.agents === "chord")) {
|
if (ui.screen === "agents" && (ui.views.agents === "orbit" || ui.views.agents === "chord")) {
|
||||||
stage();
|
stage();
|
||||||
@@ -75,33 +109,187 @@
|
|||||||
drawBubbles();
|
drawBubbles();
|
||||||
}
|
}
|
||||||
|
|
||||||
function sentRows() {
|
function knob(label, hint, attrs, change) {
|
||||||
const rows = state.messages.slice(-5).reverse();
|
const value = h("output", {class: "knob-value"});
|
||||||
return rows.length ? h("ul", {class: "mini"}, rows.map((message) => h("li", {},
|
const input = h("input", {type: "range", class: "knob", ...attrs, ...named(label, hint), on: {input: (event) => {
|
||||||
h("div", {class: "row"}, klass(message.class || "post"), when(message.created_ts)),
|
value.textContent = event.target.value;
|
||||||
h("span", {class: "muted"}, `to ${message.to === "*" ? "all" : message.to}`),
|
change(Number(event.target.value));
|
||||||
h("span", {}, short(message.text)),
|
}}});
|
||||||
voiceReplay(message.voice)))) : h("p", {class: "muted"}, "Nothing sent in the chosen time range yet.");
|
return {input, value, row: h("label", {class: "knob-row", title: hint}, h("span", {class: "knob-label", title: label}, label), input, value)};
|
||||||
|
}
|
||||||
|
|
||||||
|
function toneKnob(id, label) {
|
||||||
|
const toggle = h("button", {type: "button", class: "btn small ghost icon-only", "aria-pressed": "false", ...named(`${label} tone`), on: {click: () => {
|
||||||
|
keepRadio({sounds: {...radioSet.sounds, [id]: {...radioSet.sounds[id], on: !radioSet.sounds[id].on}}});
|
||||||
|
}}});
|
||||||
|
const level = knob(label, `${label}: tone volume; speech and the burst of these rows play at it`, {min: 0, max: 1, step: 0.05}, (volume) => {
|
||||||
|
keepRadio({sounds: {...radioSet.sounds, [id]: {...radioSet.sounds[id], volume}}});
|
||||||
|
});
|
||||||
|
const test = iconBtn("bi-play-fill", `Test ${label}`, "btn small ghost", () => play(id, loudness(id)), `Test: play the ${label} tone`);
|
||||||
|
level.row.prepend(toggle);
|
||||||
|
level.row.append(test);
|
||||||
|
return {id, toggle, level};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function setThreshold(value) {
|
||||||
|
if (value !== settings.speech.threshold && await saveSettings({...settings, space: savedSpace(), speech: {threshold: value}})) {
|
||||||
|
toast("Speech threshold saved", [`The hub renders new speech only while the host CPU lies below ${value} %.`], true);
|
||||||
|
}
|
||||||
|
drawRadioSet();
|
||||||
|
}
|
||||||
|
|
||||||
|
function knobs() {
|
||||||
|
const tones = SOUND_KINDS.map(([id, label]) => toneKnob(id, label));
|
||||||
|
const squelch = knob("Squelch", "Squelch: scales every tone, the speech and the burst; 0 plays none", {min: 0, max: 1, step: 0.05}, (value) => keepRadio({squelch: value}));
|
||||||
|
const threshold = knob("Threshold", "Speech threshold: the hub renders new speech only while the host CPU lies below it; your passkey signs each change", {min: 30, max: 90, step: 10}, (value) => {
|
||||||
|
threshold.value.textContent = `${value} %`;
|
||||||
|
});
|
||||||
|
threshold.input.addEventListener("change", (event) => setThreshold(Number(event.target.value)));
|
||||||
|
const check = iconBtn("bi-broadcast", "Radio check", "btn small ghost", () => say({steps: [{kind: "speech", text: state.speech.sample}], voice: state.speech.operator, tone: "direct", cls: "newsletter", who: HUMAN, sent: true, line: "radio check"}), "Radio check: hear the operator's voice once");
|
||||||
|
check.disabled = !state.speech.tts;
|
||||||
|
radioDom.knobs = {tones, squelch, threshold, check};
|
||||||
|
return [...tones.map((tone) => tone.level.row), squelch.row, threshold.row, h("div", {class: "knob-row"}, check)];
|
||||||
|
}
|
||||||
|
|
||||||
|
function faceplate() {
|
||||||
|
const lamp = h("span", {class: "radio-lamp", role: "img", "data-lit": "false", "data-tx": "false"}, "RX");
|
||||||
|
const field = (name) => h("span", {class: "radio-field", "data-field": name});
|
||||||
|
const [cls, mode, cpu] = [field("class"), field("mode"), field("cpu")];
|
||||||
|
const caption = h("div", {class: "radio-caption", role: "status", "aria-live": "polite", hidden: true});
|
||||||
|
const modes = h("div", {class: "segmented radio-modes", role: "group", "aria-label": "Mode"}, RadioSet.MODES.map((id) => {
|
||||||
|
const [name, label, hint] = MODE_LABELS[id];
|
||||||
|
return h("button", {type: "button", "data-mode": id, "aria-pressed": "false", ...named(label, hint), on: {click: () => {
|
||||||
|
keepRadio({mode: id});
|
||||||
|
context();
|
||||||
|
}}}, icon(name), label);
|
||||||
|
}));
|
||||||
|
radioDom.face = {lamp, cls, mode, cpu, caption, modes};
|
||||||
|
const display = h("div", {class: "radio-display", role: "group", "aria-label": "Display"}, lamp, cls, mode, cpu);
|
||||||
|
return h("div", {class: "radio-face", role: "group", "aria-label": "Radio set"}, display, caption, modes, h("div", {class: "radio-knobs", role: "group", "aria-label": "Knobs"}, knobs()));
|
||||||
|
}
|
||||||
|
|
||||||
|
function radioFilters() {
|
||||||
|
const chip = (label, pressed, hint, run, locked) => h("button", {type: "button", class: "chip", "aria-pressed": String(pressed), disabled: locked, ...named(label, hint), on: {click: run}}, label);
|
||||||
|
const classes = SPEECH_CLASSES.map(([id, label]) => chip(label, radioSet.classes.includes(id), id === "mayday" ? "MAYDAY always sounds" : `Read ${label} aloud`, () => {
|
||||||
|
keepRadio({classes: radioSet.classes.includes(id) ? radioSet.classes.filter((name) => name !== id) : [...radioSet.classes, id]});
|
||||||
|
redrawFilters();
|
||||||
|
}, id === "mayday"));
|
||||||
|
const agents = Object.entries(people()).sort(([, a], [, b]) => a.now.localeCompare(b.now)).map(([key, entry]) => chip(entry.now, !radioSet.muted.includes(key), `Read ${entry.now} aloud`, () => {
|
||||||
|
keepRadio({muted: radioSet.muted.includes(key) ? radioSet.muted.filter((name) => name !== key) : [...radioSet.muted, key]});
|
||||||
|
redrawFilters();
|
||||||
|
}, false));
|
||||||
|
return h("div", {class: "radio-read", "aria-label": "Read aloud"},
|
||||||
|
h("p", {class: "muted"}, state.speech.tts ? "The radio sounds for these classes and agents, each speaker with its own voice; MAYDAY always sounds." : `The hub speaks no line: ${state.speech.tts_off}. The burst and the procedure words stand in.`),
|
||||||
|
h("div", {class: "chips", role: "group", "aria-label": "Classes"}, classes),
|
||||||
|
agents.length ? h("div", {class: "chips", role: "group", "aria-label": "Agents"}, agents) : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
function redrawFilters() {
|
||||||
|
if (spotDom && !spotDom.gearBox.hidden) {
|
||||||
|
spotDom.gearBox.replaceChildren(radioFilters());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function keepLog(change) {
|
||||||
|
keepRadio({log: {...radioSet.log, ...change}});
|
||||||
|
}
|
||||||
|
|
||||||
|
function chipSelect(name, values) {
|
||||||
|
const chosen = radioSet.log.chips[name];
|
||||||
|
const label = CHIP_LABELS[name];
|
||||||
|
const options = [...new Set([...values, ...(chosen ? [chosen] : [])])];
|
||||||
|
const select = h("select", {class: "field", "aria-label": label, title: `${label}: filter the log`, on: {change: (event) => keepLog({chips: {...radioSet.log.chips, [name]: event.target.value}})}},
|
||||||
|
h("option", {value: ""}, `${label}: any`), options.map((value) => h("option", {value, selected: value === chosen}, name === "class" ? CLASSES[value] || value : value)));
|
||||||
|
return h("label", {class: "chip-select", "data-chip": name, "data-active": String(Boolean(chosen))}, select);
|
||||||
|
}
|
||||||
|
|
||||||
|
function logLine(row) {
|
||||||
|
const message = row.sent ? row : RadioSet.ledgerOf(row, state.messages);
|
||||||
|
const sent = Boolean(message);
|
||||||
|
const [name, label] = sent ? ["bi-arrow-up-right", "Sent"] : ["bi-arrow-down-left", "Received"];
|
||||||
|
const first = row.text.split("\n").map((part) => part.trim()).find(Boolean) || "";
|
||||||
|
return h("li", {class: "radio-row", "data-dir": sent ? "sent" : "received", "data-class": row.class},
|
||||||
|
h("details", {}, h("summary", {},
|
||||||
|
when(row.ts),
|
||||||
|
h("span", {class: "radio-dir", role: "img", ...named(label)}, icon(name)),
|
||||||
|
klass(row.class),
|
||||||
|
h("span", {class: "radio-route"}, `${row.from} → ${targetsOf(row)}`),
|
||||||
|
h("span", {class: "radio-line", title: first}, first)),
|
||||||
|
h("pre", {class: "radio-full"}, row.text),
|
||||||
|
voiceReplay(message ? message.voice : null)));
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawLog() {
|
||||||
|
const dom = radioDom.log;
|
||||||
|
const entry = radioSet.log;
|
||||||
|
const sig = JSON.stringify([state.radio.length, state.radio.length ? state.radio[state.radio.length - 1].ts : 0, state.messages.length, entry, Math.floor(Date.now() / 60000)]);
|
||||||
|
if (sig === dom.sig) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
dom.sig = sig;
|
||||||
|
const counts = RadioSet.counts(state.radio, HUMAN, state.messages);
|
||||||
|
dom.tabs.replaceChildren(...RadioSet.TABS.map((tab) => {
|
||||||
|
const [name, label, hint] = TAB_LABELS[tab];
|
||||||
|
return h("button", {type: "button", role: "tab", "data-tab": tab, "aria-selected": String(tab === entry.tab), title: hint, on: {click: () => keepLog({tab})}},
|
||||||
|
icon(name), label, h("span", {class: "count", "aria-label": `${counts[tab]} rows`}, String(counts[tab])));
|
||||||
|
}));
|
||||||
|
const choices = RadioSet.choices(state.radio);
|
||||||
|
dom.chips.replaceChildren(...RadioSet.CHIPS.map((name) => chipSelect(name, choices[name])));
|
||||||
|
if (document.activeElement !== dom.search && dom.search.value !== entry.q) {
|
||||||
|
dom.search.value = entry.q;
|
||||||
|
}
|
||||||
|
const rows = RadioSet.logRows(state.radio, entry, HUMAN, state.messages);
|
||||||
|
dom.list.replaceChildren(...(rows.length ? rows.map(logLine) : [h("li", {class: "muted"}, state.radio.length ? "No radio row matches the tab, the chips and the search." : "No radio in the chosen time range yet.")]));
|
||||||
|
}
|
||||||
|
|
||||||
|
function radioLog() {
|
||||||
|
const tabs = h("div", {class: "radio-tabs", role: "tablist", "aria-label": "Log"});
|
||||||
|
const chips = h("div", {class: "radio-chips", role: "group", "aria-label": "Log filters"});
|
||||||
|
const search = h("input", {type: "search", class: "field", placeholder: "Search the log …", maxlength: 200, ...named("Search the log"), on: {input: (event) => keepLog({q: event.target.value})}});
|
||||||
|
const clear = iconBtn("bi-x-circle", "Clear filters", "btn small ghost", () => keepRadio({log: RadioSet.cleared(radioSet.log)}), "Clear filters: every chip and the search");
|
||||||
|
const list = h("ul", {class: "radio-log", "aria-label": "Radio log"});
|
||||||
|
radioDom.log = {tabs, chips, search, list, sig: ""};
|
||||||
|
return h("section", {class: "radio-logbox", "aria-label": "Radio log"}, tabs, h("div", {class: "radio-filters"}, chips, search, clear), list);
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawRadioSet() {
|
||||||
|
if (!radioDom) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (const button of radioDom.face.modes.querySelectorAll("button[data-mode]")) {
|
||||||
|
button.setAttribute("aria-pressed", String(button.dataset.mode === radioSet.mode));
|
||||||
|
}
|
||||||
|
const {tones, squelch, threshold} = radioDom.knobs;
|
||||||
|
for (const {id, toggle, level} of tones) {
|
||||||
|
const sound = radioSet.sounds[id];
|
||||||
|
toggle.setAttribute("aria-pressed", String(sound.on));
|
||||||
|
toggle.replaceChildren(icon(sound.on ? "bi-bell" : "bi-bell-slash"));
|
||||||
|
toggle.title = `${toggle.getAttribute("aria-label")}: ${sound.on ? "on, a press mutes it" : "off, a press turns it on"}`;
|
||||||
|
level.input.value = String(sound.volume);
|
||||||
|
level.value.textContent = sound.volume.toFixed(2);
|
||||||
|
}
|
||||||
|
squelch.input.value = String(radioSet.squelch);
|
||||||
|
squelch.value.textContent = radioSet.squelch.toFixed(2);
|
||||||
|
threshold.input.value = String(settings.speech.threshold);
|
||||||
|
threshold.value.textContent = `${settings.speech.threshold} %`;
|
||||||
|
drawDisplay();
|
||||||
|
drawLog();
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawRadio() {
|
function drawRadio() {
|
||||||
if (!radioDom) {
|
if (!radioDom) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const sent = JSON.stringify(state.messages.slice(-5));
|
drawRadioSet();
|
||||||
if (sent !== radioDom.sent) {
|
|
||||||
radioDom.sent = sent;
|
|
||||||
radioDom.outbox.replaceChildren(sentRows());
|
|
||||||
}
|
|
||||||
drawStations();
|
drawStations();
|
||||||
}
|
}
|
||||||
|
|
||||||
function radioSide() {
|
function radioSide() {
|
||||||
const caption = h("div", {class: "radio-caption", role: "status", "aria-live": "polite", hidden: true});
|
|
||||||
const stations = h("ul", {class: "stations", "aria-label": "Stations"});
|
const stations = h("ul", {class: "stations", "aria-label": "Stations"});
|
||||||
const outbox = h("div", {});
|
radioDom = {stations, stationsSig: ""};
|
||||||
radioDom = {caption, stations, outbox, sent: "", stationsSig: ""};
|
const face = faceplate();
|
||||||
return h("div", {class: "radio-side"}, caption,
|
const log = radioLog();
|
||||||
h("section", {"aria-label": "Recently sent"}, h("h3", {class: "group-title"}, "Recently sent"), outbox),
|
const station = h("section", {class: "radio-station", "aria-label": "Internet radio"}, h("div", {class: "card-head"}, h("h3", {class: "group-title"}, "Internet radio"), btn("Edit stations", "btn small ghost", stationsSheet)), stations);
|
||||||
h("section", {"aria-label": "Internet radio"}, h("div", {class: "card-head"}, h("h3", {class: "group-title"}, "Internet radio"), btn("Edit stations", "btn small ghost", settingsSheet)), stations));
|
window.setTimeout(drawRadio, 0);
|
||||||
|
return {face, log, station};
|
||||||
}
|
}
|
||||||
@@ -86,52 +86,23 @@
|
|||||||
return section;
|
return section;
|
||||||
}
|
}
|
||||||
|
|
||||||
function radioSettings(close) {
|
function stationsSheet() {
|
||||||
const tones = SOUND_KINDS.map(([id, label]) => {
|
const list = h("div", {class: "mini"});
|
||||||
const on = h("input", {type: "checkbox", checked: settings.sounds[id].on, "aria-label": `${label} on`});
|
const rows = [];
|
||||||
const level = h("input", {type: "range", min: 0, max: 1, step: 0.05, value: settings.sounds[id].volume, "aria-label": `${label} volume`});
|
settings.streams.forEach((stream) => streamRow(stream, rows, list));
|
||||||
const loudest = () => Math.max(...tones.map((tone) => Number(tone.level.value)), settings.music.volume);
|
const form = h("form", {class: "settings", "aria-label": "Stations"},
|
||||||
return {id, on, level, row: h("div", {class: "sound-row"}, h("strong", {}, label), h("label", {class: "check"}, on, "on"), level, btn("Test", "btn small ghost", () => play(id, id === "mayday" ? loudest() : Number(level.value))))};
|
h("section", {}, h("h3", {}, "Internet radio"), h("p", {class: "muted"}, "Internet radio at fixed times with a 3 s fade in and out; the music pauses meanwhile. A new station plays around the clock until you change its times; in the radio panel you start and stop it by hand. The page may load sound only from these addresses; your passkey signs a new address. If a signed station redirects, the server resolves the redirect when you save a new station or a changed address and releases its target; the note after saving names it."),
|
||||||
});
|
list, h("div", {}, btn("+ Station", "btn small soft", () => streamRow({name: "", url: "", from: "00:00", to: "24:00", days: [0, 1, 2, 3, 4, 5, 6], on: true, via: []}, rows, list)))),
|
||||||
const squelch = h("input", {type: "range", min: 0, max: 1, step: 0.05, value: settings.squelch, "aria-label": "Squelch"});
|
h("div", {class: "sheet-foot"}, h("span", {class: "spacer"}), btn("Cancel", "btn ghost", closeSheet), h("button", {type: "submit", class: "btn primary"}, "Save")));
|
||||||
const speechOn = h("input", {type: "checkbox", checked: settings.speech.on, disabled: !state.speech.tts});
|
|
||||||
const spoken = SPEECH_CLASSES.map(([id, label]) => ({id, label, box: h("input", {type: "checkbox", checked: id === "mayday" || settings.speech.classes.includes(id), disabled: id === "mayday", title: id === "mayday" ? "MAYDAY is always read aloud" : null})}));
|
|
||||||
const stations = Object.entries(people()).sort(([, a], [, b]) => a.now.localeCompare(b.now))
|
|
||||||
.map(([key, entry]) => ({key, label: entry.now, box: h("input", {type: "checkbox", checked: !settings.speech.muted.includes(key), "aria-label": `Read ${entry.now} aloud`})}));
|
|
||||||
const absent = settings.speech.muted.filter((key) => !people()[key]);
|
|
||||||
const sample = btn("Test", "btn small ghost", () => say({text: state.speech.sample, voice: state.speech.operator, tone: "direct", cls: "newsletter", who: HUMAN, line: "radio check"}));
|
|
||||||
sample.disabled = !state.speech.tts;
|
|
||||||
const form = h("form", {class: "settings radio-settings", "aria-label": "Radio settings"},
|
|
||||||
h("section", {}, h("h3", {}, "Tones"), h("p", {class: "muted"}, "MAYDAY and PAN-PAN are always loudest. With several new events only the most urgent one plays."),
|
|
||||||
tones.map((tone) => tone.row), h("label", {}, "Squelch: scales every tone, 0 plays none", squelch)),
|
|
||||||
h("section", {}, h("h3", {}, "Read radio aloud"),
|
|
||||||
h("p", {class: "muted"}, state.speech.tts
|
|
||||||
? "New radio is read aloud in maritime radio style (SMCP), one message after another, each speaker with its own voice; music and internet radio go quieter meanwhile. The volume follows the tones above; MAYDAY is always spoken, and loudest."
|
|
||||||
: `The radio stays silent: ${state.speech.tts_off}.`),
|
|
||||||
h("div", {class: "days"}, h("label", {class: "check"}, speechOn, "Read radio aloud"), sample),
|
|
||||||
h("div", {class: "days", role: "group", "aria-label": "Classes"}, spoken.map((entry) => h("label", {class: "check"}, entry.box, entry.label))),
|
|
||||||
stations.length ? h("div", {class: "days", role: "group", "aria-label": "Speakers"}, stations.map((entry) => h("label", {class: "check"}, entry.box, entry.label))) : null),
|
|
||||||
h("div", {class: "actions"}, h("span", {class: "spacer"}), btn("Cancel", "btn ghost", close), h("button", {type: "submit", class: "btn primary"}, "Save")));
|
|
||||||
form.addEventListener("submit", async (event) => {
|
form.addEventListener("submit", async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
context();
|
context();
|
||||||
const next = {
|
if (await saveSettings({...settings, space: savedSpace(), streams: rows.map((entry) => entry.read())})) {
|
||||||
...settings,
|
closeSheet();
|
||||||
space: savedSpace(),
|
toast("Stations saved", [], true);
|
||||||
sounds: Object.fromEntries(tones.map((tone) => [tone.id, {on: tone.on.checked, volume: Number(tone.level.value)}])),
|
|
||||||
squelch: Number(squelch.value),
|
|
||||||
speech: {
|
|
||||||
on: speechOn.checked,
|
|
||||||
classes: spoken.filter((entry) => entry.box.checked).map((entry) => entry.id),
|
|
||||||
muted: [...absent, ...stations.filter((entry) => !entry.box.checked).map((entry) => entry.key)],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
if (await saveSettings(next)) {
|
|
||||||
close();
|
|
||||||
toast("Radio settings saved", [], true);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return form;
|
openSheet("Stations", [form], false);
|
||||||
}
|
}
|
||||||
|
|
||||||
function settingsSheet() {
|
function settingsSheet() {
|
||||||
@@ -155,9 +126,6 @@
|
|||||||
const musicOn = h("input", {type: "checkbox", checked: settings.music.on});
|
const musicOn = h("input", {type: "checkbox", checked: settings.music.on});
|
||||||
const volume = h("input", {type: "range", min: 0, max: 1, step: 0.05, value: settings.music.volume, "aria-label": "Overall volume"});
|
const volume = h("input", {type: "range", min: 0, max: 1, step: 0.05, value: settings.music.volume, "aria-label": "Overall volume"});
|
||||||
const notify = h("input", {type: "checkbox", checked: settings.notify});
|
const notify = h("input", {type: "checkbox", checked: settings.notify});
|
||||||
const list = h("div", {class: "mini"});
|
|
||||||
const rows = [];
|
|
||||||
settings.streams.forEach((stream) => streamRow(stream, rows, list));
|
|
||||||
const form = h("form", {class: "settings"},
|
const form = h("form", {class: "settings"},
|
||||||
ambience(),
|
ambience(),
|
||||||
h("section", {}, h("h3", {}, "Location"), h("p", {class: "muted"}, "Sun, moon and stars stand where you see them from here, looking south; under 🔭 Sky (key h) you turn the view. Decimal degrees, north and east positive."),
|
h("section", {}, h("h3", {}, "Location"), h("p", {class: "muted"}, "Sun, moon and stars stand where you see them from here, looking south; under 🔭 Sky (key h) you turn the view. Decimal degrees, north and east positive."),
|
||||||
@@ -165,10 +133,8 @@
|
|||||||
h("section", {}, h("h3", {}, "Appearance"), h("label", {}, "Colors", theme), custom, verdict),
|
h("section", {}, h("h3", {}, "Appearance"), h("label", {}, "Colors", theme), custom, verdict),
|
||||||
h("section", {}, h("h3", {}, "Music"), h("p", {class: "muted"}, "Generative ambient music, made offline: calm with an empty inbox, a little tenser when something waits, quieter under messages."),
|
h("section", {}, h("h3", {}, "Music"), h("p", {class: "muted"}, "Generative ambient music, made offline: calm with an empty inbox, a little tenser when something waits, quieter under messages."),
|
||||||
h("div", {class: "grid"}, h("label", {class: "check"}, musicOn, "Music on"), h("label", {}, "Overall volume (music and radio)", volume))),
|
h("div", {class: "grid"}, h("label", {class: "check"}, musicOn, "Music on"), h("label", {}, "Overall volume (music and radio)", volume))),
|
||||||
h("section", {}, h("h3", {}, "Notifications"), h("p", {class: "muted"}, "Tones, squelch and reading radio aloud sit behind the gear of the radio spot in the Agents toolbar."),
|
h("section", {}, h("h3", {}, "Notifications"), h("p", {class: "muted"}, "Mode, tones, squelch, the speech threshold, reading aloud and the stations sit in the radio panel behind the radio button in the header."),
|
||||||
h("label", {class: "check"}, notify, "Show desktop notifications when the browser allows them")),
|
h("label", {class: "check"}, notify, "Show desktop notifications when the browser allows them")),
|
||||||
h("section", {}, h("h3", {}, "Internet radio"), h("p", {class: "muted"}, "Internet radio at fixed times with a 3 s fade in and out; the music pauses meanwhile. A new station plays around the clock until you change its times; under Stream → Radio you start and stop it by hand. The page may load sound only from these addresses; your passkey signs a new address. If a signed station redirects, the server resolves the redirect when you save a new station or a changed address and releases its target; the note after saving names it."),
|
|
||||||
list, h("div", {}, btn("+ Station", "btn small soft", () => streamRow({name: "", url: "", from: "00:00", to: "24:00", days: [0, 1, 2, 3, 4, 5, 6], on: true, via: []}, rows, list)))),
|
|
||||||
passkeySection(),
|
passkeySection(),
|
||||||
h("div", {class: "sheet-foot"}, h("span", {class: "spacer"}), btn("Cancel", "btn ghost", closeSheet), h("button", {type: "submit", class: "btn primary"}, "Save")));
|
h("div", {class: "sheet-foot"}, h("span", {class: "spacer"}), btn("Cancel", "btn ghost", closeSheet), h("button", {type: "submit", class: "btn primary"}, "Save")));
|
||||||
form.addEventListener("submit", async (event) => {
|
form.addEventListener("submit", async (event) => {
|
||||||
@@ -180,10 +146,8 @@
|
|||||||
theme: theme.value,
|
theme: theme.value,
|
||||||
colors: Object.fromEntries(COLOR_FIELDS.map(([id]) => [id, colors[id].value])),
|
colors: Object.fromEntries(COLOR_FIELDS.map(([id]) => [id, colors[id].value])),
|
||||||
music: {on: musicOn.checked, volume: Number(volume.value)},
|
music: {on: musicOn.checked, volume: Number(volume.value)},
|
||||||
sounds: settings.sounds,
|
|
||||||
squelch: settings.squelch,
|
|
||||||
notify: notify.checked,
|
notify: notify.checked,
|
||||||
streams: rows.map((entry) => entry.read()),
|
streams: settings.streams,
|
||||||
speech: settings.speech,
|
speech: settings.speech,
|
||||||
space: savedSpace(),
|
space: savedSpace(),
|
||||||
estimation: settings.estimation,
|
estimation: settings.estimation,
|
||||||
|
|||||||
@@ -1,5 +1,37 @@
|
|||||||
|
function tones() {
|
||||||
|
return {sounds: radioSet.sounds, squelch: radioSet.squelch, music: settings.music};
|
||||||
|
}
|
||||||
|
|
||||||
function loudness(kind) {
|
function loudness(kind) {
|
||||||
return Voice.loudness(kind, settings);
|
return Voice.loudness(kind, tones());
|
||||||
|
}
|
||||||
|
|
||||||
|
function keepRadio(change) {
|
||||||
|
const before = radioSet.mode;
|
||||||
|
radioSet = RadioSet.valid({...radioSet, ...change});
|
||||||
|
keep(RadioSet.KEY, radioSet);
|
||||||
|
hush(before !== radioSet.mode);
|
||||||
|
drawRadioSet();
|
||||||
|
}
|
||||||
|
|
||||||
|
function hush(switched) {
|
||||||
|
const {item, source} = speech;
|
||||||
|
if (source && item && item.row && !allows(rendered(item, item.at), item.playing)) {
|
||||||
|
item.cut = true;
|
||||||
|
source.stop();
|
||||||
|
}
|
||||||
|
if (switched && radioSet.mode === "silence") {
|
||||||
|
silenceBeeps();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object|undefined} step the step the radio set plans now at the index that plays.
|
||||||
|
* @param {string} kind word, speech or burst: what plays or is about to play there.
|
||||||
|
* @returns {boolean} whether that sound may play: the planned kind, or the burst standing in for a planned speech line.
|
||||||
|
*/
|
||||||
|
function allows(step, kind) {
|
||||||
|
return Boolean(step) && (step.kind === kind || (kind === "burst" && step.kind === "speech"));
|
||||||
}
|
}
|
||||||
|
|
||||||
function speechFailed(lines) {
|
function speechFailed(lines) {
|
||||||
@@ -40,18 +72,142 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sounded(item, source, seconds) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
let timer = 0;
|
||||||
|
const ended = () => {
|
||||||
|
window.clearTimeout(timer);
|
||||||
|
if (speech.source === source) {
|
||||||
|
Object.assign(speech, {current: null, source: null});
|
||||||
|
drawSpeaker();
|
||||||
|
}
|
||||||
|
resolve();
|
||||||
|
};
|
||||||
|
source.addEventListener("ended", ended, {once: true});
|
||||||
|
timer = window.setTimeout(() => {
|
||||||
|
source.stop();
|
||||||
|
ended();
|
||||||
|
}, (seconds + SOUND_SLACK) * 1000);
|
||||||
|
duck(seconds);
|
||||||
|
Object.assign(item, {started: performance.now(), duration: seconds});
|
||||||
|
Object.assign(speech, {current: item, source});
|
||||||
|
drawSpeaker();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function burst(sound, item) {
|
||||||
|
const shape = RadioSet.burst(loudness(item.tone));
|
||||||
|
if (shape.gain <= 0) {
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
const frames = Math.ceil(sound.sampleRate * shape.seconds);
|
||||||
|
const buffer = sound.createBuffer(1, frames, sound.sampleRate);
|
||||||
|
const data = buffer.getChannelData(0);
|
||||||
|
for (let index = 0; index < frames; index += 1) {
|
||||||
|
data[index] = Math.random() * 2 - 1;
|
||||||
|
}
|
||||||
|
const source = sound.createBufferSource();
|
||||||
|
const filter = sound.createBiquadFilter();
|
||||||
|
const gain = sound.createGain();
|
||||||
|
const start = sound.currentTime;
|
||||||
|
filter.type = "bandpass";
|
||||||
|
filter.frequency.value = shape.frequency;
|
||||||
|
filter.Q.value = shape.q;
|
||||||
|
gain.gain.setValueAtTime(shape.gain, start);
|
||||||
|
gain.gain.exponentialRampToValueAtTime(0.0001, start + shape.seconds);
|
||||||
|
source.buffer = buffer;
|
||||||
|
source.connect(filter).connect(gain).connect(sound.destination);
|
||||||
|
const ended = sounded(item, source, shape.seconds);
|
||||||
|
source.start(start);
|
||||||
|
source.stop(start + shape.seconds);
|
||||||
|
return ended;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function spoken(sound, item, step, done) {
|
||||||
|
speech.loading = item;
|
||||||
|
drawBubbles();
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/tts", {method: "POST", headers: {"X-Kvb-Orchestrator": "1", "Content-Type": "application/json"}, body: JSON.stringify({text: step.text, voice: item.voice}), cache: "no-store"});
|
||||||
|
const held = response.status === 503 && (response.headers.get("Content-Type") || "").startsWith("application/json");
|
||||||
|
if (!response.ok) {
|
||||||
|
return held ? "held" : `${response.status} ${(await response.text()).trim()}`;
|
||||||
|
}
|
||||||
|
const buffer = await sound.decodeAudioData(await response.arrayBuffer());
|
||||||
|
if (speech.item !== item) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (!allows(rendered(item, done), step.kind)) {
|
||||||
|
return "dropped";
|
||||||
|
}
|
||||||
|
const source = sound.createBufferSource();
|
||||||
|
const gain = sound.createGain();
|
||||||
|
gain.gain.value = loudness(item.tone);
|
||||||
|
source.buffer = buffer;
|
||||||
|
source.connect(gain).connect(sound.destination);
|
||||||
|
const ended = sounded(item, source, buffer.duration);
|
||||||
|
source.start();
|
||||||
|
await ended;
|
||||||
|
return speech.item === item && item.cut ? "dropped" : "";
|
||||||
|
} catch (error) {
|
||||||
|
return String(error.message || error);
|
||||||
|
} finally {
|
||||||
|
if (speech.loading === item) {
|
||||||
|
speech.loading = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function liveSpeech() {
|
||||||
|
return {tts: state.speech.tts, open: state.speech.gate.open};
|
||||||
|
}
|
||||||
|
|
||||||
|
function stepOf(item, done, live) {
|
||||||
|
return (item.row ? RadioSet.plan(item.row, radioSet, live || liveSpeech()) : item.steps)[done];
|
||||||
|
}
|
||||||
|
|
||||||
|
function rendered(item, done) {
|
||||||
|
return item.row ? stepOf(item, done, {...liveSpeech(), open: true}) : item.steps[done];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function transmit(item, audio) {
|
||||||
|
let voiced = false;
|
||||||
|
for (let done = 0; ; done += 1) {
|
||||||
|
const step = stepOf(item, done);
|
||||||
|
if (!step || speech.item !== item) {
|
||||||
|
return voiced;
|
||||||
|
}
|
||||||
|
Object.assign(item, {at: done, playing: step.kind, cut: false});
|
||||||
|
if (step.kind === "burst") {
|
||||||
|
await burst(audio, item);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const failed = await spoken(audio, item, step, done);
|
||||||
|
if (failed === "dropped") {
|
||||||
|
done -= 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
voiced = voiced || (!failed && step.kind === "speech");
|
||||||
|
if (failed && failed !== "held") {
|
||||||
|
speechFailed([failed]);
|
||||||
|
}
|
||||||
|
if (failed && step.kind === "speech" && speech.item === item && allows(stepOf(item, done), "burst")) {
|
||||||
|
item.playing = "burst";
|
||||||
|
await burst(audio, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function pump() {
|
async function pump() {
|
||||||
if (speech.busy || !speech.pending.length) {
|
if (speech.busy || !speech.pending.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const [item, ...rest] = speech.pending;
|
const [item, ...rest] = speech.pending;
|
||||||
const sound = item.silent ? null : context();
|
const audio = item.silent ? null : context();
|
||||||
if (!item.silent && !sound) {
|
if (!item.silent && !audio) {
|
||||||
speech.pending = [];
|
speech.pending = [];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
speech.pending = rest;
|
Object.assign(speech, {pending: rest, busy: true, item});
|
||||||
speech.busy = true;
|
|
||||||
const next = () => {
|
const next = () => {
|
||||||
speech.busy = false;
|
speech.busy = false;
|
||||||
pump();
|
pump();
|
||||||
@@ -60,67 +216,39 @@
|
|||||||
readSilently(item, next);
|
readSilently(item, next);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
speech.loading = item;
|
const voiced = await transmit(item, audio);
|
||||||
drawBubbles();
|
if (speech.item !== item) {
|
||||||
try {
|
|
||||||
const response = await fetch("/api/tts", {method: "POST", headers: {"X-Kvb-Orchestrator": "1", "Content-Type": "application/json"}, body: JSON.stringify({text: item.text, voice: item.voice}), cache: "no-store"});
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`${response.status} ${(await response.text()).trim()}`);
|
|
||||||
}
|
|
||||||
const buffer = await sound.decodeAudioData(await response.arrayBuffer());
|
|
||||||
if (speech.loading !== item) {
|
|
||||||
next();
|
next();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const source = sound.createBufferSource();
|
Object.assign(speech, {current: null, source: null, item: null});
|
||||||
const gain = sound.createGain();
|
|
||||||
gain.gain.value = loudness(item.tone);
|
|
||||||
source.buffer = buffer;
|
|
||||||
source.connect(gain).connect(sound.destination);
|
|
||||||
source.addEventListener("ended", () => {
|
|
||||||
Object.assign(speech, {current: null, source: null});
|
|
||||||
drawSpeaker();
|
drawSpeaker();
|
||||||
next();
|
if (!voiced && settings.bubbles && item.words) {
|
||||||
}, {once: true});
|
|
||||||
duck(buffer.duration);
|
|
||||||
Object.assign(item, {started: performance.now(), duration: buffer.duration});
|
|
||||||
Object.assign(speech, {current: item, source, loading: null});
|
|
||||||
drawSpeaker();
|
|
||||||
source.start();
|
|
||||||
} catch (error) {
|
|
||||||
const kept = speech.loading === item;
|
|
||||||
speech.loading = null;
|
|
||||||
speechFailed([String(error.message || error)]);
|
|
||||||
if (kept && settings.bubbles && item.words) {
|
|
||||||
readSilently(item, next);
|
readSilently(item, next);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function cancelSpeech() {
|
function cancelSpeech() {
|
||||||
if (speech.timer) {
|
if (speech.timer) {
|
||||||
endSilent();
|
endSilent();
|
||||||
}
|
}
|
||||||
Object.assign(speech, {pending: [], current: null, loading: null});
|
const playing = speech.source;
|
||||||
if (speech.source) {
|
Object.assign(speech, {pending: [], current: null, loading: null, source: null, item: null});
|
||||||
speech.source.stop();
|
if (playing) {
|
||||||
|
playing.stop();
|
||||||
}
|
}
|
||||||
clearBubbles();
|
clearBubbles();
|
||||||
drawSpeaker();
|
drawSpeaker();
|
||||||
}
|
}
|
||||||
|
|
||||||
function speakRows(rows) {
|
function speakRows(rows) {
|
||||||
const voiced = settings.speech.on && state.speech.tts;
|
|
||||||
if (!voiced && !settings.bubbles) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (const row of rows.filter((entry) => entry.ts >= state.now - SPEECH_FRESH)) {
|
for (const row of rows.filter((entry) => entry.ts >= state.now - SPEECH_FRESH)) {
|
||||||
const audible = voiced && Voice.speakable(row, settings.speech);
|
const steps = RadioSet.plan(row, radioSet, liveSpeech());
|
||||||
if (audible || (settings.bubbles && Bubbles.picked(row, settings.speech))) {
|
if (steps.length || (settings.bubbles && Bubbles.picked(row, radioSet))) {
|
||||||
const line = row.text.split("\n").map((part) => part.trim()).find(Boolean) || "";
|
const line = row.text.split("\n").map((part) => part.trim()).find(Boolean) || "";
|
||||||
say({text: row.speech, voice: row.voice, tone: Voice.toneOf(row, HUMAN), cls: row.class, who: row.source, line: short(line), words: Bubbles.words(row.text), silent: !audible});
|
say({row, voice: row.voice, tone: Voice.toneOf(row, HUMAN), cls: row.class, who: row.source, sent: Boolean(RadioSet.ledgerOf(row, state.messages)), line: short(line), words: Bubbles.words(row.text), silent: !steps.length});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,8 @@
|
|||||||
const sky = {canvas: document.getElementById("sky"), stars: Ambience.stars(320, 7), mode: "", frame: 0};
|
const sky = {canvas: document.getElementById("sky"), stars: Ambience.stars(320, 7), mode: "", frame: 0};
|
||||||
const music = {gain: null, filter: null, drone: [], timer: 0, tension: 0, ducked: 0};
|
const music = {gain: null, filter: null, drone: [], timer: 0, tension: 0, ducked: 0};
|
||||||
const radio = {element: null, url: "", level: 0, timer: 0, playing: false, waiting: false, index: -1, pick: null, sig: "", state: "", short: "", note: "", told: null};
|
const radio = {element: null, url: "", level: 0, timer: 0, playing: false, waiting: false, index: -1, pick: null, sig: "", state: "", short: "", note: "", told: null};
|
||||||
const speech = {pending: [], busy: false, warned: 0, current: null, source: null, loading: null, timer: 0};
|
const speech = {pending: [], busy: false, warned: 0, current: null, source: null, loading: null, timer: 0, item: null};
|
||||||
|
let radioSet = RadioSet.valid(stored(RadioSet.KEY, null));
|
||||||
const mic = {recorder: null, held: false, busy: false, timer: 0};
|
const mic = {recorder: null, held: false, busy: false, timer: 0};
|
||||||
const space = {
|
const space = {
|
||||||
views: {graph: {...state.settings.space.graph}, sky: {...state.settings.space.sky}}, glides: {graph: null, sky: null}, edits: 0, saved: 0, timer: 0, look: false, compass: null,
|
views: {graph: {...state.settings.space.graph}, sky: {...state.settings.space.sky}}, glides: {graph: null, sky: null}, edits: 0, saved: 0, timer: 0, look: false, compass: null,
|
||||||
|
|||||||
@@ -0,0 +1,160 @@
|
|||||||
|
const RadioSet = (() => {
|
||||||
|
"use strict";
|
||||||
|
const KEY = "kvb-orchestrator.radio";
|
||||||
|
const MODES = ["auto", "quiet", "silence"];
|
||||||
|
const TONES = ["mayday", "review", "direct", "radio"];
|
||||||
|
const CLASSES = ["post", "call", "newsletter", "broadcast", "mayday", "pan-pan", "security"];
|
||||||
|
const TO_ALL = new Set(["newsletter", "broadcast", "mayday", "pan-pan", "security"]);
|
||||||
|
const TABS = ["sent", "received", "all"];
|
||||||
|
const CHIPS = ["class", "from", "to", "agent"];
|
||||||
|
const TEXT_LIMIT = 200;
|
||||||
|
const MUTED_LIMIT = 200;
|
||||||
|
const BURST = {seconds: 0.6, frequency: 1800, q: 0.9};
|
||||||
|
const MARK = /\[kvb-orchestrator msg ([0-9a-f]{16})\]/;
|
||||||
|
const DEFAULTS = {
|
||||||
|
mode: "auto",
|
||||||
|
sounds: {mayday: {on: true, volume: 0.9}, review: {on: true, volume: 0.6}, direct: {on: true, volume: 0.5}, radio: {on: false, volume: 0.25}},
|
||||||
|
squelch: 1,
|
||||||
|
classes: ["mayday", "pan-pan", "security"],
|
||||||
|
muted: [],
|
||||||
|
log: {tab: "received", chips: {class: "", from: "", to: "", agent: ""}, q: ""},
|
||||||
|
};
|
||||||
|
|
||||||
|
const level = (value, fallback) => (typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 1 ? value : fallback);
|
||||||
|
const short = (value) => typeof value === "string" && value.length <= TEXT_LIMIT;
|
||||||
|
const object = (value) => (value && typeof value === "object" && !Array.isArray(value) ? value : {});
|
||||||
|
|
||||||
|
function tone(raw, fallback) {
|
||||||
|
const entry = object(raw);
|
||||||
|
return {on: typeof entry.on === "boolean" ? entry.on : fallback.on, volume: level(entry.volume, fallback.volume)};
|
||||||
|
}
|
||||||
|
|
||||||
|
function log(raw) {
|
||||||
|
const entry = object(raw);
|
||||||
|
const chips = object(entry.chips);
|
||||||
|
return {
|
||||||
|
tab: TABS.includes(entry.tab) ? entry.tab : DEFAULTS.log.tab,
|
||||||
|
chips: Object.fromEntries(CHIPS.map((name) => [name, short(chips[name]) ? chips[name] : ""])),
|
||||||
|
q: short(entry.q) ? entry.q : "",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object|null} stored the radio set this browser kept, or null.
|
||||||
|
* @returns {object} the radio set with every unknown or broken field at its default; MAYDAY always stays read aloud.
|
||||||
|
*/
|
||||||
|
function valid(stored) {
|
||||||
|
const raw = object(stored);
|
||||||
|
const sounds = object(raw.sounds);
|
||||||
|
const classes = Array.isArray(raw.classes) ? raw.classes : DEFAULTS.classes;
|
||||||
|
const muted = Array.isArray(raw.muted) ? raw.muted.filter((key) => typeof key === "string" && key && key.length <= TEXT_LIMIT) : [];
|
||||||
|
return {
|
||||||
|
mode: MODES.includes(raw.mode) ? raw.mode : DEFAULTS.mode,
|
||||||
|
sounds: Object.fromEntries(TONES.map((name) => [name, tone(sounds[name], DEFAULTS.sounds[name])])),
|
||||||
|
squelch: level(raw.squelch, DEFAULTS.squelch),
|
||||||
|
classes: CLASSES.filter((name) => name === "mayday" || classes.includes(name)),
|
||||||
|
muted: [...new Set(muted)].sort().slice(0, MUTED_LIMIT),
|
||||||
|
log: log(raw.log),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function picked(row, set) {
|
||||||
|
return row.class === "mayday" || (set.classes.includes(row.class) && !set.muted.includes(row.source));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object} row a radio row of the state with word, rest and voice.
|
||||||
|
* @param {object} set the radio set.
|
||||||
|
* @param {Object<string, boolean>} live tts and open: whether the hub speaks at all and whether its CPU gate is open.
|
||||||
|
* @returns {Array<{kind: string, text: (string|undefined)}>} what plays, in order: the procedure word line in every mode, then the spoken rest in Auto below the threshold, else the noise burst, nothing more in Silence.
|
||||||
|
*/
|
||||||
|
function plan(row, set, live) {
|
||||||
|
if (!picked(row, set)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
const word = live.tts && row.voice && row.word ? [{kind: "word", text: row.word}] : [];
|
||||||
|
if (set.mode === "silence") {
|
||||||
|
return word;
|
||||||
|
}
|
||||||
|
const spoken = set.mode === "auto" && live.tts && live.open && row.voice && row.rest;
|
||||||
|
return [...word, spoken ? {kind: "speech", text: row.rest} : {kind: "burst"}];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} loudness the tone's volume times the squelch.
|
||||||
|
* @returns {Object<string, number>} seconds, frequency, q and gain of the band-passed white noise of the squelch tail.
|
||||||
|
*/
|
||||||
|
function burst(loudness) {
|
||||||
|
return {...BURST, gain: loudness};
|
||||||
|
}
|
||||||
|
|
||||||
|
function toAll(row) {
|
||||||
|
return TO_ALL.has(row.class) || !row.to.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object} row a radio row.
|
||||||
|
* @param {Array<object>} messages the hub's message ledger of the state.
|
||||||
|
* @returns {object|null} the ledger message the row relays; null for a row no message of this hub sent, whatever its sender name.
|
||||||
|
*/
|
||||||
|
function ledgerOf(row, messages) {
|
||||||
|
const found = MARK.exec(row.text);
|
||||||
|
return found ? messages.find((message) => message.id === found[1]) || null : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sentRows(messages, human) {
|
||||||
|
return messages.map((message) => ({ts: message.created_ts, from: human, to: message.to === "*" ? [] : [message.to], class: message.class || "post", text: message.text, voice: message.voice, sent: true}));
|
||||||
|
}
|
||||||
|
|
||||||
|
function tabRows(rows, tab, human, messages) {
|
||||||
|
if (tab === "sent") {
|
||||||
|
return sentRows(messages, human);
|
||||||
|
}
|
||||||
|
return rows.filter((row) => tab === "all" || toAll(row) || row.to.includes(human));
|
||||||
|
}
|
||||||
|
|
||||||
|
function matches(row, entry) {
|
||||||
|
const {chips, q} = entry;
|
||||||
|
const words = q.trim().toLowerCase();
|
||||||
|
return (!chips.class || row.class === chips.class)
|
||||||
|
&& (!chips.from || row.from === chips.from)
|
||||||
|
&& (!chips.to || row.to.includes(chips.to))
|
||||||
|
&& (!chips.agent || row.from === chips.agent || row.to.includes(chips.agent))
|
||||||
|
&& (!words || [row.text, row.from, ...row.to].join("\n").toLowerCase().includes(words));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Array<object>} rows the radio rows of the state.
|
||||||
|
* @param {object} entry the log part of the radio set: tab, chips and q.
|
||||||
|
* @param {string} human the operator's name.
|
||||||
|
* @param {Array<object>} messages the hub's message ledger; Sent lists it, not radio rows that only claim the operator's name.
|
||||||
|
* @returns {Array<object>} the rows of the tab that every chip and the search match, newest first.
|
||||||
|
*/
|
||||||
|
function logRows(rows, entry, human, messages) {
|
||||||
|
return tabRows(rows, entry.tab, human, messages).filter((row) => matches(row, entry)).sort((a, b) => b.ts - a.ts);
|
||||||
|
}
|
||||||
|
|
||||||
|
function counts(rows, human, messages) {
|
||||||
|
return Object.fromEntries(TABS.map((tab) => [tab, tabRows(rows, tab, human, messages).length]));
|
||||||
|
}
|
||||||
|
|
||||||
|
function choices(rows) {
|
||||||
|
const sorted = (values) => [...new Set(values.filter(Boolean))].sort((a, b) => a.localeCompare(b));
|
||||||
|
const names = rows.flatMap((row) => [row.from, ...row.to]);
|
||||||
|
return {
|
||||||
|
class: CLASSES.filter((name) => rows.some((row) => row.class === name)).concat(sorted(rows.map((row) => row.class)).filter((name) => !CLASSES.includes(name))),
|
||||||
|
from: sorted(rows.map((row) => row.from)),
|
||||||
|
to: sorted(rows.flatMap((row) => row.to)),
|
||||||
|
agent: sorted(names),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleared(entry) {
|
||||||
|
return {...entry, chips: Object.fromEntries(CHIPS.map((name) => [name, ""])), q: ""};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {KEY, MODES, TONES, CLASSES, TABS, CHIPS, DEFAULTS, valid, picked, plan, burst, toAll, ledgerOf, logRows, counts, choices, cleared};
|
||||||
|
})();
|
||||||
|
if (typeof module === "object" && module.exports) {
|
||||||
|
module.exports = RadioSet;
|
||||||
|
}
|
||||||
@@ -75,4 +75,51 @@ body.fit .radio-spot { flex: 0 1 auto; min-height: 0; }
|
|||||||
.composer-status { min-width: 0; overflow-wrap: anywhere; }
|
.composer-status { min-width: 0; overflow-wrap: anywhere; }
|
||||||
.voice-note { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2); }
|
.voice-note { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2); }
|
||||||
.voice-replay { max-width: 100%; height: 34px; }
|
.voice-replay { max-width: 100%; height: 34px; }
|
||||||
.mini .voice-replay, .bubble .voice-replay { width: 100%; }
|
.mini .voice-replay, .bubble .voice-replay, .radio-row .voice-replay { width: 100%; }
|
||||||
|
.radio-face { display: grid; gap: var(--sp-2); min-width: 0; padding: var(--sp-3); border: 1px solid var(--line-strong); border-radius: var(--radius); background: var(--sunken); }
|
||||||
|
.radio-display { display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2) var(--sp-3); min-width: 0; padding: var(--sp-2) var(--sp-3); border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--panel); font-family: var(--mono); font-size: var(--fs-sm); font-variant-numeric: tabular-nums; }
|
||||||
|
.radio-field { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||||
|
.radio-field[data-field="class"] { font-weight: 700; }
|
||||||
|
.radio-field[data-field="cpu"][data-open="false"] { color: var(--warn); }
|
||||||
|
.radio-lamp { display: inline-flex; align-items: center; gap: 5px; padding: 0 8px; border-radius: 999px; background: var(--neutral-soft); color: var(--muted); font-weight: 700; line-height: 20px; }
|
||||||
|
.radio-lamp::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; opacity: .5; }
|
||||||
|
.radio-lamp[data-lit="true"] { background: var(--ok-soft); color: var(--ok); }
|
||||||
|
.radio-lamp[data-lit="true"][data-tx="true"] { background: var(--bad-soft); color: var(--bad); }
|
||||||
|
.radio-lamp[data-lit="true"]::before { opacity: 1; box-shadow: 0 0 0 3px color-mix(in srgb, currentColor 25%, transparent); }
|
||||||
|
.radio-modes { flex-wrap: wrap; justify-self: start; max-width: 100%; }
|
||||||
|
.radio-modes button { display: inline-flex; align-items: center; gap: 6px; }
|
||||||
|
.radio-knobs { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 380px), 1fr)); gap: var(--sp-1) var(--sp-4); }
|
||||||
|
.knob-row { display: flex; align-items: center; gap: var(--sp-2); min-width: 0; color: var(--muted); font-size: var(--fs-sm); }
|
||||||
|
.knob-label { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||||
|
.knob { flex: 1 1 80px; min-width: 60px; accent-color: var(--accent); }
|
||||||
|
.knob-value { flex: none; min-width: 3.5em; font-family: var(--mono); font-variant-numeric: tabular-nums; text-align: right; }
|
||||||
|
.radio-read { display: grid; gap: var(--sp-2); }
|
||||||
|
.radio-read .chips { margin-bottom: 0; }
|
||||||
|
.ptt { min-width: 96px; justify-content: center; }
|
||||||
|
.radio-logbox { display: grid; gap: var(--sp-2); align-content: start; min-width: 0; }
|
||||||
|
.radio-tabs { display: flex; flex-wrap: wrap; gap: 2px; padding: 3px; border: 1px solid var(--line); border-radius: 10px; background: var(--sunken); justify-self: start; max-width: 100%; }
|
||||||
|
.radio-tabs button { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border: 0; border-radius: 8px; background: transparent; color: var(--muted); font-size: var(--fs-sm); font-weight: 580; cursor: pointer; }
|
||||||
|
.radio-tabs button[aria-selected="true"] { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
|
||||||
|
.radio-filters { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; min-width: 0; }
|
||||||
|
.radio-chips { display: flex; flex-wrap: wrap; gap: 6px; min-width: 0; }
|
||||||
|
.chip-select .field { max-width: 180px; padding: 3px 8px; border-radius: 999px; font-size: var(--fs-sm); }
|
||||||
|
.chip-select[data-active="true"] .field { border-color: transparent; background: var(--accent-soft); color: var(--accent); }
|
||||||
|
.radio-filters input[type="search"] { flex: 1 1 140px; min-width: 0; padding: 4px 10px; border: 1px solid var(--line-strong); border-radius: 10px; background: var(--panel); }
|
||||||
|
.radio-log { display: grid; gap: 4px; margin: 0; padding: 0; list-style: none; }
|
||||||
|
.radio-row details { min-width: 0; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--sunken); }
|
||||||
|
.radio-row summary { display: flex; flex-wrap: wrap; align-items: center; gap: 4px var(--sp-2); min-width: 0; padding: 5px 8px; font-size: var(--fs-sm); cursor: pointer; list-style: none; }
|
||||||
|
.radio-row summary::-webkit-details-marker { display: none; }
|
||||||
|
.radio-row time { color: var(--muted); font-variant-numeric: tabular-nums; }
|
||||||
|
.radio-dir { display: inline-flex; color: var(--muted); }
|
||||||
|
.radio-row[data-dir="sent"] .radio-dir { color: var(--accent); }
|
||||||
|
.radio-route { font-weight: 600; overflow-wrap: anywhere; }
|
||||||
|
.radio-line { flex: 1 1 160px; min-width: 0; overflow: hidden; color: var(--muted); text-overflow: ellipsis; white-space: nowrap; }
|
||||||
|
.radio-full { margin: 0; padding: 6px 10px 8px; border-top: 1px solid var(--line); white-space: pre-wrap; overflow-wrap: anywhere; font-size: var(--fs-sm); }
|
||||||
|
@media (min-width: 641px) {
|
||||||
|
.knob-label { flex: none; }
|
||||||
|
}
|
||||||
|
@media (max-width: 640px) {
|
||||||
|
body:not(.fit) .radio-spot { max-height: none; }
|
||||||
|
.radio-log { max-height: calc(100dvh - var(--head) - var(--sp-3)); overflow-y: auto; overscroll-behavior: contain; scroll-margin-top: var(--head); }
|
||||||
|
.radio-row summary { gap: 0 var(--sp-2); padding: 3px 8px; line-height: 1.3; }
|
||||||
|
}
|
||||||
@@ -20,13 +20,6 @@ const Voice = (() => {
|
|||||||
return kinds.find((kind) => present[kind] && settings.sounds[kind].on && loudness(kind, settings) > 0) || null;
|
return kinds.find((kind) => present[kind] && settings.sounds[kind].on && loudness(kind, settings) > 0) || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function speakable(row, speech) {
|
|
||||||
if (!speech.on || !row.speech || !row.voice) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return row.class === "mayday" || (speech.classes.includes(row.class) && !speech.muted.includes(row.source));
|
|
||||||
}
|
|
||||||
|
|
||||||
function rank(item) {
|
function rank(item) {
|
||||||
return Object.hasOwn(RANKS, item.cls) ? RANKS[item.cls] : ROUTINE;
|
return Object.hasOwn(RANKS, item.cls) ? RANKS[item.cls] : ROUTINE;
|
||||||
}
|
}
|
||||||
@@ -66,7 +59,7 @@ const Voice = (() => {
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {toneOf, loudness, beep, speakable, enqueue, frames, wav};
|
return {toneOf, loudness, beep, enqueue, frames, wav};
|
||||||
})();
|
})();
|
||||||
if (typeof module === "object" && module.exports) {
|
if (typeof module === "object" && module.exports) {
|
||||||
module.exports = Voice;
|
module.exports = Voice;
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ ACTIONS = """(async () => {@HELPERS@
|
|||||||
await sleep(300);
|
await sleep(300);
|
||||||
found.ambience = all(".sheet-box form > section:first-child button").map((button) => button.id);
|
found.ambience = all(".sheet-box form > section:first-child button").map((button) => button.id);
|
||||||
found.sections = all(".sheet-box form.settings > section > h3").map(text);
|
found.sections = all(".sheet-box form.settings > section > h3").map(text);
|
||||||
found.radioFields = all('.sheet-box [aria-label="Squelch"], .sheet-box [aria-label="Classes"], .sheet-box [aria-label="Speakers"], .sheet-box .sound-row').length;
|
found.radioFields = all('.sheet-box [aria-label="Squelch"], .sheet-box [aria-label="Classes"], .sheet-box [aria-label="Speakers"], .sheet-box [aria-label="Mode"], .sheet-box .sound-row, .sheet-box .stream-row').length;
|
||||||
found.settingsUnlabelled = all(".sheet-box svg.bi").map((svg) => svg.parentElement).filter((owner) => !text(owner) && !(owner.getAttribute("title") && owner.getAttribute("aria-label"))).length;
|
found.settingsUnlabelled = all(".sheet-box svg.bi").map((svg) => svg.parentElement).filter((owner) => !text(owner) && !(owner.getAttribute("title") && owner.getAttribute("aria-label"))).length;
|
||||||
document.querySelector(".sheet-head button").click();
|
document.querySelector(".sheet-head button").click();
|
||||||
document.getElementById("palette-open").click();
|
document.getElementById("palette-open").click();
|
||||||
@@ -418,7 +418,6 @@ RULES_OVERLAP = """(async () => {@HELPERS@@CLASHES@
|
|||||||
SETTINGS_OVERLAP = """(async () => {@HELPERS@@CLASHES@
|
SETTINGS_OVERLAP = """(async () => {@HELPERS@@CLASHES@
|
||||||
document.getElementById("settings-open").click();
|
document.getElementById("settings-open").click();
|
||||||
const settings = await until(() => document.querySelector(".sheet-box form.settings"), 8000);
|
const settings = await until(() => document.querySelector(".sheet-box form.settings"), 8000);
|
||||||
all("button", settings).find((button) => text(button) === "+ Station").click();
|
|
||||||
const theme = all("select", settings).find((select) => [...select.options].some((option) => option.value === "custom"));
|
const theme = all("select", settings).find((select) => [...select.options].some((option) => option.value === "custom"));
|
||||||
theme.value = "custom";
|
theme.value = "custom";
|
||||||
theme.dispatchEvent(new Event("change", {bubbles: true}));
|
theme.dispatchEvent(new Event("change", {bubbles: true}));
|
||||||
@@ -459,8 +458,10 @@ MEDIA = """
|
|||||||
"""
|
"""
|
||||||
ADD_STATION = """(async () => {@HELPERS@
|
ADD_STATION = """(async () => {@HELPERS@
|
||||||
const clock = (date) => `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`;
|
const clock = (date) => `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`;
|
||||||
document.getElementById("settings-open").click();
|
location.hash = "#stream/radio";
|
||||||
const form = await until(() => document.querySelector(".sheet-box form.settings"), 8000);
|
const edit = await until(() => all("#radio-spot button").find((button) => text(button) === "Edit stations" && button.getClientRects().length), 8000);
|
||||||
|
edit.click();
|
||||||
|
const form = await until(() => document.querySelector('.sheet-box form[aria-label="Stations"]'), 8000);
|
||||||
all("button", form).find((button) => text(button) === "+ Station").click();
|
all("button", form).find((button) => text(button) === "+ Station").click();
|
||||||
await sleep(200);
|
await sleep(200);
|
||||||
const row = all(".stream-row", form).pop();
|
const row = all(".stream-row", form).pop();
|
||||||
@@ -876,48 +877,56 @@ PRESENCE_READ = """(async () => {@HELPERS@@PRESENCE@
|
|||||||
})()""".replace("@HELPERS@", HELPERS).replace("@PRESENCE@", PRESENCE_HELPERS)
|
})()""".replace("@HELPERS@", HELPERS).replace("@PRESENCE@", PRESENCE_HELPERS)
|
||||||
GEAR_SAVE = """(async () => {@HELPERS@@CLASHES@
|
GEAR_SAVE = """(async () => {@HELPERS@@CLASHES@
|
||||||
const spot = document.getElementById("radio-spot");
|
const spot = document.getElementById("radio-spot");
|
||||||
const gear = all(".spot-head button", spot).find((button) => name(button) === "Radio settings");
|
const face = spot.querySelector(".radio-face");
|
||||||
|
const gear = all(".spot-head button", spot).find((button) => name(button) === "Read aloud");
|
||||||
gear.click();
|
gear.click();
|
||||||
const form = await until(() => spot.querySelector("form.radio-settings"), 4000);
|
const box = await until(() => spot.querySelector("#radio-gear .radio-read"), 4000);
|
||||||
if (!form) {
|
if (!box) {
|
||||||
return {error: "no radio settings behind the gear"};
|
return {error: "no read-aloud filters behind the gear"};
|
||||||
}
|
}
|
||||||
const classes = () => all('[aria-label="Classes"] input', form);
|
const chips = () => all('#radio-gear [aria-label="Classes"] button', spot);
|
||||||
|
const post = () => chips().find((chip) => text(chip) === "Post");
|
||||||
const found = {
|
const found = {
|
||||||
expanded: gear.getAttribute("aria-expanded"),
|
expanded: gear.getAttribute("aria-expanded"),
|
||||||
sections: all("section > h3", form).map(text),
|
tones: all(".radio-knobs .knob-label", face).map(text),
|
||||||
tones: all(".sound-row strong", form).map(text),
|
classes: chips().map((chip) => [text(chip), chip.getAttribute("aria-pressed"), chip.disabled]),
|
||||||
classes: classes().map((input) => [input.parentElement.textContent.trim(), input.checked, input.disabled]),
|
clashes: [clashes(face), clashes(spot.querySelector("#radio-gear"))],
|
||||||
clashes: clashes(form),
|
|
||||||
@FITS@,
|
@FITS@,
|
||||||
};
|
};
|
||||||
const squelch = form.querySelector('input[aria-label="Squelch"]');
|
const squelch = face.querySelector('input[aria-label="Squelch"]');
|
||||||
squelch.value = "@SQUELCH@";
|
squelch.value = "@SQUELCH@";
|
||||||
squelch.dispatchEvent(new Event("input", {bubbles: true}));
|
squelch.dispatchEvent(new Event("input", {bubbles: true}));
|
||||||
form.querySelector('input[aria-label="All radio on"]').checked = @TONE@;
|
const tone = face.querySelector('button[aria-label="All radio tone"]');
|
||||||
classes().find((input) => input.parentElement.textContent.trim() === "Post").checked = @POST@;
|
if (tone.getAttribute("aria-pressed") !== "@TONE@") {
|
||||||
const before = new Set(all("#toasts .toast"));
|
tone.click();
|
||||||
all("button", form).find((button) => button.type === "submit").click();
|
}
|
||||||
found.toast = await until(() => all("#toasts .toast").filter((node) => !before.has(node)).map(text).find((entry) => entry.includes("ettings")), 15000);
|
if (post().getAttribute("aria-pressed") !== "@POST@") {
|
||||||
await sleep(300);
|
post().click();
|
||||||
found.after = [gear.getAttribute("aria-expanded"), Boolean(spot.querySelector("form.radio-settings"))];
|
}
|
||||||
|
await sleep(200);
|
||||||
|
const kept = JSON.parse(localStorage.getItem("kvb-orchestrator.radio"));
|
||||||
|
found.kept = [kept.squelch, kept.sounds.radio.on, kept.classes.includes("post")];
|
||||||
|
gear.click();
|
||||||
|
await sleep(200);
|
||||||
|
found.after = [gear.getAttribute("aria-expanded"), Boolean(spot.querySelector("#radio-gear .radio-read"))];
|
||||||
document.getElementById("settings-open").click();
|
document.getElementById("settings-open").click();
|
||||||
const sheet = await until(() => document.querySelector(".sheet-box form.settings"), 4000);
|
const sheet = await until(() => document.querySelector(".sheet-box form.settings"), 4000);
|
||||||
found.general = {sections: all("section > h3", sheet).map(text), radio: all('[aria-label="Squelch"], [aria-label="Classes"], [aria-label="Speakers"], .sound-row', sheet).length};
|
found.general = {sections: all("section > h3", sheet).map(text), radio: all('[aria-label="Squelch"], [aria-label="Classes"], [aria-label="Speakers"], [aria-label="Mode"], .sound-row, .stream-row', sheet).length};
|
||||||
document.querySelector(".sheet-head button").click();
|
document.querySelector(".sheet-head button").click();
|
||||||
return found;
|
return found;
|
||||||
})()""".replace("@HELPERS@", HELPERS).replace("@CLASHES@", OVERLAP).replace("@FITS@", FITS)
|
})()""".replace("@HELPERS@", HELPERS).replace("@CLASHES@", OVERLAP).replace("@FITS@", FITS)
|
||||||
GEAR_READ = """(async () => {@HELPERS@@SPOT_OPEN@
|
GEAR_READ = """(async () => {@HELPERS@@SPOT_OPEN@
|
||||||
const gear = spot ? all(".spot-head button", spot).find((button) => name(button) === "Radio settings") : null;
|
const gear = spot ? all(".spot-head button", spot).find((button) => name(button) === "Read aloud") : null;
|
||||||
if (!gear) {
|
if (!gear) {
|
||||||
return {error: text(document.getElementById("main"))};
|
return {error: text(document.getElementById("main"))};
|
||||||
}
|
}
|
||||||
gear.click();
|
gear.click();
|
||||||
const form = await until(() => spot.querySelector("form.radio-settings"), 4000);
|
await until(() => spot.querySelector("#radio-gear .radio-read"), 4000);
|
||||||
|
const face = spot.querySelector(".radio-face");
|
||||||
return {
|
return {
|
||||||
squelch: Number(form.querySelector('input[aria-label="Squelch"]').value),
|
squelch: Number(face.querySelector('input[aria-label="Squelch"]').value),
|
||||||
tone: form.querySelector('input[aria-label="All radio on"]').checked,
|
tone: face.querySelector('button[aria-label="All radio tone"]').getAttribute("aria-pressed") === "true",
|
||||||
post: all('[aria-label="Classes"] input', form).find((input) => input.parentElement.textContent.trim() === "Post").checked,
|
post: all('#radio-gear [aria-label="Classes"] button', spot).find((chip) => text(chip) === "Post").getAttribute("aria-pressed") === "true",
|
||||||
@FITS@,
|
@FITS@,
|
||||||
};
|
};
|
||||||
})()""".replace("@HELPERS@", HELPERS).replace("@SPOT_OPEN@", SPOT_OPEN).replace("@FITS@", FITS)
|
})()""".replace("@HELPERS@", HELPERS).replace("@SPOT_OPEN@", SPOT_OPEN).replace("@FITS@", FITS)
|
||||||
@@ -1579,10 +1588,9 @@ class TestOrchestratorE2E(HubCase):
|
|||||||
|
|
||||||
def speaks(self, cues: tuple[str, ...]) -> None:
|
def speaks(self, cues: tuple[str, ...]) -> None:
|
||||||
hub = self.httpd.hub
|
hub = self.httpd.hub
|
||||||
hub.settings.put({**hub.settings.get(), "speech": {"on": True, "classes": ["mayday", "pan-pan", "security"], "muted": []}}, hub.settings.get())
|
|
||||||
spoken = tone(6, 8000)
|
spoken = tone(6, 8000)
|
||||||
|
|
||||||
def synthesize(body: bytes) -> bytes:
|
def synthesize(body: bytes, admit: Callable[[str], None]) -> bytes:
|
||||||
word = json.loads(body)["text"]
|
word = json.loads(body)["text"]
|
||||||
if word in cues:
|
if word in cues:
|
||||||
row = {"ts": time.time(), "from": "Stage", "to": [], "text": f"Disk almost full ({word})\nsecond line", "class": "security", "v": 2}
|
row = {"ts": time.time(), "from": "Stage", "to": [], "text": f"Disk almost full ({word})\nsecond line", "class": "security", "v": 2}
|
||||||
@@ -1853,7 +1861,7 @@ class TestOrchestratorE2E(HubCase):
|
|||||||
self.assertEqual(actions["keys"], ["#agents/chord", "#artifacts/tree", "#tasks/3d", "#stream"])
|
self.assertEqual(actions["keys"], ["#agents/chord", "#artifacts/tree", "#tasks/3d", "#stream"])
|
||||||
self.assertEqual(actions["ambience"], ["sky-look", "music-toggle", "leitstand", "notify"])
|
self.assertEqual(actions["ambience"], ["sky-look", "music-toggle", "leitstand", "notify"])
|
||||||
self.assertEqual((actions["settingsUnlabelled"], actions["popupUnlabelled"]), (0, 0))
|
self.assertEqual((actions["settingsUnlabelled"], actions["popupUnlabelled"]), (0, 0))
|
||||||
self.assertEqual((actions["sections"], actions["radioFields"]), (["Ambience", "Location", "Appearance", "Music", "Notifications", "Internet radio", "Passkey"], 0))
|
self.assertEqual((actions["sections"], actions["radioFields"]), (["Ambience", "Location", "Appearance", "Music", "Notifications", "Passkey"], 0))
|
||||||
for tool in ("Radio …", "Cancel mayday", "Priority …", "Drain", "Suspend", "Resume", "Firewall …", "Settings …", "Look at the sky", "Toggle control room", "Enable notifications"):
|
for tool in ("Radio …", "Cancel mayday", "Priority …", "Drain", "Suspend", "Resume", "Firewall …", "Settings …", "Look at the sky", "Toggle control room", "Enable notifications"):
|
||||||
with self.subTest(tool=tool):
|
with self.subTest(tool=tool):
|
||||||
self.assertTrue(any(tool in entry for entry in actions["palette"]), actions["palette"])
|
self.assertTrue(any(tool in entry for entry in actions["palette"]), actions["palette"])
|
||||||
@@ -2006,19 +2014,19 @@ class TestOrchestratorE2E(HubCase):
|
|||||||
self.assertEqual((sent["closed"], sent["open"]), ([True, "false"], [False, "true", True]))
|
self.assertEqual((sent["closed"], sent["open"]), ([True, "false"], [False, "true", True]))
|
||||||
self.assertEqual(sent["inline"], ["static", True, True])
|
self.assertEqual(sent["inline"], ["static", True, True])
|
||||||
self.assertEqual((sent["toolbar"], sent["channels"], sent["record"]), (["Priority", "Day/Night", "Status"], CHOICE_CHANNELS, ["Record"]))
|
self.assertEqual((sent["toolbar"], sent["channels"], sent["record"]), (["Priority", "Day/Night", "Status"], CHOICE_CHANNELS, ["Record"]))
|
||||||
self.assertEqual(sent["heads"], [["Radio settings", "Radio settings: tones, squelch and read aloud"], ["Cancel mayday", "Cancel mayday: lift the radio silence so the agents write again; your passkey signs it"]])
|
self.assertEqual(sent["heads"], [["Read aloud", "Read aloud: the classes and agents the radio sounds for"], ["Cancel mayday", "Cancel mayday: lift the radio silence so the agents write again; your passkey signs it"]])
|
||||||
self.assertEqual((sent["tall"], sent["wide"]), (False, False))
|
self.assertEqual((sent["tall"], sent["wide"]), (False, False))
|
||||||
self.assertEqual([run["cls"] for run in sent["runs"]], CHOICE_CHANNELS)
|
self.assertEqual([run["cls"] for run in sent["runs"]], CHOICE_CHANNELS)
|
||||||
for run in sent["runs"]:
|
for run in sent["runs"]:
|
||||||
direct = run["cls"] in ("post", "call")
|
direct = run["cls"] in ("post", "call")
|
||||||
self.assertEqual((run["direct"], run["question"], run["left"]), (direct, [mayday] if run["cls"] == "mayday" else [], ""), run)
|
self.assertEqual((run["direct"], run["question"], run["left"]), (direct, [mayday] if run["cls"] == "mayday" else [], ""), run)
|
||||||
self.assertTrue((run["toast"] or "").startswith(f"{labels[run['cls']]} to {'Stage' if direct else 'all'}: sent"), run)
|
self.assertTrue((run["toast"] or "").startswith(f"{labels[run['cls']]} to {'Stage' if direct else 'all'}: sent"), run)
|
||||||
self.assertEqual((saved["expanded"], saved["sections"], saved["clashes"]), ("true", ["Tones", "Read radio aloud"], {"found": [], "outside": []}))
|
self.assertEqual((saved["expanded"], saved["clashes"]), ("true", [{"found": [], "outside": []}] * 2))
|
||||||
self.assertEqual(saved["tones"], ["MAYDAY / PAN-PAN", "Reviews, asks, decisions", "Post and calls to you", "All radio"])
|
self.assertEqual(saved["tones"], ["MAYDAY / PAN-PAN", "Reviews, asks, decisions", "Post and calls to you", "All radio", "Squelch", "Threshold"])
|
||||||
self.assertEqual([entry[0] for entry in saved["classes"]], ["Post", "Call", "Newsletter", "Broadcast", "MAYDAY", "PAN-PAN", "Sécurité"])
|
self.assertEqual([entry[0] for entry in saved["classes"]], ["Post", "Call", "Newsletter", "Broadcast", "MAYDAY", "PAN-PAN", "Sécurité"])
|
||||||
self.assertEqual([entry[1:] for entry in saved["classes"] if entry[0] == "MAYDAY"], [[True, True]])
|
self.assertEqual([entry[1:] for entry in saved["classes"] if entry[0] == "MAYDAY"], [["true", True]])
|
||||||
self.assertEqual((saved["toast"], saved["after"]), ("Radio settings saved", ["false", False]))
|
self.assertEqual((saved["kept"], saved["after"]), ([float(squelch), tone == "true", post == "true"], ["false", False]))
|
||||||
self.assertEqual(saved["general"], {"sections": ["Ambience", "Location", "Appearance", "Music", "Notifications", "Internet radio", "Passkey"], "radio": 0})
|
self.assertEqual(saved["general"], {"sections": ["Ambience", "Location", "Appearance", "Music", "Notifications", "Passkey"], "radio": 0})
|
||||||
self.assertEqual((saved["tall"], saved["wide"]), (False, False))
|
self.assertEqual((saved["tall"], saved["wide"]), (False, False))
|
||||||
self.assertEqual(read, {"squelch": float(squelch), "tone": tone == "true", "post": post == "true", "tall": False, "wide": False})
|
self.assertEqual(read, {"squelch": float(squelch), "tone": tone == "true", "post": post == "true", "tall": False, "wide": False})
|
||||||
heads = {"post": ["post", "--to", "Stage"], "call": ["send", "call", "--to", "Stage"], "newsletter": ["post"]}
|
heads = {"post": ["post", "--to", "Stage"], "call": ["send", "call", "--to", "Stage"], "newsletter": ["post"]}
|
||||||
@@ -2028,8 +2036,7 @@ class TestOrchestratorE2E(HubCase):
|
|||||||
for args, (width, cls) in zip(radio, sends, strict=True):
|
for args, (width, cls) in zip(radio, sends, strict=True):
|
||||||
with self.subTest(width=width, cls=cls):
|
with self.subTest(width=width, cls=cls):
|
||||||
self.assertTrue(args[-1].endswith(f"] spot {width} {cls}"), args)
|
self.assertTrue(args[-1].endswith(f"] spot {width} {cls}"), args)
|
||||||
stored = self.httpd.hub.settings.get()
|
self.assertEqual(self.httpd.hub.settings.get()["speech"], {"threshold": 50})
|
||||||
self.assertEqual((stored["squelch"], stored["sounds"]["radio"]["on"], "post" in stored["speech"]["classes"]), (0.55, False, True))
|
|
||||||
|
|
||||||
def test_chromium_keeps_the_3d_camera_through_two_state_refreshes_at_three_widths(self):
|
def test_chromium_keeps_the_3d_camera_through_two_state_refreshes_at_three_widths(self):
|
||||||
steps = [{"width": width, "height": height, "url": f"{self.base}?{width}#agents/3d", "wait": 900, "probe": CAMERA, "shot": self.shot(f"{width}-agents-3d-steady-camera")} for width, height in WIDTHS]
|
steps = [{"width": width, "height": height, "url": f"{self.base}?{width}#agents/3d", "wait": 900, "probe": CAMERA, "shot": self.shot(f"{width}-agents-3d-steady-camera")} for width, height in WIDTHS]
|
||||||
|
|||||||
@@ -0,0 +1,503 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
HERE = Path(__file__).resolve().parent
|
||||||
|
sys.path[:0] = [str(HERE.parent), str(HERE)]
|
||||||
|
|
||||||
|
import test_orchestrator_e2e as base
|
||||||
|
from hub_case import HubCase
|
||||||
|
from hub_engines import log_rows
|
||||||
|
from hub_support import iso
|
||||||
|
from kvb_orchestrator import errors, phrasing, speechgate
|
||||||
|
|
||||||
|
OUT = Path(os.environ.get("E2E_OUT", "/tmp/radio-set-shots"))
|
||||||
|
WORD = phrasing.procedure("security")
|
||||||
|
CUES = {"low": "c000000000000001", "high": "c000000000000002", "hold": "c000000000000003", "free": "c000000000000004", "alarm": "a000000000000001", "call": "a000000000000002", "dark": "d000000000000001", "light": "d000000000000002"}
|
||||||
|
THRESHOLDS = {1920: 60, 1366: 70, 390: 80}
|
||||||
|
FIXTURE = [
|
||||||
|
{"ago": 600, "from": "Operator", "to": ["Stage"], "class": "post", "text": "rebase please"},
|
||||||
|
{"ago": 500, "from": "Stage", "to": ["Operator"], "class": "call", "text": "gate red on main"},
|
||||||
|
{"ago": 400, "from": "Docs", "to": [], "class": "newsletter", "text": "docs are green"},
|
||||||
|
{"ago": 350, "from": "Stage", "to": ["Skills session"], "class": "post", "text": "rebase done\nsecond line of the full message"},
|
||||||
|
{"ago": 320, "from": "Operator", "to": [], "class": "broadcast", "text": "deploy now"},
|
||||||
|
]
|
||||||
|
SPY = """() => {
|
||||||
|
window.e2eViolations = [];
|
||||||
|
document.addEventListener("securitypolicyviolation", (event) => window.e2eViolations.push(`${event.violatedDirective} ${event.blockedURI}`));
|
||||||
|
const log = window.e2eAudio = {tts: [], requests: [], filters: [], starts: [], oscillators: 0, levels: [], passkeys: 0};
|
||||||
|
const fetched = window.fetch.bind(window);
|
||||||
|
window.fetch = (input, init) => {
|
||||||
|
const url = String(input && input.url ? input.url : input);
|
||||||
|
const entry = url.includes("/api/tts") ? {body: JSON.parse(init.body), status: 0} : null;
|
||||||
|
log.requests.push(url);
|
||||||
|
if (entry) {
|
||||||
|
log.tts.push(entry);
|
||||||
|
}
|
||||||
|
return fetched(input, init).then((response) => {
|
||||||
|
if (entry) {
|
||||||
|
entry.status = response.status;
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
const filter = AudioContext.prototype.createBiquadFilter;
|
||||||
|
AudioContext.prototype.createBiquadFilter = function created() {
|
||||||
|
const node = filter.call(this);
|
||||||
|
log.filters.push(node);
|
||||||
|
return node;
|
||||||
|
};
|
||||||
|
const start = AudioBufferSourceNode.prototype.start;
|
||||||
|
AudioBufferSourceNode.prototype.start = function started(...args) {
|
||||||
|
log.starts.push({seconds: this.buffer ? Math.round(this.buffer.duration * 1000) / 1000 : null, channels: this.buffer ? this.buffer.numberOfChannels : null});
|
||||||
|
return start.apply(this, args);
|
||||||
|
};
|
||||||
|
const tone = OscillatorNode.prototype.start;
|
||||||
|
OscillatorNode.prototype.start = function toned(...args) {
|
||||||
|
log.oscillators += 1;
|
||||||
|
return tone.apply(this, args);
|
||||||
|
};
|
||||||
|
const level = AudioParam.prototype.setValueAtTime;
|
||||||
|
AudioParam.prototype.setValueAtTime = function levelled(value, at) {
|
||||||
|
log.levels.push(Math.round(value * 1000) / 1000);
|
||||||
|
return level.call(this, value, at);
|
||||||
|
};
|
||||||
|
if (navigator.credentials) {
|
||||||
|
const get = navigator.credentials.get.bind(navigator.credentials);
|
||||||
|
navigator.credentials.get = (options) => {
|
||||||
|
log.passkeys += 1;
|
||||||
|
return get(options);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}"""
|
||||||
|
RADIO = """
|
||||||
|
const spotToggle = document.getElementById("radio-open");
|
||||||
|
await until(() => typeof Nav === "object" && document.getElementById("radio-spot"), 8000);
|
||||||
|
if (spotToggle.getAttribute("aria-expanded") !== "true") {
|
||||||
|
spotToggle.click();
|
||||||
|
await sleep(300);
|
||||||
|
}
|
||||||
|
const spot = document.getElementById("radio-spot");
|
||||||
|
const audio = () => window.e2eAudio;
|
||||||
|
const cue = (name) => fetch(`/api/voice/${{@CUES@}[name]}`, {cache: "no-store"});
|
||||||
|
const refreshNow = () => window.dispatchEvent(new Event("focus"));
|
||||||
|
const field = (name) => text(spot.querySelector(`.radio-display [data-field="${name}"]`));
|
||||||
|
const lamp = () => spot.querySelector(".radio-lamp");
|
||||||
|
const fits = () => ({page: document.documentElement.scrollWidth <= innerWidth + 1, panel: spot.scrollWidth <= spot.clientWidth + 1});
|
||||||
|
const press = (mode) => spot.querySelector(`.radio-modes button[data-mode="${mode}"]`).click();
|
||||||
|
const pressed = () => all(".radio-modes button", spot).filter((button) => button.getAttribute("aria-pressed") === "true").map((button) => button.dataset.mode);
|
||||||
|
const mark = () => ({tts: audio().tts.length, starts: audio().starts.length, filters: audio().filters.length, oscillators: audio().oscillators, levels: audio().levels.length, requests: audio().requests.length});
|
||||||
|
const since = (at) => ({
|
||||||
|
tts: audio().tts.slice(at.tts).map((entry) => [entry.body.text, entry.status]),
|
||||||
|
starts: audio().starts.slice(at.starts),
|
||||||
|
filters: audio().filters.slice(at.filters).map((node) => [node.type, node.frequency.value]),
|
||||||
|
oscillators: audio().oscillators - at.oscillators,
|
||||||
|
levels: audio().levels.slice(at.levels),
|
||||||
|
requests: audio().requests.slice(at.requests).filter((url) => !/\\/api\\/(state|stream|events|tts|tree|subagents|voice\\/[a-d]0)/.test(url)),
|
||||||
|
});
|
||||||
|
const cpuShown = async (label) => {
|
||||||
|
refreshNow();
|
||||||
|
return Boolean(await until(() => field("cpu") === label, 12000));
|
||||||
|
};
|
||||||
|
const transmit = async (names, count) => {
|
||||||
|
const at = mark();
|
||||||
|
for (const name of names) {
|
||||||
|
await cue(name);
|
||||||
|
}
|
||||||
|
refreshNow();
|
||||||
|
await until(() => audio().tts.length >= at.tts + count && audio().tts.slice(at.tts).every((entry) => entry.status), 20000);
|
||||||
|
await sleep(1500);
|
||||||
|
await until(() => lamp().dataset.lit === "false", 4000);
|
||||||
|
return since(at);
|
||||||
|
};
|
||||||
|
const tabs = () => Object.fromEntries(all('.radio-tabs [role="tab"]', spot).map((tab) => [tab.dataset.tab, Number(text(tab.querySelector(".count")))]));
|
||||||
|
const selected = () => (spot.querySelector('.radio-tabs [aria-selected="true"]') || {dataset: {}}).dataset.tab || null;
|
||||||
|
const rows = () => all(".radio-row", spot).map((item) => ({dir: item.dataset.dir, cls: item.dataset.class, route: text(item.querySelector(".radio-route")), line: text(item.querySelector(".radio-line")), ts: Number(item.querySelector("time").dataset.ts)}));
|
||||||
|
const chip = async (name, value) => {
|
||||||
|
const select = spot.querySelector(`.chip-select[data-chip="${name}"] select`);
|
||||||
|
select.value = value;
|
||||||
|
select.dispatchEvent(new Event("change", {bubbles: true}));
|
||||||
|
await sleep(150);
|
||||||
|
};
|
||||||
|
const chips = () => Object.fromEntries(all(".chip-select", spot).map((box) => [box.dataset.chip, box.querySelector("select").value]));
|
||||||
|
const search = () => spot.querySelector('.radio-filters input[type="search"]');
|
||||||
|
const tab = async (id) => {
|
||||||
|
spot.querySelector(`.radio-tabs [data-tab="${id}"]`).click();
|
||||||
|
await sleep(150);
|
||||||
|
};
|
||||||
|
const unlabelled = () => all(".radio-face :is(button, select, input), #radio-gear :is(button, select, input), .radio-logbox :is(button, select, input), .radio-station button", spot).filter((node) => node.getClientRects().length && !node.getAttribute("aria-label") && !text(node)).length;
|
||||||
|
""".replace("@CUES@", ", ".join(f'"{name}": "{ident}"' for name, ident in CUES.items()))
|
||||||
|
FACE = """(async () => {@HELPERS@@RADIO@
|
||||||
|
await cpuShown("CPU 10 %");
|
||||||
|
const display = {lamp: [text(lamp()), lamp().dataset.lit], cls: field("class"), mode: field("mode"), cpu: field("cpu"), title: spot.querySelector('[data-field="cpu"]').title};
|
||||||
|
const modes = all(".radio-modes button", spot).map((button) => [text(button), button.getAttribute("aria-pressed"), Boolean(button.title && button.getAttribute("aria-label")), Boolean(button.querySelector("svg.bi"))]);
|
||||||
|
const knobs = all(".radio-knobs input[type=range]", spot).map((input) => [input.getAttribute("aria-label"), input.min, input.max, input.step, input.value]);
|
||||||
|
const toneToggles = all(".radio-knobs button[aria-pressed]", spot).map((button) => [button.getAttribute("aria-label"), button.getAttribute("aria-pressed")]);
|
||||||
|
const labels = all(".radio-knobs .knob-row", spot).filter((row) => row.querySelector(".knob-label")).map((row) => {
|
||||||
|
const label = row.querySelector(".knob-label");
|
||||||
|
return {text: text(label), title: label.title, whole: label.scrollWidth <= label.clientWidth + 1, row: row.scrollWidth <= row.clientWidth + 1};
|
||||||
|
});
|
||||||
|
const gear = all(".spot-head button", spot).find((button) => name(button) === "Read aloud");
|
||||||
|
gear.click();
|
||||||
|
await sleep(200);
|
||||||
|
const groups = Object.fromEntries(all("#radio-gear .chips", spot).map((group) => [group.getAttribute("aria-label"), all(".chip", group).map((chipNode) => [text(chipNode), chipNode.getAttribute("aria-pressed"), chipNode.disabled])]));
|
||||||
|
const stationsHead = text(spot.querySelector(".radio-station .group-title"));
|
||||||
|
const ptt = spot.querySelector('form.composer button[type="submit"]');
|
||||||
|
const pageFits = fits();
|
||||||
|
document.getElementById("settings-open").click();
|
||||||
|
await until(() => document.querySelector("#sheet .sheet-box"), 4000);
|
||||||
|
const sections = all("#sheet .settings h3").map(text);
|
||||||
|
all("#sheet .sheet-head button").find((button) => name(button) === "Close").click();
|
||||||
|
await sleep(200);
|
||||||
|
all(".radio-station button", spot).find((button) => text(button) === "Edit stations").click();
|
||||||
|
await until(() => document.querySelector("#sheet .sheet-box"), 4000);
|
||||||
|
const stations = [document.querySelector("#sheet .sheet-box").getAttribute("aria-label"), all("#sheet .settings h3").map(text)];
|
||||||
|
all("#sheet .sheet-head button").find((button) => name(button) === "Close").click();
|
||||||
|
await sleep(200);
|
||||||
|
return {display, modes, knobs, toneToggles, labels, groups, stationsHead, ptt: [text(ptt), ptt.title], sections, stations, fits: pageFits, unlabelled: unlabelled(), palette: getComputedStyle(document.documentElement).getPropertyValue("--panel").trim(), violations: window.e2eViolations};
|
||||||
|
})()"""
|
||||||
|
LOG = """(async () => {@HELPERS@@RADIO@
|
||||||
|
await until(() => rows().length, 8000);
|
||||||
|
const counts = tabs();
|
||||||
|
await tab("sent");
|
||||||
|
const sent = rows();
|
||||||
|
await tab("received");
|
||||||
|
const received = rows();
|
||||||
|
await tab("all");
|
||||||
|
const every = rows();
|
||||||
|
await chip("class", "post");
|
||||||
|
const posts = rows();
|
||||||
|
await chip("from", "Stage");
|
||||||
|
const fromStage = rows();
|
||||||
|
search().value = "second";
|
||||||
|
search().dispatchEvent(new Event("input", {bubbles: true}));
|
||||||
|
await sleep(150);
|
||||||
|
const narrowed = rows();
|
||||||
|
search().value = "please";
|
||||||
|
search().dispatchEvent(new Event("input", {bubbles: true}));
|
||||||
|
await sleep(150);
|
||||||
|
const none = [rows().length, text(spot.querySelector(".radio-log li.muted"))];
|
||||||
|
all(".radio-filters button", spot).find((button) => name(button) === "Clear filters").click();
|
||||||
|
await sleep(150);
|
||||||
|
const clearedChips = [chips(), search().value, rows().length];
|
||||||
|
const summary = spot.querySelector(".radio-row summary");
|
||||||
|
summary.click();
|
||||||
|
await sleep(150);
|
||||||
|
const opened = [summary.parentElement.open, text(summary.parentElement.querySelector(".radio-full"))];
|
||||||
|
await tab("received");
|
||||||
|
await chip("agent", "Stage");
|
||||||
|
search().value = "gate";
|
||||||
|
search().dispatchEvent(new Event("input", {bubbles: true}));
|
||||||
|
await sleep(200);
|
||||||
|
spot.querySelector(".radio-logbox").scrollIntoView({block: "start"});
|
||||||
|
await sleep(200);
|
||||||
|
return {counts, sent, received, every, posts, fromStage, narrowed, none, clearedChips, opened, kept: rows(), fits: fits()};
|
||||||
|
})()"""
|
||||||
|
LOG_KEPT = """(async () => {@HELPERS@@RADIO@
|
||||||
|
await until(() => spot.querySelector(".radio-tabs [role=tab]"), 8000);
|
||||||
|
await sleep(300);
|
||||||
|
const kept = {tab: selected(), chips: chips(), q: search().value, rows: rows()};
|
||||||
|
all(".radio-filters button", spot).find((button) => name(button) === "Clear filters").click();
|
||||||
|
await tab("all");
|
||||||
|
const list = spot.querySelector(".radio-log");
|
||||||
|
list.scrollIntoView({block: "start"});
|
||||||
|
await sleep(300);
|
||||||
|
const top = Math.max(list.getBoundingClientRect().top, spot.getBoundingClientRect().top, document.querySelector(".head").getBoundingClientRect().bottom);
|
||||||
|
const bottom = Math.min(list.getBoundingClientRect().bottom, spot.getBoundingClientRect().bottom, innerHeight);
|
||||||
|
const shown = all(".radio-row", list).filter((item) => item.getBoundingClientRect().top >= top - 1 && item.getBoundingClientRect().bottom <= bottom + 1).length;
|
||||||
|
return {...kept, shown, total: all(".radio-row", list).length, fits: fits()};
|
||||||
|
})()"""
|
||||||
|
SOUND = """(async () => {@HELPERS@@RADIO@
|
||||||
|
const found = {};
|
||||||
|
press("auto");
|
||||||
|
await cue("free");
|
||||||
|
await cue("low");
|
||||||
|
found.low = await cpuShown("CPU 10 %");
|
||||||
|
found.auto = await transmit(["alarm"], 2);
|
||||||
|
await cue("hold");
|
||||||
|
found.held = await transmit(["alarm"], 2);
|
||||||
|
const heldCount = audio().tts.length;
|
||||||
|
await sleep(2500);
|
||||||
|
found.retries = audio().tts.length - heldCount;
|
||||||
|
await cue("free");
|
||||||
|
press("quiet");
|
||||||
|
found.quiet = await transmit(["alarm", "call"], 1);
|
||||||
|
press("silence");
|
||||||
|
found.silence = await transmit(["alarm", "call"], 1);
|
||||||
|
found.pressed = pressed();
|
||||||
|
found.mode = field("mode");
|
||||||
|
return found;
|
||||||
|
})()"""
|
||||||
|
SOUND_KEPT = """(async () => {@HELPERS@@RADIO@
|
||||||
|
await sleep(300);
|
||||||
|
const kept = [pressed(), field("mode")];
|
||||||
|
press("auto");
|
||||||
|
return {kept, pressed: pressed()};
|
||||||
|
})()"""
|
||||||
|
BURST = """(async () => {@HELPERS@@RADIO@
|
||||||
|
const squelch = all(".radio-knobs input[type=range]", spot).find((input) => input.getAttribute("aria-label") === "Squelch");
|
||||||
|
squelch.value = "0.6";
|
||||||
|
squelch.dispatchEvent(new Event("input", {bubbles: true}));
|
||||||
|
await cue("high");
|
||||||
|
const high = await cpuShown("CPU 95 %");
|
||||||
|
const at = mark();
|
||||||
|
await cue("alarm");
|
||||||
|
refreshNow();
|
||||||
|
const lit = Boolean(await until(() => lamp().dataset.lit === "true" && audio().filters.length > at.filters, 20000));
|
||||||
|
return {high, lit, lamp: [text(lamp()), lamp().getAttribute("aria-label")], open: spot.querySelector('[data-field="cpu"]').dataset.open, heard: since(at)};
|
||||||
|
})()"""
|
||||||
|
BURST_DONE = """(async () => {@HELPERS@@RADIO@
|
||||||
|
await until(() => lamp().dataset.lit === "false", 6000);
|
||||||
|
await sleep(800);
|
||||||
|
const squelch = all(".radio-knobs input[type=range]", spot).find((input) => input.getAttribute("aria-label") === "Squelch");
|
||||||
|
squelch.value = "1";
|
||||||
|
squelch.dispatchEvent(new Event("input", {bubbles: true}));
|
||||||
|
await cue("low");
|
||||||
|
return {tts: audio().tts.slice(-2).map((entry) => [entry.body.text, entry.status]), low: await cpuShown("CPU 10 %")};
|
||||||
|
})()"""
|
||||||
|
THRESHOLD = """(async () => {@HELPERS@@RADIO@
|
||||||
|
const knob = all(".radio-knobs input[type=range]", spot).find((input) => input.getAttribute("aria-label") === "Threshold");
|
||||||
|
const before = [knob.value, audio().passkeys];
|
||||||
|
knob.value = "@VALUE@";
|
||||||
|
knob.dispatchEvent(new Event("input", {bubbles: true}));
|
||||||
|
knob.dispatchEvent(new Event("change", {bubbles: true}));
|
||||||
|
await until(() => all("#toasts .toast").length, 15000);
|
||||||
|
await sleep(600);
|
||||||
|
refreshNow();
|
||||||
|
await sleep(800);
|
||||||
|
await cue("dark");
|
||||||
|
refreshNow();
|
||||||
|
const light = getComputedStyle(document.documentElement).getPropertyValue("--panel").trim();
|
||||||
|
const dark = await until(() => {
|
||||||
|
const now = getComputedStyle(document.documentElement).getPropertyValue("--panel").trim();
|
||||||
|
return now !== light ? now : null;
|
||||||
|
}, 12000);
|
||||||
|
await sleep(400);
|
||||||
|
return {before, after: [knob.value, audio().passkeys], shown: text(knob.parentElement.querySelector(".knob-value")), title: spot.querySelector('[data-field="cpu"]').title, toasts: all("#toasts .toast strong").map(text), dark: [light, dark], fits: fits()};
|
||||||
|
})()"""
|
||||||
|
LIGHT = """(async () => {@HELPERS@@RADIO@
|
||||||
|
await cue("light");
|
||||||
|
refreshNow();
|
||||||
|
await sleep(1500);
|
||||||
|
return {violations: window.e2eViolations};
|
||||||
|
})()"""
|
||||||
|
|
||||||
|
|
||||||
|
def probe(template: str) -> str:
|
||||||
|
return template.replace("@HELPERS@", base.HELPERS).replace("@RADIO@", RADIO)
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipUnless(shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium")
|
||||||
|
class TestRadioSet(HubCase):
|
||||||
|
drive = base.TestOrchestratorE2E.drive
|
||||||
|
authenticator = base.TestOrchestratorE2E.authenticator
|
||||||
|
beside = base.TestOrchestratorE2E.beside
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
OUT.mkdir(parents=True, exist_ok=True)
|
||||||
|
now = time.time()
|
||||||
|
self.claim("s1", "Skills session")
|
||||||
|
self.write_council({
|
||||||
|
"work/s2/claim.json": {"office": "work/s2", "session": "s2", "runtime": "claude", "name": "Stage", "since": iso(now - 7200), "beat": now - 60, "work": "x"},
|
||||||
|
"news.jsonl": self.jsonl([{"ts": now - row["ago"], "v": 2, **{key: value for key, value in row.items() if key != "ago"}} for row in FIXTURE]),
|
||||||
|
})
|
||||||
|
hub = self.httpd.hub
|
||||||
|
for row in FIXTURE:
|
||||||
|
if row["from"] == "Operator":
|
||||||
|
hub.messages.add({"to": row["to"][0] if row["to"] else "*", "text": row["text"], "upload": None, "class": row["class"], "voice": None, "created_ts": now - row["ago"]})
|
||||||
|
hub.settings.put({**hub.settings.get(), "theme": "light"}, hub.settings.get())
|
||||||
|
self.cpu = 10.0
|
||||||
|
self.host_cpu(self.cpu, 0)
|
||||||
|
self.beside(lambda: self.host_cpu(self.cpu, 0))
|
||||||
|
self.sent = 0
|
||||||
|
self.holding = False
|
||||||
|
self.cues()
|
||||||
|
self.base = f"http://localhost:{self.port}/"
|
||||||
|
|
||||||
|
def load(self, cpu: float) -> None:
|
||||||
|
self.cpu = cpu
|
||||||
|
self.host_cpu(cpu, 0)
|
||||||
|
|
||||||
|
def cues(self) -> None:
|
||||||
|
hub = self.httpd.hub
|
||||||
|
heard, admit = hub.voice_notes.get, speechgate.CpuGate.admit
|
||||||
|
actions = {
|
||||||
|
CUES["low"]: lambda: self.load(10.0),
|
||||||
|
CUES["high"]: lambda: self.load(95.0),
|
||||||
|
CUES["hold"]: lambda: setattr(self, "holding", True),
|
||||||
|
CUES["free"]: lambda: setattr(self, "holding", False),
|
||||||
|
CUES["alarm"]: lambda: self.radio("security", [], "Disk at ninety percent"),
|
||||||
|
CUES["call"]: lambda: self.radio("call", ["Operator"], "Gate is green"),
|
||||||
|
CUES["dark"]: lambda: hub.settings.put({**hub.settings.get(), "theme": "dark"}, hub.settings.get()),
|
||||||
|
CUES["light"]: lambda: hub.settings.put({**hub.settings.get(), "theme": "light"}, hub.settings.get()),
|
||||||
|
}
|
||||||
|
|
||||||
|
def get(note_id: str) -> dict:
|
||||||
|
if note_id not in actions:
|
||||||
|
return heard(note_id)
|
||||||
|
actions[note_id]()
|
||||||
|
return {"id": note_id, "status": "done", "text": "", "error": "", "seconds": None, "created_ts": None}
|
||||||
|
|
||||||
|
def gate(gate_self: speechgate.CpuGate, line: str) -> None:
|
||||||
|
if self.holding and line not in phrasing.PROCEDURE_LINES:
|
||||||
|
raise errors.Held("busy")
|
||||||
|
admit(gate_self, line)
|
||||||
|
|
||||||
|
for patch in (mock.patch.object(hub.voice_notes, "get", get), mock.patch.object(speechgate.CpuGate, "admit", gate)):
|
||||||
|
patch.start()
|
||||||
|
self.addCleanup(patch.stop)
|
||||||
|
|
||||||
|
def radio(self, cls: str, to: list[str], text: str) -> None:
|
||||||
|
self.sent += 1
|
||||||
|
row = {"ts": time.time(), "from": "Stage", "to": to, "text": f"{text} {self.sent}", "class": cls, "v": 2}
|
||||||
|
with (self.council / "news.jsonl").open("a", encoding="utf-8") as handle:
|
||||||
|
handle.write(self.jsonl([row]))
|
||||||
|
|
||||||
|
def steps(self, browser: str, signed: bool) -> list[dict]:
|
||||||
|
steps = []
|
||||||
|
for width, height in base.WIDTHS:
|
||||||
|
shot = str(OUT / f"{browser}-{width}-radio")
|
||||||
|
url = f"{self.base}?{width}#stream"
|
||||||
|
value = str(THRESHOLDS[width])
|
||||||
|
steps += [
|
||||||
|
{"width": width, "height": height, "url": url, "wait": 1200, "probe": probe(FACE), "shot": f"{shot}-faceplate.png", "gesture": True},
|
||||||
|
{"width": width, "height": height, "url": None, "wait": 0, "probe": probe(LOG), "shot": f"{shot}-log-filtered.png"},
|
||||||
|
{"width": width, "height": height, "url": f"{url}-kept", "wait": 1200, "probe": probe(LOG_KEPT), "shot": f"{shot}-log.png"},
|
||||||
|
{"width": width, "height": height, "url": None, "wait": 0, "probe": probe(SOUND), "shot": f"{shot}-silence.png", "gesture": True},
|
||||||
|
{"width": width, "height": height, "url": f"{url}-mode", "wait": 1200, "probe": probe(SOUND_KEPT), "shot": None, "gesture": True},
|
||||||
|
{"width": width, "height": height, "url": None, "wait": 0, "probe": probe(BURST), "shot": f"{shot}-burst.png", "gesture": True},
|
||||||
|
{"width": width, "height": height, "url": None, "wait": 0, "probe": probe(BURST_DONE), "shot": None},
|
||||||
|
{"width": width, "height": height, "url": None, "wait": 0, "probe": probe(THRESHOLD).replace("@VALUE@", value), "shot": f"{shot}-dark.png", "gesture": True},
|
||||||
|
{"width": width, "height": height, "url": None, "wait": 0, "probe": probe(LIGHT), "shot": None},
|
||||||
|
]
|
||||||
|
return steps
|
||||||
|
|
||||||
|
def check(self, data: dict, browser: str, signed: bool) -> None:
|
||||||
|
results = iter(data["results"])
|
||||||
|
for index, (width, _) in enumerate(base.WIDTHS):
|
||||||
|
face, log, kept, sound, mode, burst, done, threshold, light = (next(results) for _ in range(9))
|
||||||
|
with self.subTest(browser=browser, width=width, part="faceplate"):
|
||||||
|
self.assertNotIn("error", face, face)
|
||||||
|
self.assertEqual(face["display"]["mode"], "Auto")
|
||||||
|
self.assertEqual((face["display"]["cpu"], face["display"]["lamp"]), ("CPU 10 %", ["RX", "false"]), face)
|
||||||
|
self.assertTrue(face["display"]["cls"], face)
|
||||||
|
self.assertEqual([mode_[:2] for mode_ in face["modes"]], [["Auto", "true"], ["Quiet", "false"], ["Silence", "false"]])
|
||||||
|
self.assertTrue(all(mode_[2] and mode_[3] for mode_ in face["modes"]), face["modes"])
|
||||||
|
self.assertEqual([knob[0] for knob in face["knobs"]], ["MAYDAY / PAN-PAN", "Reviews, asks, decisions", "Post and calls to you", "All radio", "Squelch", "Threshold"])
|
||||||
|
self.assertEqual(face["knobs"][-1][1:], ["30", "90", "10", "50" if index == 0 or not signed else str(THRESHOLDS[base.WIDTHS[index - 1][0]])])
|
||||||
|
self.assertEqual(len(face["toneToggles"]), 4)
|
||||||
|
self.assertEqual([label["text"] for label in face["labels"]], [knob[0] for knob in face["knobs"]])
|
||||||
|
for label in face["labels"]:
|
||||||
|
self.assertEqual((label["title"], label["row"]), (label["text"], True), label)
|
||||||
|
self.assertTrue(label["whole"] or width == 390, label)
|
||||||
|
self.assertEqual(face["groups"]["Classes"][4], ["MAYDAY", "true", True])
|
||||||
|
self.assertEqual([entry[1] for entry in face["groups"]["Classes"]], ["false", "false", "false", "false", "true", "true", "true"])
|
||||||
|
self.assertIn(["Stage", "true", False], face["groups"]["Agents"])
|
||||||
|
self.assertEqual(face["stationsHead"], "Internet radio")
|
||||||
|
self.assertEqual(face["ptt"][0], "PTT")
|
||||||
|
self.assertEqual([section for section in face["sections"] if section in ("Internet radio", "Tones", "Read radio aloud", "Radio")], [])
|
||||||
|
self.assertEqual(face["stations"], ["Stations", ["Internet radio"]])
|
||||||
|
self.assertEqual((face["fits"], face["unlabelled"]), ({"page": True, "panel": True}, 0))
|
||||||
|
with self.subTest(browser=browser, width=width, part="log"):
|
||||||
|
self.assertNotIn("error", log, log)
|
||||||
|
every = log["every"]
|
||||||
|
self.assertEqual(log["counts"], {"sent": len(log["sent"]), "received": len(log["received"]), "all": len(every)})
|
||||||
|
self.assertEqual([row["ts"] for row in every], sorted((row["ts"] for row in every), reverse=True))
|
||||||
|
self.assertTrue(all(row["dir"] == "sent" and row["route"].startswith("Operator →") for row in log["sent"]), log["sent"])
|
||||||
|
self.assertTrue(all(row["route"].endswith("→ all") or row["route"].endswith("→ Operator") or row["cls"] in ("pan-pan", "security", "mayday") for row in log["received"]), log["received"])
|
||||||
|
self.assertTrue(all(row["cls"] == "post" for row in log["posts"]))
|
||||||
|
self.assertEqual([row["line"] for row in log["fromStage"]], ["rebase done"])
|
||||||
|
self.assertEqual([row["line"] for row in log["narrowed"]], ["rebase done"])
|
||||||
|
self.assertEqual(log["none"][0], 0)
|
||||||
|
self.assertEqual(log["clearedChips"], [{"class": "", "from": "", "to": "", "agent": ""}, "", len(every)])
|
||||||
|
self.assertEqual(log["opened"][0], True)
|
||||||
|
self.assertTrue(log["opened"][1], log)
|
||||||
|
self.assertEqual(log["fits"], {"page": True, "panel": True})
|
||||||
|
if index == 0:
|
||||||
|
self.assertEqual(log["counts"], {"sent": 2, "received": 3, "all": 5})
|
||||||
|
self.assertEqual([row["line"] for row in log["received"]], ["deploy now", "docs are green", "gate red on main"])
|
||||||
|
self.assertEqual([row["line"] for row in log["sent"]], ["deploy now", "rebase please"])
|
||||||
|
with self.subTest(browser=browser, width=width, part="log kept"):
|
||||||
|
self.assertEqual((kept["tab"], kept["chips"]["agent"], kept["q"]), ("received", "Stage", "gate"), kept)
|
||||||
|
self.assertEqual([row["line"] for row in kept["rows"]], [row["line"] for row in log["kept"]])
|
||||||
|
self.assertIn("gate red on main", [row["line"] for row in kept["rows"]])
|
||||||
|
self.assertEqual(kept["fits"], {"page": True, "panel": True})
|
||||||
|
if width == 390:
|
||||||
|
self.assertGreaterEqual(kept["total"], 10, kept)
|
||||||
|
self.assertGreaterEqual(kept["shown"], 10, kept)
|
||||||
|
with self.subTest(browser=browser, width=width, part="sound"):
|
||||||
|
self.assertNotIn("error", sound, sound)
|
||||||
|
self.assertTrue(sound["low"], sound)
|
||||||
|
auto, held = sound["auto"], sound["held"]
|
||||||
|
self.assertEqual([entry[1] for entry in auto["tts"]], [200, 200], auto)
|
||||||
|
self.assertEqual(auto["tts"][0][0], WORD)
|
||||||
|
self.assertTrue(auto["tts"][1][0].startswith("all stations, this is Stage, Disk at ninety percent"), auto)
|
||||||
|
self.assertEqual((auto["filters"], auto["requests"]), ([], []), auto)
|
||||||
|
self.assertEqual(len(auto["starts"]), 2, auto)
|
||||||
|
self.assertEqual([entry[1] for entry in held["tts"]], [200, 503], held)
|
||||||
|
self.assertEqual(held["tts"][0][0], WORD)
|
||||||
|
self.assertEqual([kind for kind, _ in held["filters"]], ["bandpass"], held)
|
||||||
|
self.assertEqual(held["starts"][-1], {"seconds": 0.6, "channels": 1}, held)
|
||||||
|
self.assertEqual(sound["retries"], 0, "a held row is not requested again")
|
||||||
|
quiet, silence = sound["quiet"], sound["silence"]
|
||||||
|
self.assertEqual([text for text, _ in quiet["tts"]], [WORD], quiet)
|
||||||
|
self.assertEqual([kind for kind, _ in quiet["filters"]], ["bandpass"], quiet)
|
||||||
|
self.assertGreater(quiet["oscillators"], 0, quiet)
|
||||||
|
self.assertEqual([text for text, _ in silence["tts"]], [WORD], silence)
|
||||||
|
self.assertEqual((silence["filters"], silence["oscillators"], len(silence["starts"])), ([], 0, 1), silence)
|
||||||
|
self.assertEqual((sound["pressed"], sound["mode"]), (["silence"], "Silence"))
|
||||||
|
with self.subTest(browser=browser, width=width, part="mode kept"):
|
||||||
|
self.assertEqual(mode, {"kept": [["silence"], "Silence"], "pressed": ["auto"]})
|
||||||
|
with self.subTest(browser=browser, width=width, part="burst"):
|
||||||
|
self.assertNotIn("error", burst, burst)
|
||||||
|
self.assertEqual((burst["high"], burst["lit"], burst["open"]), (True, True, "false"), burst)
|
||||||
|
self.assertEqual(burst["lamp"], ["RX", "RX: sound plays"])
|
||||||
|
heard = burst["heard"]
|
||||||
|
self.assertEqual(heard["tts"], [[WORD, 200]], heard)
|
||||||
|
self.assertEqual([kind for kind, _ in heard["filters"]], ["bandpass"])
|
||||||
|
self.assertTrue(300 <= heard["filters"][0][1] <= 3400, heard)
|
||||||
|
self.assertIn({"seconds": 0.6, "channels": 1}, heard["starts"])
|
||||||
|
self.assertIn(0.3, heard["levels"], heard)
|
||||||
|
self.assertEqual(heard["requests"], [], heard)
|
||||||
|
self.assertEqual((done["tts"][-1], done["low"]), ([WORD, 200], True), done)
|
||||||
|
with self.subTest(browser=browser, width=width, part="threshold and dark"):
|
||||||
|
self.assertNotIn("error", threshold, threshold)
|
||||||
|
value = str(THRESHOLDS[width])
|
||||||
|
if signed:
|
||||||
|
self.assertEqual(threshold["after"], [value, threshold["before"][1] + 1], threshold)
|
||||||
|
self.assertEqual(threshold["shown"], f"{value} %")
|
||||||
|
self.assertIn(f"below {value} %", threshold["title"])
|
||||||
|
else:
|
||||||
|
self.assertEqual(threshold["after"], [threshold["before"][0], threshold["before"][1]], threshold)
|
||||||
|
self.assertIn("Settings: not signed", threshold["toasts"])
|
||||||
|
self.assertTrue(threshold["dark"][1] and threshold["dark"][0] != threshold["dark"][1], threshold)
|
||||||
|
self.assertEqual(threshold["fits"], {"page": True, "panel": True})
|
||||||
|
self.assertEqual([seen for seen in light["violations"] if not seen.endswith("/favicon.ico")], [])
|
||||||
|
words = [row for row in log_rows(self.engines.scratch / "piper.jsonl") if row["text"] == f"{WORD}\n"]
|
||||||
|
self.assertEqual(len(words), 1, "the procedure word line of one voice renders once and then hits the cache")
|
||||||
|
self.assertEqual(self.httpd.hub.settings.get()["speech"]["threshold"], THRESHOLDS[390] if signed else 50)
|
||||||
|
|
||||||
|
def test_chromium_plays_the_radio_set_modes_burst_and_procedure_words_and_filters_the_log_at_three_widths(self):
|
||||||
|
data = self.drive(self.steps("chromium", True), self.authenticator(), init=SPY)
|
||||||
|
(OUT / "chromium-radio-set.json").write_text(json.dumps(data, indent=1), encoding="utf-8")
|
||||||
|
self.assertEqual(data["errors"], [])
|
||||||
|
self.check(data, "chromium", True)
|
||||||
|
|
||||||
|
@unittest.skipUnless(shutil.which(os.environ.get("FIREFOX", "firefox")), "no Firefox")
|
||||||
|
def test_firefox_plays_the_radio_set_modes_burst_and_procedure_words_and_keeps_the_threshold_unsigned_at_three_widths(self):
|
||||||
|
data = self.drive(self.steps("firefox", False), None, "bidi.mjs", os.environ.get("FIREFOX", "firefox"), init=SPY)
|
||||||
|
(OUT / "firefox-radio-set.json").write_text(json.dumps(data, indent=1), encoding="utf-8")
|
||||||
|
self.assertEqual([error for error in data["errors"] if "/favicon.ico" not in error], [])
|
||||||
|
self.check(data, "firefox", False)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
@@ -6,6 +6,7 @@ import shutil
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
|
from collections.abc import Callable
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
@@ -36,6 +37,11 @@ FADE_SLACK = 50
|
|||||||
GUARD = """() => {
|
GUARD = """() => {
|
||||||
window.e2eViolations = [];
|
window.e2eViolations = [];
|
||||||
document.addEventListener("securitypolicyviolation", (event) => window.e2eViolations.push(`${event.violatedDirective} ${event.blockedURI}`));
|
document.addEventListener("securitypolicyviolation", (event) => window.e2eViolations.push(`${event.violatedDirective} ${event.blockedURI}`));
|
||||||
|
try {
|
||||||
|
localStorage.setItem("kvb-orchestrator.radio", JSON.stringify({mode: "silence", classes: ["broadcast"]}));
|
||||||
|
} catch (error) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}"""
|
}"""
|
||||||
HOOKED = f"() => {{\n ({GUARD})();\n ({base.GRAPH_HOOK})();\n}}"
|
HOOKED = f"() => {{\n ({GUARD})();\n ({base.GRAPH_HOOK})();\n}}"
|
||||||
READ = """
|
READ = """
|
||||||
@@ -233,7 +239,7 @@ class TestWorkbenchBubbles(HubCase):
|
|||||||
hub = self.httpd.hub
|
hub = self.httpd.hub
|
||||||
spoken = base.tone(1, 8000)
|
spoken = base.tone(1, 8000)
|
||||||
|
|
||||||
def synthesize(body: bytes) -> bytes:
|
def synthesize(body: bytes, admit: Callable[[str], None]) -> bytes:
|
||||||
word = json.loads(body)["text"]
|
word = json.loads(body)["text"]
|
||||||
if word == f"{CUE}-long":
|
if word == f"{CUE}-long":
|
||||||
with (self.council / "news.jsonl").open("a", encoding="utf-8") as handle:
|
with (self.council / "news.jsonl").open("a", encoding="utf-8") as handle:
|
||||||
@@ -241,8 +247,8 @@ class TestWorkbenchBubbles(HubCase):
|
|||||||
elif word.startswith(CUE):
|
elif word.startswith(CUE):
|
||||||
now = time.time()
|
now = time.time()
|
||||||
rows = [
|
rows = [
|
||||||
{"ts": now - 0.01, "from": "Stage", "to": [], "text": FIRST, "class": "security", "v": 2},
|
{"ts": now - 0.01, "from": "Stage", "to": [], "text": FIRST, "class": "broadcast", "v": 2},
|
||||||
{"ts": now, "from": "Docs" if word.endswith("-docs") else "Skills session", "to": [], "text": SECOND, "class": "security", "v": 2},
|
{"ts": now, "from": "Docs" if word.endswith("-docs") else "Skills session", "to": [], "text": SECOND, "class": "broadcast", "v": 2},
|
||||||
]
|
]
|
||||||
with (self.council / "news.jsonl").open("a", encoding="utf-8") as handle:
|
with (self.council / "news.jsonl").open("a", encoding="utf-8") as handle:
|
||||||
handle.write(self.jsonl(rows))
|
handle.write(self.jsonl(rows))
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ from hub_support import (
|
|||||||
server,
|
server,
|
||||||
signing,
|
signing,
|
||||||
)
|
)
|
||||||
|
from kvb_orchestrator import engines
|
||||||
|
|
||||||
|
|
||||||
class HubCase(unittest.TestCase):
|
class HubCase(unittest.TestCase):
|
||||||
@@ -246,6 +247,29 @@ class HubCase(unittest.TestCase):
|
|||||||
def jsonl(self, rows: list[dict]) -> str:
|
def jsonl(self, rows: list[dict]) -> str:
|
||||||
return "".join(json.dumps(row) + "\n" for row in rows)
|
return "".join(json.dumps(row) + "\n" for row in rows)
|
||||||
|
|
||||||
|
def host_cpu(self, cpu: float | None, age: float) -> None:
|
||||||
|
"""Args:
|
||||||
|
cpu: the host CPU percent of the newest W4 sample, None unknown.
|
||||||
|
age: how many seconds before now the sampler wrote it.
|
||||||
|
"""
|
||||||
|
folder = self.kept / "resources"
|
||||||
|
folder.mkdir(exist_ok=True)
|
||||||
|
now = time.time()
|
||||||
|
rows = [{"t": now - age - 60, "cpu": 5.0, "ram": 20.0}, {"t": now - age, "cpu": cpu, "ram": 20.0}]
|
||||||
|
written = folder / f".samples-{threading.get_ident()}.jsonl"
|
||||||
|
written.write_text(self.jsonl(rows), encoding="utf-8")
|
||||||
|
os.replace(written, folder / "samples.jsonl")
|
||||||
|
|
||||||
|
def hand_out(self, voice: str) -> None:
|
||||||
|
"""Args:
|
||||||
|
voice: an installed voice the hub then hands out to a speaker, so
|
||||||
|
/api/tts accepts it.
|
||||||
|
"""
|
||||||
|
speech = self.httpd.hub.speech
|
||||||
|
keys = [f"s{number}" for number in range(1000)]
|
||||||
|
key = next((key for key in keys if engines.voice_of(key, speech.pool, speech.operator) == voice), "Operator")
|
||||||
|
self.assertEqual(speech.voice_of(key), voice)
|
||||||
|
|
||||||
def post_json(self, path: str, payload: object, headers: dict[str, str]) -> tuple[int, dict, bytes]:
|
def post_json(self, path: str, payload: object, headers: dict[str, str]) -> tuple[int, dict, bytes]:
|
||||||
return self.call("POST", path, json.dumps(payload).encode(), {**headers, "Content-Type": "application/json"})
|
return self.call("POST", path, json.dumps(payload).encode(), {**headers, "Content-Type": "application/json"})
|
||||||
|
|
||||||
|
|||||||
@@ -41,16 +41,16 @@ const code = fs.readFileSync(process.argv[2], "utf8");
|
|||||||
const settle = () => new Promise((resolve) => setImmediate(resolve));
|
const settle = () => new Promise((resolve) => setImmediate(resolve));
|
||||||
async function failed(bubbles, item, cancel) {
|
async function failed(bubbles, item, cancel) {
|
||||||
const shown = [];
|
const shown = [];
|
||||||
const speech = {pending: [], busy: false, current: null, loading: null, timer: 0, warned: 0};
|
const speech = {pending: [], busy: false, current: null, loading: null, item: null, timer: 0, warned: 0};
|
||||||
const fetch = async () => {
|
const fetch = async () => {
|
||||||
if (cancel) {
|
if (cancel) {
|
||||||
speech.loading = null;
|
speech.item = null;
|
||||||
}
|
}
|
||||||
return {ok: false, status: 503, text: async () => "tts down "};
|
return {ok: false, status: 503, headers: {get: () => "text/plain"}, text: async () => "tts down "};
|
||||||
};
|
};
|
||||||
vm.runInNewContext(`${code}\\nsay(item);`, {
|
vm.runInNewContext(`${code}\\nsay(item);`, {
|
||||||
Bubbles, speech, fetch, item, Date, settings: {bubbles}, SPEECH_QUEUE: 9, SPEECH_QUIET: 0,
|
Bubbles, speech, fetch, item, Date, settings: {bubbles}, SPEECH_QUEUE: 9, SPEECH_QUIET: 0, radioSet: {},
|
||||||
Voice: {enqueue: (pending, next) => [...pending, next]},
|
Voice: {enqueue: (pending, next) => [...pending, next], loudness: () => 0}, RadioSet: {burst: (gain) => ({gain})},
|
||||||
toast: (title, lines) => shown.push(`${title}: ${lines.join(" ")}`),
|
toast: (title, lines) => shown.push(`${title}: ${lines.join(" ")}`),
|
||||||
context: () => ({}), drawBubbles: () => {}, drawSpeaker: () => {},
|
context: () => ({}), drawBubbles: () => {}, drawSpeaker: () => {},
|
||||||
performance: {now: () => 1000}, window: {setTimeout: () => 7, clearTimeout: () => {}},
|
performance: {now: () => 1000}, window: {setTimeout: () => 7, clearTimeout: () => {}},
|
||||||
@@ -61,12 +61,14 @@ async function failed(bubbles, item, cancel) {
|
|||||||
return {current: speech.current === item, busy: speech.busy, timer: speech.timer, started: item.started ?? null, duration: item.duration ?? null, shown};
|
return {current: speech.current === item, busy: speech.busy, timer: speech.timer, started: item.started ?? null, duration: item.duration ?? null, shown};
|
||||||
}
|
}
|
||||||
(async () => {
|
(async () => {
|
||||||
const row = () => ({text: "t", who: "A", line: "Disk almost full", words: ["Disk", "almost", "full"]});
|
const row = () => ({steps: [{kind: "speech", text: "t"}], who: "A", line: "Disk almost full", words: ["Disk", "almost", "full"]});
|
||||||
console.log(JSON.stringify({
|
console.log(JSON.stringify({
|
||||||
fallback: await failed(true, row(), false),
|
fallback: await failed(true, row(), false),
|
||||||
off: await failed(false, row(), false),
|
off: await failed(false, row(), false),
|
||||||
sample: await failed(true, {text: "t", who: "Operator", line: "radio check"}, false),
|
sample: await failed(true, {steps: [{kind: "speech", text: "t"}], who: "Operator", line: "radio check"}, false),
|
||||||
cancelled: await failed(true, row(), true),
|
cancelled: await failed(true, row(), true),
|
||||||
|
burst: await failed(true, {...row(), steps: [{kind: "burst"}]}, false),
|
||||||
|
unread: await failed(false, {...row(), steps: [{kind: "burst"}]}, false),
|
||||||
}));
|
}));
|
||||||
})();
|
})();
|
||||||
"""
|
"""
|
||||||
@@ -190,6 +192,12 @@ class TestSpeechFallback(unittest.TestCase):
|
|||||||
with self.subTest(case=case):
|
with self.subTest(case=case):
|
||||||
self.assertEqual(data[case], {"current": False, "busy": False, "timer": 0, "started": None, "duration": None, "shown": failed})
|
self.assertEqual(data[case], {"current": False, "busy": False, "timer": 0, "started": None, "duration": None, "shown": failed})
|
||||||
|
|
||||||
|
def test_a_row_the_burst_stands_in_for_reads_at_reading_pace_only_while_bubbles_are_on(self):
|
||||||
|
done = subprocess.run(["node", "-e", SPEECH_PROBE, str(BUBBLES), str(SPEECH)], capture_output=True, text=True, timeout=60, check=True)
|
||||||
|
data = json.loads(done.stdout)
|
||||||
|
self.assertEqual(data["burst"], {"current": True, "busy": True, "timer": 7, "started": 1000, "duration": 1.5, "shown": []})
|
||||||
|
self.assertEqual(data["unread"], {"current": False, "busy": False, "timer": 0, "started": None, "duration": None, "shown": []})
|
||||||
|
|
||||||
|
|
||||||
class TestBubbleStop(unittest.TestCase):
|
class TestBubbleStop(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|||||||
@@ -0,0 +1,635 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import subprocess
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from hub_support import HUB
|
||||||
|
from kvb_orchestrator import council
|
||||||
|
|
||||||
|
RADIOSET = HUB / "templates" / "radioset.js"
|
||||||
|
PROBE = """
|
||||||
|
const R = require(process.argv[1]);
|
||||||
|
const input = JSON.parse(process.argv[2]);
|
||||||
|
const out = {};
|
||||||
|
for (const [name, args] of Object.entries(input)) {
|
||||||
|
out[name] = args.map((call) => R[call[0]](...call.slice(1)));
|
||||||
|
}
|
||||||
|
out.constants = {key: R.KEY, modes: R.MODES, tones: R.TONES, classes: R.CLASSES, tabs: R.TABS, chips: R.CHIPS, defaults: R.DEFAULTS};
|
||||||
|
console.log(JSON.stringify(out));
|
||||||
|
"""
|
||||||
|
PAGE_LOAD = HUB / "templates" / "page-load.js"
|
||||||
|
STALE_PROBE = """
|
||||||
|
const fs = require("fs");
|
||||||
|
const vm = require("vm");
|
||||||
|
const code = fs.readFileSync(process.argv[1], "utf8");
|
||||||
|
const stub = () => undefined;
|
||||||
|
const settle = async () => {
|
||||||
|
for (let round = 0; round < 8; round += 1) {
|
||||||
|
await new Promise((resolve) => setImmediate(resolve));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
function page() {
|
||||||
|
const pending = [];
|
||||||
|
const announced = [];
|
||||||
|
const timers = [];
|
||||||
|
const fetch = (url, options) => new Promise((resolve, reject) => {
|
||||||
|
pending.push({resolve, reject});
|
||||||
|
if (options && options.signal) {
|
||||||
|
options.signal.addEventListener("abort", () => reject(new Error("aborted")));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const context = {
|
||||||
|
fetch, AbortController, STATE_TIMEOUT: 15000, windowQuery: () => "", FADE: 0, MOVE: 0,
|
||||||
|
state: {now: 0, radio: []}, ui: {offline: false}, settingsSig: "", motion: {current: new Map([[1, 1]]), asOf: null},
|
||||||
|
window: {getSelection: () => "", setTimeout: (run, ms) => timers.push({run, ms}), clearTimeout: stub}, document: {hidden: false},
|
||||||
|
adoptSettings: stub, tuneMusic: stub, drawSky: stub, render: stub, drawTime: stub, drawAgent: stub, drawStream: stub, relayout: stub, pulses: stub, tick: stub,
|
||||||
|
};
|
||||||
|
context.announce = () => announced.push(context.state.now);
|
||||||
|
vm.runInNewContext(code, context);
|
||||||
|
return {context, pending, announced, timers};
|
||||||
|
}
|
||||||
|
const answer = (now) => ({ok: true, json: async () => ({now, radio: []})});
|
||||||
|
(async () => {
|
||||||
|
const slow = page();
|
||||||
|
slow.context.load();
|
||||||
|
for (let ticket = 1; ticket <= 6; ticket += 1) {
|
||||||
|
slow.context.load();
|
||||||
|
slow.pending[ticket - 1].resolve(answer(ticket));
|
||||||
|
await settle();
|
||||||
|
}
|
||||||
|
slow.pending[6].resolve(answer(7));
|
||||||
|
await settle();
|
||||||
|
const stale = page();
|
||||||
|
stale.context.load();
|
||||||
|
stale.context.load();
|
||||||
|
stale.pending[1].resolve(answer(3));
|
||||||
|
await settle();
|
||||||
|
stale.pending[0].resolve(answer(2));
|
||||||
|
await settle();
|
||||||
|
const failing = page();
|
||||||
|
failing.context.load();
|
||||||
|
failing.context.load();
|
||||||
|
failing.pending[1].resolve(answer(5));
|
||||||
|
await settle();
|
||||||
|
failing.pending[0].reject(new Error("503"));
|
||||||
|
await settle();
|
||||||
|
const lateError = failing.context.ui.offline;
|
||||||
|
failing.context.load();
|
||||||
|
failing.pending[2].reject(new Error("503"));
|
||||||
|
await settle();
|
||||||
|
const polling = page();
|
||||||
|
polling.context.load();
|
||||||
|
polling.context.poll();
|
||||||
|
const busy = polling.pending.length;
|
||||||
|
polling.pending[0].resolve(answer(1));
|
||||||
|
await settle();
|
||||||
|
polling.context.poll();
|
||||||
|
const hung = page();
|
||||||
|
for (let tick = 0; tick < 60; tick += 1) {
|
||||||
|
hung.context.poll();
|
||||||
|
await settle();
|
||||||
|
}
|
||||||
|
const stuck = [hung.pending.length, hung.context.ui.offline];
|
||||||
|
hung.timers.forEach((timer) => timer.run());
|
||||||
|
await settle();
|
||||||
|
hung.context.poll();
|
||||||
|
console.log(JSON.stringify({
|
||||||
|
slow: slow.announced, stale: [stale.context.state.now, stale.announced], errors: [lateError, failing.context.ui.offline], polls: [busy, polling.pending.length],
|
||||||
|
hung: {stuck, freed: [hung.pending.length, hung.context.ui.offline], waits: hung.timers.map((timer) => timer.ms)},
|
||||||
|
}));
|
||||||
|
})();
|
||||||
|
"""
|
||||||
|
PAGE_SPEECH = HUB / "templates" / "page-speech.js"
|
||||||
|
PAGE_RADIOVIEW = HUB / "templates" / "page-radioview.js"
|
||||||
|
TRANSMIT_PROBE = """
|
||||||
|
const fs = require("fs");
|
||||||
|
const vm = require("vm");
|
||||||
|
const RadioSet = require(process.argv[1]);
|
||||||
|
const speechCode = fs.readFileSync(process.argv[2], "utf8");
|
||||||
|
const viewCode = fs.readFileSync(process.argv[3], "utf8");
|
||||||
|
const stub = () => undefined;
|
||||||
|
const settle = async () => {
|
||||||
|
for (let round = 0; round < 40; round += 1) {
|
||||||
|
await new Promise((resolve) => setImmediate(resolve));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const row = {class: "security", source: "s1", voice: "v1", word: "W", rest: "R", text: "t"};
|
||||||
|
async function queued(flip) {
|
||||||
|
const heard = [];
|
||||||
|
const sandbox = {
|
||||||
|
RadioSet, radioSet: RadioSet.valid({mode: "auto"}), state: {speech: {tts: true, gate: {open: true}}},
|
||||||
|
speech: {pending: [], busy: false, current: null, source: null, item: null, timer: 0}, settings: {bubbles: false}, SPEECH_QUEUE: 9,
|
||||||
|
Voice: {enqueue: (pending, next) => [...pending, next]}, drawBubbles: stub, drawSpeaker: stub,
|
||||||
|
};
|
||||||
|
vm.runInNewContext(speechCode, sandbox);
|
||||||
|
sandbox.context = () => ({});
|
||||||
|
sandbox.spoken = async (audio, item, step) => {
|
||||||
|
heard.push(step.text);
|
||||||
|
if (heard.length === 1 && flip) {
|
||||||
|
sandbox.radioSet = RadioSet.valid({mode: flip});
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
};
|
||||||
|
sandbox.burst = async () => {
|
||||||
|
heard.push("burst");
|
||||||
|
};
|
||||||
|
sandbox.say({row});
|
||||||
|
sandbox.say({row});
|
||||||
|
await settle();
|
||||||
|
return heard;
|
||||||
|
}
|
||||||
|
async function stalled() {
|
||||||
|
const waits = [];
|
||||||
|
const sandbox = {
|
||||||
|
speech: {current: null, source: null}, SOUND_SLACK: 2, performance: {now: () => 0}, duck: stub, drawSpeaker: stub,
|
||||||
|
window: {setTimeout: (run, ms) => waits.push({run, ms}), clearTimeout: stub},
|
||||||
|
};
|
||||||
|
vm.runInNewContext(speechCode, sandbox);
|
||||||
|
let done = false;
|
||||||
|
let stops = 0;
|
||||||
|
const source = {addEventListener: stub, stop: () => {
|
||||||
|
stops += 1;
|
||||||
|
}};
|
||||||
|
sandbox.sounded({}, source, 0.5).then(() => {
|
||||||
|
done = true;
|
||||||
|
});
|
||||||
|
await settle();
|
||||||
|
const before = [done, sandbox.speech.source === source];
|
||||||
|
waits.forEach((wait) => wait.run());
|
||||||
|
await settle();
|
||||||
|
return {before, after: [done, sandbox.speech.source], waits: waits.map((wait) => wait.ms), stops};
|
||||||
|
}
|
||||||
|
function pressed() {
|
||||||
|
const calls = [];
|
||||||
|
const sandbox = {RadioSet, h: (tag, attrs, ...children) => ({tag, attrs: attrs || {}, children: children.flat()}), icon: (name) => name, named: (label, hint) => ({"aria-label": label, title: hint})};
|
||||||
|
vm.runInNewContext(`${viewCode}\\nradioDom = {};`, sandbox);
|
||||||
|
sandbox.knobs = () => [];
|
||||||
|
sandbox.context = () => calls.push("context");
|
||||||
|
sandbox.keepRadio = (change) => calls.push(change);
|
||||||
|
const modes = sandbox.faceplate().children.find((child) => child.attrs["aria-label"] === "Mode");
|
||||||
|
modes.children.forEach((button) => button.attrs.on.click());
|
||||||
|
return calls;
|
||||||
|
}
|
||||||
|
(async () => {
|
||||||
|
console.log(JSON.stringify({
|
||||||
|
auto: await queued(""), silence: await queued("silence"), quiet: await queued("quiet"), stalled: await stalled(), pressed: pressed(),
|
||||||
|
}));
|
||||||
|
})();
|
||||||
|
"""
|
||||||
|
VOICE_JS = HUB / "templates" / "voice.js"
|
||||||
|
MODES_PROBE = """
|
||||||
|
const fs = require("fs");
|
||||||
|
const vm = require("vm");
|
||||||
|
const RadioSet = require(process.argv[1]);
|
||||||
|
const Voice = require(process.argv[2]);
|
||||||
|
const code = fs.readFileSync(process.argv[3], "utf8");
|
||||||
|
const stub = () => undefined;
|
||||||
|
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
const until = async (check) => {
|
||||||
|
for (let round = 0; round < 500 && !check(); round += 1) {
|
||||||
|
await wait(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const WORD = "MAYDAY MAYDAY MAYDAY";
|
||||||
|
const rest = (id) => `this is ${id}, over`;
|
||||||
|
function radio({running = true, hold = "", long = "", fail = 0, scale = 1000, bubbles = false} = {}) {
|
||||||
|
const run = {log: [], releases: [], gesture: running, sources: [], beeps: 0, reads: 0};
|
||||||
|
class Source {
|
||||||
|
constructor() {
|
||||||
|
Object.assign(this, {buffer: null, ended: null, stopped: false, audible: false});
|
||||||
|
}
|
||||||
|
connect(node) {
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
addEventListener(type, ended) {
|
||||||
|
this.ended = ended;
|
||||||
|
}
|
||||||
|
label() {
|
||||||
|
return this.buffer && this.buffer.text ? this.buffer.text : "burst";
|
||||||
|
}
|
||||||
|
start() {
|
||||||
|
run.sources.push(this);
|
||||||
|
if (audio.state === "running") {
|
||||||
|
audio.play(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stop(when) {
|
||||||
|
if (when === undefined && !this.stopped) {
|
||||||
|
this.stopped = true;
|
||||||
|
run.log.push(["stop", this.label()]);
|
||||||
|
setImmediate(() => this.ended && this.ended());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const audio = {
|
||||||
|
state: running ? "running" : "suspended", currentTime: 0, sampleRate: 8000, destination: {},
|
||||||
|
play(source) {
|
||||||
|
if (!source.audible && !source.stopped) {
|
||||||
|
source.audible = true;
|
||||||
|
run.log.push(["audible", source.label(), sandbox.radioSet.mode]);
|
||||||
|
if (source.label() !== long) {
|
||||||
|
setImmediate(() => source.ended && source.ended());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
resume() {
|
||||||
|
if (run.gesture) {
|
||||||
|
audio.state = "running";
|
||||||
|
run.sources.forEach((source) => audio.play(source));
|
||||||
|
}
|
||||||
|
return Promise.resolve();
|
||||||
|
},
|
||||||
|
createBufferSource: () => new Source(),
|
||||||
|
createGain: () => ({gain: {value: 0, setValueAtTime: stub, exponentialRampToValueAtTime: stub}, connect: (node) => node}),
|
||||||
|
createBiquadFilter: () => ({type: "", frequency: {value: 0}, Q: {value: 0}, connect: (node) => node}),
|
||||||
|
createBuffer: (channels, frames) => ({getChannelData: () => new Float32Array(frames)}),
|
||||||
|
decodeAudioData: async (data) => ({duration: data.text === long ? 60 : 1, text: data.text}),
|
||||||
|
};
|
||||||
|
const sandbox = {
|
||||||
|
RadioSet, Voice, radioSet: RadioSet.valid(null), state: {now: 1000, messages: [], speech: {tts: true, gate: {open: true}}},
|
||||||
|
speech: {pending: [], busy: false, warned: 0, current: null, source: null, loading: null, timer: 0, item: null},
|
||||||
|
settings: {bubbles, music: {on: false, volume: 0.4}}, SPEECH_QUEUE: 20, SPEECH_QUIET: 60000, SOUND_SLACK: 2, HUMAN: "Operator", Bubbles: {reading: () => 0.001},
|
||||||
|
keep: stub, drawRadioSet: stub, drawBubbles: stub, drawSpeaker: stub, duck: stub, clearBubbles: stub, toast: (title) => run.log.push(["toast", title]),
|
||||||
|
silenceBeeps: () => {
|
||||||
|
run.beeps += 1;
|
||||||
|
},
|
||||||
|
performance: {now: () => 0}, window: {setTimeout: (next, ms) => setTimeout(next, ms / scale), clearTimeout: (id) => clearTimeout(id)},
|
||||||
|
context: () => {
|
||||||
|
if (audio.state === "suspended") {
|
||||||
|
audio.resume();
|
||||||
|
}
|
||||||
|
return audio;
|
||||||
|
},
|
||||||
|
fetch: async (url, options) => {
|
||||||
|
const text = JSON.parse(options.body).text;
|
||||||
|
run.log.push(["tts", text]);
|
||||||
|
if (text === hold) {
|
||||||
|
await new Promise((resolve) => run.releases.push(resolve));
|
||||||
|
}
|
||||||
|
if (fail && text !== WORD) {
|
||||||
|
return {ok: false, status: fail, headers: {get: () => (fail === 503 ? "application/json" : "text/plain")}, text: async () => (fail === 503 ? "{\\"reason\\": \\"busy\\"}" : "piper failed")};
|
||||||
|
}
|
||||||
|
return {ok: true, status: 200, headers: {get: () => "audio/wav"}, arrayBuffer: async () => ({text}), text: async () => ""};
|
||||||
|
},
|
||||||
|
};
|
||||||
|
vm.runInNewContext(code, sandbox);
|
||||||
|
const read = sandbox.readSilently;
|
||||||
|
sandbox.readSilently = (item, next) => {
|
||||||
|
run.reads += 1;
|
||||||
|
read(item, next);
|
||||||
|
};
|
||||||
|
run.state = sandbox.state;
|
||||||
|
run.finish = () => run.sources.filter((source) => source.audible && !source.stopped).forEach((source) => source.ended && source.ended());
|
||||||
|
run.say = (id) => sandbox.say({row: {class: "mayday", word: WORD, rest: rest(id), voice: "v", source: id, from: id, to: [], text: "help", ts: 1000}, voice: "v", tone: "mayday", cls: "mayday", who: id, line: "help", words: [], silent: false});
|
||||||
|
run.press = (mode) => {
|
||||||
|
sandbox.keepRadio({mode});
|
||||||
|
sandbox.context();
|
||||||
|
};
|
||||||
|
run.keep = (change) => sandbox.keepRadio(change);
|
||||||
|
run.heard = () => run.log.filter(([event]) => event === "audible").map(([, text, mode]) => [text, mode]);
|
||||||
|
run.stops = () => run.log.filter(([event]) => event === "stop").map(([, text]) => text);
|
||||||
|
run.idle = () => !sandbox.speech.busy && !sandbox.speech.pending.length;
|
||||||
|
return run;
|
||||||
|
}
|
||||||
|
async function pressedWhilePlaying(long, count, mode) {
|
||||||
|
const run = radio({long});
|
||||||
|
run.say("A");
|
||||||
|
await until(() => run.heard().length === count);
|
||||||
|
run.press(mode);
|
||||||
|
await wait(20);
|
||||||
|
return {heard: run.heard(), stops: run.stops()};
|
||||||
|
}
|
||||||
|
async function failedWhileRendering(fail, mode) {
|
||||||
|
const run = radio({hold: rest("A"), fail});
|
||||||
|
run.say("A");
|
||||||
|
await until(() => run.releases.length);
|
||||||
|
run.press(mode);
|
||||||
|
run.releases.forEach((release) => release());
|
||||||
|
await until(run.idle);
|
||||||
|
await wait(10);
|
||||||
|
return run.heard();
|
||||||
|
}
|
||||||
|
(async () => {
|
||||||
|
const inflight = radio({hold: rest("A")});
|
||||||
|
inflight.say("A");
|
||||||
|
await until(() => inflight.releases.length);
|
||||||
|
inflight.press("silence");
|
||||||
|
inflight.releases.forEach((release) => release());
|
||||||
|
await until(inflight.idle);
|
||||||
|
const playing = await pressedWhilePlaying(rest("A"), 2, "silence");
|
||||||
|
const word = await pressedWhilePlaying(WORD, 1, "silence");
|
||||||
|
const quiet = await pressedWhilePlaying(rest("A"), 2, "quiet");
|
||||||
|
const failed = {
|
||||||
|
silence503: await failedWhileRendering(503, "silence"), silence500: await failedWhileRendering(500, "silence"), quiet503: await failedWhileRendering(503, "quiet"),
|
||||||
|
};
|
||||||
|
const kept = radio({fail: 500, long: "burst", scale: 10});
|
||||||
|
kept.say("A");
|
||||||
|
await until(() => kept.heard().length === 2);
|
||||||
|
kept.keep({squelch: 0.5});
|
||||||
|
kept.keep({log: {tab: "all", chips: {}, q: "gate"}});
|
||||||
|
await wait(20);
|
||||||
|
const fallback = {heard: kept.heard(), stops: kept.stops()};
|
||||||
|
const beeper = radio({});
|
||||||
|
beeper.press("quiet");
|
||||||
|
const quietBeeps = beeper.beeps;
|
||||||
|
beeper.press("silence");
|
||||||
|
const silenceBeeps = beeper.beeps;
|
||||||
|
beeper.keep({squelch: 0.5});
|
||||||
|
beeper.press("silence");
|
||||||
|
const beeps = [quietBeeps, silenceBeeps, beeper.beeps];
|
||||||
|
const natural = {};
|
||||||
|
for (const flag of ["open", "tts"]) {
|
||||||
|
const run = radio({long: rest("A"), bubbles: true});
|
||||||
|
run.say("A");
|
||||||
|
await until(() => run.heard().length === 2);
|
||||||
|
if (flag === "open") {
|
||||||
|
run.state.speech.gate = {open: false};
|
||||||
|
} else {
|
||||||
|
run.state.speech.tts = false;
|
||||||
|
}
|
||||||
|
run.finish();
|
||||||
|
await until(run.idle);
|
||||||
|
await wait(10);
|
||||||
|
natural[flag] = {heard: run.heard(), reads: run.reads};
|
||||||
|
}
|
||||||
|
const renderedRun = radio({hold: rest("A")});
|
||||||
|
renderedRun.say("A");
|
||||||
|
await until(() => renderedRun.releases.length);
|
||||||
|
renderedRun.state.speech.gate = {open: false};
|
||||||
|
renderedRun.releases.forEach((release) => release());
|
||||||
|
await until(renderedRun.idle);
|
||||||
|
await wait(10);
|
||||||
|
const renderedLine = renderedRun.heard();
|
||||||
|
const suspended = radio({running: false});
|
||||||
|
suspended.say("B");
|
||||||
|
suspended.say("C");
|
||||||
|
await until(suspended.idle);
|
||||||
|
const started = suspended.sources.length;
|
||||||
|
suspended.gesture = true;
|
||||||
|
suspended.press("silence");
|
||||||
|
await wait(20);
|
||||||
|
console.log(JSON.stringify({
|
||||||
|
inflight: {tts: inflight.log.filter(([event]) => event === "tts").map(([, text]) => text), heard: inflight.heard()},
|
||||||
|
playing, word, quiet, failed, fallback, beeps, natural, renderedLine, suspended: {started, heard: suspended.heard(), stopped: suspended.sources.filter((source) => source.stopped).length},
|
||||||
|
}));
|
||||||
|
})();
|
||||||
|
"""
|
||||||
|
PAGE_ALERTS = HUB / "templates" / "page-alerts.js"
|
||||||
|
BEEPS_PROBE = """
|
||||||
|
const fs = require("fs");
|
||||||
|
const vm = require("vm");
|
||||||
|
const code = fs.readFileSync(process.argv[1], "utf8");
|
||||||
|
const stub = () => undefined;
|
||||||
|
const stops = [];
|
||||||
|
let made = 0;
|
||||||
|
const audio = {
|
||||||
|
currentTime: 0, destination: {},
|
||||||
|
createOscillator: () => {
|
||||||
|
made += 1;
|
||||||
|
return {type: "", frequency: {value: 0}, connect: (node) => node, addEventListener: stub, start: stub, stop: (when) => stops.push(when === undefined ? "now" : "scheduled")};
|
||||||
|
},
|
||||||
|
createGain: () => ({gain: {setValueAtTime: stub, exponentialRampToValueAtTime: stub}, connect: (node) => node}),
|
||||||
|
};
|
||||||
|
const sandbox = {TONES: {mayday: [[880, 0, 0.2], [660, 0.25, 0.2]], radio: [[440, 0, 0.1]]}, duck: stub, context: () => audio};
|
||||||
|
vm.runInNewContext(code, sandbox);
|
||||||
|
sandbox.play("mayday", 0.8);
|
||||||
|
sandbox.play("radio", 0.5);
|
||||||
|
const now = () => stops.filter((kind) => kind === "now").length;
|
||||||
|
const queued = [made, now()];
|
||||||
|
sandbox.silenceBeeps();
|
||||||
|
const silenced = now();
|
||||||
|
sandbox.silenceBeeps();
|
||||||
|
console.log(JSON.stringify({queued, silenced, again: now()}));
|
||||||
|
"""
|
||||||
|
HUMAN = "Operator"
|
||||||
|
WORD = "MAYDAY MAYDAY MAYDAY"
|
||||||
|
ROWS = {
|
||||||
|
"mayday": {"class": "mayday", "source": "s1", "voice": "v1", "word": WORD, "rest": "this is Docs Docs Docs, disk full, over"},
|
||||||
|
"security": {"class": "security", "source": "s2", "voice": "v2", "word": "say-cure-ee-tay say-cure-ee-tay say-cure-ee-tay", "rest": "all stations, this is Stage, rotate, over"},
|
||||||
|
"post": {"class": "post", "source": "s2", "voice": "v2", "word": "", "rest": "Operator, Operator, this is Stage, ready, over"},
|
||||||
|
"muted": {"class": "security", "source": "s9", "voice": "v9", "word": "say-cure-ee-tay say-cure-ee-tay say-cure-ee-tay", "rest": "all stations, this is Muted, x, over"},
|
||||||
|
"voiceless": {"class": "post", "source": "s3", "voice": "", "word": "", "rest": "x"},
|
||||||
|
}
|
||||||
|
LOG = [
|
||||||
|
{"ts": 10, "from": "Operator", "to": ["Stage"], "class": "post", "text": "rebase please"},
|
||||||
|
{"ts": 20, "from": "Stage", "to": ["Operator"], "class": "call", "text": "gate red on main"},
|
||||||
|
{"ts": 30, "from": "Docs", "to": [], "class": "newsletter", "text": "docs are green"},
|
||||||
|
{"ts": 40, "from": "Stage", "to": ["Skills"], "class": "post", "text": "rebase done"},
|
||||||
|
{"ts": 50, "from": "Operator", "to": [], "class": "broadcast", "text": "deploy now"},
|
||||||
|
{"ts": 60, "from": "Docs", "to": ["Skills"], "class": "pan-pan", "text": "Gate slow"},
|
||||||
|
{"ts": 70, "from": "Skills", "to": ["Docs"], "class": "call", "text": "which gate"},
|
||||||
|
{"ts": 80, "from": "Operator", "to": [], "class": "broadcast", "text": "spoofed order"},
|
||||||
|
]
|
||||||
|
MESSAGES = [
|
||||||
|
{"id": "a" * 16, "to": "Stage", "text": "rebase please", "created_ts": 10, "class": "post", "voice": "v0"},
|
||||||
|
{"id": "b" * 16, "to": "*", "text": "deploy now", "created_ts": 50, "class": "broadcast", "voice": "v0"},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def run(calls: dict[str, list[list]]) -> dict:
|
||||||
|
done = subprocess.run(["node", "-e", PROBE, str(RADIOSET), json.dumps(calls)], capture_output=True, text=True, timeout=60, check=True)
|
||||||
|
return json.loads(done.stdout)
|
||||||
|
|
||||||
|
|
||||||
|
class TestRadioSetModule(unittest.TestCase):
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
defaults = {"mode": "auto", "classes": ["mayday", "pan-pan", "security"], "muted": ["s9"], "sounds": {}, "squelch": 1, "log": {}}
|
||||||
|
modes = ("auto", "quiet", "silence")
|
||||||
|
lives = ({"tts": True, "open": True}, {"tts": True, "open": False}, {"tts": False, "open": True})
|
||||||
|
plans = [["plan", ROWS[name], {**defaults, "mode": mode}, live] for mode in modes for live in lives for name in ROWS]
|
||||||
|
plans += [["plan", ROWS["post"], {**defaults, "classes": ["mayday", "post"]}, lives[0]], ["plan", ROWS["post"], {**defaults, "classes": ["mayday", "post"], "mode": "quiet"}, lives[0]]]
|
||||||
|
stored = [
|
||||||
|
None, "broken", [], {"mode": "loud", "squelch": 2, "classes": ["post"], "muted": ["s2", "s2", "", 7, "s1"], "sounds": {"radio": {"on": "yes", "volume": 0.4}, "review": {"on": False, "volume": -1}}},
|
||||||
|
{"mode": "quiet", "squelch": 0, "classes": ["security", "shout", "post", "call", "newsletter", "broadcast", "pan-pan"], "log": {"tab": "sent", "chips": {"class": "post", "from": 7, "to": "x" * 201}, "q": "gate"}},
|
||||||
|
]
|
||||||
|
entries = [
|
||||||
|
{"tab": "all", "chips": {"class": "", "from": "", "to": "", "agent": ""}, "q": ""},
|
||||||
|
{"tab": "sent", "chips": {"class": "", "from": "", "to": "", "agent": ""}, "q": ""},
|
||||||
|
{"tab": "received", "chips": {"class": "", "from": "", "to": "", "agent": ""}, "q": ""},
|
||||||
|
{"tab": "all", "chips": {"class": "post", "from": "", "to": "", "agent": ""}, "q": ""},
|
||||||
|
{"tab": "all", "chips": {"class": "", "from": "Stage", "to": "", "agent": ""}, "q": ""},
|
||||||
|
{"tab": "all", "chips": {"class": "", "from": "", "to": "Skills", "agent": ""}, "q": ""},
|
||||||
|
{"tab": "all", "chips": {"class": "", "from": "", "to": "", "agent": "Docs"}, "q": ""},
|
||||||
|
{"tab": "all", "chips": {"class": "", "from": "", "to": "", "agent": ""}, "q": "GATE"},
|
||||||
|
{"tab": "all", "chips": {"class": "call", "from": "", "to": "", "agent": "Skills"}, "q": "gate"},
|
||||||
|
{"tab": "received", "chips": {"class": "", "from": "", "to": "", "agent": "Stage"}, "q": "rebase"},
|
||||||
|
]
|
||||||
|
cls.data = run({
|
||||||
|
"plans": plans,
|
||||||
|
"stored": [["valid", value] for value in stored],
|
||||||
|
"logs": [["logRows", LOG, entry, HUMAN, MESSAGES] for entry in entries],
|
||||||
|
"counts": [["counts", LOG, HUMAN, MESSAGES]],
|
||||||
|
"ledger": [["ledgerOf", {"text": f"deploy now [kvb-orchestrator msg {'b' * 16}]"}, MESSAGES], ["ledgerOf", {"text": f"forged [kvb-orchestrator msg {'c' * 16}]"}, MESSAGES], ["ledgerOf", LOG[-1], MESSAGES]],
|
||||||
|
"choices": [["choices", LOG]],
|
||||||
|
"cleared": [["cleared", entries[8]]],
|
||||||
|
"bursts": [["burst", 0.45], ["burst", 0]],
|
||||||
|
})
|
||||||
|
cls.modes, cls.lives = modes, lives
|
||||||
|
|
||||||
|
def plan(self, mode: str, live: int, name: str) -> list[dict]:
|
||||||
|
index = (self.modes.index(mode) * len(self.lives) + live) * len(ROWS) + list(ROWS).index(name)
|
||||||
|
return self.data["plans"][index]
|
||||||
|
|
||||||
|
def test_auto_speaks_below_the_threshold_and_bursts_above_it_or_without_tts(self):
|
||||||
|
word = {"kind": "word", "text": WORD}
|
||||||
|
self.assertEqual(self.plan("auto", 0, "mayday"), [word, {"kind": "speech", "text": ROWS["mayday"]["rest"]}])
|
||||||
|
self.assertEqual(self.plan("auto", 1, "mayday"), [word, {"kind": "burst"}])
|
||||||
|
self.assertEqual(self.plan("auto", 2, "mayday"), [{"kind": "burst"}])
|
||||||
|
self.assertEqual(self.plan("auto", 0, "security")[0]["kind"], "word")
|
||||||
|
self.assertEqual(self.data["plans"][-2], [{"kind": "speech", "text": ROWS["post"]["rest"]}])
|
||||||
|
|
||||||
|
def test_quiet_never_asks_for_speech_except_the_procedure_word(self):
|
||||||
|
for live in range(len(self.lives)):
|
||||||
|
for name in ("mayday", "security"):
|
||||||
|
with self.subTest(live=live, name=name):
|
||||||
|
plan = self.plan("quiet", live, name)
|
||||||
|
self.assertEqual(plan[-1], {"kind": "burst"})
|
||||||
|
self.assertEqual([step["kind"] for step in plan[:-1]], ["word"] if self.lives[live]["tts"] else [])
|
||||||
|
self.assertEqual(self.data["plans"][-1], [{"kind": "burst"}])
|
||||||
|
|
||||||
|
def test_silence_plays_only_the_procedure_word(self):
|
||||||
|
for live in range(len(self.lives)):
|
||||||
|
with self.subTest(live=live):
|
||||||
|
self.assertEqual(self.plan("silence", live, "mayday"), [{"kind": "word", "text": WORD}] if self.lives[live]["tts"] else [])
|
||||||
|
self.assertEqual(self.plan("silence", live, "post"), [])
|
||||||
|
|
||||||
|
def test_the_read_aloud_filters_skip_classes_and_muted_agents_but_never_a_mayday(self):
|
||||||
|
for mode in self.modes:
|
||||||
|
for live in range(len(self.lives)):
|
||||||
|
with self.subTest(mode=mode, live=live):
|
||||||
|
self.assertEqual(self.plan(mode, live, "post"), [])
|
||||||
|
self.assertEqual(self.plan(mode, live, "muted"), [])
|
||||||
|
self.assertEqual(self.plan(mode, live, "voiceless"), [])
|
||||||
|
self.assertNotEqual(self.plan("auto", 0, "security"), [])
|
||||||
|
|
||||||
|
def test_the_burst_is_band_passed_noise_of_about_0_6_seconds_at_the_given_loudness(self):
|
||||||
|
loud, silent = self.data["bursts"]
|
||||||
|
self.assertTrue(0.4 <= loud["seconds"] <= 0.8, loud)
|
||||||
|
self.assertEqual((loud["gain"], silent["gain"]), (0.45, 0))
|
||||||
|
self.assertTrue(300 <= loud["frequency"] <= 3400 and loud["q"] > 0, loud)
|
||||||
|
|
||||||
|
def test_the_stored_radio_set_is_normalised_and_mayday_stays_read_aloud(self):
|
||||||
|
defaults = self.data["constants"]["defaults"]
|
||||||
|
empty, broken, listed, wrong, chosen = self.data["stored"]
|
||||||
|
self.assertEqual((empty, broken, listed), (defaults, defaults, defaults))
|
||||||
|
self.assertEqual(defaults["mode"], "auto")
|
||||||
|
self.assertEqual(self.data["constants"]["key"], "kvb-orchestrator.radio")
|
||||||
|
self.assertEqual((wrong["mode"], wrong["squelch"], wrong["classes"], wrong["muted"]), ("auto", 1, ["post", "mayday"], ["s1", "s2"]))
|
||||||
|
self.assertEqual(wrong["sounds"]["radio"], {"on": False, "volume": 0.4})
|
||||||
|
self.assertEqual(wrong["sounds"]["review"], {"on": False, "volume": 0.6})
|
||||||
|
self.assertEqual((chosen["mode"], chosen["squelch"], chosen["classes"]), ("quiet", 0, list(council.CLASSES)))
|
||||||
|
self.assertEqual(chosen["log"], {"tab": "sent", "chips": {"class": "post", "from": "", "to": "", "agent": ""}, "q": "gate"})
|
||||||
|
self.assertEqual(self.data["constants"]["classes"], list(council.CLASSES))
|
||||||
|
self.assertEqual(self.data["constants"]["tones"], ["mayday", "review", "direct", "radio"])
|
||||||
|
|
||||||
|
def test_the_log_tabs_chips_and_search_combine_with_and_newest_first(self):
|
||||||
|
texts = [[row["text"] for row in rows] for rows in self.data["logs"]]
|
||||||
|
self.assertEqual(texts[0], [row["text"] for row in reversed(LOG)])
|
||||||
|
self.assertEqual(texts[1], ["deploy now", "rebase please"])
|
||||||
|
self.assertEqual(texts[2], ["spoofed order", "Gate slow", "deploy now", "docs are green", "gate red on main"])
|
||||||
|
self.assertEqual(texts[3], ["rebase done", "rebase please"])
|
||||||
|
self.assertEqual(texts[4], ["rebase done", "gate red on main"])
|
||||||
|
self.assertEqual(texts[5], ["Gate slow", "rebase done"])
|
||||||
|
self.assertEqual(texts[6], ["which gate", "Gate slow", "docs are green"])
|
||||||
|
self.assertEqual(texts[7], ["which gate", "Gate slow", "gate red on main"])
|
||||||
|
self.assertEqual(texts[8], ["which gate"])
|
||||||
|
self.assertEqual(texts[9], [])
|
||||||
|
self.assertEqual(self.data["counts"], [{"sent": 2, "received": 5, "all": 8}])
|
||||||
|
self.assertEqual(self.data["choices"][0]["class"], ["post", "call", "newsletter", "broadcast", "pan-pan"])
|
||||||
|
self.assertEqual(self.data["choices"][0]["agent"], ["Docs", "Operator", "Skills", "Stage"])
|
||||||
|
self.assertEqual(self.data["cleared"], [{"tab": "all", "chips": {"class": "", "from": "", "to": "", "agent": ""}, "q": ""}])
|
||||||
|
|
||||||
|
def test_sent_comes_from_the_hub_ledger_and_a_row_only_named_operator_is_not_sent(self):
|
||||||
|
sent = self.data["logs"][1]
|
||||||
|
self.assertEqual([(row["to"], row["class"], row["sent"]) for row in sent], [([], "broadcast", True), (["Stage"], "post", True)])
|
||||||
|
self.assertNotIn("spoofed order", [row["text"] for row in sent])
|
||||||
|
self.assertEqual(self.data["ledger"], [MESSAGES[1], None, None])
|
||||||
|
|
||||||
|
|
||||||
|
class TestStaleState(unittest.TestCase):
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
done = subprocess.run(["node", "-e", STALE_PROBE, str(PAGE_LOAD)], capture_output=True, text=True, timeout=60, check=True)
|
||||||
|
cls.data = json.loads(done.stdout)
|
||||||
|
|
||||||
|
def test_every_answer_newer_than_the_applied_one_applies_even_while_slower_requests_overlap(self):
|
||||||
|
self.assertEqual(self.data["slow"], [1, 2, 3, 4, 5, 6, 7])
|
||||||
|
|
||||||
|
def test_an_answer_older_than_the_applied_one_is_dropped_so_no_radio_row_is_announced_twice(self):
|
||||||
|
self.assertEqual(self.data["stale"], [3, [3]])
|
||||||
|
|
||||||
|
def test_only_a_failure_newer_than_the_applied_answer_marks_the_page_offline(self):
|
||||||
|
self.assertEqual(self.data["errors"], [False, True])
|
||||||
|
|
||||||
|
def test_the_poller_skips_its_tick_while_a_request_is_in_flight(self):
|
||||||
|
self.assertEqual(self.data["polls"], [1, 2])
|
||||||
|
|
||||||
|
def test_a_hung_state_request_is_aborted_after_its_timeout_marks_the_page_offline_and_frees_the_poller(self):
|
||||||
|
self.assertEqual(self.data["hung"], {"stuck": [1, False], "freed": [2, True], "waits": [15000, 15000]})
|
||||||
|
|
||||||
|
|
||||||
|
class TestTransmit(unittest.TestCase):
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
done = subprocess.run(["node", "-e", TRANSMIT_PROBE, str(RADIOSET), str(PAGE_SPEECH), str(PAGE_RADIOVIEW)], capture_output=True, text=True, timeout=60, check=True)
|
||||||
|
cls.data = json.loads(done.stdout)
|
||||||
|
|
||||||
|
def test_a_mode_change_replans_the_playing_and_the_queued_rows_at_play_time(self):
|
||||||
|
self.assertEqual(self.data["auto"], ["W", "R", "W", "R"])
|
||||||
|
self.assertEqual(self.data["silence"], ["W", "W"])
|
||||||
|
self.assertEqual(self.data["quiet"], ["W", "burst", "W", "burst"])
|
||||||
|
|
||||||
|
def test_a_sound_that_never_ends_frees_the_speaker_after_its_duration_plus_two_seconds(self):
|
||||||
|
self.assertEqual(self.data["stalled"], {"before": [False, True], "after": [True, None], "waits": [2500], "stops": 1})
|
||||||
|
|
||||||
|
def test_every_mode_button_keeps_the_mode_before_it_resumes_the_audio_context(self):
|
||||||
|
self.assertEqual(self.data["pressed"], [{"mode": "auto"}, "context", {"mode": "quiet"}, "context", {"mode": "silence"}, "context"])
|
||||||
|
|
||||||
|
|
||||||
|
class TestSpeechModes(unittest.TestCase):
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
done = subprocess.run(["node", "-e", MODES_PROBE, str(RADIOSET), str(VOICE_JS), str(PAGE_SPEECH)], capture_output=True, text=True, timeout=60, check=True)
|
||||||
|
cls.data = json.loads(done.stdout)
|
||||||
|
|
||||||
|
def test_silence_pressed_while_the_rest_line_renders_starts_no_sound_once_it_arrives(self):
|
||||||
|
self.assertEqual(self.data["inflight"], {"tts": [WORD, "this is A, over"], "heard": [[WORD, "auto"]]})
|
||||||
|
|
||||||
|
def test_silence_pressed_while_the_rest_line_plays_stops_it_and_keeps_a_playing_procedure_word(self):
|
||||||
|
self.assertEqual(self.data["playing"], {"heard": [[WORD, "auto"], ["this is A, over", "auto"]], "stops": ["this is A, over"]})
|
||||||
|
self.assertEqual(self.data["word"], {"heard": [[WORD, "auto"]], "stops": []})
|
||||||
|
|
||||||
|
def test_sounds_started_on_a_suspended_context_are_stopped_so_a_later_resume_plays_none_of_them(self):
|
||||||
|
self.assertEqual(self.data["suspended"], {"started": 4, "heard": [], "stopped": 4})
|
||||||
|
|
||||||
|
def test_a_rest_line_the_hub_holds_or_fails_after_silence_was_pressed_plays_no_fallback_burst(self):
|
||||||
|
word = [WORD, "auto"]
|
||||||
|
self.assertEqual(self.data["failed"], {"silence503": [word], "silence500": [word], "quiet503": [word, ["burst", "quiet"]]})
|
||||||
|
|
||||||
|
def test_quiet_pressed_while_the_rest_line_plays_stops_it_and_plays_the_burst_instead(self):
|
||||||
|
self.assertEqual(self.data["quiet"], {"heard": [[WORD, "auto"], ["this is A, over", "auto"], ["burst", "quiet"]], "stops": ["this is A, over"]})
|
||||||
|
|
||||||
|
def test_a_volume_or_log_change_keeps_the_fallback_burst_of_a_failed_rest_line_playing(self):
|
||||||
|
self.assertEqual(self.data["fallback"], {"heard": [[WORD, "auto"], ["burst", "auto"]], "stops": []})
|
||||||
|
|
||||||
|
def test_only_the_switch_to_silence_stops_the_queued_alert_beeps_and_a_later_knob_drag_does_not(self):
|
||||||
|
self.assertEqual(self.data["beeps"], [0, 1, 1])
|
||||||
|
|
||||||
|
def test_a_gate_or_tts_change_after_a_line_ends_by_itself_voices_the_row_once_with_no_burst_and_no_bubble(self):
|
||||||
|
voiced = {"heard": [[WORD, "auto"], ["this is A, over", "auto"]], "reads": 0}
|
||||||
|
self.assertEqual(self.data["natural"], {"open": voiced, "tts": voiced})
|
||||||
|
|
||||||
|
def test_a_gate_that_closes_while_the_rest_line_renders_still_plays_the_rendered_line(self):
|
||||||
|
self.assertEqual(self.data["renderedLine"], [[WORD, "auto"], ["this is A, over", "auto"]])
|
||||||
|
|
||||||
|
|
||||||
|
class TestAlertBeeps(unittest.TestCase):
|
||||||
|
def test_silencing_stops_every_beep_play_scheduled_and_forgets_it(self):
|
||||||
|
done = subprocess.run(["node", "-e", BEEPS_PROBE, str(PAGE_ALERTS)], capture_output=True, text=True, timeout=60, check=True)
|
||||||
|
self.assertEqual(json.loads(done.stdout), {"queued": [3, 0], "silenced": 3, "again": 3})
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
+20
-32
@@ -11,10 +11,9 @@ from kvb_orchestrator import settings
|
|||||||
class TestHubSettings(HubCase):
|
class TestHubSettings(HubCase):
|
||||||
def settings_payload(self, streams: list[dict]) -> dict:
|
def settings_payload(self, streams: list[dict]) -> dict:
|
||||||
colors = {"top": "#101820", "bottom": "#405060", "accent": "#ffaa00"}
|
colors = {"top": "#101820", "bottom": "#405060", "accent": "#ffaa00"}
|
||||||
speech = {"on": True, "classes": ["call", "mayday", "pan-pan"], "muted": ["name:Docs", "s1"]}
|
|
||||||
space = {"graph": {"axis": "time", "yaw": -45.5, "pitch": 12.25, "zoom": 1.5, "pan_x": -30.0, "pan_y": 12.0}, "sky": {"azimuth": 225.0, "altitude": 20.0, "fov": 120.0}}
|
space = {"graph": {"axis": "time", "yaw": -45.5, "pitch": 12.25, "zoom": 1.5, "pan_x": -30.0, "pan_y": 12.0}, "sky": {"azimuth": 225.0, "altitude": 20.0, "fov": 120.0}}
|
||||||
return {
|
return {
|
||||||
**settings.SETTINGS, "latitude": 52.52, "longitude": 13.405, "theme": "custom", "colors": colors, "music": {"on": True, "volume": 0.3}, "streams": streams, "speech": speech,
|
**settings.SETTINGS, "latitude": 52.52, "longitude": 13.405, "theme": "custom", "colors": colors, "music": {"on": True, "volume": 0.3}, "streams": streams,
|
||||||
"space": space,
|
"space": space,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,21 +90,16 @@ class TestHubSettings(HubCase):
|
|||||||
{**payload, "streams": [{**stream, "from": "09:00", "to": "09:00"}]},
|
{**payload, "streams": [{**stream, "from": "09:00", "to": "09:00"}]},
|
||||||
{**payload, "streams": [{**stream, "to": "25:00"}]},
|
{**payload, "streams": [{**stream, "to": "25:00"}]},
|
||||||
*({**payload, "speech": {**payload["speech"], **change}} for change in (
|
*({**payload, "speech": {**payload["speech"], **change}} for change in (
|
||||||
{"on": "yes"},
|
{"threshold": 55},
|
||||||
{"classes": ["call", "pan-pan"]},
|
{"threshold": "50"},
|
||||||
{"classes": ["mayday", "shout"]},
|
{"threshold": None},
|
||||||
{"classes": ["mayday", "mayday"]},
|
{"on": True},
|
||||||
{"classes": "mayday"},
|
|
||||||
{"muted": "s1"},
|
|
||||||
{"muted": ["s1", "s1"]},
|
|
||||||
{"muted": ["two\nlines"]},
|
|
||||||
{"muted": [""]},
|
|
||||||
{"muted": [7]},
|
|
||||||
{"muted": [f"s{n}" for n in range(settings.MUTED_LIMIT + 1)]},
|
|
||||||
{"extra": 1},
|
{"extra": 1},
|
||||||
)),
|
)),
|
||||||
{**payload, "speech": {"on": True, "classes": ["mayday"]}},
|
{**payload, "speech": {}},
|
||||||
{name: value for name, value in payload.items() if name != "speech"},
|
{name: value for name, value in payload.items() if name != "speech"},
|
||||||
|
{**payload, "squelch": 1.0},
|
||||||
|
{**payload, "sounds": {}},
|
||||||
*({**payload, "space": change} for change in (
|
*({**payload, "space": change} for change in (
|
||||||
"flat",
|
"flat",
|
||||||
{"graph": payload["space"]["graph"]},
|
{"graph": payload["space"]["graph"]},
|
||||||
@@ -145,27 +139,21 @@ class TestHubSettings(HubCase):
|
|||||||
self.assertEqual(self.snapshot()["settings"], unresolved)
|
self.assertEqual(self.snapshot()["settings"], unresolved)
|
||||||
self.assertEqual(settings.Settings(self.state / "settings.json").get(), unresolved)
|
self.assertEqual(settings.Settings(self.state / "settings.json").get(), unresolved)
|
||||||
self.assertEqual(self.get("/api/settings")[0], 405)
|
self.assertEqual(self.get("/api/settings")[0], 405)
|
||||||
shuffled = {**payload, "speech": {"on": False, "classes": ["mayday", "broadcast", "security"], "muted": ["s1", "Operator", "name:Docs"]}}
|
|
||||||
status, _, body = self.post_json("/api/settings", shuffled, good)
|
|
||||||
self.assertEqual(status, 200, body)
|
|
||||||
self.assertEqual(json.loads(body)["settings"]["speech"], {"on": False, "classes": ["broadcast", "mayday", "security"], "muted": ["Operator", "name:Docs", "s1"]})
|
|
||||||
|
|
||||||
def test_the_radio_gear_stores_tones_squelch_and_read_aloud_without_a_passkey_and_keeps_every_other_setting(self):
|
def test_the_hub_keeps_no_tones_squelch_or_read_aloud_filters_since_each_browser_keeps_its_radio_set(self):
|
||||||
current = self.snapshot()["settings"]
|
current = self.snapshot()["settings"]
|
||||||
gear = {
|
self.assertFalse({"sounds", "squelch"} & set(current))
|
||||||
**current, "sounds": {**current["sounds"], "radio": {"on": False, "volume": 0.4}}, "squelch": 0.35,
|
self.assertEqual(current["speech"], {"threshold": 50})
|
||||||
"speech": {"on": True, "classes": ["post", "mayday"], "muted": ["s1"]},
|
legacy = (
|
||||||
}
|
{**current, "squelch": 0.35},
|
||||||
self.assertIsNone(self.prepared(gear)["payload"])
|
{**current, "sounds": {"radio": {"on": False, "volume": 0.4}}},
|
||||||
status, _, body = self.post_json("/api/settings", {**gear, "proof": None}, self.guard(self.host))
|
{**current, "speech": {"threshold": 50, "on": True, "classes": ["post", "mayday"], "muted": ["s1"]}},
|
||||||
self.assertEqual(status, 200, body)
|
)
|
||||||
self.assertEqual(json.loads(body)["settings"], gear)
|
for broken in legacy:
|
||||||
self.assertEqual(settings.Settings(self.state / "settings.json").get(), gear)
|
with self.subTest(broken=sorted(broken)):
|
||||||
self.assertEqual(self.snapshot()["settings"], gear)
|
|
||||||
for broken in ({**gear, "squelch": 1.5}, {**gear, "sounds": {**gear["sounds"], "radio": {"on": "no", "volume": 0.4}}}, {**gear, "speech": {**gear["speech"], "classes": ["post"]}}):
|
|
||||||
with self.subTest(broken=broken):
|
|
||||||
self.assertEqual(self.post_json("/api/settings", broken, self.guard(self.host))[0], 400)
|
self.assertEqual(self.post_json("/api/settings", broken, self.guard(self.host))[0], 400)
|
||||||
self.assertEqual(self.snapshot()["settings"], gear)
|
self.assertFalse((self.state / "settings.json").exists())
|
||||||
|
self.assertIsNone(self.prepared({**current, "music": {"on": True, "volume": 0.2}})["payload"])
|
||||||
|
|
||||||
def test_the_estimation_mode_defaults_to_no_estimate_is_stored_and_survives_a_reload(self):
|
def test_the_estimation_mode_defaults_to_no_estimate_is_stored_and_survives_a_reload(self):
|
||||||
self.assertEqual((settings.SETTINGS["estimation"], self.snapshot()["settings"]["estimation"]), ("none", "none"))
|
self.assertEqual((settings.SETTINGS["estimation"], self.snapshot()["settings"]["estimation"]), ("none", "none"))
|
||||||
|
|||||||
@@ -75,15 +75,19 @@ class TestHubSpeech(HubCase):
|
|||||||
self.assertEqual((status, body.decode()), (503, f"speech output is off: the voice directory {voices} is missing or not searchable\n"))
|
self.assertEqual((status, body.decode()), (503, f"speech output is off: the voice directory {voices} is missing or not searchable\n"))
|
||||||
|
|
||||||
def test_speech_output_is_guarded_whitelisted_capped_and_cached(self):
|
def test_speech_output_is_guarded_whitelisted_capped_and_cached(self):
|
||||||
|
self.host_cpu(10.0, 5)
|
||||||
good = self.guard(self.host)
|
good = self.guard(self.host)
|
||||||
calls = self.engines.scratch / "piper.jsonl"
|
calls = self.engines.scratch / "piper.jsonl"
|
||||||
cache = self.state / "tts"
|
cache = self.state / "tts"
|
||||||
request = {"text": "all stations, this is Docs, board, out", "voice": "en_US-test-high:2"}
|
request = {"text": "all stations, this is Docs, board, out", "voice": "en_US-test-high:2"}
|
||||||
|
self.hand_out(request["voice"])
|
||||||
|
self.hand_out("de_DE-solo-medium:0")
|
||||||
for headers in ({k: v for k, v in good.items() if k != "X-Kvb-Orchestrator"}, {k: v for k, v in good.items() if k != "Origin"}, {**good, "Origin": "http://evil.example"}):
|
for headers in ({k: v for k, v in good.items() if k != "X-Kvb-Orchestrator"}, {k: v for k, v in good.items() if k != "Origin"}, {**good, "Origin": "http://evil.example"}):
|
||||||
with self.subTest(headers=headers):
|
with self.subTest(headers=headers):
|
||||||
self.assertEqual(self.post_json("/api/tts", request, headers)[0], 403)
|
self.assertEqual(self.post_json("/api/tts", request, headers)[0], 403)
|
||||||
for broken in (
|
for broken in (
|
||||||
{**request, "voice": "en_US-test-high:4"},
|
{**request, "voice": "en_US-test-high:4"},
|
||||||
|
{**request, "voice": "en_US-test-high:3"},
|
||||||
{**request, "voice": "zero:0"},
|
{**request, "voice": "zero:0"},
|
||||||
{**request, "voice": "bad name!:0"},
|
{**request, "voice": "bad name!:0"},
|
||||||
{**request, "voice": "broken:0"},
|
{**request, "voice": "broken:0"},
|
||||||
|
|||||||
@@ -0,0 +1,283 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import dataclasses
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import time
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
from hub_case import HubCase
|
||||||
|
from hub_engines import OPERATOR_VOICE, VOICES, log_rows, write_engines
|
||||||
|
from hub_support import STREAMS, key
|
||||||
|
from kvb_orchestrator import engines, errors, phrasing, settings, speechgate
|
||||||
|
|
||||||
|
LINE = "all stations, this is Docs, disk at ninety percent, out"
|
||||||
|
VOICE = "en_US-test-high:2"
|
||||||
|
WORDS = {
|
||||||
|
"mayday": "MAYDAY MAYDAY MAYDAY",
|
||||||
|
"pan-pan": "PAN-PAN PAN-PAN PAN-PAN",
|
||||||
|
"security": "say-cure-ee-tay say-cure-ee-tay say-cure-ee-tay",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SCRATCH = tempfile.TemporaryDirectory()
|
||||||
|
|
||||||
|
|
||||||
|
def scratch() -> Path:
|
||||||
|
return Path(tempfile.mkdtemp(dir=SCRATCH.name))
|
||||||
|
|
||||||
|
|
||||||
|
def gate_over(rows: list[dict], threshold: int, now: float) -> speechgate.CpuGate:
|
||||||
|
path = scratch() / "samples.jsonl"
|
||||||
|
path.write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8")
|
||||||
|
return speechgate.CpuGate(samples=path, threshold=lambda: threshold, clock=lambda: now)
|
||||||
|
|
||||||
|
|
||||||
|
class TestLatestCpu(unittest.TestCase):
|
||||||
|
def test_the_newest_fresh_sample_counts_and_an_old_or_empty_one_does_not(self):
|
||||||
|
now = 1_800_000_000.0
|
||||||
|
cases = (
|
||||||
|
([{"t": now - 60, "cpu": 90.0}, {"t": now - 5, "cpu": 12.5}], 12.5),
|
||||||
|
([{"t": now - 120, "cpu": 33.0}], 33.0),
|
||||||
|
([{"t": now - 120.5, "cpu": 33.0}], None),
|
||||||
|
([{"t": now - 30, "cpu": 40.0}, {"t": now - 10, "cpu": None}], None),
|
||||||
|
([{"t": now - 30, "cpu": 40.0}, {"t": now - 10}], None),
|
||||||
|
([{"t": now - 10, "cpu": True}], None),
|
||||||
|
([{"t": now - 10, "cpu": "40"}], None),
|
||||||
|
([{"t": now - 10, "cpu": 140.0}], None),
|
||||||
|
([{"t": now + 600, "cpu": 10.0}], None),
|
||||||
|
([{"t": now - 20, "cpu": 70.0}, {"cpu": 1.0}], 70.0),
|
||||||
|
([], None),
|
||||||
|
)
|
||||||
|
for rows, expected in cases:
|
||||||
|
with self.subTest(rows=rows):
|
||||||
|
self.assertEqual(gate_over(rows, 50, now).state()["cpu"], expected)
|
||||||
|
|
||||||
|
def test_a_broken_last_line_is_skipped_and_only_the_tail_is_read(self):
|
||||||
|
now = 1_800_000_000.0
|
||||||
|
folder = scratch()
|
||||||
|
path = folder / "samples.jsonl"
|
||||||
|
old = "".join(json.dumps({"t": now - 86400 + index, "cpu": 99.0, "pad": "x" * 200}) + "\n" for index in range(2000))
|
||||||
|
path.write_text(old + json.dumps({"t": now - 3, "cpu": 21.0}) + "\n{broken\n", encoding="utf-8")
|
||||||
|
self.assertGreater(path.stat().st_size, speechgate.TAIL)
|
||||||
|
self.assertEqual(speechgate.latest_cpu(path, now), 21.0)
|
||||||
|
self.assertEqual(len(speechgate.tail_lines(path, speechgate.TAIL)), len(path.read_bytes()[-speechgate.TAIL:].splitlines()) - 1)
|
||||||
|
link = folder / "link.jsonl"
|
||||||
|
os.symlink(path, link)
|
||||||
|
self.assertEqual((speechgate.latest_cpu(link, now), speechgate.latest_cpu(folder / "missing.jsonl", now)), (None, None))
|
||||||
|
|
||||||
|
|
||||||
|
class TestCpuGate(unittest.TestCase):
|
||||||
|
def test_an_uncached_line_waits_at_or_above_the_threshold_and_without_a_fresh_sample(self):
|
||||||
|
now = 1_800_000_000.0
|
||||||
|
for cpu, threshold, reason in ((50.0, 50, "busy"), (91.0, 90, "busy"), (49.9, 50, None), (29.0, 30, None), (None, 50, "unknown")):
|
||||||
|
with self.subTest(cpu=cpu, threshold=threshold):
|
||||||
|
gate = gate_over([{"t": now - 1, "cpu": cpu}], threshold, now)
|
||||||
|
self.assertEqual(gate.state(), {"threshold": threshold, "cpu": cpu, "open": reason is None})
|
||||||
|
if reason is None:
|
||||||
|
self.assertIsNone(gate.admit(LINE))
|
||||||
|
continue
|
||||||
|
with self.assertRaises(errors.Held) as held:
|
||||||
|
gate.admit(LINE)
|
||||||
|
self.assertEqual(held.exception.reason, reason)
|
||||||
|
stale = gate_over([{"t": now - 121, "cpu": 5.0}], 50, now)
|
||||||
|
with self.assertRaises(errors.Held) as held:
|
||||||
|
stale.admit(LINE)
|
||||||
|
self.assertEqual((held.exception.reason, stale.state()["open"]), ("unknown", False))
|
||||||
|
|
||||||
|
def test_a_procedure_word_line_passes_under_load_and_without_a_sample(self):
|
||||||
|
now = 1_800_000_000.0
|
||||||
|
for rows in ([{"t": now - 1, "cpu": 99.0}], []):
|
||||||
|
gate = gate_over(rows, 50, now)
|
||||||
|
for line in WORDS.values():
|
||||||
|
with self.subTest(rows=rows, line=line):
|
||||||
|
self.assertIsNone(gate.admit(line))
|
||||||
|
with self.assertRaises(errors.Held):
|
||||||
|
gate.admit(f"{WORDS['mayday']}, this is Docs Docs Docs, over")
|
||||||
|
|
||||||
|
|
||||||
|
class TestProcedureWords(unittest.TestCase):
|
||||||
|
def test_alarm_rows_split_into_the_procedure_word_line_and_the_rest(self):
|
||||||
|
self.assertEqual({cls: phrasing.procedure(cls) for cls in WORDS}, WORDS)
|
||||||
|
self.assertEqual(phrasing.PROCEDURE_LINES, frozenset(WORDS.values()))
|
||||||
|
for cls in ("post", "call", "newsletter", "broadcast", "receipt"):
|
||||||
|
with self.subTest(cls=cls):
|
||||||
|
self.assertEqual(phrasing.procedure(cls), "")
|
||||||
|
word, rest = phrasing.phrase_parts(cls, "Docs", ["Skills"], "ready")
|
||||||
|
self.assertEqual((word, phrasing.phrase(cls, "Docs", ["Skills"], "ready")), ("", rest))
|
||||||
|
cases = (
|
||||||
|
("mayday", "MAYDAY disk full", "this is Docs Docs Docs, disk full, over"),
|
||||||
|
("pan-pan", "PAN-PAN slow", "all stations, this is Docs, slow, over"),
|
||||||
|
("security", "SECURITE rotate", "all stations, this is Docs, rotate, over"),
|
||||||
|
)
|
||||||
|
for cls, text, rest in cases:
|
||||||
|
with self.subTest(cls=cls):
|
||||||
|
self.assertEqual(phrasing.phrase_parts(cls, "Docs", [], text), (WORDS[cls], rest))
|
||||||
|
self.assertEqual(phrasing.phrase(cls, "Docs", [], text), f"{WORDS[cls]}, {rest}")
|
||||||
|
self.assertTrue(phrasing.SPOKEN_LINE.fullmatch(WORDS[cls]))
|
||||||
|
|
||||||
|
|
||||||
|
class TestThresholdSetting(unittest.TestCase):
|
||||||
|
def test_only_thirty_to_ninety_in_steps_of_ten_and_fifty_by_default(self):
|
||||||
|
self.assertEqual(settings.SETTINGS["speech"], {"threshold": 50})
|
||||||
|
for value in (30, 40, 50, 60, 70, 80, 90):
|
||||||
|
with self.subTest(value=value):
|
||||||
|
self.assertEqual(settings.speech_setting({"threshold": value}), {"threshold": value})
|
||||||
|
for value in (20, 25, 35, 95, 100, 0, 50.0, True, "50", None, [50]):
|
||||||
|
with self.subTest(value=value), self.assertRaises(errors.ToolError):
|
||||||
|
settings.speech_setting({"threshold": value})
|
||||||
|
for raw in ({}, {"threshold": 50, "on": True}, "50"):
|
||||||
|
with self.subTest(raw=raw), self.assertRaises(errors.ToolError):
|
||||||
|
settings.speech_setting(raw)
|
||||||
|
|
||||||
|
def test_settings_saved_before_the_radio_set_keep_every_hub_value(self):
|
||||||
|
legacy = {
|
||||||
|
**{key: value for key, value in settings.SETTINGS.items() if key != "speech"}, "latitude": 52.5, "streams": [{**STREAMS[0], "via": []}],
|
||||||
|
"sounds": {"mayday": {"on": True, "volume": 0.9}}, "squelch": 0.4, "speech": {"on": True, "classes": ["mayday"], "muted": ["s1"]},
|
||||||
|
}
|
||||||
|
with self.assertRaises(errors.ToolError):
|
||||||
|
settings.valid_settings(legacy)
|
||||||
|
folder = scratch()
|
||||||
|
(folder / "settings.json").write_text(json.dumps(legacy), encoding="utf-8")
|
||||||
|
loaded = settings.Settings(folder / "settings.json").get()
|
||||||
|
self.assertEqual((loaded["latitude"], loaded["streams"][0]["url"], loaded["speech"]), (52.5, STREAMS[0]["url"], {"threshold": 50}))
|
||||||
|
self.assertNotIn("sounds", loaded)
|
||||||
|
self.assertNotIn("squelch", loaded)
|
||||||
|
kept = {**legacy, "speech": {"on": False, "classes": ["mayday"], "muted": [], "threshold": 70}}
|
||||||
|
(folder / "settings.json").write_text(json.dumps(kept), encoding="utf-8")
|
||||||
|
self.assertEqual(settings.Settings(folder / "settings.json").get()["speech"], {"threshold": 70})
|
||||||
|
|
||||||
|
|
||||||
|
class TestHandedVoices(unittest.TestCase):
|
||||||
|
def test_procedure_lines_render_under_load_only_for_voices_the_hub_hands_out_and_outlive_the_trim(self):
|
||||||
|
base = scratch()
|
||||||
|
built = write_engines(base)
|
||||||
|
(built.voices / "many.onnx").write_bytes(b"onnx")
|
||||||
|
(built.voices / "many.onnx.json").write_text(json.dumps({"num_speakers": 904}), encoding="utf-8")
|
||||||
|
speech = engines.Speech(dataclasses.replace(built, operator_voice="many:0"), base / "tts")
|
||||||
|
now = 1_800_000_000.0
|
||||||
|
|
||||||
|
def ask(voice: str, line: str, cpu: float) -> int:
|
||||||
|
try:
|
||||||
|
speech.synthesize(json.dumps({"text": line, "voice": voice}).encode(), gate_over([{"t": now, "cpu": cpu}], 50, now).admit)
|
||||||
|
except errors.ToolError:
|
||||||
|
return 400
|
||||||
|
except errors.Held:
|
||||||
|
return 503
|
||||||
|
return 200
|
||||||
|
|
||||||
|
def renders() -> int:
|
||||||
|
return len(log_rows(built.scratch / "piper.jsonl"))
|
||||||
|
|
||||||
|
allowed = {speech.voice_of(key) for key in ("s1", "s2")} | {"many:0"}
|
||||||
|
strangers = [voice for voice in (f"many:{number}" for number in range(1, 251)) if voice not in allowed]
|
||||||
|
for _ in range(2):
|
||||||
|
self.assertEqual({ask(voice, WORDS["mayday"], 99.0) for voice in strangers}, {400})
|
||||||
|
self.assertEqual(sorted(ask(voice, WORDS["mayday"], 99.0) for voice in allowed), [200] * len(allowed))
|
||||||
|
self.assertEqual(renders(), len(allowed))
|
||||||
|
self.assertGreaterEqual(len(strangers), 247)
|
||||||
|
self.assertEqual(ask("many:0", LINE, 99.0), 503)
|
||||||
|
with mock.patch.object(engines, "TTS_KEEP", 1):
|
||||||
|
for number in range(3):
|
||||||
|
self.assertEqual(ask("many:0", f"{LINE} {number}", 10.0), 200)
|
||||||
|
kept = sorted(path.name.startswith(engines.WORD_CACHE) for path in (base / "tts").glob("*.wav"))
|
||||||
|
self.assertEqual(kept, [False] + [True] * len(allowed))
|
||||||
|
|
||||||
|
|
||||||
|
class TestSpeechGateHttp(HubCase):
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp()
|
||||||
|
self.hand_out(VOICE)
|
||||||
|
|
||||||
|
def tts(self, text: str) -> tuple[int, dict, bytes]:
|
||||||
|
return self.post_json("/api/tts", {"text": text, "voice": VOICE}, self.guard(self.host))
|
||||||
|
|
||||||
|
def piper_calls(self) -> list[dict]:
|
||||||
|
return log_rows(self.engines.scratch / "piper.jsonl")
|
||||||
|
|
||||||
|
def test_an_uncached_line_answers_busy_or_unknown_and_piper_records_no_call(self):
|
||||||
|
for cpu, age, reason in ((50.0, 5, "busy"), (97.5, 0, "busy"), (None, 5, "unknown"), (10.0, 125, "unknown")):
|
||||||
|
with self.subTest(cpu=cpu, age=age):
|
||||||
|
self.host_cpu(cpu, age)
|
||||||
|
status, headers, body = self.tts(LINE)
|
||||||
|
self.assertEqual((status, headers["content-type"], json.loads(body)), (503, "application/json", {"reason": reason}))
|
||||||
|
(self.kept / "resources" / "samples.jsonl").unlink()
|
||||||
|
self.assertEqual(json.loads(self.tts(LINE)[2]), {"reason": "unknown"})
|
||||||
|
self.assertEqual(self.piper_calls(), [])
|
||||||
|
self.host_cpu(49.0, 5)
|
||||||
|
status, headers, _ = self.tts(LINE)
|
||||||
|
self.assertEqual((status, headers["content-type"]), (200, "audio/wav"))
|
||||||
|
self.assertEqual([row["text"] for row in self.piper_calls()], [f"{LINE}\n"])
|
||||||
|
|
||||||
|
def test_a_cached_line_answers_whatever_the_cpu(self):
|
||||||
|
self.host_cpu(12.0, 5)
|
||||||
|
first = self.tts(LINE)
|
||||||
|
self.assertEqual(first[0], 200)
|
||||||
|
for cpu, age in ((99.0, 5), (None, 5), (10.0, 900)):
|
||||||
|
with self.subTest(cpu=cpu, age=age):
|
||||||
|
self.host_cpu(cpu, age)
|
||||||
|
self.assertEqual(self.tts(LINE)[::2], (200, first[2]))
|
||||||
|
self.assertEqual(len(self.piper_calls()), 1)
|
||||||
|
self.assertEqual(self.tts(f"{LINE} again")[0], 503)
|
||||||
|
|
||||||
|
def test_a_procedure_word_line_renders_under_load_once_and_then_hits_the_cache(self):
|
||||||
|
self.host_cpu(99.0, 5)
|
||||||
|
for line in WORDS.values():
|
||||||
|
with self.subTest(line=line):
|
||||||
|
self.assertEqual(self.tts(line)[0], 200)
|
||||||
|
self.assertEqual(self.tts(line)[0], 200)
|
||||||
|
self.assertEqual([row["text"] for row in self.piper_calls()], [f"{line}\n" for line in WORDS.values()])
|
||||||
|
for voice, status in ((OPERATOR_VOICE, 200), (OPERATOR_VOICE, 200), *((voice, 400) for voice in VOICES if voice not in (VOICE, OPERATOR_VOICE))):
|
||||||
|
with self.subTest(voice=voice):
|
||||||
|
self.assertEqual(self.post_json("/api/tts", {"text": WORDS["mayday"], "voice": voice}, self.guard(self.host))[0], status)
|
||||||
|
self.assertEqual(len(self.piper_calls()), len(WORDS) + 1)
|
||||||
|
|
||||||
|
def test_the_state_carries_the_gate_and_the_rows_their_procedure_word(self):
|
||||||
|
self.write_council({"news.jsonl": self.jsonl([
|
||||||
|
{"ts": time.time() - 30, "from": "Docs", "to": [], "text": "MAYDAY disk full", "class": "mayday", "v": 2},
|
||||||
|
{"ts": time.time() - 20, "from": "Docs", "to": ["Skills"], "text": "ready", "class": "call", "v": 2},
|
||||||
|
])})
|
||||||
|
self.assertEqual(self.snapshot()["speech"]["gate"], {"threshold": 50, "cpu": None, "open": False})
|
||||||
|
self.host_cpu(42.0, 5)
|
||||||
|
self.assertEqual(self.snapshot()["speech"]["gate"], {"threshold": 50, "cpu": 42.0, "open": True})
|
||||||
|
self.host_cpu(50.0, 5)
|
||||||
|
state = self.snapshot()
|
||||||
|
self.assertEqual(state["speech"]["gate"], {"threshold": 50, "cpu": 50.0, "open": False})
|
||||||
|
rows = [(row["word"], row["rest"], row["speech"]) for row in state["radio"]]
|
||||||
|
self.assertEqual(rows, [
|
||||||
|
(WORDS["mayday"], "this is Docs Docs Docs, disk full, over", f"{WORDS['mayday']}, this is Docs Docs Docs, disk full, over"),
|
||||||
|
("", "Skills, Skills, this is Docs, ready, over", "Skills, Skills, this is Docs, ready, over"),
|
||||||
|
])
|
||||||
|
|
||||||
|
def test_the_threshold_changes_only_with_a_valid_passkey_signature(self):
|
||||||
|
good = self.guard(self.host)
|
||||||
|
current = self.snapshot()["settings"]
|
||||||
|
raised = {**current, "speech": {"threshold": 70}}
|
||||||
|
self.assertIsNone(json.loads(self.post_json("/api/settings/sign", current, good)[2])["payload"])
|
||||||
|
self.assertEqual(self.post_json("/api/settings", {**raised, "proof": None}, good)[:3:2], (400, b"not signed: no signature\n"))
|
||||||
|
signer = self.pin()
|
||||||
|
status, _, body = self.post_json("/api/settings/sign", raised, good)
|
||||||
|
self.assertEqual(status, 200, body)
|
||||||
|
request = json.loads(body)["payload"]
|
||||||
|
self.assertEqual(len(request["items"]), 1)
|
||||||
|
stranger = self.post_json("/api/settings", {**raised, "proof": key(-7, "stranger").proof(request, self.origin)}, good)
|
||||||
|
self.assertEqual(stranger[:3:2], (400, b"not signed: the signing key is not pinned\n"))
|
||||||
|
self.assertEqual(self.snapshot()["speech"]["gate"]["threshold"], 50)
|
||||||
|
lowered = {**current, "speech": {"threshold": 30}}
|
||||||
|
self.assertEqual(self.post_json("/api/settings", {**lowered, "proof": signer.proof(request, self.origin)}, good)[:3:2], (400, b"not signed: the signature does not list this item\n"))
|
||||||
|
status, _, body = self.post_json("/api/settings", {**raised, "proof": signer.proof(request, self.origin)}, good)
|
||||||
|
self.assertEqual((status, json.loads(body)["settings"]["speech"]), (200, {"threshold": 70}), body)
|
||||||
|
self.assertEqual(self.snapshot()["speech"]["gate"]["threshold"], 70)
|
||||||
|
self.assertEqual(settings.Settings(self.state / "settings.json").get()["speech"], {"threshold": 70})
|
||||||
|
for wrong in (35, 100, 20, "70", 70.0):
|
||||||
|
with self.subTest(wrong=wrong):
|
||||||
|
self.assertEqual(self.post_json("/api/settings/sign", {**current, "speech": {"threshold": wrong}}, good)[0], 400)
|
||||||
|
self.host_cpu(60.0, 5)
|
||||||
|
self.assertEqual(self.tts(LINE)[0], 200)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
@@ -169,7 +169,7 @@ class TestHubState(HubCase):
|
|||||||
self.assertTrue(all(row["voice"] in VOICES and row["voice"] != OPERATOR_VOICE for row in state["radio"]))
|
self.assertTrue(all(row["voice"] in VOICES and row["voice"] != OPERATOR_VOICE for row in state["radio"]))
|
||||||
self.assertEqual(state["speech"], {
|
self.assertEqual(state["speech"], {
|
||||||
"tts": True, "stt": True, "tts_off": "", "stt_off": "", "voices": len(VOICES), "operator": OPERATOR_VOICE, "sample": "all stations, this is Operator, radio check, out",
|
"tts": True, "stt": True, "tts_off": "", "stt_off": "", "voices": len(VOICES), "operator": OPERATOR_VOICE, "sample": "all stations, this is Operator, radio check, out",
|
||||||
"limit": phrasing.SPEECH_LIMIT, "rate": engines.STT_RATE, "seconds": engines.STT_SECONDS,
|
"limit": phrasing.SPEECH_LIMIT, "rate": engines.STT_RATE, "seconds": engines.STT_SECONDS, "gate": {"threshold": 50, "cpu": None, "open": False},
|
||||||
})
|
})
|
||||||
self.assertAlmostEqual(state["now"] - state["since"], view.RADIO_WINDOW)
|
self.assertAlmostEqual(state["now"] - state["since"], view.RADIO_WINDOW)
|
||||||
self.assertEqual((state["night"], state["silence"]), ("day", None))
|
self.assertEqual((state["night"], state["silence"]), ("day", None))
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from urllib.parse import quote
|
|||||||
|
|
||||||
from hub_case import HubCase
|
from hub_case import HubCase
|
||||||
from hub_support import ASK
|
from hub_support import ASK
|
||||||
from kvb_orchestrator import council, events, settings, skill_scripts, view
|
from kvb_orchestrator import council, events, skill_scripts, view
|
||||||
|
|
||||||
|
|
||||||
class TestHubSurface(HubCase):
|
class TestHubSurface(HubCase):
|
||||||
@@ -63,15 +63,6 @@ class TestHubSurface(HubCase):
|
|||||||
])
|
])
|
||||||
self.assertEqual(council.CLASSES, ("post", "call", "newsletter", "broadcast", "mayday", "pan-pan", "security"))
|
self.assertEqual(council.CLASSES, ("post", "call", "newsletter", "broadcast", "mayday", "pan-pan", "security"))
|
||||||
|
|
||||||
def test_read_aloud_lists_the_seven_classes_and_migrates_old_choices(self):
|
|
||||||
self.assertEqual(settings.speech_setting({"on": True, "classes": ["security", "mayday", "post", "call", "newsletter", "broadcast", "pan-pan"], "muted": []})["classes"], list(council.CLASSES))
|
|
||||||
for classes in (["direct", "mayday"], ["post"], ["mayday", "receipt"]):
|
|
||||||
with self.subTest(classes=classes), self.assertRaises(settings.ToolError):
|
|
||||||
settings.speech_setting({"on": True, "classes": classes, "muted": []})
|
|
||||||
stored = self.state / "old-settings.json"
|
|
||||||
stored.write_text(json.dumps({**settings.SETTINGS, "speech": {"on": True, "classes": ["direct", "pan-pan", "mayday"], "muted": []}}), encoding="utf-8")
|
|
||||||
self.assertEqual(settings.Settings(stored).get()["speech"]["classes"], ["post", "call", "mayday", "pan-pan"])
|
|
||||||
|
|
||||||
def test_funk_an_alle_sends_the_chosen_class_and_newsletter_goes_by_post(self):
|
def test_funk_an_alle_sends_the_chosen_class_and_newsletter_goes_by_post(self):
|
||||||
signer = self.pin()
|
signer = self.pin()
|
||||||
|
|
||||||
|
|||||||
+11
-21
@@ -8,9 +8,11 @@ import subprocess
|
|||||||
import unittest
|
import unittest
|
||||||
import wave
|
import wave
|
||||||
|
|
||||||
from hub_support import VOICE
|
from hub_support import HUB, VOICE
|
||||||
from kvb_orchestrator import engines, settings
|
from kvb_orchestrator import engines, settings
|
||||||
|
|
||||||
|
RADIOSET = HUB / "templates" / "radioset.js"
|
||||||
|
|
||||||
VOICE_PROBE = """
|
VOICE_PROBE = """
|
||||||
const V = require(process.argv[1]);
|
const V = require(process.argv[1]);
|
||||||
const settings = JSON.parse(process.argv[2]);
|
const settings = JSON.parse(process.argv[2]);
|
||||||
@@ -23,19 +25,12 @@ const rows = [
|
|||||||
{class: "broadcast", to: [], source: "s3", speech: "b", voice: "v"},
|
{class: "broadcast", to: [], source: "s3", speech: "b", voice: "v"},
|
||||||
{class: "broadcast", to: [], source: "s3", speech: "", voice: "v"},
|
{class: "broadcast", to: [], source: "s3", speech: "", voice: "v"},
|
||||||
];
|
];
|
||||||
const filters = [
|
|
||||||
{on: false, classes: ["broadcast", "direct", "security", "pan-pan", "mayday"], muted: []},
|
|
||||||
{on: true, classes: ["mayday"], muted: ["s1"]},
|
|
||||||
{on: true, classes: ["direct", "mayday"], muted: ["s2"]},
|
|
||||||
{on: true, classes: ["broadcast", "direct", "security", "pan-pan", "mayday"], muted: []},
|
|
||||||
];
|
|
||||||
let pending = [];
|
let pending = [];
|
||||||
for (const cls of ["broadcast", "direct", "security", "mayday", "broadcast", "pan-pan", "mayday", "direct"]) {
|
for (const cls of ["broadcast", "direct", "security", "mayday", "broadcast", "pan-pan", "mayday", "direct"]) {
|
||||||
pending = V.enqueue(pending, {cls, text: `${cls}-${pending.length}`}, 6);
|
pending = V.enqueue(pending, {cls, text: `${cls}-${pending.length}`}, 6);
|
||||||
}
|
}
|
||||||
console.log(JSON.stringify({
|
console.log(JSON.stringify({
|
||||||
tones: rows.map((row) => V.toneOf(row, "Operator")),
|
tones: rows.map((row) => V.toneOf(row, "Operator")),
|
||||||
spoken: filters.map((filter) => rows.map((row) => V.speakable(row, filter))),
|
|
||||||
queue: pending.map((item) => item.cls),
|
queue: pending.map((item) => item.cls),
|
||||||
loud: settings.map((one) => Object.fromEntries(["mayday", "review", "direct", "radio"].map((tone) => [tone, V.loudness(tone, one)]))),
|
loud: settings.map((one) => Object.fromEntries(["mayday", "review", "direct", "radio"].map((tone) => [tone, V.loudness(tone, one)]))),
|
||||||
beeps: settings.map((one) => V.beep({mayday: true, review: true, direct: true, radio: true}, one, ["mayday", "review", "direct", "radio"])),
|
beeps: settings.map((one) => V.beep({mayday: true, review: true, direct: true, radio: true}, one, ["mayday", "review", "direct", "radio"])),
|
||||||
@@ -48,12 +43,15 @@ console.log(JSON.stringify({
|
|||||||
class TestVoiceModule(unittest.TestCase):
|
class TestVoiceModule(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
quiet = {name: {"on": False, "volume": 0.1} for name in settings.TONES}
|
defaults = json.loads(subprocess.run(["node", "-e", "console.log(JSON.stringify(require(process.argv[1]).DEFAULTS))", str(RADIOSET)], capture_output=True, text=True, timeout=60, check=True).stdout)
|
||||||
|
tones = list(defaults["sounds"])
|
||||||
|
base = {"sounds": defaults["sounds"], "squelch": defaults["squelch"], "music": settings.SETTINGS["music"]}
|
||||||
|
quiet = {name: {"on": False, "volume": 0.1} for name in tones}
|
||||||
cls.settings = [
|
cls.settings = [
|
||||||
settings.SETTINGS,
|
base,
|
||||||
{**settings.SETTINGS, "music": {"on": True, "volume": 1.0}, "sounds": quiet},
|
{**base, "music": {"on": True, "volume": 1.0}, "sounds": quiet},
|
||||||
{**settings.SETTINGS, "sounds": {**settings.SETTINGS["sounds"], "mayday": {"on": True, "volume": 0.0}, "radio": {"on": True, "volume": 0.8}}},
|
{**base, "sounds": {**base["sounds"], "mayday": {"on": True, "volume": 0.0}, "radio": {"on": True, "volume": 0.8}}},
|
||||||
*({**settings.SETTINGS, "squelch": 0.0, "music": {"on": music, "volume": 1.0}, "sounds": {name: {"on": True, "volume": 1.0} for name in settings.TONES}} for music in (True, False)),
|
*({**base, "squelch": 0.0, "music": {"on": music, "volume": 1.0}, "sounds": {name: {"on": True, "volume": 1.0} for name in tones}} for music in (True, False)),
|
||||||
]
|
]
|
||||||
command = ["node", "-e", VOICE_PROBE, str(VOICE), json.dumps(cls.settings)]
|
command = ["node", "-e", VOICE_PROBE, str(VOICE), json.dumps(cls.settings)]
|
||||||
done = subprocess.run(command, capture_output=True, text=True, timeout=60, check=True)
|
done = subprocess.run(command, capture_output=True, text=True, timeout=60, check=True)
|
||||||
@@ -71,14 +69,6 @@ class TestVoiceModule(unittest.TestCase):
|
|||||||
self.assertEqual(self.data["beeps"], ["mayday", None, "mayday", None, None])
|
self.assertEqual(self.data["beeps"], ["mayday", None, "mayday", None, None])
|
||||||
self.assertEqual([set(loud.values()) for loud in self.data["loud"][3:]], [{0.0}, {0.0}])
|
self.assertEqual([set(loud.values()) for loud in self.data["loud"][3:]], [{0.0}, {0.0}])
|
||||||
|
|
||||||
def test_the_radio_filter_picks_classes_and_stations(self):
|
|
||||||
self.assertEqual(self.data["spoken"], [
|
|
||||||
[False] * 7,
|
|
||||||
[True, False, False, False, False, False, False],
|
|
||||||
[True, False, False, False, True, False, False],
|
|
||||||
[True, True, True, True, True, True, False],
|
|
||||||
])
|
|
||||||
|
|
||||||
def test_the_queue_puts_alarms_first_and_drops_routine_traffic(self):
|
def test_the_queue_puts_alarms_first_and_drops_routine_traffic(self):
|
||||||
self.assertEqual(self.data["queue"], ["mayday", "mayday", "pan-pan", "security", "broadcast", "direct"])
|
self.assertEqual(self.data["queue"], ["mayday", "mayday", "pan-pan", "security", "broadcast", "direct"])
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user