@svrooij/sonos-tts-polly

1.0.9 • Public • Published

Text to speech server (for sonos)

Support me on Github github issues docker pulls npm License

Sonos typescript Sonos2mqtt Sonos api documentation Sonos cli

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.

Sponsors get access to hosted version

A hosted version of this text-to-speech server is available for my sponsors, send me a message about it.

API

  • 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"
}

Hosting this server

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.

Run your own server

  1. Generate credentials in the Amazon management console.
  2. Create .env file, use .env-sample
  3. Fire up the docker container docker run --env-file .env -p 5601:5601 svrooij/sonos-tts-polly
  4. (optional) Map the cache folder to a folder to keep then after a restart.
  5. (optional) Setup a reverse proxy in front of the node container.

Run your own server (in node)

  1. Generate credentials in the Amazon management console.
  2. Install package npm i -g @svrooij/sonos-tts-polly
  3. Run the app sonos-tts-polly --port 5601 --amazonKey your_amazon_key --amazonSecret your_amazon_secret

Developer notes

  1. Library is written in TypeScript
  2. Build library with npm run build
  3. Lint before commit npm run lint or npm run lint-fix (no errors allowed)
  4. Build docker container docker build . or docker build -t svrooij/sonos-tts-polly .

Debug in VSCode

Copy .env-sample to .env and input your Amazon credentials. Then you can debug this app by pressing F5.

Contributing

Be nice to each other. This server is build in my spare time!

Versions

Current Tags

Version History

Package Sidebar

Install

npm i @svrooij/sonos-tts-polly

Weekly Downloads

1

Version

1.0.9

License

MIT

Unpacked Size

40.5 kB

Total Files

19

Last publish

Collaborators

  • svrooij