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:

0. 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

1. Disable frequency scaling in the BIOS.

2. 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

3. 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.

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

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

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

7. disable 'Intel Virtualization Technology', 'C-state' and on-board sound in the BIOS. 3. use an USB hub between pc and DAC

8. 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


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

10. Install ananicy.

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


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

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

Deadbeef
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.

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

14. 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!

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

Firefox
16. Add to /etc/pulse/default.pa:
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.

No comments:

Post a Comment