The NixOS wiki is outdated and is being shut down. If you wish to add or improve NixOS documentation, please grab a ticket from the Move the wiki! milestone.

Audio HOWTO

From Nix Wiki
Jump to: navigation, search

ALSA

Getting ALSA to work

  • on a console fire up alsamixer
 alsamixer
  • you see plenty of vertical bars?
    • you should be okay
  • you see very few vertical bars and the sound card (top-left) is something like "PC Speaker"?
    • hit the 'S' key, you should be able to switch to the "real" audio card (if not your audio card is likely to not being supported yet).
    • when the real audio card is selected you should be viewing the "plenty vertical bars" thing.
      • first thing to do is to disable pc speaker (kernel module "snd-pcsp", see below.

Make your audio card the default ALSA card

Sometimes the pc-speaker is the default audio card for ALSA. You can make your real sound card default instead. For example, if your sound card is "hda-intel" then add

  boot.extraModprobeConfig = ''
    options snd slots=snd-hda-intel
  '';

to your /etc/nixos/configuration.nix.

Sometimes, we may want to disable one of intel cards. Here is how to disable first card, but enable the second one.

  boot.extraModprobeConfig = ''
    options snd_hda_intel enable=0,1
  '';

Alternatively you can ...

Disable PC Speaker "audio card"

edit /etc/nixos/configuration.nix and add "snd_pcsp" to boot.blacklistedKernelModules option:

 boot.blacklistedKernelModules = [ "snd_pcsp" ];

Now reboot and retry from the beginning (i.e. check that your real card is shown by alsamixer without using the 'S' key).

Other hardware specific problems

Some hardware specific problems can be resolved by adjusting the options for the sound module. For example, the microphone may be stuck on an unusably low volume. First you should be sure that you have already checked the settings in alsamixer to make sure nothing is muted, and also any physical buttons on your computer (I have twice overlooked the mute button on laptops!).

You should be able to look up the available options for model in HD-Audio-Models.txt. You can try them out interactively as follows:

  1. Close any applications using the sound card
    1. See if any applications are using the sound card
      $ lsof /dev/snd/*
      COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
      pulseaudi 14080 goibhniu 30u CHR 116,7 0t0 5169 /dev/snd/controlC0
      pulseaudi 14080 goibhniu 37u CHR 116,7 0t0 5169 /dev/snd/controlC0
    2. Kill them
      for any process apart from pulseaudio you could just do:
      $ kill -9 14080
      but in the case of pulseaudio you have to prevent it from respawning itself automatically
      $ mkdir -p ~/.config/pulse && echo "autospawn=no" >> ~/.config/pulse/client.conf
      you can then stop pulseaudio with:
      $ pulseaudio -k # or kill it by process id
  2. Unload the snd-hda-intel module
    rmmod snd-hda-intel
  3. Find your model
    grep Codec /proc/asound/card0/codec*
  4. Look up the model options for your card
  5. Try each one
    modprobe snd-hda-intel model=3stack-6ch
  6. Test if this has fixed your problem (tip: aplay and arecord are alsa based command line tools you can use to quickly check)
  7. Repeat until you have exhausted all the options or have fixed your problem
  8. TIDY UP!
    Don't forget to re-enable pulse autospawning by changing "autospawn=no" to "autospawn=yes" in ~/.config/pulse/client.conf

Once you have found a setting that works, you can add it to your configuration file:

  boot.extraModprobeConfig = ''
    options snd-hda-intel model=YOUR_MODEL 
  '';

Much of this is taken from https://help.ubuntu.com/community/HdaIntelSoundHowto which also has additional tips.

PulseAudio

Enabling PulseAudio

In your config file:

hardware.pulseaudio.enable = true

You can check if everything works by using pavucontrol to see the audio streams and to make sure that PulseAudio detects your audio hardware.

Please ensure you have turned off the devices you do not want in the configuration section of pavucontrol.

If (and only if) you enable pulseaudio systemwide with: hardware.pulseaudio.systemWide, you will also need to add your users to the audio group:

$usermod -a -G audio myusername

If a user is not a member the audio group only a dummy device will appear in pavucontrol.

The following command should only show that pulseaudio (and nothing else) is using the sound devices, if you see something like "plugin-container" in the COMMAND column then something is definitely not right, perhaps you have a local ~/.asoundrc which overrides the global alsa settings?.

  $ lsof /dev/snd/*
  COMMAND     PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
  pulseaudi 14080 goibhniu   30u   CHR  116,7      0t0 5169 /dev/snd/controlC0
  pulseaudi 14080 goibhniu   37u   CHR  116,7      0t0 5169 /dev/snd/controlC0

Troubleshooting

Sound is not working after system switch

Recently (01-2016) the pulse device names changed, from simple numbers like 0 to longer descripive strings like alsa_output.pci-0000_00_1b.0.analog-stereo

Your “new” device might be muted, so check that. Also, your shortcuts might not work anymore.

To find out the device name:

pactl info | sed -n 's/default sink: \(.*\)$/\1/p'

and to increase the volume by 2db:

pactl set-sink-volume $(pactl info | sed -n 's/default sink: \(.*\)$/\1/p') +2db

JACK

Using JACK with PulseAudio

Jack Audio Connection Kit is used by most of the serious audio applications on Linux. It provides real-time, low latency connections for both audio and MIDI data between applications that implement its API. NixOS uses the dbus version of JACK2 (jackdbus). This can be used together with pulseaudio with a little configuration. The result is that you don't have to manually hunt down applications which are using the sound device and kill them before starting JACK. You can also continue to use non-JACK aware applications (e.g. flash) at the same time as using JACK applications (e.g. Ardour).

First, you will need to have a pulseaudio build with jack support built in. The relevant line from your configuration.nix may look like

hardware.pulseaudio = {
  enable = true;
  package = pkgs.pulseaudioFull;
  # or use
  # package = pkgs.pulseaudioLight.override { jackaudioSupport = true; };
};

after rebuilding with `nixos-rebuild switch`, you may need to

  1. Load the sequencer and midi kernel modules
    boot.kernelModules = [ "snd-seq" "snd-rawmidi" ];
  1. Configure QjackCtl
    1. Enable jackdbus
      Setup -> Settings tab -> Server Prefix: jackdbus
      Setup -> Misc tab -> Enable D-Bus interface: check

You should now be able to start JACK with QjackCtl, you will notice a new playback and capture device in your sound mixer along with your normal devices.

Kmix-pulseaudio-with-jack.png

Troubleshooting JACK and PulseAudio

$ pactl load-module module-jack-sink channels=2
Failure: Module initalization failed

Check if you have previous settings in ~/.config/jack/conf.xml. Try renaming this file and running the pactl command again.

Otherwise, you may get further info by disabling PulseAudio respawning (see above) and starting it in verbose mode:

$ pulseaudio -vvv

Routing non JACK applications trough JACK without PulseAudio

http://alsa.opensrc.org/Jack_and_Loopback_device_as_Alsa-to-Jack_bridge

In your configuration, put:

kernelModules = [ "snd-aloop" ];


System optimizations for low latency audio with JACK

Some of the following settings, documented in http://wiki.linuxmusicians.com/doku.php?id=system_configuration and https://wiki.archlinux.org/index.php/Pro_Audio can be very helpful to reduce xruns and improve responsiveness and are required for certain programs to run at all e.g. Ardour. The kernelPackages section was taken from https://github.com/rockfabrik/deployment/blob/master/modules/profiles/dj.nix#L32

An easy way of setting all of these, plus a true realtime kernel, is here: https://github.com/musnix/musnix

  boot = {
    kernelModules = [ "snd-seq" "snd-rawmidi" ];
    kernel.sysctl = { "vm.swappiness" = 10; "fs.inotify.max_user_watches" = 524288; };
    kernelParams = [ "threadirq" ];
    kernelPackages = let
    rtKernel = pkgs.linuxPackagesFor (pkgs.linux.override {
      extraConfig = ''
        PREEMPT_RT_FULL? y
        PREEMPT y
        IOSCHED_DEADLINE y
        DEFAULT_DEADLINE y
        DEFAULT_IOSCHED "deadline"
        HPET_TIMER y
        CPU_FREQ n
        TREE_RCU_TRACE n
      '';
    }) pkgs.linuxPackages;
    in rtKernel;

    postBootCommands = ''
      echo 2048 > /sys/class/rtc/rtc0/max_user_freq
      echo 2048 > /proc/sys/dev/hpet/max-user-freq
      setpci -v -d *:* latency_timer=b0
      setpci -v -s $00:1b.0 latency_timer=ff
    '';
    /*The SOUND_CARD_PCI_ID can be obtained like so:*/
    /*$ lspci | grep -i audio*/
  };

  powerManagement.cpuFreqGovernor = "performance";

  fileSystems =
  {
    "/" = { options = "noatime,errors=remount-ro"; };
  };

  security.pam.loginLimits =
  [
    { domain = "@audio"; item = "memlock"; type = "-"; value = "unlimited"; }
    { domain = "@audio"; item = "rtprio"; type = "-"; value = "99"; }
    { domain = "@audio"; item = "nofile"; type = "soft"; value = "99999"; }
    { domain = "@audio"; item = "nofile"; type = "hard"; value = "99999"; }
  ];

  services = {
    udev = {
      packages = [ pkgs.ffado ]; # If you have a FireWire audio interface
      extraRules = ''
        KERNEL=="rtc0", GROUP="audio"
        KERNEL=="hpet", GROUP="audio"
      '';
    };
    cron.enable =false;
  };

  shellInit = ''
    export VST_PATH=/nix/var/nix/profiles/default/lib/vst:/var/run/current-system/sw/lib/vst:~/.vst
    export LXVST_PATH=/nix/var/nix/profiles/default/lib/lxvst:/var/run/current-system/sw/lib/lxvst:~/.lxvst
    export LADSPA_PATH=/nix/var/nix/profiles/default/lib/ladspa:/var/run/current-system/sw/lib/ladspa:~/.ladspa
    export LV2_PATH=/nix/var/nix/profiles/default/lib/lv2:/var/run/current-system/sw/lib/lv2:~/.lv2
    export DSSI_PATH=/nix/var/nix/profiles/default/lib/dssi:/var/run/current-system/sw/lib/dssi:~/.dssi
  '';

  users = {
    extraUsers.yourname= {
      extraGroups = [ "wheel" "audio" ];
    };
  };

A simple script to torture test all ladspa and LV2 plugins

The script below will run every installed plugin in your system trough https://github.com/cth103/plugin-torture and open the results in $EDITOR.

It needs plugin-torture, zsh, urxvt and ladspa-sdk to run. Adapt as needed.

#!/usr/bin/env zsh
# todo:
# check each plugin in a .so: use the -i option to select the index of ladspa plugins
# make it work propery when some plugins in a set ar OK, and some are not.
# make an option to reset the plugin paths
# include the results of lv2bm: if a plugin has a wide range of cpu-usage: remove it.
# make a version that tests jack programs
#
DATE=`date +%d-%m-%Y---%H-%M-%S`
DIR=~/plugin-torture/$DATE

rm -rf ~/plugin-torture/safeLadspa/ &&
rm -rf ~/plugin-torture/safeLV2/ &&
mkdir -p ~/plugin-torture/safeLadspa &&
mkdir -p ~/plugin-torture/safeLV2 &&
mkdir -p $DIR &&

if [ $LADSPA_PATH != ~/plugin-torture/safeLadspa ]; then
  export OLD_LADSPA_PATH=$LADSPA_PATH
fi

if [ $LV2_PATH != ~/plugin-torture/safeLV2 ]; then
  export OLD_LV2_PATH=$LV2_PATH
fi

for plugins  (${(s.:.)OLD_LADSPA_PATH}/*;) {
  analyseplugin $plugins | grep 'Hard Real-Time'
  if [ $? -eq 0 ]; then
    plugin-torture --evil -d -a --ladspa --plugin $plugins | tee >(>> $DIR/ladspa-evil.txt) | grep 'WARNING'
    if [ $? -eq 1 ]; then
     ln -s $plugins ~/plugin-torture/safeLadspa
    fi
  fi
}

for plugins  (${(s.:.)OLD_LV2_PATH}/*;) {
  if [ -f $plugins/manifest.ttl ]; then
    cat $plugins/*.ttl | grep 'hardRTCapable'
    if [ $? -eq 0 ]; then
      plugin-torture --evil -d -a --lv2 --plugin $plugins/manifest.ttl | tee >(>> $DIR/lv2-evil.txt) | grep 'WARNING'
      if [ $? -eq 1 ]; then
         ln -s $plugins ~/plugin-torture/safeLV2
      fi
    fi
  fi
  }

export LADSPA_PATH=~/plugin-torture/safeLadspa
export LV2_PATH=~/plugin-torture/safeLV2

#open
urxvtc -e $EDITOR $DIR/* &

Troubleshooting

Fixing sound in KDE

Tango-dialog-warning.png This article or section is out of date.
Reason: (is this now obsolete? 2011/08) (Discuss)
Tango-dialog-warning.png

(alsamixer shows my audio card by default, but i get no sound in KDE)

  • be sure to explicitly install pkgs.kde44.phonon in /etc/nixos/configuration.nix (environment.kdePackages option) or via nix-env -i

After this step you should be seeing xine and gstreamer backends in the systemsettings / Computer Administration / Multimedia 's Backends tab.

  • If you still see Pulseaudio as the only option in the main tab of systemsettings / Computer Administration / Multimedia, do the following:
 $ rm -rf ~/.pulse/*
 $ killall5 -9 pulseaudio
  • Log out of kde and re-login

Now you should be able to select your audio card, default hw0,0 etc as your audio device in systemsettings / Computer Administration / Multimedia

Test the audio and enjoy the relaxing KDE's arpeggio.