Implementation of /api/stop-wav

Hello everybody,

I hope everyone is well and I wish you and your friends and family health and well-being!

Now to my question: Is it possible to implement a corresponding /api/stop-wav in addition to the /api/play-wav to stop the currently playing wav file?

I use Rhasspy to alert me to incoming calls. So far I had used mpd. This does not work after moving to another Pi. I think mpd can’t address the speaker anymore because it seems to be in use.

If there is a solution to stop playing the wav on Rhasspy it would be amazing because the ring tone of the caller should stop as soon as you pick up or the caller hangs up. Otherwise the complete file will be played.

I have done it this way so far:

- alias: rhasspy_notify_caller_ringtone_play
  initial_state: true
  trigger:
    - platform: template
      value_template: '{{states.sensor.fritzbox_callmonitor.state == "ringing"}}'
      for:
        seconds: 3
  condition:
    condition: and
    conditions:
    
      # Nur im erlaubten Zeitraum
      - condition: template
        value_template: '{{states.binary_sensor.parents_awake_time.state|replace("on","True")|replace("off","False")}}'

      # Nur Anrufer mit bekannter Nummer
      - condition: template
        value_template: '{{ states.sensor.fritzbox_callmonitor.attributes.from != "" }}'
      - condition: template
        value_template: '{{ states.sensor.fritzbox_callmonitor.attributes.from_name != "" }}'
      - condition: template
        value_template: '{{ states.sensor.fritzbox_callmonitor.attributes.from_name != empty }}'
      - condition: template
        value_template: '{{ states.sensor.fritzbox_callmonitor.attributes.from_name != "unknown" }}'

      # Nur wenn einer von beiden zu Hause ist
      - condition: or
        conditions:
          - condition: template
            value_template: "{{ states.device_tracker.SMARTPHONE1.state == 'home' }}"
          - condition: template
            value_template: "{{ states.device_tracker.SMARTPHONE2.state == 'home' }}"

  action:
    - service_template: >
          {%- if states.sensor.fritzbox_callmonitor.attributes.from_name == "CALLER_NO_RINGTONE" -%}
            shell_command.error_trap
          {%- elif states.sensor.fritzbox_callmonitor.attributes.from_name == "WIFE" -%}
            shell_command.call_legend_of_zelda
          {%- else -%}
            shell_command.call_teddy_caique
          {% endif %}

shell command:

call_teddy_caique: "curl -X POST 'http://IP:PORT/api/play-wav' -H 'Content-Type: audio/wav' --data-binary @'/config/wav/call_teddy_caique.wav'"
call_legend_of_zelda: "curl -X POST 'http://IP:PORT/api/play-wav' -H 'Content-Type: audio/wav' --data-binary @'/config/wav/call_legend_of_zelda.wav'"

Wav files must be located in …HomeAssistant/wav

To stop the ringtone I used the following automation (as I said before, I lack the possibility for this now)

- alias: rhasspy_notify_caller_ringtone_stop
  initial_state: true
  trigger:
    - platform: template
      value_template: '{{states.sensor.fritzbox_callmonitor.state != "ringing"}}'

  action:
    - service: shell_command.Please_make_it_possible_to_stop_playing_the_wav

Just use pulseaudio - I had a lot of trouble with rhasspy on the pi in venv without pulseadio (still having regular segfaults when restarting).

I followed this reference: https://raven4.cz/wp/pulseaudio-in-system-wide-mode/

However, I work with another person using rhasspy and he menaged to just start pulseaudio as user as daemon right beofer launching rhasspy - so installing pulseaudio and wrapping this into a script might be all that’s needed.

And yes, I also kind of run mpd through snapcast (https://github.com/badaix/snapcast) on my pi (if you use the second solution, make sure to run mpd as the same user as rhasspy and pulseaudio).