home-assistant / plugin-audio

Pulseaudio implementation for Home Assistant
Apache License 2.0
24 stars 13 forks source link

Pulseaudio fails to restore original sample settings #167

Closed NecrumBlack closed 2 months ago

NecrumBlack commented 3 months ago

Hello, i'm having problems with pulseaudio on the hassio-audio image, every time I try to use the voice assist (wyoming protocol+ assist microphone+openwakeword+piper+whisper addons) or just piper using the Text-to-speech (TTS): Speak service trough the VLC-TELNET entity, the audio logs get spammed with this message until the memory runs out.

PaltaHass hassio_audio[1543]: W: [alsa-sink-ff880000.i2s-ES8316 HiFi ES8316 HiFi-0] alsa-sink.c: Resume failed, couldn't restore original sample settings.

what could be causing this?

thanks in advance ha audio info.txt ha audio logs (before and after killing pulseaudio).txt ha info.txt pa-info (in hassio-audio)

NecrumBlack commented 2 months ago

looking at module-alsa-sink.c i've found:

 if (!pa_sample_spec_equal(&ss, &u->sink->sample_spec)) {
        pa_log_warn("Resume failed, couldn't restore original sample settings.");
        goto fail;
    }

And the PA wiki says "This tests if the sample format specifications are identical."

Should I change somethink in order to make the hassio_audio container work properly?

agners commented 2 months ago

Most likely the driver or your platform doesn't support the sample rate TTS is trying to use :cry:

Not sure what sample rate TTS is using. Have you tried playing sound directly with various sample rates on Debian on this platform (maybe you have to shutdown the Supervisor service and all containers to avoid conflicts).

Did you try Home Assistant OS on that same platform? The ODROID-C2 is not all that well tested, so I am not sure if audio works on it :cry:

NecrumBlack commented 2 months ago

Hi agners, thanks for your help. I'm running HA Supervised on an Orangepi 4 LTS wich appears as an Odroid-C2 (HASS OS is not officially supported ).

Here are some details I got from the card hardware playing in baremetal:

$ aplay -l

**** List of PLAYBACK Hardware Devices ****
card 0: rockchipes8316c [rockchip-es8316c], device 0: ff880000.i2s-ES8316 HiFi ES8316 HiFi-0 [ff880000.i2s-ES8316 HiFi ES8316 HiFi-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: hdmisound [hdmi-sound], device 0: ff8a0000.i2s-i2s-hifi i2s-hifi-0 [ff8a0000.i2s-i2s-hifi i2s-hifi-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

$ aplay -v -D hw:0,0 --dump-hw-params /usr/share/sounds/alsa/Front_Right.wav

Playing WAVE '/usr/share/sounds/alsa/Front_Right.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
HW Params of device "hw:0,0":
--------------------
ACCESS:  MMAP_INTERLEAVED RW_INTERLEAVED
FORMAT:  S16_LE S24_LE
SUBFORMAT:  STD
SAMPLE_BITS: [16 32]
FRAME_BITS: [32 64]
CHANNELS: 2
RATE: [8000 48000]
PERIOD_TIME: (666 8192000]
PERIOD_SIZE: [32 65536]
PERIOD_BYTES: [256 524288]
PERIODS: [2 4096]
BUFFER_TIME: (1333 16384000]
BUFFER_SIZE: [64 131072]
BUFFER_BYTES: [256 524288]
TICK_TIME: ALL
--------------------
aplay: set_params:1358: Channels count non available

$ aplay -v -D pulse --dump-hw-params /usr/share/sounds/alsa/Front_Right.wav

Playing WAVE '/usr/share/sounds/alsa/Front_Right.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
HW Params of device "pulse":
--------------------
ACCESS:  RW_INTERLEAVED
FORMAT:  U8 S16_LE S16_BE S24_LE S24_BE S32_LE S32_BE FLOAT_LE FLOAT_BE MU_LAW A_LAW S24_3LE S24_3BE
SUBFORMAT:  STD
SAMPLE_BITS: [8 32]
FRAME_BITS: [8 1024]
CHANNELS: [1 32]
RATE: [1 384000]
PERIOD_TIME: (2 4294967295)
PERIOD_SIZE: [1 1398102)
PERIOD_BYTES: [128 1398102)
PERIODS: [3 1024]
BUFFER_TIME: (7 4294967295]
BUFFER_SIZE: [3 4194304]
BUFFER_BYTES: [384 4194304]
TICK_TIME: ALL
--------------------
ALSA <-> PulseAudio PCM I/O Plugin
Its setup is:
  stream       : PLAYBACK
  access       : RW_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 1
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 24000
  period_size  : 6000
  period_time  : 125000
  tstamp_mode  : NONE
  tstamp_type  : GETTIMEOFDAY
  period_step  : 1
  avail_min    : 6000
  period_event : 0
  start_threshold  : 24000
  stop_threshold   : 24000
  silence_threshold: 0
  silence_size : 0
  boundary     : 6755399441055744000

here is the output inside the hassio-audio container: hassio-audio:/# aplay -v -D pulse --dump-hw-params /usr/share/sounds/alsa/Front_Right.wav

Playing WAVE '/usr/share/sounds/alsa/Front_Right.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
HW Params of device "pulse":
--------------------
ACCESS:  RW_INTERLEAVED
FORMAT:  U8 S16_LE S16_BE S24_LE S24_BE S32_LE S32_BE FLOAT_LE FLOAT_BE MU_LAW A_LAW S24_3LE S24_3BE
SUBFORMAT:  STD
SAMPLE_BITS: [8 32]
FRAME_BITS: [8 1024]
CHANNELS: [1 32]
RATE: [1 384000]
PERIOD_TIME: (2 4294967295)
PERIOD_SIZE: [1 1398102)
PERIOD_BYTES: [128 1398102)
PERIODS: [3 1024]
BUFFER_TIME: (7 4294967295]
BUFFER_SIZE: [3 4194304]
BUFFER_BYTES: [384 4194304]
TICK_TIME: ALL
--------------------
ALSA <-> PulseAudio PCM I/O Plugin
Its setup is:
  stream       : PLAYBACK
  access       : RW_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 1
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 24000
  period_size  : 6000
  period_time  : 125000
  tstamp_mode  : NONE
  tstamp_type  : GETTIMEOFDAY
  period_step  : 1
  avail_min    : 6000
  period_event : 0
  start_threshold  : 24000
  stop_threshold   : 24000
  silence_threshold: 0
  silence_size : 0
  boundary     : 6755399441055744000
^CAborted by signal Interrupt...
aplay: pcm_write:2146: write error: Interrupted system call

I've got to abort it because it start spawning the "couldn't restore original sample settings" message.

agners commented 2 months ago

Thanks for confirming. So yeah, this is a platform specific driver issue then :cry:

I'd report this to the Orangepi 4 folks.

NecrumBlack commented 2 months ago

I'm not so sure, mostly because the TTS part works correctly if Spotify connect is playing music inside HA, and audio also works fine in baremetal, it's just when pulseaudio is connected through the hassio audio sock that fails to resume the sink.

agners commented 2 months ago

I guess it works correctly when Spotify is playing because then Pulseaudio mixes the two audio streams, and resamples TTS to whatever Spotify is using. To me it seems that the driver is advertising support for a sample rate (and Pulseaudio is trying to use it when only TTS is running) which it can't handle when trying to actually play it.

NecrumBlack commented 2 months ago

You were right all along (go figure!) after further testing with speaker-test, audio plays correctly at 44100Hz but breaks at 48000Hz. Is there a way to configure pulseaudio inside the container/image to use a fixed sample rate? (non volatile config files?)

thanks for your patience

agners commented 2 months ago

No, currently we don't have such a "force baudrate option". In fact, i am not even sure if Pulseaudio has that. There is default-sample-rate, but I'd assume that Pulseaudio would also pick a higher sample rate if needed.

The problem here is really the audio driver advertises it is capable of 48000Hz (see RATE: [8000 48000] when using aplay on the HW directly). And Pulseaudio (correctly) makes use of it. As a simple fix, you probably can just delete that from the audio driver, but it really would need a kernel change.

NecrumBlack commented 2 months ago

In the end it was the Assist Microphone add-on who was causing all the playback issues, once deactivated, every sample rate works perfectly. Now i'm trying to know what's causing such problem, I'll post again if I find something.

agners commented 2 months ago

Maybe Assist Microphone plug-in uses an audio input which influences clocks of your audio output such that it can't run on those two different rates? :thinking: What are your ALSA audio inputs (arecord -l)?

NecrumBlack commented 2 months ago

arecord -l

**** List of CAPTURE Hardware Devices ****
card 0: rockchipes8316c [rockchip-es8316c], device 0: ff880000.i2s-ES8316 HiFi ES8316 HiFi-0 [ff880000.i2s-ES8316 HiFi ES8316 HiFi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0

arecord -L


null
Discard all samples (playback) or generate zero samples (capture)
hw:CARD=rockchipes8316c,DEV=0
rockchip-es8316c, ff880000.i2s-ES8316 HiFi ES8316 HiFi-0
Direct hardware device without any conversions
plughw:CARD=rockchipes8316c,DEV=0
rockchip-es8316c, ff880000.i2s-ES8316 HiFi ES8316 HiFi-0
Hardware device with all software conversions
default:CARD=rockchipes8316c
rockchip-es8316c, ff880000.i2s-ES8316 HiFi ES8316 HiFi-0
Default Audio Device
sysdefault:CARD=rockchipes8316c
rockchip-es8316c, ff880000.i2s-ES8316 HiFi ES8316 HiFi-0
Default Audio Device
dsnoop:CARD=rockchipes8316c,DEV=0
rockchip-es8316c, ff880000.i2s-ES8316 HiFi ES8316 HiFi-0
Direct sample snooping device```