Skip to content

Instantly share code, notes, and snippets.

@Psychokiller1888
Last active September 1, 2019 23:53
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Psychokiller1888/7c4783c645d0a580aa595e7823bf3da1 to your computer and use it in GitHub Desktop.
Save Psychokiller1888/7c4783c645d0a580aa595e7823bf3da1 to your computer and use it in GitHub Desktop.
Give access to Google Wavenet to your Snips assistant
#!/usr/bin/env bash
# Shell script to use Google Wavenet as Snips TTS
# Install mpg123: sudo apt-get install mpg123
# Install Google SDK: https://cloud.google.com/text-to-speech/docs/quickstart-protocol.
# Follow point 6. to initialize the sdk after creating your service account. There is an apt-get install procedure!!
# Set the correct path to your googlecredentials.json file
export GOOGLE_APPLICATION_CREDENTIALS=""
# Set your cache path
cache=""
# Edit /etc/snips.toml
# Set "customtts" as snips-tts provider
#
# Add as customtts: command = ["/home/pi/ProjectAlice/shell/snipsWavenet.sh", "%%OUTPUT_FILE%%", "%%LANG%%", "US", "Wavenet-C", "FEMALE", "%%TEXT%%", "44100"]
# Change "US" to another language country code, "GB" per exemple for a british voice
# You can customize the "Wavenet-C" to another voice of your choice. https://cloud.google.com/text-to-speech/docs/voices
# Fit "FEMALE" to the voice gender you want. Note this is linked to google voices
# You can change the sample rate, the last argument, to your needs
# Restart snips: systemctl restart snips-*
outfile="$1"
lang="$2"
country="$3"
voice="$4"
gender="$5"
text="$6"
sampleRate="$7"
mkdir -pv "$cache"
languageCode="$lang"-"$country"
googleVoice="$languageCode"-"$voice"
text=${text//\'/\\\'}
md5string="$text""$googleVoice""$sampleRate"
hash="$(echo -n "$md5string" | md5sum | sed 's/ .*$//')"
cachefile="$cache""$hash".wav
downloadFile="/tmp/""$hash"
if [[ ! -f "$cachefile" ]]; then
curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
--data "{
'input':{
'text': '$text'
},
'voice':{
'languageCode':'$languageCode',
'name':'$googleVoice',
'ssmlGender':'$gender'
},
'audioConfig':{
'audioEncoding':'MP3'
}
}" "https://texttospeech.googleapis.com/v1/text:synthesize" > "$downloadFile"
sed -i 's/audioContent//' "$downloadFile" && \
tr -d '\n ":{}' < "$downloadFile" > "$downloadFile".tmp && \
base64 "$downloadFile".tmp --decode > "$downloadFile".mp3
mpg123 --quiet --wav "$cachefile" "$downloadFile".mp3
rm "$downloadFile" && \
rm "$downloadFile".tmp && \
rm "$downloadFile".mp3
fi
cp "$cachefile" "$outfile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment