HomePhabricator
Script to play a random sound at KDE login

While I was waiting for one of my servers to perform a test backup operation I was opened my music folder to find something to play. In the root of my music folder, usually ignored, was the a file named Win95-startup-sound.mp3, for a little bit of fun I decided to play it. The fun didn't end when the sound finished.

If you're not already aware, I no longer run any version of Windows as my operating system. I run Ubuntu with KDE as my desktop environment, but my default configuration didn't include a log in or log off sound. So I thought it might be a bit of fun to play the Windows 95 start up sound when I logged in for old time sake. The old sound reminds me of I was a kid starting up mum's laptop to play Windows CD-ROM games because our 486DX100 was only running DOS and Windows 3.1 and only had floppy drives at the time.

Rather than just playing the one sound on login every time I thought it might be nice to play a random login sound from my computing history. KDE lets you run a command on login and it is easy to configure (i.e. you don't need to modify configuration files to get it to work). So I wrote a simple bash script to play a random .wav or .ogg file from a folder.

The script

randomstartupsound.sh
#! /bin/bash

SOUND_FOLDER=randomstartup

#If aparameter is 
if [ ! "$1" = "" ] ; then
	SOUND_FOLDER=$1
fi

#If the path is not absolute then try to find it in the current working directory.
#otherwise try from the script directory.

#Is the path relative?
if [[ ! "$SOUND_FOLDER" = /* ]]; then
	#If it isn't available from the current working directory...
	if [ ! -d "$SOUND_FOLDER" ]; then
		#Try and find it relative to the script.
		SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
		if [ -d "$SCRIPT_DIR/$SOUND_FOLDER" ]; then
			SOUND_FOLDER="$SCRIPT_DIR/$SOUND_FOLDER"
		else
			echo "Error: Folder \"$SOUND_FOLDER\" not found."
			exit ;
		fi
	fi
fi

#Calculate the number of files in the directory.
NUMBER_OF_FILES=`ls $SOUND_FOLDER/*.wav $SOUND_FOLDER/*.ogg -l | wc -l`
#Select a random index.
let "INDEX=$RANDOM % $NUMBER_OF_FILES"

file_to_play=0

#Loop through the files until the file_to_play count is equal to the index.
for f in $SOUND_FOLDER/*.wav $SOUND_FOLDER/*.ogg
do
	if [ $file_to_play -eq $INDEX ]; then
		echo "I will play $f"
		paplay $f
	fi
	let "file_to_play=$file_to_play+1"
done

Make sure your script is executable. If you're not sure, open a terminal where you saved the script and type chmod +x randomstartupsound.sh

Create a folder next to the script, in our case it is named randomstartup and put any number of wav of ogg files you like in it. I "borrowed" files from winhistory.de and also copied the KDE login sounds from /usr/share/sounds/.

You can either pass the folder name to the script or have a folder named randomstartup sitting next to the script. The script has some checking to try to determine if the folder is an absolute path, if it is in the current working directory or sitting next to the script.

Open your KDE system settings and open the Application and System Notifications section:

Notifications

In Manage Notifications on the Applications tab, change the Event source: drop down to KDE Workspace.

In the Run command section type the path to your saved script (or use the browse button to find it). You can optionally include the folder name as illustrated in the image. The image is specifying the default so it makes no difference in this case.

Login Notification Location

You obviously don't need to limit the files to Windows start up sounds for a bit of fun. Anything that has a positive memory or that you enjoy now would be great. I would avoid those that have bad memories associated with it.

You can easily modify the script to play more than wav or ogg files. I chose to only use wav and ogg files as all of the sounds I wanted to randomise were in those formats.

Written by 0xseantasker on May 19 2015, 11:09 PM.
User
Projects
None
Subscribers
None

Event Timeline