Cotton code
86 lines · 3.8 KB · ShellRaw
#!/usr/bin/env bash# Called only by ui-tests-stub.sh --voice, inside its private workspace and build lock.# Plays recordings through the shipped dictation (VoiceGateTests, on this Mac —# the iOS simulator cannot run the speech model) and scores what it heard.# Three sources of audio, by what the caller set:#   (none)                       the synthetic corpus: rendered with macOS voices,#                                again in its second voice, and each name alone#                                for the synthesizer probe#   VOICEGATE_RECORDING + _SCRIPT  one recording of the whole script read aloud#                                (the owner's Voice Memo, read in place), cut on#                                its pauses into one file per line (split.py)#   VOICEGATE_MANIFEST           {hints, utterances: [{text, file}]}, a file per lineset -euo pipefailDIR="$1"; OUT="$2"; REPO="$3"HERE="$REPO/scripts/voicegate"# shellcheck source=../lib/owned-root.shsource "$REPO/scripts/lib/owned-root.sh"AUDIO="$DIR/voicegate/audio"refuse_wide_root "the voicegate audio folder" "$AUDIO"rm -rf "$AUDIO"mkdir -p "$AUDIO" "$OUT"if [ -n "${VOICEGATE_RECORDING:-}" ]; then  [ -n "${VOICEGATE_SCRIPT:-}" ] || { echo '[voice] --recording needs --script <json>' >&2; exit 64; }  cp "$VOICEGATE_SCRIPT" "$AUDIO/corpus.json"  python3 "$HERE/split.py" "$VOICEGATE_RECORDING" "$AUDIO/corpus.json" "$AUDIO"  echo '[]' > "$AUDIO/probes.json"elif [ -n "${VOICEGATE_MANIFEST:-}" ]; then  python3 - "$VOICEGATE_MANIFEST" "$AUDIO" <<'PY'import json, subprocess, sysfrom pathlib import Pathmanifest, out = json.loads(Path(sys.argv[1]).read_text()), Path(sys.argv[2])for n, u in enumerate(manifest['utterances'], 1):    subprocess.run(['afconvert', '-f', 'WAVE', '-d', 'LEI16@16000', '-c', '1', u['file'], str(out / f'u{n:02d}.wav')],                   check=True)(out / 'corpus.json').write_text(json.dumps(    {'hints': manifest['hints'], 'utterances': [{'text': u['text']} for u in manifest['utterances']]}))(out / 'probes.json').write_text('[]')PYelse  cp "$HERE/corpus.json" "$AUDIO/corpus.json"  python3 - "$AUDIO" "$HERE" <<'PY'import json, subprocess, sysfrom pathlib import Pathout = Path(sys.argv[1])sys.path.insert(0, sys.argv[2])from report import hint_list, names_incorpus = json.loads((out / 'corpus.json').read_text())second = corpus['second_voice']def render(voice, text, name):    subprocess.run(['say', '-v', voice, '-o', str(out / name),                    '--file-format=WAVE', '--data-format=LEI16@16000', text], check=True)hints = hint_list(corpus)pairs = []for n, u in enumerate(corpus['utterances'], 1):    # The corpus's own voices, then every line again in the second voice.    render(u['voice'], u['text'], f'u{n:02d}.wav')    render(second, u['text'], f'r{n:02d}.wav')    for name in names_in(u['text'], hints):        for voice in (u['voice'], second):            if (voice, name) not in pairs:                pairs.append((voice, name))# The synthesizer probe: each name said alone by each voice that speaks it.probes = []for i, (voice, name) in enumerate(pairs, 1):    render(voice, name + '.', f'p{i:03d}.wav')    probes.append({'voice': voice, 'name': name, 'file': f'p{i:03d}.wav'})(out / 'probes.json').write_text(json.dumps(probes))PYfiset +e(cd "$DIR" && COTTON_VOICEGATE_DIR="$AUDIO" COTTON_UI_TESTS_REPO_ROOT="$REPO" \  swift test --filter VoiceGateTests) > "$OUT/test.log" 2>&1status=$?set -eif [ "$status" -ne 0 ]; then  grep -E "error:|failed|Test Case" "$OUT/test.log" | head -40 >&2  echo "[voice] FAILED: swift test exited $status (full log: $OUT/test.log)" >&2  exit "$status"fi[ -s "$AUDIO/heard.json" ] || { echo '[voice] FAILED: the harness wrote nothing' >&2; exit 1; }cp "$AUDIO/heard.json" "$OUT/heard.json"python3 "$HERE/report.py" "$AUDIO/corpus.json" "$OUT/heard.json" | tee "$OUT/report.txt"