Echo voice now used for its adjusters so prieview can be done while setting the voice.
This commit is contained in:
1 parent
70d447667e
commit
0834b47b55
26 files changed
+1080
-52
No files matched your search
@@ -0,0 +1,209 @@
|
||||
"""Real reader/preferences/GTK flow against a private, completion-aware SSIP server."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import subprocess
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
|
||||
from native_x11 import wait_for
|
||||
|
||||
|
||||
class PreviewSpeechServer:
|
||||
def __init__(self, path):
|
||||
self.listener = socket.socket(socket.AF_UNIX)
|
||||
self.listener.bind(str(path))
|
||||
self.listener.listen()
|
||||
self.lock = threading.RLock()
|
||||
self.clients = {}
|
||||
self.spoken = []
|
||||
threading.Thread(target=self.accept_clients, daemon=True).start()
|
||||
|
||||
def accept_clients(self):
|
||||
while True:
|
||||
try:
|
||||
connection, _ = self.listener.accept()
|
||||
except OSError:
|
||||
return
|
||||
with self.lock:
|
||||
clientId = len(self.clients) + 1
|
||||
client = {"socket": connection, "lock": threading.RLock(), "notifications": False,
|
||||
"module": "main", "voice": "", "active": None}
|
||||
self.clients[clientId] = client
|
||||
threading.Thread(target=self.serve_client, args=(clientId, client), daemon=True).start()
|
||||
|
||||
def reply(self, client, text):
|
||||
with client["lock"]:
|
||||
try:
|
||||
client["socket"].sendall(text.encode())
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def finish(self, clientId, client, record, canceled=False):
|
||||
with self.lock:
|
||||
if client["active"] is not record:
|
||||
return
|
||||
client["active"] = None
|
||||
record["finished"], record["canceled"] = True, canceled
|
||||
if client["notifications"]:
|
||||
code = 703 if canceled else 702
|
||||
self.reply(client, f"{code}-1\r\n{code}-{clientId}\r\n{code} DONE\r\n")
|
||||
|
||||
def serve_client(self, clientId, client):
|
||||
try:
|
||||
with client["socket"].makefile("rb") as stream:
|
||||
for raw in stream:
|
||||
command = raw.decode().strip()
|
||||
response = "200 OK\r\n"
|
||||
if command == "HISTORY GET CLIENT_ID":
|
||||
response = f"245-{clientId}\r\n245 OK\r\n"
|
||||
elif command == "LIST OUTPUT_MODULES":
|
||||
response = "200-echo-a\r\n200-echo-b\r\n200-main\r\n200 OK\r\n"
|
||||
elif command == "LIST SYNTHESIS_VOICES":
|
||||
voice = {"echo-a": "Echo A", "echo-b": "Echo B", "main": "Main"}[client["module"]]
|
||||
response = f"200-{voice}\ten\tnone\r\n200 OK\r\n"
|
||||
elif command.startswith("SET SELF OUTPUT_MODULE "):
|
||||
client["module"] = command.removeprefix("SET SELF OUTPUT_MODULE ")
|
||||
elif command.startswith("SET SELF SYNTHESIS_VOICE "):
|
||||
client["voice"] = command.removeprefix("SET SELF SYNTHESIS_VOICE ")
|
||||
elif command.startswith("SET SELF NOTIFICATION ALL "):
|
||||
client["notifications"] = command.endswith(" on")
|
||||
elif command.startswith("CANCEL "):
|
||||
with self.lock:
|
||||
targetId = int(command.split()[1])
|
||||
target = self.clients[targetId]
|
||||
if target["active"] is not None:
|
||||
self.finish(targetId, target, target["active"], canceled=True)
|
||||
elif command == "SPEAK":
|
||||
parts = []
|
||||
for line in stream:
|
||||
if line == b".\r\n":
|
||||
break
|
||||
parts.append(line.decode().rstrip("\r\n"))
|
||||
record = {"text": "\n".join(parts), "module": client["module"],
|
||||
"voice": client["voice"], "finished": False, "canceled": False}
|
||||
with self.lock:
|
||||
self.spoken.append(record)
|
||||
client["active"] = record
|
||||
self.reply(client, "230 OK\r\n200 QUEUED\r\n")
|
||||
if client["notifications"]:
|
||||
self.reply(client, f"701-1\r\n701-{clientId}\r\n701 BEGIN\r\n")
|
||||
timer = threading.Timer(0.25, self.finish, args=(clientId, client, record))
|
||||
timer.daemon = True
|
||||
timer.start()
|
||||
continue
|
||||
self.reply(client, response)
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
|
||||
def snapshot(self):
|
||||
with self.lock:
|
||||
return [dict(record) for record in self.spoken]
|
||||
|
||||
def close(self):
|
||||
self.listener.close()
|
||||
for client in self.clients.values():
|
||||
client["socket"].close()
|
||||
|
||||
|
||||
def check_runtime():
|
||||
import pyatspi
|
||||
directory = Path(tempfile.mkdtemp(prefix="cthulhu-echo-runtime-"))
|
||||
binary = os.environ["CTHULHU_BINARY"]
|
||||
wmConfig = directory / "i3.config"
|
||||
wmConfig.write_text(f"font pango:monospace 10\nipc-socket {directory / 'i3.sock'}\n")
|
||||
server = PreviewSpeechServer(directory / "speech.sock")
|
||||
wm = subprocess.Popen(["i3", "-c", str(wmConfig)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
reader = None
|
||||
try:
|
||||
wait_for(lambda: "0x" in subprocess.check_output(["xprop", "-root", "_NET_SUPPORTING_WM_CHECK"], text=True), "private WM missing")
|
||||
configPath = directory / "settings.toml"
|
||||
configPath.write_text('''schema-version = 2
|
||||
active-profile = "default"
|
||||
[profiles.default.speech]
|
||||
output-module = "main"
|
||||
[profiles.default.braille]
|
||||
enabled = false
|
||||
[profiles.default.sound]
|
||||
enabled = false
|
||||
[profiles.default.key-echo]
|
||||
enabled = false
|
||||
custom-voice = true
|
||||
custom-speech-server = true
|
||||
speech-server = "echo-a"
|
||||
[profiles.default.key-echo.voice]
|
||||
rate = 50
|
||||
average-pitch = 5
|
||||
gain = 5
|
||||
[profiles.default.key-echo.voice.family]
|
||||
name = "Echo A"
|
||||
lang = "en"
|
||||
''')
|
||||
reader = subprocess.Popen([binary, "--config", str(configPath), "--settings", "--no-tentacles",
|
||||
"--speech-socket", str(directory / "speech.sock"),
|
||||
"--debug-file", str(directory / "reader.log")],
|
||||
stdout=subprocess.DEVNULL, stderr=(directory / "stderr.log").open("w"))
|
||||
def find_window():
|
||||
for application in pyatspi.Registry.getDesktop(0):
|
||||
for window in application:
|
||||
if window and window.name == "Cthulhu Preferences":
|
||||
return window
|
||||
window = wait_for(find_window, "reader's preferences did not open")
|
||||
def walk(node):
|
||||
yield node
|
||||
for child in node:
|
||||
if child is not None:
|
||||
yield from walk(child)
|
||||
controls = {node.name: node for node in walk(window) if node.name}
|
||||
xid = subprocess.check_output(["xdotool", "search", "--onlyvisible", "--name", "^Cthulhu Preferences$"], text=True).splitlines()[0]
|
||||
subprocess.run(["xdotool", "windowactivate", "--sync", xid], check=True)
|
||||
tabs = next(node for node in walk(window) if node.getRole() == pyatspi.ROLE_PAGE_TAB_LIST)
|
||||
assert tabs.querySelection().selectChild(4)
|
||||
assert controls["Use custom echo voice"].queryComponent().grabFocus()
|
||||
wait_for(lambda: any(record["text"] == "checked" and record["finished"]
|
||||
for record in server.snapshot()), "initial checkbox announcement did not finish")
|
||||
time.sleep(0.3)
|
||||
def key(*keys):
|
||||
subprocess.run(["xdotool", "key", "--clearmodifiers", *keys], check=True)
|
||||
def check_output(keys, expected):
|
||||
offset = len(server.snapshot())
|
||||
key(*keys)
|
||||
def completed():
|
||||
records = server.snapshot()[offset:]
|
||||
matching = [record for record in records if record["text"] in {text for text, _, _ in expected}]
|
||||
return len(matching) >= len(expected) and all(record["finished"] for record in matching)
|
||||
wait_for(completed, f"preview did not complete: {keys}")
|
||||
# The original delayed menu-item event followed its preview by up
|
||||
# to 160 ms. Require quiescence beyond that delay, then inspect all
|
||||
# speech requests, including duplicates in the main voice.
|
||||
time.sleep(0.25)
|
||||
records = server.snapshot()[offset:]
|
||||
got = [(record["text"], record["module"], record["voice"]) for record in records]
|
||||
assert got == expected, (keys, got, expected)
|
||||
assert not any(record["canceled"] for record in records), (keys, records)
|
||||
check_output(("Tab",), [("Key echo speech synthesizer", "main", ""), ("echo-a", "echo-a", "Echo A")])
|
||||
check_output(("Down",), [("echo-b", "echo-b", "")])
|
||||
check_output(("Tab",), [("Key echo person", "main", ""), ("Default", "echo-b", "")])
|
||||
check_output(("Down",), [("Echo B (en)", "echo-b", "Echo B")])
|
||||
for control in ("rate", "pitch", "volume"):
|
||||
value = "50.0" if control == "rate" else "5.0"
|
||||
check_output(("Tab",), [("Key echo " + control, "main", ""), (value, "echo-b", "Echo B")])
|
||||
for control in ("pitch", "rate", "person", "synthesizer"):
|
||||
value = {"pitch": "5.0", "rate": "50.0", "person": "Echo B (en)", "synthesizer": "echo-b"}[control]
|
||||
label = "Key echo speech synthesizer" if control == "synthesizer" else "Key echo " + control
|
||||
check_output(("shift+Tab",), [(label, "main", ""), (value, "echo-b", "Echo B")])
|
||||
print(f"PASS: real reader + GTK + SSIP focus/selection previews, voice completion, no duplicate menu-item speech; evidence: {directory}")
|
||||
finally:
|
||||
if reader is not None:
|
||||
reader.terminate()
|
||||
try:
|
||||
reader.wait(timeout=5)
|
||||
except subprocess.TimeoutExpired:
|
||||
reader.kill()
|
||||
reader.wait(timeout=5)
|
||||
wm.terminate()
|
||||
wm.wait(timeout=5)
|
||||
server.close()
|
||||
(directory / "speech.json").write_text(json.dumps(server.snapshot(), indent=2))
|
||||
Reference in new issue
Block a user