TIP Audio notifications in KDE 3 without aRts

From Gentoo Linux Wiki

Jump to: navigation, search
This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other

Contents

[edit] Introduction

The K Desktop Environment supports audio notifications without the use of the aRts sound system, by specifying a custom program to play audio files. This program must take one argument - the name of the file and return immediately after being called. This tip shows a simple script that plays various audio files and how to use it for audio notifications in KDE 3.

[edit] Prerequisites

You'll need these packages:

  1. media-sound/mpg123 - for playing mp3 files
  2. media-sound/vorbis-tools - for playing ogg files
  3. media-sound/alsa-utils - for wav files
  4. media-video/mplayer - for anything else (used as a just-in-case fallback)

[edit] The script

Put this script somewhere in your $PATH (like /usr/local/bin).

File: /usr/local/bin/play_play.sh
#!/bin/sh
if [ $# -ne 1 ]; then
        echo "Exactly one argument expected."
        exit 1
fi

type=$(file -b "$1")
case "$type" in
        *Vorbis* )
                ogg123 -q "$1" &
                ;;
        *MP3* )
                mpg123 -q "$1" &
                ;;
        *WAVE* )
                aplay -q "$1" &
                ;;
        * )
                mplayer -vo null -really-quiet "$1" &
                ;;
esac

And make it executable

chmod a+x /usr/local/bin/play_play.sh

[edit] Configuring KDE

Open Control Centre. In the tree on the left-hand side, expand Sound & Multimedia and click on System Notifications. In the bottom-right of the window, click on the Player Settings button; in the newly opened dialog, choose the Use an external player radio button and type play_play.sh in the text-box beneath. Click OK. You can now enable and enjoy audio notifications in KDE without needing aRts.

[edit] The script in C

And, for absolutely no reason whatsoever, here is a C version of the script.

File: fork_play.c
#include <unistd.h>
#include <stdio.h>
#include <string.h>

char extension_is(const char *fname, const char *ext)
{
        size_t fnl = strlen(fname);
        int last_dot;
        for (last_dot=fnl-1;last_dot>=0;last_dot--)
                if (fname[last_dot] == '.')
                        break;

        if (last_dot == -1 || last_dot == fnl-1)
                return 0;

        size_t el = strlen(ext);
        if (el != fnl - last_dot - 1)
                return 0;

        return (strncmp(&(fname[last_dot+1]),ext,el) == 0);
}

int main(int args, char **argv)
{
        pid_t pid = fork();

        if (pid == 0) {
                if (extension_is(argv[1],"wav"))
                        execl("/usr/bin/play","/usr/bin/play","-q",argv[1],NULL);
                else if (extension_is(argv[1],"ogg"))
                        execl("/usr/bin/ogg123","/usr/bin/ogg123","-q",argv[1],NULL);
                else if (extension_is(argv[1],"mp3"))
                        execl("/usr/bin/mpg123","/usr/bin/mpg123","-q",argv[1],NULL);
                else
                        execl("/usr/bin/mplayer","/usr/bin/mplayer","-vo","null","-really-quiet",argv[1],NULL);
        }

        return 0;
}
Personal tools