This is a small webserver that downloads text-to-speech files from Amazon Polly for the requested text and language. It is build as an optional extension of node-sonos-ts to support text-to-speech on your sonos system.
A hosted version of this text-to-speech server is available for my sponsors, send me a message about it.
http://your_ip:5601/api/voices
-> Show all voices supported by Amazon at this moment.http://your_ip:5601/api/:lang/:text
(if enabled in config) -> Download TTS file, and return location. (The files are cached indefinitely)http://your_ip:5601/api/generate
-> will do the same as the get request but no hard url text encoding.import fetch from 'node-fetch'
import { Request } from 'node-fetch'
const request = new Request(
'http://your_ip:5601/api/generate',
{
method: 'POST',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({ text: 'Hello world', lang: 'en-US', gender: 'male', engine: 'neural' })
}
)
fetch(request)
.then(response => {
if (response.ok) {
return response.text()
} else {
throw new Error(`Http status ${response.status} (${response.statusText})`)
}
})
.then(JSON.parse)
.then(resp => {
console.log(resp)
})
This will return the following, depending on if you have set the cacheUri
the cdnUri
will be shown.
{
"cdnUri": "https://cacheUri/en-US/4b6eddb411d4cec3933528bfca05341828ca7593.mp3",
"uri": "http://your_ip:5601/cache/en-US/4b6eddb411d4cec3933528bfca05341828ca7593.mp3"
}
I would recommend hosting this server on a local server, not connected to the internet. Amazon charges money for their Polly service, so it's not nice if someone else is using your Amazon credits to use TTS on their own systems.
If you're hosting this on a public server, be sure to put a reverse proxy in front of it, like nginx. And do some rate-limitting or ip whitelisting.
.env
file, use .env-sampledocker run --env-file .env -p 5601:5601 svrooij/sonos-tts-polly
npm i -g @svrooij/sonos-tts-polly
sonos-tts-polly --port 5601 --amazonKey your_amazon_key --amazonSecret your_amazon_secret
npm run build
npm run lint
or npm run lint-fix
(no errors allowed)docker build .
or docker build -t svrooij/sonos-tts-polly .
Copy .env-sample
to .env
and input your Amazon credentials. Then you can debug this app by pressing F5
.
Be nice to each other. This server is build in my spare time!