Wondering what’s next for npm?Check out our public roadmap! »

    @svrooij/sonos-tts-polly

    1.0.5 • Public • Published

    Text to speech server (for sonos)

    Support me on Github github issues docker pulls npm

    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' })
      }
    )
    
    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 tsc
    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 .

    Deebug in VSCode

    Add the following .vscode/launch.json file, change your keys and your can debug the app very easily.

    {
      // Use IntelliSense to learn about possible attributes.
      // Hover to view descriptions of existing attributes.
      // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Launch Program",
          "preLaunchTask": "tsc: build - tsconfig.json",
          "skipFiles": [
            "<node_internals>/**"
          ],
          "program": "${workspaceFolder}\\src\\index.ts",
          "outFiles": [
            "${workspaceFolder}/dist/**/*.js"
          ],
          "args": ["--amazonKey", "YOUR_OWN_KEY", "--amazonSecret", "YOUR_OWN_SECRET"]
        }
      ]
    }

    Contributing

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

    Install

    npm i @svrooij/sonos-tts-polly

    DownloadsWeekly Downloads

    2

    Version

    1.0.5

    License

    MIT

    Unpacked Size

    38.9 kB

    Total Files

    19

    Last publish

    Collaborators

    • avatar