Skip to content

Instantly share code, notes, and snippets.

@danilop
Last active June 10, 2024 04:57
Show Gist options
  • Save danilop/b652f61b9944ff950ce7b1c0734dbb05 to your computer and use it in GitHub Desktop.
Save danilop/b652f61b9944ff950ce7b1c0734dbb05 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Define default voice
default_voice="Ruth"
# Define available voices
voices=(
"Ruth:Female English (US)"
"Matthew:Male English (US)"
"Amy:Female English (British)"
)
# Function to display usage
usage() {
echo "Usage: $0 [-v <voice>]" >&2
echo "Available voices:" >&2
for voice in "${voices[@]}"; do
name="${voice%:*}"
desc="${voice#*:}"
if [[ "$name" == "$default_voice" ]]; then
echo " $name: $desc - Default" >&2
else
echo " $name: $desc" >&2
fi
done
exit 1
}
# Check if AWS CLI is installed
if ! command -v aws &> /dev/null; then
echo "AWS CLI is not installed. Please install it first."
exit 1
fi
# Parse command-line options
while getopts ":v:" opt; do
case $opt in
v)
voice=$OPTARG
valid=0
for v in "${voices[@]}"; do
if [[ "${v%:*}" == "$voice" ]]; then
valid=1
break
fi
done
if [ $valid -eq 0 ]; then
echo "Invalid voice: $voice" >&2
usage
fi
;;
*)
echo "Invalid option: -$OPTARG" >&2
usage
;;
esac
done
shift $((OPTIND - 1))
# Set default voice if not provided
voice=${voice:-$default_voice}
# Check if input is from a pipe or keyboard
if [ -t 0 ]; then
echo "Enter text to synthesize (press Ctrl+D to finish):"
text=$(cat)
else
text=$(cat)
fi
# Create temporary files
tmp_text=$(mktemp)
tmp_voice=$(mktemp)
# Write text to temporary file
echo "$text" > "$tmp_text"
# Synthesize speech using AWS Polly
echo "Using ${voice} voice."
aws polly synthesize-speech --output-format mp3 --region us-east-1 \
--text "file://$tmp_text" \
--voice-id "$voice" --engine neural "$tmp_voice"
# Play the synthesized speech
afplay "$tmp_voice"
# Clean up temporary files
rm "$tmp_text" "$tmp_voice"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment