Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play an audio file with API #216

Closed
sadmin91 opened this issue Apr 15, 2021 · 12 comments
Closed

Play an audio file with API #216

sadmin91 opened this issue Apr 15, 2021 · 12 comments

Comments

@sadmin91
Copy link

Is there a way to play an audio file stored locally (on profile folder for example) by a call of Rhasspy API (HTTP or MQTT)?

Regards

@synesthesiam
Copy link
Contributor

Sure 🙂

curl -X POST -H 'Content-Type: audio/wav' --data-binary @- 'http://localhost:12101/api/play-wav' < /path/to/my.wav

The Content-Type is important, since otherwise Rhasspy will interpret that data as a file path or URL.

For MQTT, check out the playBytes message

@sadmin91
Copy link
Author

Thank you for this answer.
I have found this possibility on the doc.

My original question is to use a Rhasspy API with a path of a wav file (stored on Rhasspy profile) to play them.
Do you think that is an option to add to play-wav API?

@Romkabouter
Copy link
Contributor

Can you explain with a bit more detail of what you want to achieve? Playing wave file is already supported.

@sadmin91
Copy link
Author

Hello,
Yes, I know that is possible to play a wav file with a HTTP API call.
Actually, with this API, the wav content need to be send as call payload.
I would like to know if it would be possible to add a new API to play a wav file stored locally on rhasspy.
The parameter to this API could be the path of the wav file.

I hope this explanation is more clear.

@Romkabouter
Copy link
Contributor

If I understand correctly, you want an endpoint (like http://localhost:12101/api/play-sound) and you can post a filename to it (mysound.wav).
The file mysound.wav should be on the Rhasspy instance, in a path managed in the profile settings.

Correct?

@synesthesiam
Copy link
Contributor

synesthesiam commented Apr 30, 2021

@Romkabouter is correct. There are 3 modes for the play-wav API:

  1. If the Content-Type header is set to audio/wav, the data is interpreted as WAV audio (otherwise, it's decoded as a string)
  2. If the string starts with a / then it's interpreted as a file path
  3. Otherwise, the string is interpreted as a URL

@sadmin91
Copy link
Author

@Romkabouter, yes, this the idea.

@synesthesiam, theses modes is already available on the current version of the API?
(because I don’t find this information on the documentation).
For the second option, the path is absolute (based on root level of the filesystem) or relative (based on Rhasspy profile, for example)?

@synesthesiam
Copy link
Contributor

Yes, it's currently in the API (/api/play-wav), but you're correct that the documentation hasn't been updated yet. The code is here if you want to see the logic.

For the second option, the path is absolute (based on root level of the filesystem) or relative (based on Rhasspy profile, for example)?

It's assumed to be an absolute path right now. One useful change might be to use a regex to check if its a URL first, and if not interpret as a path relative to the profile dir (or absolute path if it starts with /).

@sadmin91
Copy link
Author

sadmin91 commented May 4, 2021

Thank you @synesthesiam for this answer.
As the documentation has been updated, it will be possible to close this issue.

@guttermonk
Copy link

Any idea what I'm doing wrong?

2023-05-03 02:45:05.875 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: curl -X POST -H 'Content-Type: audio/wav' --data-binary @- 'http://localhost:12101/api/play-wav' < /home/rhasspy/.config/rhasspy/profiles/en/wav/doorbell.wav, return code: 1

@C64ever
Copy link

C64ever commented May 7, 2023

Any idea what I'm doing wrong?

2023-05-03 02:45:05.875 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: curl -X POST -H 'Content-Type: audio/wav' --data-binary @- 'http://localhost:12101/api/play-wav' < /home/rhasspy/.config/rhasspy/profiles/en/wav/doorbell.wav, return code: 1

Here's the curl command I have set up as a shell command in my configuration file so I can call it from a script. Hope this helps guide you to a solution.

shell_command:
play_notification_sound: '/usr/bin/curl -H "Content-Type: audio/wav" --max-time 1 -X post --data-binary "@../../../../config/www/media/audio/chime.wav" http://192.168.1.8:12101/api/play-wav?siteId=master,satellite,satellitebedroom'

@guttermonk
Copy link

@C64ever thanks for replying. That was way more painful than it needed to be, but I just got it.

For anyone else that stumbles on this post running rhasspy in Docker with the recommended Docker installation:

$ docker run -d -p 12101:12101 \
      --name rhasspy \
      --restart unless-stopped \
      -v "$HOME/.config/rhasspy/profiles:/profiles" \
      -v "/etc/localtime:/etc/localtime:ro" \
      --device /dev/snd:/dev/snd \
      rhasspy/rhasspy \
      --user-profiles /profiles \
      --profile en
  1. copy your wav file to your profiles folder or a folder in your profiles folder: ~/.config/rhasspy/profiles/
  2. restart rhasspy
  3. add this to your shell_command:
    ring_doorbell: "curl -X POST 'http://192.168.1.88:12101/api/play-wav' -d '/profiles/doorbell.wav'"
    Credit to eairy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants