diyAudio logo

LINUX Audio Tweaks


Changelog: Last edited 12/04/08 soundcheck


To improve audio performance it is recommended to reduce system activities to a minimum.
You can further play with some kernel parameters to improve sound by limiting jitter effects.
Below you'll find a set off actions you might want to run before you run your Audio application.


Contents:

1. Basics
2. How to setup a RAMDISK
3. How to mount your Windows partition read/write
4. How to setup your environment
5. How to setup ALSA/USB - Some hints
6. Turning off your monitor and graphic adapter
7. How to change your processor frequency and governors manually
8. How to apply realtime priorities to an application

1. Basics



Basic hints, that makes hacker live much easier:

1. Run all activities and commands as root from an X-terminal ( sys admin privileges)
What to do to avoid typing "sudo" all the time - just enter:
$ sudo su
a the prompt will show "root" privileges.

NOTE: Running as root over a longer period in a terminal bears a security risk. If you have confidential
data on the system you might want to run sudo all the time. However sudo is not the same as root.
To apply certain tweaks mentioned below you need to be root.

Q: How to manage a X-terminal
1. Ubuntu: Under Applications/Accessories you'll find an application called Terminal - Open this.
2. From that "Terminal" you can start $gnome-terminal which is a 2nd option
3. Now you could start a 2nd terminal e.g. to get to root privileges by typing $ sudo gnome-terminal
4. If you havn't started the 2nd terminal as a background-process to have the 1st terminal still accessable, you can type
CTRL-Z in the first terminal ( which stops the foreground process in the Terminal 1) and you enter $ bg in terminal 1.
This changes terminal 2 to a background process. (This applies of course to any other application you start from a terminal)




2. Copy and paste Command lines
If you want to copy below command lines, you
copy them by marking them with the known CTRL-C.
Pasting it into the X-terminal works by pushing the left and right mouse button at the same time

With the newest Gnome Ubuntu Intrepid you can use also Drag & Drop the marked area.


2. How to setup a RAMDISK



For best playback results I recommend to put your data on a Ramdisk.
You should have at least 2GB of RAM available to be able to store a complete CD on the ramdisk.

There is actually no need to setup a file system to be able work with a ram-disk.
It already exists as /dev/shm. It'll be 50% of your RAM max. You can just copy your files over there
and play it from there.

The load time for e.g. 800MB varies from approx. 4-15s depending on your system
Copying and playing all your tracks to/from RAMDISK, will again improve your sound quality.

To avoid activities by other programs storing stuff on /tmp on the harddrive during playback you can also make
/tmp a ramdisk by typing following commands as root.

user$ sudo su

root$ echo "tmpfs /tmp tmpfs defaults,size=1028m 0 0" >> /etc/fstab

This will give you a 1GB ramdisk. Change the 1028kB according to your ram size.


3. How to mount your Windows NTFS partition read/write



NTFS partitions are mounted read-olny by default .

You can have a look in your /etc/fstab by starting a terminal and typing:

$ less /etc/fstab

Your windows partition, usually (/dev/sda1) is mounted as type ntfs by default after installation.

Older systems with fat32 filesystems are mounted as e.g. /dev/hda. These do have read-write access.

The problem here is that you can not write on the ntfs partition by default.

You need to have the package ntfs-3g installed for getting write access. It is actually installed by default.

Ubuntu Intrepid:

1. The required ntfs-3g package part of distribution already, but if you need to install it do following

$ sudo apt-get install ntfs-3g

2. replace in /etc/fstab the string "ntfs" with "ntfs-3g" on your windows partition, by starting an editor as root e.g.

root$ gedit /etc/fstab

< replace ntfs with ntfs-3g within the respective Windows mount section and save it >

e.g.

/dev/sda1 /media/windows ntfs-3g rw 0 0

6. reboot or
sudo umount /media/windows && sudo mount /media/windows

7. To check if it works do following:

$ sudo touch /media/sda1/test
$ ls -l /media/sda1/test

Now it should print a file with 0 size.
Clean it up.

$ rm /media/sda1/test

4. How to setup your environment


Below you find some Linux tweaks to optimize your environment and PC.
I'd recommend to try each of them line by line first to make sure it's working.
I won't explain what I am doing - perhaps - later on. Put the command in Google
and you'll find an answer.

Install powertop first:

$ sudp apt-get install powertop
root$ powertop

Run it. If you run your PC off mains in case you own a notebook you'll get power-consumption values listed

Powertop itself gives you some hints to improve the system performance. If you run powertop you'should see quite less activities on the PC.


I recommend to put all those commands below in a script which you start as root
before you start your favorite audio-application.

Or you put certain strings into /etc/rc.local. If being put there they will be started right at system boot.

Lets call the script "audio-prep".

$ cd ~
$ touch audio-prep
$ gedit audio-prep

copy below lines in the editor - SWAP (find/replace) HASH with the sign hash!!!!!!!!
you could check each line manually to see if it works, just leave out " > /dev/null 2>&1" to see the output of the command at the commandline
NOT all tweask make sense for your setup PC

HASH!/bin/sh

checkroot () {

if [ "$UID" -ne 0 ]
then
echo "Must be root to run this script..."
exit 0
fi
}

prepenvironment () {

echo -n "Preparing environment..."
echo 4096 > /proc/sys/dev/rtc/max-user-freq > /dev/null 2>&1
echo 1500 > /proc/sys/vm/dirty_writeback_centisecs > /dev/null 2>&1
echo min_power > /sys/class/scsi_host/host0/link_power_management_policy > /dev/null 2>&1
echo ondemand > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor > /dev/null 2>&1
echo 5 > /proc/sys/vm/laptop_mode > /dev/null 2>&1
echo 1 > /proc/sys/kernel/sched_compat_yield > /dev/null 2>&1
ethtool -s eth0 wol d # > /dev/null 2>&1
HASH cpufreq-set -f XXXXXX > /dev/null 2>&1
hdparm -M 128 /dev/sda > /dev/null 2>&1
hdparm -B 32 /dev/sda > /dev/null 2>&1
mount -o remount,relatime / > /dev/null 2>&1
hal-disable-polling --device /dev/cdromc ifconfig eth0 down > /dev/null 2>&1
HASH echo "suspend" >/sys/bus/usb/devices/usb1/power/level > /dev/null 2>&1 # Choose the best USB port soundwise
HASH echo "suspend" >/sys/bus/usb/devices/usb2/power/level > /dev/null 2>&1 # Shutdown the other ports
HASH echo "suspend" >/sys/bus/usb/devices/usb3/power/level > /dev/null 2>&1 # lsusb will show you your port
HASH echo "suspend" >/sys/bus/usb/devices/usb5/power/level > /dev/null 2>&1

HASH sysctl -w kernel.sched_batch_wakeup_granularity_ns=10 # for rt-kernel
HASH sysctl -w kernel.sched_latency_ns=100000 # for rt-kernel
HASH sysctl -w kernel.sched_min_granularity_ns=100000 # for rt-kernel
HASH sysctl -w kernel.sched_wakeup_granularity_ns=5 # for rt-kernel
}

stopproc () {
/bin/echo -n "Stopping all relevant deamons, processes and turning swap off..."
swapoff -a # > /dev/null 2>&1
/etc/init.d/apmd stop # > /dev/null 2>&1
HASH/etc/init.d/networking stop # > /dev/null 2>&1
/etc/init.d/cron stop # > /dev/null 2>&1
/etc/init.d/klogd stop # > /dev/null 2>&1
/etc/init.d/sysklogd stop # > /dev/null 2>&1
/etc/init.d/acpid stop # > /dev/null 2>&1
/etc/init.d/cupsys stop # > /dev/null 2>&1
/etc/init.d/atd stop > # /dev/null 2>&1
killall update-notifier # > /dev/null 2>&1
killall gnome-screensaver # > /dev/null 2>&1
killall gnome-power-manager # > /dev/null 2>&1
killall gnome-volume-manager # > /dev/null 2>&1
/bin/echo "....done"
}


checkroot
prepenvironment
stopproc

exit 0



save & exit


$ sudo cp audio-prep /usr/local/bin
$ sudo chmod 755 /usr/local/bin/aprep

RUN with:

$ sudo audio-prep

Of course you could also start audio-prep at bootup, if you put the line (prior to the exit 0 line)

/usr/local/bin/audio-prep

into /etc/rc.local

5. How to tweak ALSA USB


Alsa comes with a mixer called DMIX, which is soundwise not any better than the well known Microsoft Mixer.

The great thing under Linux -- You can directly access your soundcards. There is no need to have ASIO or KernelStreaming?
or similar. This is one of the biggest advantages from OSX and Linux over MS products

Your application can directly access the sound-device, usually called hw:0,0 or hw:1,0 (alernatively plughw0 or plughw1, which is the 2nd best choice).

a. To make sure that the usb-sound card always gets a fixed index e.g. 0 "hw:0" the default index if you want to run your USB card as default card
b. nrpacks limits the data-packages to one per URB (USB request block), which leads to much less jitter

$ sudo gedit /etc/modprobe.d/alsa-base


search for snd-usb-audio change line according to below


options snd-usb-audio index=0 nrpacks=1


save it


That's mainly it.

Another big step forward is patching the usb-drivers slightly. Have a look a the manual
ALSA installtion WIKI over here.


I experienced quite some improvements by turning the monitor and graphic card off during playback.

Note:The known command $xset dpms off switches the monitor not completely off!

Just run from another terminal:

$ sudo vbetool dpms off

now a bit of blind typing:

$ sudo vbetool dpms on

I created some aliases in my ~/.bashrc to have access to self defined shortcuts, such as "vbe" for screen-on at any time

E.g. add the two lines to the bottom of ~/.bashrc

$echo "alias vbon='vbetool dpms on'" >> ~/.bashrc
$echo "alias vboff='vbetool dpms off'" >> ~/.bashrc


You need to open a new terminal:

Type $vboff (turns your screen off) $vbon (turns it back on)


7. How to change your processor frequency and governors manually


I experienced differences in sound quality when increasing the processor clock.
Even if the processor is running at low utilization, it'll have a positive impact to increase
the clock.
The drawbacks are more heat, thus more fan activity, and higher power consumption.

In my case I realized that the processor was running at 1000Mhz, I changed it
to the maximum 2160Mhz, which was generating lots of heat.
I finally ended up at 1670Mhz, being a good compromise between heat/noise and sound-quality.
This way I can run the PC for at least ten minutes without fan or continuously on
the lowest fan speed

What to you might want to try:

You need to download and install cpufrequtils first.

$ sudo apt-get install cpufrequtils

$ sudo cpufreq-info

It'll show you all frequencies you're able to set

$ sudo cpufreq-set -f xxxx

will set the frequency accordingly.

You can also set different governors such as powersave, ondemand, performance

In earlier described script you can find the command how to change it.
Performance will set the processors to maximum performance and powersave to minimum.


Feel free to contact me if you want to add more hints.

8. How to apply realtime priorities to an application


Assuming you got a realtime kernel installed it is quite easy to apply rt-priorities to a process.
Of course best would be if the application itself would deliver a well optimised scheduler setup.
Unfortunately none of the standard applications, such as Amarok or Audacious is offering such a
feature. Good old XMMS, ecasound or brutefir as well as Jack are providing rt-kernel support.
However with small command you can assign rt-priorities to a process.

Just do:

$ sudo chrt -f -p 99 audacious

This puts audacious on rt-prio 99 - the highest possible - and -f stands for using the sched-fifo scheduler


END