#!/usr/bin/env bash
# Text-to-speech via Piper (local, free). Usage: speak.sh "text to say" [output.ogg]
set -euo pipefail
PIPER_DIR=/root/tools/piper/piper
VOICE=/root/tools/piper/voices/en_US-joe-medium.onnx
TEXT="$1"
OUT="${2:-/tmp/tts-$$.ogg}"
WAV="/tmp/tts-$$.wav"

echo "$TEXT" | "$PIPER_DIR/piper" -m "$VOICE" -f "$WAV" -q
ffmpeg -y -loglevel error -i "$WAV" -c:a libopus -b:a 32k "$OUT"
rm -f "$WAV"
echo "$OUT"
