RPi3BにAlmaLinux + PulseAudio+ Spotifydでサウンドサーバを構築

# 目的
Volumioほどの機能はいらず、PulseAudioとSpotifydが動くラズパイの構築

# 環境
- Raspberry Pi 3 Model B
- AlmaLinux release 9.3 (aarch64)
- DAC(HiFiBerry DAC+)

# 前提
AlmaLinxuをRPiに入れるところは省略します
DACを有効にするため、/boot/config.txtは下記を設定してください
```
dtparam=i2c_arm=on
dtparam=i2s=on

force_eeprom_read=0
dtoverlay=hifiberry-dacplus
dtoverlay=disable-wifi
dtoverlay=disable-bt
```

# 設定
```bash
$ sudo dnf install cargo openssl-devel git pulseaudio alsa-lib-devel alsa-utils alsa-plugins-pulseaudio

$ cd /usr/local/src
$ git clone https://github.com/Spotifyd/spotifyd.git
$ cd spotifyd

# buildは時間かかるよ
$ cargo build --release
$ sudo mv ./target/release/spotifyd /usr/local/bin/

$ mkdir -p ~/.config/systemd/user

$ cat << '_EOL_' > ~/.config/systemd/user/pulseaudio.socket
[Unit]
Description=Sound Service

Requires=pulseaudio.socket
ConditionUser=!root

[Service]
ExecStart=/usr/bin/pulseaudio --daemonize=no --log-target=journal
LockPersonality=yes
MemoryDenyWriteExecute=yes
NoNewPrivileges=yes
Restart=on-failure
RestrictNamespaces=yes
SystemCallArchitectures=native
SystemCallFilter=@system-service
# Note that notify will only work if --daemonize=no
Type=notify
UMask=0077
Slice=session.slice

[Install]
Also=pulseaudio.socket
WantedBy=default.target
_EOL_

$ cat << '_EOL_' > ~/.config/systemd/user/pulseaudio.socket
[Unit]
Description=Sound System
ConditionUser=!root

[Socket]
Priority=6
Backlog=5
ListenStream=%t/pulse/native

[Install]
WantedBy=sockets.target
_EOL_

$ sudo usermod -aG audio `whoami`

$ sudo loginctl enable-linger `whoami`

$ systemctl --user daemon-reload
$ systemctl --user  status pulseaudio.service
$ systemctl --user  enable --now pulseaudio.service
$ systemctl --user  status pulseaudio.service
```
```bash
$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: sndrpihifiberry [snd_rpi_hifiberry_dacplus], device 0: HiFiBerry DAC+ HiFi pcm512x-hifi-0 [HiFiBerry DAC+ HiFi pcm512x-hifi-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

$ sudo vi /etc/pulse/default.pa
load-module module-stream-restore restore_device=false
# 末尾に restore_device=false を追記

$ pacmd list-sinks  | grep name:
        name: <alsa_output.platform-soc_sound.stereo-fallback>

$ pacmd set-default-sink alsa_output.platform-soc_sound.stereo-fallback
```

spotifydでALSAを指定するのでasound.confでPulseに向けてやります
```bash
sudo su -
cat << '_EOL_' > /etc/asound.conf
pcm.!default {
        type pulse
}
_EOL_
```

```bash
$ mkdir -p ~/.config/spotifyd/
$ cat << _EOL_ > ~/.config/spotifyd/spotifyd.conf
[global]
username = "***USERNAME***"
password = "***PASSWORD***"
use_keyring = false

backend = "alsa"

device_name = "Alma-Pi"
device_type = "speaker"

mixer = "Digital"
bitrate = 320

cache_path = "/tmp/spotifyd/"
max_cache_size = 1024000000
no_audio_cache = false

volume_normalisation = true
normalisation_pregain = -6
_EOL_

cat << _EOL_ > ~/.config/systemd/user/spotifyd.service
[Unit]
Description=A spotify playing daemon
Documentation=https://github.com/Spotifyd/spotifyd
Wants=sound.target
After=sound.target
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/local/bin/spotifyd --no-daemon
Restart=always
RestartSec=10

[Install]
WantedBy=default.target
_EOL_

$ systemctl --user daemon-reload
$ systemctl --user  status spotifyd.service
$ systemctl --user  enable --now spotifyd.service
$ systemctl --user  status spotifyd.service
```

## 参考記事
https://qiita.com/propella/items/4699eda71cd742cba8d3