Search:  
Gentoo Wiki

TIP_Audio_notifications_in_KDE_3_without_aRts

This article is part of the Tips & Tricks series.
Terminals / Shells Network X Window System Portage System Filesystems Kernel Other

Contents

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.

Prerequisites

You'll need these packages:

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

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" &
                ;;
        * )     # Some audio files are identified as type "data", look at extension.
                case "$(echo "$1" | grep -o '[.][[:alnum:]]*$')" in
                        .ogg )
                                ogg123 -q "$1" &
                                ;;
                        .mp3 )
                                mpg321 -q "$1" &
                                ;;
                        .wav )
                                aplay -q "$1" &
                                ;;
                        * )
                                mplayer -vo null -really-quiet "$1" &
                                ;;
                esac
                ;;
esac

And make it executable

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

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.

The script in C

And, if you insist on speed, here is a C version of the script (indenting is two spaces, so the code fits on the page).

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

int main(int argc, char **argv)
{
  if (argc != 2) {
    printf("Exactly one argument expected.\n");
    return 0;
  }
  char *ext = strrchr(argv[1], '.');
  if (ext == NULL) {
    return 1;
  }
  if (fork() == 0) {
    if (strcmp(ext, "wav") == 0)
      execl("/usr/bin/aplay", "/usr/bin/aplay", "-q", argv[1], NULL);
    else if (strcmp(ext, "ogg") == 0)
      execl("/usr/bin/ogg123", "/usr/bin/ogg123", "-q", argv[1], NULL);
    else if (strcmp(ext, "mp3") == 0)
      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;
}
Retrieved from "http://www.gentoo-wiki.info/TIP_Audio_notifications_in_KDE_3_without_aRts"

Last modified: Fri, 05 Sep 2008 05:52:00 +0000 Hits: 1,995