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>
This commit is contained in:
robertwinkler@posteo.net
2026-11-03 14:54:37 -06:00
parent c5128f0d31
commit e2b303b5ed
5 changed files with 703 additions and 44 deletions
+96 -7
View File
@@ -9,7 +9,10 @@ Real-time bilingual subtitles for conference talks, translating between English
- **Manual Language Switch**: Force output to English (`--target-lang en`) or Spanish (`--target-lang es`)
- **Automatic Language Detection**: Input language is auto-detected, output is always in target language (EN or ES)
- **Strict Hallucination Filtering**: Blocks Whisper hallucinations like "thank you for watching", "the train is...", etc.
- **Robust Against Noise**: High thresholds (RMS=1500, min 2.5 seconds) to block background noise while accepting normal speech
- **Robust Against Noise**: Configurable RMS threshold and minimum segment length block background noise; tune them per device/room with `--calibrate`
- **Calibration Mode**: `--calibrate` measures your room's noise floor vs. your speech level and saves optimal parameters to `calibration.json`
- **Any Audio Device**: External webcams/USB interfaces are opened at their native sample rate (e.g. 48 kHz) and resampled to 16 kHz in software; `--agc` compensates quiet built-in mics
- **Guided Startup**: `./start.sh` sets up the venv, asks which audio device to use, offers calibration, gates live mode behind a passing end-to-end `--test`, and opens the subtitles in a kiosk-mode browser
- **Professional Output**: Only outputs when valid speech is detected, completely silent otherwise
- **Clean Port Management**: Automatically frees the port when the app is closed (Ctrl-C)
- **Low Latency**: Optimized for minimal delay
@@ -31,23 +34,63 @@ Real-time bilingual subtitles for conference talks, translating between English
cd live-en-es-conference-subtitles-with-groq
```
2. Install dependencies:
2. Install dependencies into a virtualenv (this is what `start.sh` does automatically on first run):
```bash
pip install sounddevice numpy requests python-dotenv
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt # sounddevice, numpy, requests, python-dotenv
```
On Debian/Ubuntu you may also need the PortAudio system library:
```bash
sudo apt install libportaudio2 # plus python3-venv if venv creation fails
```
3. Create a `.env` file with your Groq API key:
3. Create a `.env` file with your Groq API key (get one free at [groq.com](https://groq.com)):
```bash
echo "GROQ_API_KEY=your_api_key_here" > .env
```
## Usage
4. Optional but recommended: a Chromium-based browser (`google-chrome`, `chromium`) or
`firefox` for the automatic kiosk-mode subtitle display.
## Running
### Guided start (recommended): `./start.sh`
### Quick Start
```bash
python3 live-en-es-conference-subtitles-with-groq.py
./start.sh # extra args are passed to the python program, e.g.:
./start.sh --target-lang es # force Spanish output
PORT=5000 ./start.sh # use a different web port (default 8080)
```
`start.sh` walks you through the whole setup:
1. **Virtualenv** — creates `.venv` and installs `requirements.txt` if missing.
2. **Device selection** — lists all audio input devices with their supported sample
rates (from `--list-devices-json`) and asks which one to use (e.g. your webcam).
Devices that don't support 16 kHz are handled automatically via resampling.
3. **Calibration (optional)** — runs `--calibrate`: stay silent for the first 60 %
of the time, then speak. Saves tuned thresholds to `calibration.json`.
4. **Test gate** — runs `--test`: captures audio while you speak and verifies that
speech is detected, Groq Whisper transcribes it, and the LLM translates it.
**Live transcription only starts if this test passes** (exit code 0); otherwise
you can retry or abort.
5. **Live mode** — starts the subtitle server in the background.
6. **Kiosk browser** — opens `http://127.0.0.1:8080` fullscreen in
`chrome/chromium --kiosk` (or `firefox --kiosk`). If no `DISPLAY` is set (e.g.
over SSH), it tells you the URL to open manually. Ctrl-C stops everything.
### Manual start
```bash
.venv/bin/python live-en-es-conference-subtitles-with-groq.py --list-devices # pick a device index
.venv/bin/python live-en-es-conference-subtitles-with-groq.py --device 4 --calibrate 20 # optional but recommended
.venv/bin/python live-en-es-conference-subtitles-with-groq.py --device 4 --test 10 # optional gate
.venv/bin/python live-en-es-conference-subtitles-with-groq.py --device 4 # live mode
```
Then open `http://<this-machine>:8080` on the subtitle screen (or let `start.sh`
do it in kiosk mode).
### Options
- `--list-devices`: List available audio input devices
@@ -73,6 +116,52 @@ python3 live-en-es-conference-subtitles-with-groq.py
```
Useful when mentioning mixed terms like "Instituto Politécnico Nacional" or "Sociedad Mexicana de Proteómica" - the output will stay in the target language.
### Audio devices, sample rates, and calibration
- The input device is opened at a sample rate it actually supports. Webcams and USB
interfaces often only support 44100/48000 Hz, so the audio is captured at the
native rate and resampled to 16 kHz in software — no more "invalid sample rate"
errors with external devices. `--list-devices` now also shows the supported
sample rates of every input device.
- `--samplerate HZ`: Request a specific input sample rate (default: 16000). If the
device does not support it, its native rate is used automatically.
- `--list-devices-json`: Print input devices (index, name, supported rates) as JSON.
Used by `start.sh` for the interactive device picker.
- `--test [SECONDS]`: End-to-end test (default 10 s): capture audio while you speak,
then check that (a) speech is detected above the silence threshold, (b) Groq Whisper
returns a transcription, and (c) the LLM returns a translation. Exits 0 only if all
pass. `start.sh` runs this before going live and aborts if it fails.
- `--calibrate [SECONDS]`: Calibration mode (default 20 s). Stay **silent** for the
first 60 % of the time (measures the room noise floor), then **speak a few
sentences** for the last 40 % (measures speech level). It then recommends a
silence threshold between the two, probes the supported sample rates, and saves
everything to `calibration.json`:
```bash
python3 live-en-es-conference-subtitles-with-groq.py --device 4 --calibrate 20
```
Run it once per machine/device/room (silent office vs. noisy conference give very
different thresholds). The file is loaded automatically on the next start
(`--no-config` ignores it; CLI flags override it).
- Manual tuning overrides (all optional, defaults shown):
```bash
python3 live-en-es-conference-subtitles-with-groq.py --silence-rms 800 # RMS threshold (int16) for speech detection
python3 live-en-es-conference-subtitles-with-groq.py --min-send 2.0 # minimum segment seconds sent to the API
python3 live-en-es-conference-subtitles-with-groq.py --silence-flush 0.5 # trailing silence that cuts a segment
python3 live-en-es-conference-subtitles-with-groq.py --max-segment 3.5 # maximum segment length
python3 live-en-es-conference-subtitles-with-groq.py --block-ms 20 # audio callback block size
```
A too-high `--silence-rms` (the old default of 1500) makes quiet built-in laptop
mics produce no output at all — lower it, or use `--calibrate`.
- `--agc`: Normalize the level of each segment before sending it to Groq. Useful
for quiet built-in microphones. Calibration enables it automatically when your
speech level is low.
### Access Subtitles
Open a web browser and navigate to (default port is 8080):