Switch between Voice HAT and bcm2835


7 posts
by busyman96 » Sat Jun 10, 2017 7:38 pm
Hi guys,

Has anyone managed to get audio device switching to work?
It seems that bcm2835 has been disabled completely.

Going to /boot/config.txt and uncommenting this line at least makes the device show up in the Audio Device Settings UI:
Code: Select all
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
dtoverlay=i2s-mmap
dtoverlay=googlevoicehat-soundcard


Now running aplay -l shows:
Code: Select all
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 8/8
  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
  Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: sndrpigooglevoi [snd_rpi_googlevoicehat_soundcar], device 0: Google voiceHAT SoundCard HiFi voicehat-hifi-0 []
  Subdevices: 1/1
  Subdevice #0: subdevice #0


Then I added bcm2835 (card 0, device 0) in /etc/asound.conf as mentioned here:
Code: Select all
options snd_bcm2835 index=1  #Not sure if necessary

pcm.speaker {
  type plug
  slave {
    pcm "hw:0,0"
  }
}


(No extra mic in arecord -l, aside from the one from the Voice HAT)

Now some testing:

1. Switching to Analog audio from the UI and running aplay with default works perfectly through my headphones:
Code: Select all
aplay -q -t raw -D default -c 1 -f S32_LE -r 48000 < /usr/share/sounds/alsa/Front_Center.wav

The same as specifying the device itself:
Code: Select all
aplay -q -t raw -D plughw:CARD=ALSA,DEV=0 -c 1 -f S32_LE -r 48000 < /usr/share/sounds/alsa/Front_Center.wav


2. Switching to snd_rpi_googlevoicehat_soundcard and running aplay with default does not work at all:
Code: Select all
aplay -q -t raw -D default -c 1 -f S32_LE -r 48000 < /usr/share/sounds/alsa/Front_Center.wav
Warning: format is changed to S16_LE
aplay: set_params:1233: Sample format non available
Available formats:
- S32_LE

However running with the device work perfectly on the AIY speaker:
Code: Select all
aplay -q -t raw -D plughw:CARD=sndrpigooglevoi,DEV=0 -c 1 -f S32_LE -r 48000 < /usr/share/sounds/alsa/Front_Center.wav


3. Switching to Analog and running src/main.py does not work, it seems like it looking for a mic that doesn't exist:
Code: Select all
OpenAlsaHandle PcmOpen: No such file or directory
[7118:7173:ERROR:audio_input_processor.cc(756)] Input error
[2017-06-10 22:33:55,699] INFO:root:ON_MUTED_CHANGED:
  {'is_muted': False}
[2017-06-10 22:33:55,700] INFO:root:ON_START_FINISHED
[2017-06-10 22:33:55,701] INFO:main:ready...
Say "OK, Google" then speak, or press Ctrl+C to quit...
[2017-06-10 22:33:55,704] INFO:root:ON_ASSISTANT_ERROR:
  {'is_fatal': True}


4. Switching to snd_rpi_googlevoicehat_soundcard and running src/main.py does not work anymore:
Code: Select all
ALSA lib pcm.c:7935:(snd_pcm_set_params) Rate doesn't match (requested 16000Hz, get 48000Hz)
OpenAlsaHandle PcmSetParams: Invalid argument
CloseAlsaHandle PcmDrop: Input/output error
[5624:5663:ERROR:audio_input_processor.cc(756)] Input error
[2017-06-10 22:28:32,724] INFO:root:ON_MUTED_CHANGED:
  {'is_muted': False}
[2017-06-10 22:28:32,725] INFO:root:ON_START_FINISHED
[2017-06-10 22:28:32,726] INFO:main:ready...
Say "OK, Google" then speak, or press Ctrl+C to quit...
[2017-06-10 22:28:32,728] INFO:root:ON_ASSISTANT_ERROR:
  {'is_fatal': True}


Any idea on what I'm missing? I'm not very keen on hardcoding the device.

Thanks,
Gabriel
Posts: 11
Joined: Thu Jun 01, 2017 2:03 pm
by cyrillou » Sun Jun 11, 2017 7:03 pm
Hi busyman96!

You can configure the audio device so that the sndrpigooglevoi is card 0

Code: Select all
options snd_rpigooglevoi index=0
options snd_bcm2835 index=-2
Posts: 14
Joined: Sat May 20, 2017 3:28 pm
by busyman96 » Tue Jun 13, 2017 6:39 pm
That made no difference, cyrillou. It still does not correctly associate the voice HAT with default when I switch.

Edit: It seems that running aplay with default does not perform the software conversion that plughw does.

Edit 2: Looking more into this I noticed that pimixer (the UI that controls the volume) generates/modifies .asourndrc when you switch the device which I think overrides /etc/asound.conf:
Code: Select all
pcm.!default {
   type hw
   card 0
}

ctl.!default {
   type hw
   card 0
}

Code: Select all
pcm.!default {
   type hw
   card 1
}

ctl.!default {
   type hw
   card 1
}


This is bad because default ends up accessing hw directly, making softvol useless. That might also explain why it's looking for an existent mic. Wonder if it knows to contextually modify 0->1 and viceversa and only in default. Will try tomorrow.
Posts: 11
Joined: Thu Jun 01, 2017 2:03 pm
by craigm26 » Wed Jun 14, 2017 3:48 pm
let us know how this experiment turns out.

I'm trying to make a usb-micro B microphone the default audio device and it's a maze of trying to figure out what is needed to make that happen. (my project does not need speakers).
Posts: 2
Joined: Wed Jun 14, 2017 3:38 pm
by busyman96 » Wed Jun 14, 2017 9:59 pm
I've given up on trying to configure ALSA manually and installed pulseaudio and pavucontrol to handle the switching. It works to an extent, however they seem to only work when running with sudo, with pavucontrol only listing a dummy device otherwise, and pulseaudio erroring with connection refused.
Posts: 11
Joined: Thu Jun 01, 2017 2:03 pm
by craigm26 » Wed Jun 14, 2017 10:18 pm
I'll keep messing around with PulseAudio. See what happens.
Posts: 2
Joined: Wed Jun 14, 2017 3:38 pm
by busyman96 » Mon Jun 19, 2017 8:29 pm
Just an update - managed to get rid of the sudo issue, but not sure what did it:
- Added pi user to audio, pulse and pulse-access groups
- Disabled volume applet (I'm using pavucontrol instead)
- Killed all the processes listed with fuser -v /dev/snd/*
- Ran pulseaudio --start

Also made pulse the default card in /etc/asound.conf so that I don't have to specify "-D pulse" for every alsa application.

The AIY app works fine without any code modifications, and even the check_audio script works but I had to comment out a return if VoiceHAT is not the first card.

The only issue I'm left facing is that volume control does not seem to work and it's very loud. Could be because I removed the soft volume customizations from /etc/asound.conf. It's funny because "amixer get Master" returns the correct volume level displayed in pavucontrol.

Source:
https://wiki.archlinux.org/index.php/Pu ... g#No_cards
https://www.freedesktop.org/wiki/Softwa ... fectSetup/
Posts: 11
Joined: Thu Jun 01, 2017 2:03 pm