Last active
July 18, 2025 15:55
-
-
Save Manamama/5a76b442feeb35a96b42343d06dcee6c to your computer and use it in GitHub Desktop.
Adapted piperme shell function for Termux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#See here: https://github.com/Manamama/Ubuntu_Scripts_1/blob/main/piperme_function.sh too. | |
piperme () | |
{ | |
echo "piperme: Using piper TTS to voice text via sox. Version 2.0 (adapted)"; | |
local voice_model_dir="/data/data/com.termux/files/home/.cache/piper"; | |
local lang="en"; | |
local input_text=""; | |
local input_file=""; | |
local voice_base=""; | |
# Display usage if no arguments are provided | |
if [[ "$#" -eq 0 ]]; then | |
echo "Usage: piperme [-pl|-en] <text_to_speak> | -f <file_to_speak>"; | |
echo " -pl: Use Polish voice (pl_PL-darkman-medium)"; | |
echo " -en: Use English voice (en_US-libritts-high, default)"; | |
echo " <text_to_speak>: The text to synthesize."; | |
echo " -f <file_to_speak>: Path to a file containing text to synthesize."; | |
echo "Example: piperme -en \"Hello, how are you?\""; | |
echo "Example: piperme -pl -f my_polish_text.txt"; | |
return 0; | |
fi; | |
# Parse language argument | |
if [[ "$1" == "-pl" ]]; then | |
lang="pl"; | |
shift; | |
elif [[ "$1" == "-en" ]]; then | |
lang="en"; | |
shift; | |
fi; | |
# Set voice model based on language | |
if [[ "$lang" == "pl" ]]; then | |
voice_base="pl_PL-darkman-medium"; | |
else | |
voice_base="en_US-libritts-high"; | |
fi; | |
echo; | |
echo "Model used: $voice_base"; | |
echo; | |
# Playback options for sox's play command | |
local play_opts="-r 22050 -c 1 -e float -b 32 -t raw -"; | |
local stdin_cmd=""; | |
# Handle input: file or direct text | |
if [[ "$1" == "-f" ]]; then | |
shift; | |
if [[ -z "$1" ]]; then | |
echo "Error: Missing filename after -f." 1>&2; | |
return 1; | |
fi; | |
if [[ ! -f "$1" ]]; then | |
echo "Error: File '$1' not found." 1>&2; | |
return 1; | |
fi; | |
stdin_cmd="cat \"$1\""; | |
else | |
if [[ -z "$1" ]]; then | |
echo "Error: Please provide text or use -f <file>." 1>&2; | |
return 1; | |
fi; | |
input_text="$*"; | |
stdin_cmd="echo \"$input_text\""; | |
fi; | |
# Check for existence of ONNX model files | |
local model_file="$voice_model_dir/$voice_base.onnx"; | |
local config_file="$voice_model_dir/$voice_base.onnx.json"; # Still check for json config | |
if [[ ! -f "$model_file" ]] || [[ ! -f "$config_file" ]]; then | |
echo "Error: Voice model files for '$voice_base' missing in '$voice_model_dir'." 1>&2; | |
return 1; | |
fi; | |
# Construct the piper command | |
# The new piper CLI expects model name and PIPER_VOICE_PATH env var | |
local piper_cmd="PIPER_VOICE_PATH=\"$voice_model_dir\" piper -m \"$voice_base\" --output-raw -"; | |
# Execute the command | |
time eval "$stdin_cmd | $piper_cmd | play $play_opts" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See here the back story:
OHF-Voice/piper1-gpl#7
See here: https://github.com/Manamama/Ubuntu_Scripts_1/blob/main/piperme_function.sh too.