Files
CyberGril/samples/companion/tools/check_microphone.py
T

90 lines
3.6 KiB
Python

"""Synthetic WAV through Chromium's microphone device, not a human VAD pass."""
import asyncio
import json
import pathlib
from playwright.async_api import async_playwright
ROOT = pathlib.Path(__file__).resolve().parents[1]
OUT = ROOT / "evidence"
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(
args=[
"--no-sandbox",
"--autoplay-policy=no-user-gesture-required",
"--use-fake-device-for-media-stream",
"--use-fake-ui-for-media-stream",
f"--use-file-for-fake-audio-capture={OUT / 'synthetic-microphone.wav'}%noloop",
]
)
context = await browser.new_context(ignore_https_errors=True, permissions=["microphone"])
page = await context.new_page()
errors = []
page.on("pageerror", lambda error: errors.append(str(error)))
await page.goto("https://192.168.200.12:8014")
await page.wait_for_function(
"document.querySelector('#status').textContent.includes('就绪')"
)
await page.click("#enter")
await page.wait_for_function(
"document.querySelector('#status').textContent.includes('正在听')", timeout=40000
)
await page.click("#record")
await page.click("#mic")
await page.wait_for_function(
"document.querySelector('#mic').getAttribute('aria-pressed')==='true'"
)
await page.fill("#text", "请用三句话描述你眼前卧室的温暖氛围,大约六十个字。")
await page.click("button[type=submit]")
await page.wait_for_function(
"window.sampleEvents.some(e=>e.event==='cancel'&&e.detail.reason==='microphone-onset')",
timeout=25000,
)
await page.wait_for_function(
"document.querySelector('#transcript').textContent.includes('识别:')", timeout=15000
)
await page.wait_for_function(
"window.sampleEvents.filter(e=>e.event==='input-finished').length>=2", timeout=15000
)
await page.wait_for_timeout(18000)
await page.click("#record")
await page.wait_for_selector("#download:not([hidden])")
async with page.expect_download() as pending:
await page.click("#download")
await (await pending.value).save_as(OUT / "synthetic-microphone-av.webm")
events = await page.evaluate("window.sampleEvents")
onset = next(
e["at"]
for e in events
if e["event"] == "cancel" and e["detail"]["reason"] == "microphone-onset"
)
audible_before = [e for e in events if e["event"] == "first-audible" and e["at"] < onset]
audible_after = [e for e in events if e["event"] == "first-audible" and e["at"] > onset]
result = dict(
fixture="CosyVoice synthesized WAV through fake Chromium microphone; no human recording",
secure_context=await page.evaluate("isSecureContext"),
transcript=await page.locator("#transcript").inner_text(),
speech_was_audible_before_onset=bool(audible_before),
new_reply_after_asr=bool(audible_after),
events=events,
pageerrors=errors,
)
(OUT / "synthetic-microphone.json").write_text(
json.dumps(result, ensure_ascii=False, indent=2)
)
await page.click("#mic")
await page.click("#back")
await page.wait_for_timeout(4000)
await browser.close()
print(
json.dumps({k: v for k, v in result.items() if k != "events"}, ensure_ascii=False),
flush=True,
)
asyncio.run(main())