Skip to content

Instantly share code, notes, and snippets.

@willwhui
Last active April 18, 2022 10:14
Show Gist options
  • Save willwhui/861bf7eaa713935cf9ce2b1f3cd919e7 to your computer and use it in GitHub Desktop.
Save willwhui/861bf7eaa713935cf9ce2b1f3cd919e7 to your computer and use it in GitHub Desktop.
MacBookAir6,2安装Ubuntu的小坑填法
@willwhui
Copy link
Author

willwhui commented Feb 21, 2022

蓝牙耳机声音卡顿

原因:给耳机的声音缓存不够
解决方案:设置一个适合的缓存

可用的方案:

# .sh file
# display info
pactl list | grep -Pzo '.*bluez_card(.*\n)*'

# set buffer time
sudo pactl set-port-latency-offset bluez_card.XX_XX_XX_XX_XX_XX headphone-output 100

echo ">>>>>> wait for reconnect: <<<<<<"
sudo service bluetooth restart

echo ">>>>>> ***  reconnect in 3 seconds *** <<<<<<"
sleep 1
echo ">>>>>> ** reconnect in 2 seconds ** <<<<<<"
sleep 1
echo ">>>>>> * reconnect in 1 second * <<<<<<"

# reconnect
sudo bluetoothctl connect XX:XX:XX:XX:XX:XX

# display info
pactl list | grep -Pzo '.*bluez_card(.*\n)*'

参见:
https://askubuntu.com/questions/475987/a2dp-on-pulseaudio-terrible-choppy-skipping-audio
As none of the other answers worked on my system (Ubuntu 18.04 LTS on a 2012 MacBook Air), I found my solution on the german ubuntuusers wiki. English summary of the german instructions:

The choppy output might be caused by the A2DP implementation, and how it buffers sound before encoding it. For me, changing this buffer's size solved the choppy sound problem. You need to perform three steps:

Find necessary info about the bluetooth device (while it is connected!)

pactl list | grep -Pzo '.*bluez_card(.*\n)*'

The output should be something like

Name: bluez_card.28_11_A5_84_B6_F9
Driver: module-bluez5-device.c
...
Ports:
speaker-output: Speaker (priority: 0, latency offset: 0 usec, available)
    Part of profile(s): a2dp_sink, headset_head_unit
speaker-input: Bluetooth Input (priority: 0, latency offset: 0 usec, not available)
    Part of profile(s): headset_head_unit

We see that the buffers have currently 0 latency. In the next step, you will need the NAME and PORT of your output. In this example, these are bluez_card.28_11_A5_84_B6_F9 and speaker-output, respectively.

Set the buffer size (latency) of your card to a suitable value with this command pattern:

pactl set-port-latency-offset <NAME> <PORT> <BUFFER_SIZE_MICROSECONDS> 

The latency unit of the following command is microseconds, so I'm using a 50 millisecond buffer for my command here:

pactl set-port-latency-offset bluez_card.28_11_A5_84_B6_F9 speaker-output 50000 

Restart your bluetooth service to apply your change

sudo service bluetooth restart

As there is usually no documentation about this, you may have to experiment with higher or lower buffer values. Many people people posted their working latencies in the comments to this answer. Check them out for guidance on the latency value.

@willwhui
Copy link
Author

willwhui commented Feb 25, 2022

合盖后先 sleep再hibernate

参见这里这里

综合起来这样做,运行:
sudo vi /etc/systemd/sleep.conf
打开这个配置文件后,
去掉AllowSuspendThenHibernate=yes的注释(不过,按道理说默认是yes,应该不用去掉也可以工作)
去掉#HibernateDelaySec=10min一行的注释,改为:
HibernateDelaySec=600
表示suspend后,10分钟之后开始hibernate

运行:
sudo vi /etc/systemd/logind.conf
修改里面的几行为:

HandleLidSwitch=suspend-then-hibernate
HandleLidSwitchExternalPower=suspend-then-hibernate
HandleLidSwitchDocked=suspend-then-hibernate

然后重启对应的服务:
systemctl restart systemd-logind.service

如果现在测试一下hibernate功能的话,会发现:
从休眠状态恢复后,并非休眠前的情况,而是类似于关机重启。

解决之前,为了防止后续的操作导致启动失败,可以先做个准备,让系统启动菜单显示几秒钟,在/etc/default/grub文件中做如下更改:

GRUB_TIMEOUT_STYLE=menu
GRUB_TIMEOUT=2

接下来,开始操作解决休眠恢复无效的方案。
参见这里
上文配置过程中会出现如下警告:

update-initramfs: Generating /boot/initrd.img-5.4.0-100-generic
W: initramfs-tools configuration sets RESUME=UUID=b355eafd-25fb-4934-9c3c-1ca6071659c2
W: but no matching swap device is available.
...

根据这里的说明,是因为文件etc/initramfs-tools/conf.d/resume 中的设置是不必要的,删除或注释调它
#RESUME=UUID=b355eafd-25fb-4934-9c3c-1ca6071659c2 resume_offset=3487744
重新运行
sudo update-initramfs -c -k all

然后重启,测试hibernate功能,看是否成功
sudo systemctl hibernate

注:必须保证有足够大的swapfile,增大swapfile的做法参考这里
注:目前建议的swapfile大小参见这里
注:如果swapfile太小,将无法进入hibernate状态,sudo systemctl suspend-then-hibernatesudo systemctl hibernate 等方法都将不可用,因为不够将内存和已经交换到swapfile的数据同时写入swapfile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment