Sending audio to the speakers on a Kodi system
I have a Kodi system (LibreElec) with an always-on amplifier and speakers connected and have long wanted to send out text via TTS over those speakers, regardless of the current "playing" state of Kodi -- basically, mix it in with whatever is playing.
What didn't work
Home Assistant: again, it never fails to fail me. Of all the addons and integrations that I've tried, most were able to send text to the speakers, but all did stop the currently playing media and failed to restart it after.
Pulseaudio: there are some sources that say you can basically open up PulseAudio on Kodi to the network and then route audio to it from pulseaudio on another machine. I never got that to work.
What works: snapcast
You need a snapserver for that, which coordinates everything (and can
do a lot more than what I'm using it for), on some server. In
snapserver.conf, enable the raw TCP socket:
source = pipe:///tmp/snapfifo?name=pipe
source = tcp://0.0.0.0?name=default
The name "default" needs to be assigned to it.
Then you install the "snapclient" plugin on Kodi, configure the server address and select the audio device ID.
Now you can stream raw (i.e. headerless) S16LE stereo 48k audio samples to port 4953 on your snapserver, and Kodi will play it back. No stopping of currently playing media or anything.
Gratitious ffmpeg command lines
Boy, are the docs confusing if you want to concatenate multiple audio files of different formats and send them to stdout (to be piped into netcat).
What finally worked:
ffmpeg -loglevel quiet -i file1.mp3 -i file2.wav -filter_complex "[0:0] [1:0] concat=n=2:v=0:a=1" -acodec pcm_s16le -f s16le -
Why two files? Because of course the TTS-generated text needs to be prefixed with a nice gong sound.