Shell script to use Google Wavenet as Snips TTS

November 5, 2018 · View on GitHub

#!/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""lang"-"country" googleVoice="languageCode""languageCode"-"voice" text=${text//'/\'}

md5string="text""text""googleVoice""sampleRate"hash="sampleRate" hash="(echo -n "md5string"md5sumseds/.md5string" | md5sum | sed 's/ .*//')"

cachefile="cache""cache""hash".wav downloadFile="/tmp/""$hash"

if [[ ! -f "cachefile"]];thencurlH"Authorization:Bearer"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: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""cachefile" "outfile"