HOWTO Virtual PC Speaker

From Gentoo Linux Wiki

Jump to: navigation, search
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

xset can be used to set your PC speaker bell's pitch, volume, and duration.

man xset

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

Install the beep kernel 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+:

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:


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;

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
Personal tools