svrooij / node-sonos-ts

:speaker: Sonos control library, use this library in your own appliction.
https://sonos-ts.svrooij.io/
MIT License
81 stars 18 forks source link

Integrated TTS #161

Closed amcdnl closed 2 years ago

amcdnl commented 2 years ago

Awesome package, thanks for building this.

It would be awesome if the TTS was integrated natively. There are a variety of solutions out there that might make this possible like: https://www.npmjs.com/package/say

Wanted to see if you had explored this before ... if nothing else it would make it easier for others to get started.

svrooij commented 2 years ago

This library has some integration for text-to-speech….

It requires a lot of boilerplate I would rather not include in this library, which is why I created this package. Think:

im open to supporting other sources for the text-to-speech files, the difference between the way I choose and other solutions I’ve seen is that this first calls the text to speech server to generate the speech mp3 and then just queues that. Others are encoding the text in the url (which gives issues).

I also provide a dedicated tts url to all my sponsors, which makes enabling tts a matter of setting a single environment variable, as you can also see in sonos2mqtt.

amcdnl commented 2 years ago

@svrooij - Yup - I saw that package. My only fear about a remote TTS would be time.

I'm using this to notify me when motion is outside my home and the faster the better. My google nest cams -> homebridge -> homekit -> sonos.

svrooij commented 2 years ago

What this package does in essence is:

  1. Hash the text an language
  2. Check if local file exists (cached :wink:)
  3. Query the remote TTS service to generate audio for some text
  4. Save the generated audio as an mp3 on the local system (with the hash as filename)
  5. Run the notification command

If speed is the main concern, you could of course pre-generate the mp3 files and host them on a webserver locally. And instead of calling the tts command, you would call the notification command with your local hosted files.

You could even consider sponsoring me, and use my cloud hosted version to pre-generate your mp3 files that you then save locally.

No to mention the fact that you could also run the remote package in docker on a raspberry pi. That would give you local cached messages and auto addes not cached messages to the cache.

You're probably doing something like: Movement detected at {name-of-cam}, which would only result in x number of messages depending on the amount of camera's.

I would also accept a PR on this package supporting say instead of a remote service (amazon in this case).

svrooij commented 2 years ago

How about build support for home assistant text to speech since everybody is already using that anyway.

amcdnl commented 2 years ago

Thanks for the link!

I actually use Homebridge ( though I'm probably going to end up moving to HomeAssistant for more advanced things ) and I actually wrote a quick plugin using your library - https://github.com/amcdnl/homebridge-sonos-announcer to do what I wanted.

While we are on the topic, I was trying to resolve a file locally but I got mime type errors. I was wondering if you had a good example of how you would do that.

manager.PlayNotification({
  trackUri: path.resolve('./hello.mp3')
});

Reference: https://github.com/amcdnl/homebridge-sonos-announcer/blob/master/src/index.ts#L72

svrooij commented 2 years ago

Your Sonos speaker cannot access files from the machine running the script. You have to run a small webserver that will serve the mp3.

Like if you would host an html file. This is exactly what I meant by all the things you need to include, this is also the reason for me to build a separate library just for tts

amcdnl commented 2 years ago

Your Sonos speaker cannot access files from the machine running the script. You have to run a small webserver that will serve the mp3.

Ah, I see how it works now. Ok all that makes sense. Thanks for explaining that.