Skip to content

Instantly share code, notes, and snippets.

@JayGoldberg
Last active April 16, 2025 18:42
Show Gist options
  • Save JayGoldberg/08244d7d31fa9df84b884959acb27163 to your computer and use it in GitHub Desktop.
Save JayGoldberg/08244d7d31fa9df84b884959acb27163 to your computer and use it in GitHub Desktop.
Script to generate audio output from Google Text-to-Speech API
#!/bin/bash
# GCPmakeSpeech.sh
# run in Google Cloud Shell in the Cloud Console
# adapted from https://cloud.google.com/text-to-speech/docs/create-audio-text-command-line
# Check if text argument is provided
if [ -z "$1" ]; then
echo "Usage: $0 \"TEXT_TO_SYNTHESIZE\" [OUTPUT_FILE.mp3]"
exit 1
fi
TEXT_TO_SYNTHESIZE="$1"
OUTPUT_FILE="${2:-synthspeech.mp3}" # Default output file is output.mp3
# Escape double quotes within the input text for JSON
#ESCAPED_TEXT=$(echo "$TEXT_TO_SYNTHESIZE" | sed 's/"/\\"/g')
# Construct the JSON payload with the provided text
JSON_PAYLOAD=$(printf '{
"input": {
"text": "%s"
},
"voice": {
"languageCode": "en-gb",
"name": "en-GB-Standard-A",
"ssmlGender": "FEMALE"
},
"audioConfig": {
"audioEncoding": "MP3"
}
}' "$TEXT_TO_SYNTHESIZE")
# Google Cloud Text-to-Speech API endpoint
TTS_ENDPOINT="https://texttospeech.googleapis.com/v1/text:synthesize"
# Construct the curl command
curl -s -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-H "X-Goog-User-Project: $(gcloud config list --format='value(core.project)')" \
-d "$JSON_PAYLOAD" \
"${TTS_ENDPOINT}" > response.json
# curl -X POST \
# -H "Authorization: Bearer $(gcloud auth print-access-token)" \
# -H "x-goog-user-project: $GOOGLE_CLOUD_PROJECT" \
# -H "Content-Type: application/json; charset=utf-8" \
# -d "$JSON_PAYLOAD" \
# "https://texttospeech.googleapis.com/v1/text:synthesize" | jq -r '.audioContent' | base64 -d > tts.mp3
# Check if the request was successful
if jq -e '.audioContent' response.json > /dev/null; then
# Extract the audio content from the JSON response
AUDIO_CONTENT=$(jq -r '.audioContent' response.json)
# Decode the base64 audio content and save it to the output file
echo "$AUDIO_CONTENT" | base64 -d > "$OUTPUT_FILE"
echo "Audio content saved to: $OUTPUT_FILE"
else
echo "Error synthesizing text:"
cat response.json
fi
# Clean up the temporary response file
rm response.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment