Skip to content

Instantly share code, notes, and snippets.

@davit312
Last active August 19, 2024 04:07
Show Gist options
  • Save davit312/fe2e35d6a9ebdb959b1124904815efc1 to your computer and use it in GitHub Desktop.
Save davit312/fe2e35d6a9ebdb959b1124904815efc1 to your computer and use it in GitHub Desktop.
Frontend for piper-tts
#!/usr/bin/env bash
# This model has high quality voice and proccessing time is small
FAVORITE_MODEL="en_US-ryan-low.onnx"
if [[ "$1" == "-h" ]]; then
echo -e "
Frontend for piper-tts to stram syntesized audio.
Streamming is faster then full audio generaation: result become available while program continues in background.
Use this if no need to save generated result.
Script can start without parameters or with:
* \t [text]
* \t [text model_file_fullname]
Download piper binaries from: \t https://github.com/rhasspy/piper/releases
Voices download links are in: \t https://github.com/rhasspy/piper/blob/master/VOICES.md
Put models .onnx and .onnx.json files in directory containing piper binary: $(dirname "$0")
Export variable PIPER_MODEL_NAME (including file exension), to specify preferred model
"
exit 255
fi
if [[ -L "${BASH_SOURCE[0]}" ]]; then
SCRIPT_FILE=$(readlink -f "${BASH_SOURCE[0]}")
else
SCRIPT_FILE="${BASH_SOURCE[0]}"
fi
PIPER_DIR="$(dirname "$SCRIPT_FILE")"
cd "$PIPER_DIR"
if [[ ! -f './piper' ]]; then
echo "ERROR: piper binarty file not found in this folder"
echo "put this script file in the same folder with piper binary"
exit 2
fi
if [[ ! $(which aplay) ]]; then
echo "ERROR: aplay not found"
echo "install alsa-tools package to able play audio"
exit 1
fi
ARG_VALUE="$2" # done for beeing able to modify if needed
if [ -n "$ARG_VALUE" ]; then # if customer provided model name in args, set that
MODEL_NAME="$ARG_VALUE"
elif [ -n "$PIPER_MODEL_NAME" ]; then # if env kontains model name, set that
MODEL_NAME="$PIPER_MODEL_NAME"
echo -e "Setting value from environment \n PIPER_MODEL_NAME=$PIPER_MODEL_NAME\n"
else
# set harcoded model name,
# you can change it to you favorite, and not set env varivale or command argument
MODEL_NAME="$FAVORITE_MODEL"
allow_user_imput="1"
fi
count=1
models=()
for model in *.onnx; do
models+=("$model")
if [[ "$MODEL_NAME" == "$model" ]]; then
model_number=$count
fi
((count+=1))
done
if (( count < 1 )); then
echo "No models file found"
echo "put models .onnx and .onnx.json files in directory with piper binary"
exit 3
fi
if [ -z $model_number ]; then
echo "WARNING! Reseting default setting for '$MODEL_NAME', no model file found"
allow_user_imput="1"
model_number=-9000
unset MODEL_NAME
unset ARG_VALUE
fi
if [ -n "$allow_user_imput" ]; then # if user not provided model name as argunebt and not in env
count=1
echo -e " \t Installed models list:"
for model in "${models[@]}"; do
echo "$count: $model"
((count+=1))
done
models_array_length="${#models[@]}"
read -p "Type model number (Default is $model_number): " model_number_read
if [ -n "$model_number_read" ]; then
if [ $model_number_read -eq $model_number_read ]; then
model_number=$model_number_read
if [[ "$model_number" > "$models_array_length" || "$model_number" < 1 ]]; then
echo "ERROR: Selected value is out of range 1 to $models_array_length"
exit 6
fi
else
echo "ERROR: Input is not a number"
exit 4
fi
fi
fi
((model_number-=1))
_model="${models["$model_number"]}"
if [[ "$_model" =~ .*"low".* ]]; then
_rate=16000
else
_rate=22050
fi
if [[ -z "$1" ]]; then
read -p "Type text to read: " _text
else
_text="$1"
fi
echo -e "\nModel: \t $_model"
echo -e "Rare: \t $_rate\n"
./piper --model ${_model} --output-raw <<< "${_text}" | aplay -r ${_rate} -f S16_LE -t raw
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment