hex/scripts/test-linux-dictation.sh
Luke Parker f6f73ed1af
feat(dictation): stream every dictation on macOS and Linux (#144)
* feat(dictation): stream every dictation through one shared engine

The transcription engine now builds on macOS, Linux, and Windows. The capture
owns its live stream, so every OS feeds it the same way. macOS drives its
warm transcriber through the engine and drops pause chunking for dictation;
Linux streams on Vulkan with the 200 ms release tail. Meetings keep the pause
segmenter.

* test(linux): replay the dictation fixture again with a CPU-forced live stream

* refactor(dictation): delete helpers that only served pause chunking

* docs(dictation): describe streaming on every OS and drop pause chunking

Adds the opt-in Metal stream harness and records its first run on an M4 Air.

* refactor(settings): remove the Transcribe during pauses setting now that dictation streams
2026-10-19 13:00:12 +10:00

135 lines
5.3 KiB
Shell
Executable file

#!/usr/bin/env bash
set -euo pipefail
if [[ ${1:-} != --inside ]]; then
if [[ $# != 3 || $(id -u) == 0 ]]; then
echo "Usage: $0 /path/to/voice-control /path/to/model.gguf /path/to/jfk.wav (non-root)" >&2
exit 1
fi
for command in cc pkg-config pulseaudio pactl paplay xdotool xvfb-run timeout; do
command -v "$command" >/dev/null
done
exec timeout --kill-after=10 300 xvfb-run -a --server-args="-screen 0 1280x900x24 -noreset" \
"$0" --inside "$(realpath "$1")" "$(realpath "$2")" "$(realpath "$3")"
fi
shift
binary=$1
model=$2
audio=$3
root=$(cd -- "$(dirname -- "$0")/.." && pwd)
work=$(mktemp -d)
listener=
app=
target=
pulse=
finish() {
status=$?
trap - EXIT INT TERM
for pid in "$app" "$listener" "$target" "$pulse"; do
if [[ -n "$pid" ]]; then kill "$pid" 2>/dev/null || true; fi
done
for ((attempt = 0; attempt < 100; attempt++)); do
if [[ -z $(jobs -pr) ]]; then break; fi
sleep 0.05
done
for pid in $(jobs -pr); do kill -KILL "$pid" 2>/dev/null || true; done
wait 2>/dev/null || true
if [[ $status != 0 ]]; then
for log in "$work"/*.log "$work"/support/logs/live.ndjson; do
if [[ -f "$log" ]]; then printf '\n%s\n' "$log"; cat "$log"; fi
done
fi
rm -rf -- "$work"
exit "$status"
}
trap finish EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
export HOME="$work/home" XDG_RUNTIME_DIR="$work/runtime"
export HEX_APPLICATION_SUPPORT_DIR="$work/support" NO_COLOR=1
export PULSE_SERVER="unix:$work/pulse" PULSE_SOURCE=hex-fixture.monitor
# GPUI still needs software Vulkan; only inference uses the CPU in this fixture.
export GDK_BACKEND=x11 GGML_VK_VISIBLE_DEVICES=""
unset WAYLAND_DISPLAY VK_ICD_FILENAMES
mkdir -m 700 "$HOME" "$XDG_RUNTIME_DIR" "$HEX_APPLICATION_SUPPORT_DIR"
mkdir "$HEX_APPLICATION_SUPPORT_DIR/models"
# This GTK target uses Ctrl-V. Omitting the preference from an existing settings
# file deliberately selects the legacy terminal Ctrl-Shift-V behavior.
printf '{"sound_effect_volume":0,"paste_with_shift":false}\n' > "$HEX_APPLICATION_SUPPORT_DIR/linux-settings.json"
ln -s "$model" "$HEX_APPLICATION_SUPPORT_DIR/models/$(basename "$model")"
printf 'pcm.!default { type pulse server "%s" }\n' "$PULSE_SERVER" > "$HOME/.asoundrc"
pulseaudio -n --daemonize=no --use-pid-file=no --exit-idle-time=-1 \
--load="module-native-protocol-unix socket=$work/pulse auth-anonymous=1" \
--load='module-null-sink sink_name=hex-fixture rate=16000 channels=1' \
>"$work/pulse.log" 2>&1 &
pulse=$!
for ((attempt = 0; attempt < 100; attempt++)); do
if pactl info >/dev/null 2>&1; then break; fi
sleep 0.05
done
pactl info >/dev/null
cc "$root/tests/fixtures/wayland-paste-target.c" $(pkg-config --cflags --libs gtk+-3.0) -o "$work/target"
"$work/target" "$work/pasted.txt" >"$work/target.log" 2>&1 &
target=$!
window=$(xdotool search --sync --name '^Hex Wayland Test$' | head -n 1)
xdotool windowfocus --sync "$window"
"$binary" service >"$work/listener.log" 2>&1 &
listener=$!
echo 'Waiting for the model and virtual microphone...'
for ((attempt = 0; attempt < 1200; attempt++)); do
kill -0 "$listener"
if grep -q 'HEX dictation is ready' "$work/listener.log"; then break; fi
sleep 0.05
done
grep -q 'HEX dictation is ready' "$work/listener.log"
# Settings is a client; the complete dictation below runs after that client exits.
"$binary" app >"$work/app.log" 2>&1 &
app=$!
xdotool search --sync --onlyvisible --all --pid "$app" --name '^HEX$' >/dev/null
kill -TERM "$app"
wait "$app"
app=
kill -0 "$listener"
# Holds the shortcut over the speech, then waits until the target holds
# `$1` transcripts of it.
dictate() {
xdotool windowfocus --sync "$window"
echo 'Replaying speech through the held dictation shortcut...'
xdotool keydown Alt_L keydown space
sleep 0.5
paplay --device=hex-fixture "$audio"
sleep 0.5
xdotool keyup space keyup Alt_L
echo 'Waiting for transcribed text in the focused GTK target...'
for ((attempt = 0; attempt < 1800; attempt++)); do
kill -0 "$listener"
if (($(grep -oi 'ask what you can do for your country' "$work/pasted.txt" 2>/dev/null | wc -l) >= $1)); then break; fi
sleep 0.05
done
(($(grep -oi 'ask not what your country can do for you' "$work/pasted.txt" | wc -l) == $1))
(($(grep -oi 'ask what you can do for your country' "$work/pasted.txt" | wc -l) == $1))
}
dictate 1
printf 'Captured, transcribed, and pasted: %s\n' "$(<"$work/pasted.txt")"
# The CPU never streams by default, so that dictation took the whole-clip path.
grep -Eq 'transcribed Linux dictation.*source.{0,32}offline' "$work/listener.log"
kill -TERM "$listener"
wait "$listener"
# Force a live stream on the CPU to exercise capture-time feeding, the finish,
# and (when the stream lags behind real time) the whole-clip fallback.
HEX_STREAMING=1 "$binary" service >"$work/stream.log" 2>&1 &
listener=$!
for ((attempt = 0; attempt < 1200; attempt++)); do
kill -0 "$listener"
if grep -q 'HEX dictation is ready' "$work/stream.log"; then break; fi
sleep 0.05
done
grep -q 'streaming transcription is available' "$work/stream.log"
dictate 2
grep -E 'transcribed Linux dictation.*source.{0,32}(stream|fallback)' "$work/stream.log"
kill -TERM "$listener"
wait "$listener"
listener=
outputs=$(pactl list short source-outputs)
test -z "$outputs"
echo 'Linux service dictation (whole clip and live stream) after Settings exits and orderly shutdown passed.'