Search:  
Gentoo Wiki

HOWTO_Virtual_PC_Speaker


This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Contents

Overview

This page contains instructions for configuring a virtual PC speaker for Gentoo Linux. Instead of playing a beep via a PC's built in speaker when requested, a virtual PC speaker plays a sound file via a regular audio card. This is useful if:

  1. You prefer to play a sound file instead of the normal beep.
  2. Your computer does not have a built in speaker for console beeps and bells.

Related and alternative solutions

ALSA PCM PC Speaker

The ALSA PCM PC driver which makes a normal PC speaker play sound files instead of just beeps.

See:

Visual Bell

In Gnome:

Enable: Desktop > Preferences > Sound > System Bell > Visual feedback

No Bell

See:

Customized Bell in Xorg

Adjusting the default pitch / volume

If you have a working PC speaker, and only want to adjust the volume / pitch:

xset b <volume%> <frequency_hz> <duration_ms>

Using xbelld

If you don't have a PC speaker (or simply want to customise it beyond your hardware limitations), then you can try using xbelld. This can emulate the PC speaker beep via your sound card (using ALSA) , and let you adjust the volume, frequency and duration. (It can also natively play WAVE files, or run an external command if you so desire).

Unfortunately xbelld is not yet in portage (2008-04-13). But I have submitted an ebuild here. Now once you've emerged xbelld (preferably with the alsa USE flag), put

xbelld -bt50 -F400 -v127 -d100

in ~/.xinitrc. You can control the duration, frequency and volume using the -d, -F and -v options. Alternately you can play WAVE files, or execute external programs using the following forms:

xbelld -bcf beep.wav
xbelld -be aplay -q beep.wav

See

man xbelld

for a complete list of options.

Using xkbevd

xkbevd can be used to run an external command every time the X bell rings.

emerge xkbevd

And put the following in your ~/.xkb/xkbevd.cf

soundDirectory	= ""
soundCmd	= "aplay -q"

Bell() 			"~/etc/beep.wav"
Bell(ImAlive)		"/usr/share/sounds/purple/login.wav"

Then do

xkbevd -bg

from an open terminal, or in your ~/.xinitrc. For further options, see

man xkbevd

(There's also the example.cf file in the source archive, which didn't make it into the distribution)

Background

This solution is based on these sources:

Instructions

Optionally, disable the PC speaker driver

To disable the PC speaker driver, set your kernel configuration as indicated:

Linux Kernel Configuration:
Device Drivers  --->
  Input device support  --->
    Generic input layer  --->
      Micellaneous devices -->
        <N> PC Speaker support
Note: Enable pcspkr when using /dev/input/by-path/platform-pcspkr-event-spkr

Install the beep kernel module (Only needed if you answered no to pcspkr module)

Download, build, and install the beep.ko kernel module.

For pre-kernel 2.6.15:

wget http://www.carcosa.net/jason/software/beep/beep-2.6.x.tar.gz
tar -vzxf beep-2.6.x.tar.gz

For kernel 2.6.15+:

Warning: This will not compile on kernels 2.6.24 and later
Note: You may need to add #include <linux/sched.h> to the kernel includes section of the beep.c file so it will compile successfully with the make command.
wget http://www.carcosa.net/jason/software/beep/beep-2.6.15+.tar.gz
tar -vzxf beep-2.6.15+.tar.gz

Then:

cd beep-2.6.x
make
mkdir -p /lib/modules/`uname -r`/kernel/drivers/input/misc
install -m 644 beep.ko /lib/modules/`uname -r`/kernel/drivers/input/misc/beep.ko
depmod -a
modprobe beep

The installation part will need to be redone each time you recompile your kernel.

Install the init file

Create the /etc/init.d/beepd file:

File: /etc/init.d/beepd
#!/sbin/runscript
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

depend() {
    after alsasound
}

start() {
    ebegin "Starting beepd"
    start-stop-daemon --start --quiet --background \
       --pidfile /var/run/beepd.pid --make-pidfile \
       --exec /usr/local/sbin/beepd_aplay.plx ${BEEPD_FILE}
    eend $?
}

stop() {
    ebegin "Stopping beepd"
    start-stop-daemon --stop --quiet --pidfile /var/run/beepd.pid
    eend $?
}

Chmod it appropriately:

chmod a+rx /etc/init.d/beepd

Install the daemon

Create the /usr/local/sbin/beepd_aplay.plx file:

If you are using the beep module

File: /usr/local/sbin/beepd_aplay.plx
 #!/usr/bin/perl
 # beepd_aplay.plx - originally by Frank Johnson <ratty@they.org>
 #
 # Usage: beepd_aplay.plx [something.wav]
 #    beepd_aplay.plx reads from your /dev/beep device and plays a sound
 #    every time you would normally hear a console beep
 #

 $file = $ARGV[0] || "_YOUR_DEFAULT_HERE_";

 die "$file is not a file\n" if (! -f $file);

 my $trysI  = 5;
 
 while (--$trysI && !open(BEEP,"/dev/beep")) {
     sleep 1;
 }
 
 $trysI || die "Can't open /dev/beep: $m\n";

 while (1) {
     if (read(BEEP,$a,1)) {
        while (waitpid(-1, WNOHANG) != -1) {}  # Reap zombies

        fork || exec ("/usr/bin/aplay", "$file");
     }
 }

 close BEEP;

If you are using pcspkr

File: /usr/local/sbin/beepd_aplay.plx
 #!/usr/bin/perl
 # beepd_aplay.plx - originally by Frank Johnson <ratty@they.org>
 #
 # Usage: beepd_aplay.plx [something.wav]
 #    beepd_aplay.plx reads from your /dev/beep device and plays a sound
 #    every time you would normally hear a console beep
 #

 $file = $ARGV[0] || "_YOUR_DEFAULT_HERE_";

 die "$file is not a file\n" if (! -f $file);

 my $trysI  = 5;
 
 while (--$trysI && !open(BEEP,"/dev/input/by-path/platform-pcspkr-event-spkr")) {
     sleep 1;
 }
 
 $trysI || die "Can't open /dev/beep: $m\n";

 while (1) {
     if (read(BEEP,$a,48)) {
        while (waitpid(-1, WNOHANG) != -1) {}  # Reap zombies

        fork || exec ("/usr/bin/aplay", "$file");
     }
 }

 close BEEP;

Chmod it appropriately:

chmod a+rx /usr/local/sbin/beepd_aplay.plx

Create a configuration file

Create the /etc/conf.d/beepd file:

File: /etc/conf.d/beepd
BEEPD_FILE="/usr/share/sounds/generic.wav"

Change the sound file to your liking.

Sources of sound files

If needed, make sure root has a .asoundrc

If you normally, use a .asoundrc file. Make sure root has a copy in root's home directory.

Test ALSA as root

Make sure ALSA will play the sound file as root:

aplay /usr/share/sounds/generic.wav

Start and test the daemon

/etc/init.d/beepd start
/bin/echo -e '\a'

Set beepd to be launched on system startup

rc-update add beepd default

Add the beep module to /etc/modules.autoload.d/kernel-2.6:

File: /etc/modules.autoload.d/kernel-2.6
beep
Retrieved from "http://www.gentoo-wiki.info/HOWTO_Virtual_PC_Speaker"

Last modified: Fri, 05 Sep 2008 06:23:00 +0000 Hits: 20,400