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.
- GET
http://your_ip:5601/api/voices
-> Show all voices supported by Amazon at this moment. - GET
http://your_ip:5601/api/:lang/:text
(if enabled in config) -> Download TTS file, and return location. (The files are cached indefinitely) - POST
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.
- Generate credentials in the Amazon management console.
- Create
.env
file, use .env-sample - Fire up the docker container
docker run --env-file .env -p 5601:5601 svrooij/sonos-tts-polly
- (optional) Map the cache folder to a folder to keep then after a restart.
- (optional) Setup a reverse proxy in front of the node container.
- Generate credentials in the Amazon management console.
- Install package
npm i -g @svrooij/sonos-tts-polly
- Run the app
sonos-tts-polly --port 5601 --amazonKey your_amazon_key --amazonSecret your_amazon_secret
- Library is written in TypeScript
- Build library with
npm run build
- Lint before commit
npm run lint
ornpm run lint-fix
(no errors allowed) - Build docker container
docker build .
ordocker 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!