Skip to content

Instantly share code, notes, and snippets.

@vijinho
Last active January 7, 2025 16:07
Show Gist options
  • Save vijinho/a7fb6fca11d30c968143cff60393a008 to your computer and use it in GitHub Desktop.
Save vijinho/a7fb6fca11d30c968143cff60393a008 to your computer and use it in GitHub Desktop.
TTS example script for Coqui-AI https://github.com/coqui-ai/TTS which speaks-out a fortune, for testing models and vocoders
#!/bin/bash
# requires https://github.com/coqui-ai/TTS
# Associative arrays for mapping IDs to model and vocoder names
declare -A model_ids=(
[11]="tts_models/en/ljspeech/tacotron2-DDC"
[12]="tts_models/en/ljspeech/tacotron2-DDC_ph"
[15]="tts_models/en/ljspeech/tacotron2-DCA"
[20]="tts_models/en/ljspeech/neural_hmm"
)
declare -A vocoder_ids=(
[4]="vocoder_models/en/ljspeech/multiband-melgan"
[6]="vocoder_models/en/ljspeech/univnet"
)
# Function to display help
usage() {
cat <<EOF
Usage: $0 [-m <model_id>] [-v <vocoder_id>] [-f <output_file>] [-p] [-r]
Options:
-m <model_id> : Model ID (11, 12, 15, 20). Default: Random valid model.
-v <vocoder_id> : Vocoder ID (4, 5). Default: Random valid vocoder.
-f <output_file>: Path to save the output file. Default: /tmp/tts_m<model_id>_v<vocoder_id>-YYYYMMDD-HHMMSS.wav
-p : Pipe the output to aplay (for audio playback).
-r : Randomize valid numbers for model_id and vocoder_id.
-h, --help : Display this help message.
Examples:
$0 -m 12 -v 4 -f /home/user/output.wav
$0 -r -p
EOF
exit 1
}
# Check if the provided IDs are valid
model_id=""
vocoder_id=""
while getopts "m:v:f:prh" opt; do
case ${opt} in
m )
model_id=$OPTARG
;;
v )
vocoder_id=$OPTARG
;;
f )
out_path=$OPTARG
;;
p )
pipe_out=true
;;
r )
# Randomize valid numbers for the model_id and vocoder_id
random_model_id=$((RANDOM % ${#model_ids[@]} + 10)) # Start from index 10 to avoid conflicts with custom IDs
random_vocoder_id=$((RANDOM % ${#vocoder_ids[@]} + 3)) # Start from index 3 for vocoders
model_id=$random_model_id
vocoder_id=$random_vocoder_id
;;
h )
usage
;;
\? )
echo "Invalid option: -$OPTARG" 1>&2
usage
;;
: )
echo "Invalid option: -$OPTARG requires an argument" 1>&2
usage
;;
esac
done
# Validate model_id and vocoder_id
if [[ -z "${model_ids[$model_id]}" ]]; then
echo "Invalid model ID: $model_id. Valid models: ${!model_ids[@]}"
usage
fi
if [[ -z "${vocoder_ids[$vocoder_id]}" ]]; then
echo "Invalid vocoder ID: $vocoder_id. Valid vocoders: ${!vocoder_ids[@]}"
usage
fi
# Assign out_path if not provided
if [[ -z "$out_path" ]]; then
out_path="$TEMP/tts_m${model_id}_v${vocoder_id}-$(date "+%Y%m%d-%H%M%S").wav"
else
# Ensure the provided path is writable
out_dir=$(dirname "$out_path")
if [ ! -d "$out_dir" ] || [ ! -w "$out_dir" ]; then
echo "Invalid output directory: $out_dir"
usage
fi
fi
# Retrieve the corresponding model and vocoder names
model_name="${model_ids[$model_id]}"
vocoder_name="${vocoder_ids[$vocoder_id]}"
# Execute the TTS command
text="`fortune -a`"
tts_command="tts --text \"$text\" --model_name \"$model_name\" --vocoder_name \"$vocoder_name\" --out_path \"$out_path\""
if [ "$pipe_out" = true ]; then
tts_command="$tts_command --pipe_out | aplay"
fi
echo "Executing: $tts_command"
eval "$tts_command"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment