Files
robertwinkler@posteo.net e2b303b5ed Add device-aware audio input, calibration/test modes, and guided start.sh
- Open input devices at a supported native sample rate and resample to
  16 kHz in software (fixes 'Invalid sample rate' with webcams/USB mics)
- Lower default SILENCE_RMS to 800, add --agc, make all segmentation
  thresholds CLI-configurable, load/save them via calibration.json
- Add --calibrate (measures room noise vs. speech, recommends threshold)
  and --test (end-to-end mic + Groq ASR + translation gate, exit 0 on pass)
- Add --list-devices-json for machine-readable device listing
- Add start.sh: venv setup, interactive device picker, optional
  calibration, test gate before live mode, kiosk-mode browser launch
- Add requirements.txt; document installation and running in README

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-11-03 14:54:37 -06:00

125 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# start.sh — guided startup for live EN-ES conference subtitles.
#
# Flow: set up the virtualenv -> detect audio input devices and ask which one
# to use -> offer calibration -> run an end-to-end test (mic + Groq) and only
# proceed if it passes -> start live transcription/translation -> open the
# subtitle page in a kiosk-mode browser.
#
# Usage: ./start.sh [extra args for the python program, e.g. --target-lang es]
# Env: PORT=8080 to change the web port (default 8080).
set -euo pipefail
cd "$(dirname "$0")"
PROG="live-en-es-conference-subtitles-with-groq.py"
PORT="${PORT:-8080}"
URL="http://127.0.0.1:${PORT}"
# --- 1. Virtualenv -----------------------------------------------------------
if [ ! -x .venv/bin/python ]; then
echo "[start] creating virtualenv in .venv ..."
python3 -m venv .venv
.venv/bin/pip install --quiet --upgrade pip
.venv/bin/pip install --quiet -r requirements.txt
fi
PY=.venv/bin/python
if [ ! -f .env ]; then
echo "[start] WARNING: no .env file found. Create one with: GROQ_API_KEY=\"gsk_...\""
fi
# --- 2. Detect audio input devices and ask which to use ----------------------
echo "[start] detecting audio input devices ..."
mapfile -t DEVICES < <("$PY" "$PROG" --list-devices-json | "$PY" -c '
import json, sys
for d in json.load(sys.stdin):
rates = ",".join(str(r) for r in d["supported_rates"]) or "unknown"
print("%s|%s|%s|%s" % (d["index"], d["name"], d["default_samplerate"], rates))
')
if [ "${#DEVICES[@]}" -eq 0 ]; then
echo "[start] ERROR: no audio input devices found. Plug in a microphone/webcam."
exit 1
fi
echo "[start] available input devices:"
for d in "${DEVICES[@]}"; do
IFS='|' read -r i n def rates <<<"$d"
echo " [$i] $n (default ${def} Hz, supports: ${rates} Hz)"
done
DEV=""
while [ -z "$DEV" ]; do
read -rp "[start] select device index: " choice
for d in "${DEVICES[@]}"; do
if [ "${d%%|*}" = "$choice" ]; then DEV="$choice"; break; fi
done
[ -n "$DEV" ] || echo "[start] invalid choice, try again."
done
echo "[start] using device [$DEV]"
# --- 3. Offer calibration ----------------------------------------------------
read -rp "[start] run calibration now (recommended after changing device/room)? [Y/n] " ans
if [[ ! "$ans" =~ ^[Nn] ]]; then
read -rp "[start] calibration length in seconds [20]: " secs
secs="${secs:-20}"
"$PY" "$PROG" --device "$DEV" --calibrate "$secs"
fi
# --- 4. Test gate: live mode only starts if the test passes ------------------
while true; do
read -rp "[start] run the audio+API test before going live? [Y/n] " ans
if [[ "$ans" =~ ^[Nn] ]]; then
echo "[start] aborting: live transcription requires a passing test."
exit 1
fi
read -rp "[start] test length in seconds [10]: " tsecs
tsecs="${tsecs:-10}"
if "$PY" "$PROG" --device "$DEV" --test "$tsecs"; then
echo "[start] test passed."
break
fi
echo "[start] test FAILED."
read -rp "[start] retry the test? [Y/n] " retry
if [[ "$retry" =~ ^[Nn] ]]; then
echo "[start] aborting: live transcription requires a passing test."
exit 1
fi
done
# --- 5. Start live transcription/translation ---------------------------------
echo "[start] starting live subtitles on ${URL} ..."
"$PY" "$PROG" --device "$DEV" --port "$PORT" "$@" &
APP_PID=$!
BROWSER_PID=""
cleanup() {
[ -n "$BROWSER_PID" ] && kill "$BROWSER_PID" 2>/dev/null || true
kill "$APP_PID" 2>/dev/null || true
}
trap cleanup EXIT INT TERM
# wait until the web server answers
for _ in $(seq 1 50); do
curl -sf "${URL}/latest" >/dev/null 2>&1 && break
sleep 0.2
done
# --- 6. Open the subtitle page in a kiosk-mode browser -----------------------
if [ -z "${DISPLAY:-}" ]; then
echo "[start] no DISPLAY set - open ${URL} manually on the subtitle screen."
else
BROWSER=""
for b in google-chrome google-chrome-stable chromium chromium-browser firefox; do
if command -v "$b" >/dev/null 2>&1; then BROWSER="$b"; break; fi
done
if [ -z "$BROWSER" ]; then
echo "[start] no supported browser found - open ${URL} manually."
elif [ "$BROWSER" = "firefox" ]; then
echo "[start] opening ${URL} in Firefox kiosk mode ..."
firefox --kiosk "$URL" & BROWSER_PID=$!
else
echo "[start] opening ${URL} in ${BROWSER} kiosk mode ..."
"$BROWSER" --kiosk --no-first-run --disable-infobars --autoplay-policy=no-user-gesture-required "$URL" & BROWSER_PID=$!
fi
fi
echo "[start] running. Press Ctrl-C to stop (browser and server are closed automatically)."
wait "$APP_PID"