snipsco / snips-issues

Feel free to share your bugs with us.
14 stars 5 forks source link

[audio-server] Audio issues using alsa playback backend (drain error + sound is cut) #203

Open cburghardt opened 4 years ago

cburghardt commented 4 years ago

What's the issue

I just upgraded my system (Raspi 4 with Stretch packages) to 0.64. Unfortunately the error ALSA function 'snd_pcm_drain' failed with error 'EIO: I/O error' is back. None of the known workarounds fix it. Once I downgraded to 0.63.3 the problem disappears.

Expected Same behavior as 1.2.2

Version

1.3.0 (0.64.0)

Environment


[workaround edit by @cpoisson ]

Workaround 1 - Back to 0.63.3

Downgrade snips-tts and snips-audio-server to 1.2.2 (0.63.3)

sudo apt install snips-audio-server=0.63.3
sudo apt install snips-tts=0.63.3

Workaround 2 - Back to portaudio

Switch back to portaudio for playback.

Workaround 3 - bcm2835

If you are using the pi 3 or pi 4 bcm2835 embedded sound card.

pcm.!default {
    type asym
    playback.pcm {
        type plug
        slave.pcm "hw:0,0"
        slave.rate 48000  # Does not work if this is removed.
    }
    capture.pcm {
        type plug
        slave.pcm "hw:1,0"   # Configure the correct hw:Id,SubId for your capture device
    }
}

Workaround 4 : Delegate to dmix and dsnoop

Delegate to dmix to make sure that the sound is played well using alsa

pcm.!default {
    type asym
    playback.pcm "playback"
    capture.pcm "capture"
}

pcm.playback {
    type plug
    slave.pcm "dmixed"
}

pcm.capture {
    type plug
    slave.pcm "array"
}

pcm.dmixed {
    type dmix
    ipc_key 555555
    ipc_key_add_uid yes
    ipc_perm 0666
    slave.pcm "hw:1,0"  # Set the correct hw:Id,SubId for your playback device
}

pcm.array {
    type dsnoop
    ipc_key 666666
    ipc_key_add_uid yes
    ipc_perm 0666
    slave.pcm "hw:1,0"  # Set the correct hw:Id,SubId for your capture device
}

This configuration has been tested on the following devices

Audio device Playback Capture Details
bcm2835 (raspberry pi) Does not work. playback is stuck waiting for /dev/snd/timer (known issue)
Jabra PHS002W
UGREEN USB Sound card
PS Eye Camera
Respeaker PiHAT 2 mics
Respeaker 6 Microphones Playback does not work. The default configuration installed by the drivers works correctly tough

Related Issue

cpoisson commented 4 years ago

Thanks for your ticket @cburghardt,

Quick question, Which sound card are you using as your main sound playback interface?

Can you post as well your asound.conf configuration.

Many thanks for your feedbacks

cburghardt commented 4 years ago

I use the onboard sound card of the raspberry.

pcm.jack {                  
        type hw             
        card 0              
        device 0            
        rate 16000          
} 

pcm.!default {
  type asym
   playback.pcm {
     type plug
     slave.pcm "jack"
   }
   capture.pcm {
     type plug
     slave.pcm "hw:1,0"
   }
}
cpoisson commented 4 years ago

Ok,

So the issue is indeed related to #201

Using alsa, the ring buffer parameters setup done by the audio server is incorrect, and seems to induce multiple issues:

This issue seems to appears with different behavior depending on your sound card.

Pi 3 Embedded sound card

pi@snips-pi-qa-assistant:~ $ cat /proc/asound/card0/pcm0p/sub0/hw_params
access: MMAP_INTERLEAVED
format: S16_LE
subformat: STD
channels: 1
rate: 48000 (48000/1)
period_size: 512
buffer_size: 65536

Jabra

pi@snips-pi-qa-assistant:~ $ cat /proc/asound/card1/pcm0p/sub0/hw_params
access: MMAP_INTERLEAVED
format: S16_LE
subformat: STD
channels: 2
rate: 48000 (48000/1)
period_size: 256
buffer_size: 262144

Typical parameters induced by aplay

What seems to work Using the following configuration and portaudio backend. Things seems to work again.

pcm.!default {
    type asym
    playback.pcm {
        type plug
        slave.pcm "hw:0,0" # the pi3 sound card
        slave.rate 48000 # to avoid port audio sampling issues

    }
    capture.pcm {
        type plug
        slave.pcm "hw:2,0" # A usb microphone plugged on the my raspberry pi.
    }
}

Which give me the following hw parameters when playing a sound sample with the audio-server.

pi@snips-pi-qa-assistant:~ $ cat /proc/asound/card0/pcm0p/sub0/hw_params
access: MMAP_INTERLEAVED
format: S16_LE
subformat: STD
channels: 2
rate: 48000 (48000/1)
period_size: 836
buffer_size: 4184

The buffer size is back to a reasonable value. The sound issues disappears.

(I found some documentation for the one interested)

cpoisson commented 4 years ago

@marcteyssier, @cburghardt

I gave a try to your configuration (coming from the forum I suppose), using portaudio.

snips-audio-server -v --portaudio_playback=default

Seems to work correclty I don't see any errors.

Can you patch your /etc/snips.toml with portaudio_playback = "default" under [snips-audio-server] section, to give a shot to this workaround (1.3.0 only) ?

cburghardt commented 4 years ago

So I upgraded to 0.64 and applied the changes. Result: caused by: an error occured in the audio pipeline: ErrorMessage { msg: "Couldn\'t find a PortAudio device with the name default" }

cpoisson commented 4 years ago

Ok, that's another one to figure out.

I also tried to delegate things to dmix. Works great with the piHAT 2 (provided the user using the sound is in the audio group)

But as soon as I switch on the raspberry pi even aplay deadlock. After an inspection with strace I think I stumbled on this known issue

marcteyssier commented 4 years ago

Hi, this work for me with my respeaker mic 4 array. I heard all the sentence.

Thank you for your help

cpoisson commented 4 years ago

Regarding the bcm2835 (raspi sound card) it is really impossible to use it with dmix on my side. The thing just stay stuck.

Otherwise the following asound.conf configuration works perfectly on the hw available on my desk using the default alsa playback.

/etc/asound.conf


pcm.!default {
    type asym
    playback.pcm "playback"
    capture.pcm "capture"
}

pcm.playback {
    type plug
    slave.pcm "dmixed"
}

pcm.capture {
    type plug
    slave.pcm "array"
}

pcm.dmixed {
    type dmix
    ipc_key 555555
    ipc_key_add_uid yes
    ipc_perm 0666
    slave.pcm "hw:5,0"
}

pcm.array {
    type dsnoop
    ipc_key 666666
    ipc_key_add_uid yes
    ipc_perm 0666
    slave.pcm "hw:5,0"
}

Hardware tested

Audio device Playback Capture Details
bcm2835 (raspberry pi) playback is stuck waiting for /dev/snd/timer (known issue)
Jabra PHS002W
UGREEN USB Sound card
PS Eye Camera
Respeaker PiHAT 2 mics
cburghardt commented 4 years ago

So how do I convince the audio server to use portaudio if that seems to work? My main unit just uses the onboard raspi sound card for playback. Recording is done via satellites.

cpoisson commented 4 years ago

The test above was done using alsa for playback and capture.

I had the following configuration working on my side using portaudio on the bcm2835, it should match your needs

pcm.!default {
    type asym
    playback.pcm {
        type plug
        slave.pcm "hw:0,0"
        slave.rate 48000
    }
}

Test it along

sudo -u _snips snips-audio-server -v --disable-capture --portaudio_playback="default"

Fun fact, it stops working if you remove slave.rate 48000, with the same error as yours.

[18:27:16.007495] ERROR:snips_audio_server_hermes::player: An error happened while trying to play some audio: could not play "c38d0537-611b-4611-a81a-9862f2701d81"
 -> caused by: an error occured in the audio pipeline: ErrorMessage { msg: "Couldn\'t find a PortAudio device with the name default" }
 -> caused by: Couldn't find a PortAudio device with the name default
cburghardt commented 4 years ago

Result:

[08:17:03.272191] DEBUG:snips_audio_server_hermes::player: Playing bytes EWQUWP1YISAA0LP8
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 923
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 923
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_hw.c:1822:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1822:(_snd_pcm_hw_open) Invalid value for card
[08:17:03.321778] ERROR:snips_audio_server_hermes::player: An error happened while trying to play some audio: could not play "EWQUWP1YISAA0LP8"
 -> caused by: an error occured in the audio pipeline: ErrorMessage { msg: "Couldn\'t find a PortAudio device with the name default" }
 -> caused by: Couldn't find a PortAudio device with the name default
cpoisson commented 4 years ago

🤔 ok, now I'm running short of ideas.

Can you show me the content of aplay -l on your device?

This configuration works on 3 out of 3 Pi 3 boards using version 0.64.0

Here are the logs I obtain.

:~ $ snips-audio-server -v --disable-capture --portaudio_playback=default
[14:39:28.521619] DEBUG:snips_common_cli::cli: conf lookup: snips-audio-server.no_exit_on_all_panics -> Ok(None)
[14:39:28.521984] DEBUG:snips_common_cli::cli: conf lookup: snips-common.no_exit_on_all_panics -> Ok(None)
...
[14:39:36.319702] DEBUG:snips_audio_server_hermes::player: Playing bytes dummysessionid
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 923
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 923
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_asym.c:106:(_snd_pcm_asym_open) capture slave is not defined
[14:39:36.365710] INFO :snips_audio_portaudio            : Playing "dummysessionid" using output "default", wav spec: WavSpec { channels: 2, sample_rate: 22050, bits_per_sample: 16, sample_format: Int }
[14:39:36.372966] DEBUG:snips_audio_portaudio            : Padding output with 23 frames
[14:39:36.952786] INFO :snips_audio_portaudio            : Playing of "dummysessionid" finished
[14:39:36.954156] DEBUG:snips_audio_server_hermes::player: Finished Playing bytes dummysessionid

You should not have those following lines in your log

ALSA lib pcm_hw.c:1822:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1822:(_snd_pcm_hw_open) Invalid value for card

Looks like a configuration issue to me but I prefer to confirm that with your device information (aplay -l)

cburghardt commented 4 years ago

It's a pi 4 maybe that's the difference.

**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 7/7
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 IEC958/HDMI [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: ALSA [bcm2835 ALSA], device 2: bcm2835 IEC958/HDMI1 [bcm2835 IEC958/HDMI1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
Alelelex commented 4 years ago

Hi, I have the same problem here. I tried a lot of solution without success. My error is a bit different is "sam watch" : [Audio Server] reported an error on site default : An error happened while trying to play some audio

It doesn't work better by downgrading snips-tts and snips-audio-server to version 0.63.3.

On hardware side I tried with a Jabra PHS001U and a Respeaker 6mics with the same result for both.

Let me know if you need more informations.

cpoisson commented 4 years ago

@Alelelex It could be a very different issue. Let's try not to clutter this issue and deal that on the forum

cpoisson commented 4 years ago

@cburghardt

I could grab a Pi 4 on debian buster.

pi@snips-pi-qa-test:~ $ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 10 (buster)
Release:    10
Codename:   buster

I do confirm that the following configuration is working with snips-audio-server 0.64.0 using portaudio backend for playback

pcm.!default {
    type asym
    playback.pcm {
        type plug
        slave.pcm "hw:0,0"
        slave.rate 48000
    }
}

(same test command)

 sudo -u _snips snips-audio-server -v --disable-capture --portaudio_playback=default
SvenSchiffner commented 4 years ago

I can confirm that the workaround works for me. But with portaudio I have to remove capture.pcm from asound.conf. Otherwise I also get the PortAudio default device error.

cburghardt commented 4 years ago

Thanks a lot @sven5s - that was the key. With that modification it works.

cpoisson commented 4 years ago

@cburghardt

Nice to heart that!

I'll keep it open to follow up this issue until we release a definitive fix.

cpoisson commented 4 years ago

For the record,

Trying to fix this issue we still stumbled on AudioError(OtherError(Error("snd_pcm_drain", Sys(EIO)) could not drain pcm)) using the bcm2835 embedded sound card.

It seems that there is a kernel known issue about that => raspberrypi/linux#2983

The current version is 4.19.66, rolling back to kernel version 4.19.36 or upgrading to 4.19.79 fix the issue on my side.

sudo rpi-update c50c65e52bad69e8fa7d6576d9e2399eade7faf6 => 4.19.36 sudo rpi-update 985bc5353e4f5fe5a11c8b6c4c646dc7165bbc21 => 4.19.79 Please read rpi-update documentation before using it.

I guess this particular issue will be solved in a next official kernel update.

thorstenMueller commented 4 years ago

I can confirm that the downgrade on 4.19.36 works for my setup (all snips components running 0.64.0, raspberry 3b running stetch). As user of matrix voice i was unable to build kernel modules because of missing kernel headers for .36 kernel version. After using "rpi-source" building matrix voice modules works again.

Andergraw commented 4 years ago

I was having issues with cutted sounds and unstable audio server and upgrading kernel to 4.19.79 solved them all. Thanks!