使用树莓派为音响添加AirPlay功能

今年618大出血买了音响功放,接口很多,但是就是没用接入WiFi当网络音响的功能,也就是苹果的AirPlay或安卓的DLNA/UPnP。虽然天逸已经提供了安卓APP,但是目前并没有苹果的APP,不过好在我有空闲的树莓派,可以将功放升级成AirPlay设备。

背景

一开始,我是把大硬盘直接挂在树莓派上,用Raspberry OS系统GUI播放音乐(比如用vlc player),这期间总是出现各种各样的不爽,比如压缩软件不顺手啊、中/日/法语字符集不全啊、2.4G的WiFi信号晚上不稳定啊什么的……所以就想利用树莓派给功放的DAC加上AirPlay功能,直接挂在家里的WiFi上或插网线做多媒体音箱。

1 shairport-sync - AirPlay

shairport-sync这个库是专门用来支持AirPlay的,主要参考树莓派的简要安装说明以及故障排查

例行调整命令行的用户体验:

1
2
3
4
sudo apt install -y ufw zsh vim tmux
chsh -s $(which zsh)
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

1.1 关闭WiFi适配器的省电模式或者低功耗模式

对于树莓派3或者更高级的自带WiFi的板子,需要手动关闭省电模式,否则AirPlay可能会在家里的WiFi环境中消失一段时间,或者产生很大的延迟:

1
sudo iwconfig wlan0 power off

不过我打算用的是树莓派2B,iwconfig里能看到Power Management:off,因此不需要做上面的操作。

1.2 添加防火墙例外

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo ufw allow proto tcp to 0.0.0.0/0 port 22 comment "sshd listen port"
sudo ufw allow proto tcp to 0.0.0.0/0 port 80 comment "for http server"
sudo ufw allow proto tcp to 0.0.0.0/0 port 5900 comment "vnc server listen port"
sudo ufw allow proto tcp to 0.0.0.0/0 port 8000 comment "for python -m http.server 8000"

sudo ufw allow from 192.168.1.1/24 to any port 5353 comment "shairport-sync"
sudo ufw allow proto tcp from 192.168.1.1/24 to any port 3689 comment "shairport-sync"
sudo ufw allow proto tcp from 192.168.1.1/24 to any port 5000:5005 comment "shairport-sync"
sudo ufw allow proto udp from 192.168.1.1/24 to any port 6000:6005 comment "shairport-sync"
sudo ufw allow proto udp from 192.168.1.1/24 to any port 35000:65535 comment "shairport-sync"

sudo ufw default deny

sudo ufw enable

1.3 安装依赖

1
2
3
sudo apt install -y build-essential git xmltoman autoconf automake libtool \
libpopt-dev libconfig-dev libasound2-dev avahi-daemon \
libavahi-client-dev libssl-dev libsoxr-dev

1.4 下载并安装

1
2
3
4
5
6
git clone https://github.com/mikebrady/shairport-sync.git
cd shairport-sync
autoreconf -fi
./configure --sysconfdir=/etc --with-alsa --with-soxr --with-avahi --with-ssl=openssl --with-systemd
make
sudo make install

1.5 设置开机启动

1
2
sudo systemctl enable shairport-sync
sudo systemctl start shairport-sync

1.6 设置配置文件

我的板子是Raspberry Pi 2 Model B Rev 1.1,把天逸AD86-D功放miniUSB声卡插在树莓派的USB口上,在功放切换到PC模式时,aplay -l的输出为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
**** List of PLAYBACK Hardware Devices ****
card 0: b1 [bcm2835 HDMI 1], device 0: bcm2835 HDMI 1 [bcm2835 HDMI 1]
Subdevices: 4/4
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
card 1: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
Subdevices: 4/4
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
card 2: AUDIO [USB AUDIO], device 0: USB Audio [USB Audio]
Subdevices: 0/1
Subdevice #0: subdevice #0

因此,我应该将shairport-sync配置文件(在make install输出中可以看到配置文件位于/etc/shairport-sync.conf)的alsa部分里output_device改为hw:2(这里写hw:2,0应该也可以),通过alsamixer选择USB AUDIO声卡时可以看到mixerPCM,所以mixer_control_name也得修改为PCM

1
2
3
4
5
6
alsa =
{
output_device = "hw:2"; // the name of the alsa output device. Use "shairport-sync -h" to discover the names of ALSA hardware devices. "alsamixer" or "aplay" to find out the names of devices, mixers, etc.
mixer_control_name = "PCM"; // the name of the mixer to use to adjust output volume. If not specified, volume in adjusted in software.
...
}

保存并重启服务:

1
sudo systemctl start shairport-sync

此时可以在macOS的声音控制面板中看到一个名为Raspberrypi的扬声器。切换到这个扬声器时,电脑只能控制是否静音,不能控制音量;而树莓派上的alsamixer不论选什么卡都无法控制音量,因此只能通过功放控制音量。

2 samba - 文件夹共享

为了解决把硬盘挂载树莓派上不爽的体验,可以选择用samba把盘共享在网络上。不过samba库有个几十兆,可以临时调整树莓派的路由表,把网关指向家里的OpenWrt上,这样下载软件包更快:

1
sudo route add default gw 192.168.1.150

2.1 安装samba

1
sudo apt install samba samba-common-bin

2.2 挂载硬盘

我的硬盘是希捷12年前的FreeAgent古董盘,在RaspberryOS上是自动挂载到/media/pi/FreeAgent Drive上的,大概是自动帮我执行了:

1
sudo mount /dev/sda1/ "/media/pi/FreeAgent Drive"

2.3 修改配置文件

修改samba的配置文件,将挂载点发布,并设置读写权限和认证。在/etc/samba/smb.conf的最后追加:

1
2
3
4
5
6
[FreeAgentShare]
path = /media/pi/FreeAgent Drive
writeable=Yes
create mask=0777
directory mask=0777
public=no

2.4 设置访问用户

pi添加为samba的认证用户,并设置密码:

1
sudo smbpasswd -a pi

2.5 重启服务

1
sudo systemctl restart smbd

2.6 配置防火墙例外

1
2
3
4
sudo ufw allow proto udp from 192.168.1.1/24 to any port 137 comment "samba"
sudo ufw allow proto udp from 192.168.1.1/24 to any port 138 comment "samba"
sudo ufw allow proto tcp from 192.168.1.1/24 to any port 139 comment "samba"
sudo ufw allow proto tcp from 192.168.1.1/24 to any port 445 comment "samba"

2.7 连接

使用macOS连接,打开Finder,⌘+K打开“连接服务器”,地址填写smb://raspberrypi.local/FreeAgentShare,输入用户名密码就能连上了。(树莓派的默认主机名就是raspberrypi.local

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×