Monday, April 22, 2019

Optimize Linux audio

Optimize Linux audio

These actions result in a click-free audio using an USB DAC and Linux. You may not need all of them, just try them, in this order:

1. Convert your >96 kHz flac files to 44 kHz. You probably won't hear the difference.

In Linux:
sox "inputfile_96k.flac" -G -b 16 "outputfile_44k.flac" rate -v -L 44100 dither 

2. Use the MOC player  (Music On Console)

3. Disable frequency scaling in the BIOS.

4. Improve Linux responsiveness by adding these lines to /etc/sysctl.d/99-sysctl.conf:
vm.swappiness = 1
vm.vfs_cache_pressure = 50
vm.dirty_background_ratio = 1
vm.dirty_ratio = 40

5. Add these kernel parameters : 
intel_idle.max_cstate=0 processor.max_cstate=1 usbcore.autosuspend=-1 sysrq_always_enabled=1 isolcpus nohz_full=1-4 rcu_nocbs=1-4 threadirqs intel_pstate=disable
Underlined are most relevant.

In my experience this helps the best if dropouts occuring during playing a flac file.

6. If frequency scaling is not disabled in the BIOS, install 'cpupower', enable it and modify the following line in /etc/default/cpupower: governor='performance' 

7. 'reflac' the flac files. Use compression '-2'.
This helps against dropouts when the flac file begins to play.

8. Install the package 'realtime-privileges' and add the user to the group 'realtime': usermod -a -G realtime <username>

9. disable 'Intel Virtualization Technology', 'C-state' and on-board sound in the BIOS. 

10. change the contents of /etc/pulse/daemon.conf to:
high-priority = yes
nice-level = -11
realtime-scheduling = yes
realtime-priority = 9
rlimit-rtprio = 9
rlimit-rttime = -1
daemonize = no

11. Add the user to the 'audio' group: usermod -a -G audio $USER

12. Install ananicy.

13. Modify /etc/asound.conf to:
pcm.!default {
   type plug
   slave.pcm hw

14. Modify /etc/security/limits.conf with the following lines:
@audio - rtprio 99
@audio - memlock unlimited
@audio - nice -10

15. Bypass PulseAudio when playing music with Deadbeef or Audacious.

In configuration, Sound, set the output plugin to 'ALSA output plugin'.

In configuration, Plugins, 'ALSA output plugin', de-select 'Use ALSA-resampling'.

In configuration, DSP, remove the resampler.

In 'Audio settings' select the output plugin 'ALSA'.

16. create ~/.asoundrc for near bit perfect audio via ALSA:
pcm.usb-audio {
    type hw
    card 1
    defaults.pcm.rate_converter "samplerate_best"
    defaults.ctl.card 1
    defaults.pcm.card 1
    defaults.timer.card 1
ctl.usb-audio {
    type hw
    card 1

Beware your volume control is not PulseAudio anymore but Deadbeef or Audacious. If the volume in these are set to maximum, be sure to lower the volume first!

17. Convert your flac files to wav and back. In some cases this helps.

19. Add to /etc/pulse/
load-module module-udev-detect tsched=0

USB isolator
Later I got an USB isolator to be placed between the USB DAC and the pc:

While it does not replace the actions above and the effect is almost too subtle to notice, perhaps the sound is a bit more 'spacious'. It does not hurt I guess.

Disclaimer: always beware of a high sound volume, it can damage your hearing! First close your volume control and slowly increase the volume.