收起左侧

哈曼卡顿拯救计划,将你的飞牛成为Airplay接收器

0
回复
81
查看
[ 复制链接 ]

2

主题

3

回帖

0

牛值

江湖小虾

哈曼卡顿拯救计划

因为落叶乌龟的梗,买了哈曼卡顿音响,买回来发现这个家伙竟然只支持蓝牙和aux有线连接,每次要放手机的音乐都要去按一下开关、蓝牙连接,期间要是换手机更是麻烦得很,时间一长成了美丽的**。

琢磨一下,有不有随时能够接收远程音乐投放的方案呢。网上找找,果然有airplay/dlan网络音频接收器,但是价格普遍500往上,秉着不能再花第二笔冤枉钱,刚好NAS上有aux输出口,遂决定自己想办法。

ubuntu server + shairport-sync

airplay播放应用范围这么广,肯定早就有大佬准备好了方案,找了找果然github上果然有,8.3K星项目shairport-sync,https://github.com/mikebrady/shairport-sync

按照教程部署,果然完美解决,日子就这么过下去了,后来飞牛os横空出世,手搓的ubuntu server怎么都不香了,前段时间最期待的功能【zfs】也上线了(开发团队太给力了),于是把主力机从ubuntu server装成飞牛,然后发现哈曼卡顿又挂了,再装回去肯定不可能了,那么飞牛能不能也装上shairport-sync呢?毕竟不能像ubuntu一样随便折腾,网上找了找资料,好像拿飞牛部署shairport-sync的帖子不多,有ubuntu成功的经验,那就自己试试吧,期间各种试错,好在最终搞定,想着踩过的雷,大家就别再踩了,把最后成功的方法简单分享一下,正文开始。

飞牛OS + shairport-sync

PRE-STEP检查你的硬件有不有声卡

一般看看硬件的IO挡板那里有不有aux接口(3.5mm的音频输出)、spdif接口等,如果有HDMI接口也是可以输出音频的,如果都没有,理论上来说有USB接口也行,插一个usb声卡也是可以的(我没试过,但相信只要飞牛能够识别到,应该就可以)

STEP1 安装ALSA

是的飞牛系统自带是没有ALSA(LINUX的音频驱动框架,简单说安装了它才能驱动你的声卡输出音频)。

apt update
apt install alsa-utils

STEP2 测试音频输出

安装好了以后,就可以正式测试你的飞牛能不能输出音频了

aplay -l

输入命令后系统会打印出飞牛正常驱动的音频输出设备,以我的飞牛为例,输出如下

root@PaperFish:/# aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: Generic [HD-Audio Generic], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: Generic [HD-Audio Generic], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: Generic [HD-Audio Generic], device 8: HDMI 2 [HDMI 2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: Generic [HD-Audio Generic], device 9: HDMI 3 [HDMI 3]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: pcsp [pcsp], device 0: pcspeaker [pcsp]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: Generic_1 [HD-Audio Generic], device 0: ALC897 **og [ALC897 **og]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: Generic_1 [HD-Audio Generic], device 1: ALC897 Digital [ALC897 Digital]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

card 0是HDMI接口;
card1 是我主板的蜂鸣器,就是出错哔哔叫的;
重点card2就是我的板载声卡ALC897(记错ALC开头的,大多数板载声卡都是这个开头),可以看到分出来的两个口:device0【**og】是模拟信号就是AUX口(常见的3.5mm耳机插口);
device1【Digital】是数字信号是spdif口(有两种形式:同轴莲花口、光纤口)
有的可能用笔记本装飞牛,可能还有扬声器设备,五花八门的这里就不赘述了。

插**的音响,有两种方式测试成功了没有

第一种:

speaker-test -D plughw:2,0

-D 是指定设备,为什么是plughw:2,0 第一个数字是声卡,第二个数字是设备,我的音响插在aux口【**og】上,所以是 card2 device0 ,每台飞牛情况不一样自己数清楚。

如果音响发出噪音,基本成功了80%。

第二种,找一个wav音频播放一下:

aplay -D hw:2,0 /path/*.wav

为什么是hw:2,0原因同上,如果音响播放出音乐,恭喜基本成功了80%。

如果没响也别急,我一开始也没响,下面有几种情况

STEP2-1 为啥不响?

1.音响声音太小

是的,我第一次部署就是哈曼卡顿声音太小没听到,调试了办法,头都想破了没想出为什么,把音响声音开大点就好了。

2.系统静音了

是的,我第二次部署的时候发现飞牛安装好ALSA后是默认静音,这次又把头想破了没想出为什么,解除静音就好了。

alsamixer

进入到一个由命令行组成的图形界面,上下左右可以操作,这里先按F6,就可以选择声卡了,选择对应的声卡按回车,就可以调音量,类似于windows的声音合成器,会有好几个设备,注意master和播放设备是不是'MM'('MM'静音,此时上下调音量是没用的),按M就可以解除静音成为'00',然后esc退出即可。

image.png

3.选错设备了

root@PaperFish:/vol2/1000/Downloads# aplay -D hw:1,0 suzume_no_tojimari.wav
Playing WAVE 'suzume_no_tojimari.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
aplay: set_params:1358: Channels count non available

选择错设备会报错,我这里故意选择了我的主板蜂鸣器,换换设备没准你的飞牛有好几个音频输出口

4.aplay -l 没有设备

要么你的飞牛没有声卡硬件,要么驱动没装好,这个网上找找教程好了,千人千问俺也不一定了解。

STEP3 安装shairport-sync

通过了前面音频测试,后面就简单加速了。跟着大佬的项目教程来就行(shairport-sync/BUILD.md at master · mikebrady/shairport-sync · GitHub),我这里相当于中文简单翻译一下。

安装编译环境

apt update
apt install --no-install-recommends build-essential git autoconf automake libtool libpopt-dev libconfig-dev libasound2-dev avahi-daemon libavahi-client-dev libssl-dev libsoxr-dev libplist-dev libsodium-dev libavutil-dev libavcodec-dev libavformat-dev uuid-dev libgcrypt-dev xxd

这里注意两点,一是不要手欠升级包 apt upgrade,飞牛OS有些需要指定版本的依赖,升级以后可能飞牛会出现问题。二这里除了编译依赖环境以外还安装了git,飞牛的应用商店里也有git安装,直接apt包安装可能不太好使

安装好依赖以后,把shairport-sync的源代码下载下来,这里建议放在飞牛OS用于储存的位置,我是在储存空间2创建了一个AppData的文件夹用来放各种应用,大家可以提前建立好文件夹。

cd /vol2/1000/AppData 
git clone https://github.com/mikebrady/shairport-sync.git

这里下载好了,目录里就多了一个shairport的文件夹,【在这停顿】这里如果需要airplay2,还需要部署这个大佬编写的nqptp服务(同步音频的时间戳,可以实现多个音响全屋同步,推荐部署),如果部署airplay1就不需要了(跳过NQPTP安装的步骤)


下载NQPTP源码

cd /vol2/1000/AppData
git clone https://github.com/mikebrady/nqptp.git

编译安装NQPTP

cd nqptp
autoreconf -fi
./configure --with-systemd-startup
make
make install

设置NQPTP开启自启、启动服务

systemctl enable nqptp
systemctl start nqptp

检查NQPTP启动成功与否,出现active (running)就是成功

systemctl status nqptp

安装shaiport-sync,和编译安装NQPTP差不多

cd shairport-sync
autoreconf -fi
./configure --sysconfdir=/etc --with-alsa --with-soxr --with-avahi --with-ssl=openssl --with-systemd --with-airplay-2

这里注意如果你没安装NQPTP,去掉--with-airplay-2,否则最后启动服务的时候会因找不到NQPTP报错。

后面的操作都一样

make
make install

作者提醒autoreconf的过程可能会花点时间
By the way, the 'autoreconf' step may take quite a while – please be patient!
和硬件配置有关,我反正是光速搞定。

到这一步,安装就完成了最后就是调试了。

STEP4 调试shairport-sync

shairport-sync的配置文件是/etc/shairport-sync.conf里面有很多参数(全都是注释),对音频品质有要求的可以自己探索,这里就说两个关键的:
1修改服务名字(你手机上airplay设备显示的名字)
2指定播放设备。

cd /etc
vi shairport-sync.conf

// General Settings
general =
{
        name = "Mrs.Ha";
...
}
// Back End Settings

// These are parameters for the "alsa" audio back end.
// For this section to be operative, Shairport Sync must be built with the following configuration flag:
// --with-alsa
alsa = 
{
        output_device = "hw:2,0";
...}

shairport-sync.conf上直接加两行也行,删除注释改值也行,新建一个shairport-sync.conf文件里面只保留下面两段也行。

general = {name = "Mrs.Ha";}
alsa = {output_device = "hw:2,0";}

其中,"Mrs.Ha"是我给airplay设备起的名字,哈曼卡顿=蛤女士
"hw:2,0"在测试音频的部分解释过了,这两个不要照抄了。

配置文件修改好了以后,可以先运行shairport-sync -v进入测试状态,正常会输出一大段配置情况

root@PaperFish:/etc# shairport-sync -v
         0.000478280 "shairport.c:2282" Startup in AirPlay 2 mode, with features 0x405c4a00,0x1c340 on device "00:e0:22:05:15:f0".
         0.000023480 "shairport.c:2321" Version String: "4.3.7-5-g3c8ceb7c-AirPlay2-smi10-OpenSSL-Avahi-ALSA-soxr-sysconfdir:/etc"
         0.000002590 "shairport.c:2340" Command Line: "shairport-sync -v".
         0.000044280 "shairport.c:2376" Log Verbosity is 1.
         0.000004230 "audio_alsa.c:1082" alsa: alsa_maximum_stall_time of 0.200000 sec.
         0.000005440 "audio_alsa.c:1371" alsa: disable_standby_mode is "never".
         0.000002280 "audio_alsa.c:1375" alsa: disable_standby_mode_silence_threshold is 0.040000 seconds.
         0.000001780 "audio_alsa.c:1377" alsa: disable_standby_mode_silence_scan_interval is 0.004000 seconds.
         0.000004960 "audio_alsa.c:1417" alsa: output device name is "hw:2,0".
         0.000028740 "shairport.c:2444" disable_resend_requests is off.
         0.000005440 "shairport.c:2445" diagnostic_drop_packet_fraction is 0.000000. A value of 0.0 means no packets will be dropped deliberately.
         0.000002690 "shairport.c:2449" statistics_requester status is 0.
         0.000002760 "shairport.c:2454" rtsp listening port is 7000.
         0.000002550 "shairport.c:2455" udp base port is 6001.
         0.000002100 "shairport.c:2456" udp port range is 10.
         0.000003340 "shairport.c:2457" player name is "Mrs.Ha".
         0.000001930 "shairport.c:2458" backend is "alsa".
         0.000002450 "shairport.c:2459" run_this_before_play_begins action is "(null)".
         0.000002110 "shairport.c:2460" run_this_after_play_ends action is "(null)".
         0.000003990 "shairport.c:2461" wait-cmd status is 0.
         0.000001960 "shairport.c:2462" run_this_before_play_begins may return output is 0.
         0.000001900 "shairport.c:2463" run_this_if_an_unfixable_error_is_detected action is "(null)".
         0.000002690 "shairport.c:2465" run_this_before_entering_active_state action is  "(null)".
         0.000003800 "shairport.c:2467" run_this_after_exiting_active_state action is  "(null)".
         0.000003380 "shairport.c:2469" active_state_timeout is  10.000000 seconds.
         0.000004580 "shairport.c:2470" mdns backend "(null)".
         0.000002070 "shairport.c:2472" interpolation setting is "auto".
         0.000002490 "shairport.c:2476" interpolation soxr_delay_threshold is 30000000.
         0.000002720 "shairport.c:2477" resync time is 0.050000 seconds.
         0.000002300 "shairport.c:2478" resync recovery time is 0.100000 seconds.
         0.000002870 "shairport.c:2479" allow a session to be interrupted: 0.
         0.000002230 "shairport.c:2480" busy timeout time is 120.
         0.000002650 "shairport.c:2481" drift tolerance is 0.002000 seconds.
         0.000002320 "shairport.c:2482" password is not set.
         0.000002420 "shairport.c:2483" default airplay volume is: -24.000000.
         0.000004300 "shairport.c:2484" high threshold airplay volume is: -16.000000.
         0.000004410 "shairport.c:2486" check for higher-than-threshold volume for new play session is disabled.
         0.000002410 "shairport.c:2492" ignore_volume_control is 0.
         0.000004480 "shairport.c:2496" volume_max_db is not set
         0.000004160 "shairport.c:2497" volume range in dB (zero means use the range specified by the mixer): 0.
         0.000003360 "shairport.c:2499" volume_range_combined_hardware_priority (1 means hardware mixer attenuation is used first) is 0.
         0.000003420 "shairport.c:2503" playback_mode is 0 (0-stereo, 1-mono, 1-reverse_stereo, 2-both_left, 3-both_right).
         0.000002390 "shairport.c:2505" disable_synchronization is 0.
         0.000004360 "shairport.c:2506" use_mmap_if_available is 1.
         0.000002330 "shairport.c:2507" output_format automatic selection is enabled.
         0.000003810 "shairport.c:2511" output_rate automatic selection is enabled.
         0.000002190 "shairport.c:2515" audio backend desired buffer length is 0.200000 seconds.
         0.000003070 "shairport.c:2517" audio_backend_buffer_interpolation_threshold_in_seconds is 0.120000 seconds.
         0.000002690 "shairport.c:2519" audio backend latency offset is 0.000000 seconds.
         0.000004380 "shairport.c:2521" audio backend silence lead-in time is "auto".
         0.000001950 "shairport.c:2525" zeroconf regtype is "_raop._tcp".
         0.000002880 "shairport.c:2526" decoders_supported field is 1.
         0.000002360 "shairport.c:2527" use_apple_decoder is 0.
         0.000001700 "shairport.c:2528" alsa_use_hardware_mute is 0.
         0.000002280 "shairport.c:2532" no special mdns service interface was requested.
         0.000004250 "shairport.c:2535" configuration file name "/etc/shairport-sync.conf" resolves to "/etc/shairport-sync.conf".
         0.000005010 "shairport.c:2566" loudness is 0.
         0.000001980 "shairport.c:2567" loudness reference level is -20.000000
         0.000186270 "shairport.c:2694" NQPTP is online.
         1.499917005 "shairport.c:255" "soxr" interpolation has been chosen.

说明配置好了,你的iphone应该就可以搜索到你的airplay设备了,放一首歌试试,终端也会一直打印推流的信息,从你点播放到音响出声应该会有几秒延迟,属于正常情况,后期也可以慢慢调试参数,减少延迟。
如果播放正常,那就常态化运行起来好了ctrl+c退出测试模式,还是设置开启自启,同时启动服务

systemctl enable shairport-sync
systemctl start shairport-sync

恭喜你,这就完事了。

一些提醒事项

如果播放的音乐断断续续,或者音质不对劲,可以设置buffer_size增大、增大缓冲区来提高音频质量,但是如果设备性能确实不行的话也是没办法的,我曾经尝试用香橙派部署shairport-sync cpu实在太差导致延迟,可以在调试模式下shairport-sync -v看到日志输出

0.075171125 "player.c:2908" Large negative (i.e. early) sync error of -41451 frames (-0.939932 seconds), at frame: 1602805350.

这种要么就算了,强扭的瓜不甜。

收藏
送赞 1
分享

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则