Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from ALSA to PulseAudio #153

Open
wants to merge 9 commits into
base: buster
Choose a base branch
from
14 changes: 14 additions & 0 deletions etc/asound.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Use PulseAudio by default
pcm.!default {
type pulse
fallback "sysdefault"
hint {
show on
description "Default ALSA Output (currently PulseAudio Sound Server)"
}
}

ctl.!default {
type pulse
fallback "sysdefault"
}
2 changes: 0 additions & 2 deletions etc/mycroft/mycroft.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"play_wav_cmdline": "aplay -Dhw:0,0 %1",
"play_mp3_cmdline": "mpg123 -a hw:0,0 %1",
"enclosure": {
"platform": "picroft"
},
Expand Down
44 changes: 33 additions & 11 deletions home/pi/audio_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,47 @@
# called by auto_run.sh. This file will never be replaced by an update.


# Uncomment one of lines to change output audio such as the HDMI port, e.g. the
# connected TV's speakers. By default audio is output by the headphone jack.
# You can check what output devices are available with the command:
# `pactl list sinks short`
#
# You can check what ALSA output you have with the command `aplay -L`
# The analog headphone jack would be listed as:
# alsa_output.platform-bcm2835_audio.analog-stereo
#
# Below are the assumed output locations for each output type.
# While the Raspberry Pi is a standard board, these have been known to vary by
# system; you can always use `aplay -L` to check
# sudo amixer cset numid=3 "0" # audio out to the USB soundcard
# sudo amixer cset numid=3 "1" # audio out the analog speaker/headphone jack
# sudo amixer cset numid=3 "2" # audio out the HDMI port (e.g. TV speakers)
# The Pi's digital audio output will be listed as:
# alsa_output.platform-bcm2835_audio.digital-stereo
#
# Whilst a USB speaker will be listed more explicitly eg:
# alsa_output.usb-SEEED_ReSpeaker_4_Mic_Array__UAC1.0_-00.analog-stereo
#
# You can set one of these as the default PulseAudio device by uncommenting
# one of these lines or adding your own:
# pactl set-default-sink alsa_output.platform-bcm2835_audio.analog-stereo
# pactl set-default-sink alsa_output.platform-bcm2835_audio.digital-stereo
# pactl set-default-sink alsa_output.usb-SEEED_ReSpeaker_4_Mic_Array__UAC1.0_-00.analog-stereo
# pactl set-default-sink $DEVICE_NAME


# You can check what input devices are available with the command:
# `pactl list sources short`
#
# You can set one of these as the default input device by uncommenting
# one of these lines or adding your own:
# pactl set-default-source alsa_output.platform-bcm2835_audio.analog-stereo
# pactl set-default-source alsa_output.platform-bcm2835_audio.digital-stereo
# pactl set-default-source alsa_output.usb-SEEED_ReSpeaker_4_Mic_Array__UAC1.0_-00.analog-stereo
# pactl set-default-source $DEVICE_NAME


# Set the default volume level; 75 is the current default in auto_run.sh
#
# amixer set Master 75%
# pactl set-sink-volume @DEFAULT_SINK@ 75%
#
# You can set a default volume for specific output devices by using the device
# name in place of "@DEFAULT_SINK@"


# Any speaker output you want on startup!
# You can make Mycroft say anything you want on startup!
# Try uncommenting one of the following:
# speak "It is so good to be back online!"
# speak "I come in peace"
# speak "I am Mycroft. Take me to your leader!"
63 changes: 31 additions & 32 deletions home/pi/auto_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,15 @@ function save_choices() {
}

function set_volume() {
# Use amixer to set the volume level
# This attempts to set both "Master" and "PCM"
# Use PulseAudio to set the volume level

amixer set PCM $@ > /dev/null 2>&1
amixer set Master $@ > /dev/null 2>&1
pactl set-sink-volume @DEFAULT_SINK@ $@
}

function save_volume() {
# Save command to amixer to set the volume level
# Save PulseAudio volume command to set the default volume level

echo "amixer set PCM $@ > /dev/null 2>&1" >> ~/audio_setup.sh
echo "amixer set Master $@ > /dev/null 2>&1" >> ~/audio_setup.sh
echo "pactl set-sink-volume @DEFAULT_SINK@ $@" >> ~/audio_setup.sh
}

function network_setup() {
Expand Down Expand Up @@ -302,24 +299,29 @@ function setup_wizard() {
1)
echo "$key - Analog audio"
# audio out the analog speaker/headphone jack
sudo amixer cset numid=3 "1" > /dev/null 2>&1
echo 'sudo amixer cset numid=3 "1" > /dev/null 2>&1' >> ~/audio_setup.sh
pactl set-default-sink alsa_output.platform-bcm2835_audio.analog-stereo
echo 'pactl set-default-sink alsa_output.platform-bcm2835_audio.analog-stereo' >> ~/audio_setup.sh
audio="analog_audio"
break
;;
2)
echo "$key - HDMI audio"
# audio out the HDMI port (e.g. TV speakers)
sudo amixer cset numid=3 "2" > /dev/null 2>&1
echo 'sudo amixer cset numid=3 "2" > /dev/null 2>&1' >> ~/audio_setup.sh
pactl set-default-sink alsa_output.platform-bcm2835_audio.digital-stereo
echo 'pactl set-default-sink alsa_output.platform-bcm2835_audio.digital-stereo' >> ~/audio_setup.sh
audio="hdmi_audio"
break
;;
3)
echo "$key - USB audio"
# audio out to the USB soundcard
sudo amixer cset numid=3 "0" > /dev/null 2>&1
echo 'sudo amixer cset numid=3 "0" > /dev/null 2>&1' >> ~/audio_setup.sh
echo "Select your output device"
pactl list sinks short | awk '{printf(" %d) %s\n", NR, $2)}'
echo -n "${HIGHLIGHT}Choice:${RESET} "
read -N1 -s card_num
card_name=$(pactl list sinks short | awk '{print$2}' | sed -n ${card_num}p)
pactl set-default-sink ${card_name}
echo 'pactl set-default-sink ${card_name}' >> ~/audio_setup.sh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of wonder if it would behoove us here to emit pactl unload-module module-suspend-on-idle by default here, given that we know that suspension is a problem with many devices. If that doesn't sound reasonable, maybe we could add it as a comment to the sample audio_setup.sh above?

audio="usb_audio"
break
;;
Expand Down Expand Up @@ -355,12 +357,6 @@ function setup_wizard() {
# See: https://github.com/google/aiyprojects-raspbian/issues/297
sudo sed -i -e "s/^load-module module-suspend-on-idle/#load-module module-suspend-on-idle/" /etc/pulse/default.pa

# Changes mycroft.conf to use the default output device
sudo sed -i \
-e "s/aplay -Dhw:0,0 %1/aplay %1/" \
-e "s/mpg123 -a hw:0,0 %1/mpg123 %1/" \
/etc/mycroft/mycroft.conf

# Install asound.conf
sudo cp AIY-asound.conf /etc/asound.conf

Expand All @@ -387,11 +383,11 @@ function setup_wizard() {
sudo /home/pi/mycroft-core/.venv/bin/python dfu.py --download 1_channel_firmware.bin
cd ..

# Configure Mycroft to use plughw:ArrayUAC10,0 (Seeed device)
sudo sed -i \
-e "s/aplay -Dhw:0,0 %1/aplay -Dplughw:ArrayUAC10,0 %1/" \
-e "s/mpg123 -a hw:0,0 %1/mpg123 -a plughw:ArrayUAC10,0 %1/" \
/etc/mycroft/mycroft.conf
# Configure PulseAudio to use Seeed device
pactl set-default-source alsa_input.usb-SEEED_ReSpeaker_4_Mic_Array__UAC1.0_-00.analog-mono
pactl set-default-sink alsa_output.usb-SEEED_ReSpeaker_4_Mic_Array__UAC1.0_-00.analog-stereo
echo 'pactl set-default-source alsa_input.usb-SEEED_ReSpeaker_4_Mic_Array__UAC1.0_-00.analog-mono' >> ~/audio_setup.sh
echo 'pactl set-default-sink alsa_output.usb-SEEED_ReSpeaker_4_Mic_Array__UAC1.0_-00.analog-stereo' >> ~/audio_setup.sh

audio="seed_mic_array_20"
break
Expand Down Expand Up @@ -502,10 +498,18 @@ function setup_wizard() {
;;
4)
echo "$key - Other"
echo "Other microphone _might_ work, but there are no guarantees."
echo "Other microphones _might_ work, but there are no guarantees."
echo "We'll run the tests, but you are on your own. If you have"
echo "issues, the most likely cause is an incompatible microphone."
echo "The PS Eye is cheap -- save yourself hassle and just buy one!"
echo ""
echo "Select your input device"
pactl list sources short | awk '{printf(" %d) %s\n", NR, $2)}'
echo -n "${HIGHLIGHT}Choice:${RESET} "
read -N1 -s card_num
card_name=$(pactl list sources short | awk '{print$2}' | sed -n ${card_num}p)
pactl set-default-source ${card_name}
echo 'pactl set-default-source ${card_name}' >> ~/audio_setup.sh
mic="other"
break
;;
Expand Down Expand Up @@ -693,12 +697,7 @@ function setup_wizard() {

function speak() {
# Generate TTS audio using Mimic 1
~/mycroft-core/mimic/bin/mimic -t $@ -o /tmp/speak.wav

# Play the audio using the configured WAV output mechanism
wavcmd=$( jq -r ".play_wav_cmdline" /etc/mycroft/mycroft.conf )
wavcmd="${wavcmd/\%1/\/tmp\/speak.wav}"
$( $wavcmd >/dev/null 2>&1 )
~/mycroft-core/mimic/bin/mimic -t $@
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good that the user may hear some output as a result of this being called, but using mimic esp when it is not compiled for pulseaudio doesn't tell us much about whether e.g. mycroft-speak or the codepath that mycroft actually uses to do TTS will work for them, will it?

}

######################
Expand Down Expand Up @@ -876,7 +875,7 @@ then
# the script will likely be overwritten during later updates.
#
# Default to analog audio jack at 75% volume
amixer cset numid=3 "1" > /dev/null 2>&1
pactl set-default-sink alsa_output.platform-bcm2835_audio.analog-stereo
set_volume 75%

# Check for custom audio setup
Expand Down