Fandom

Music Player Daemon Community Wiki

Hack:GoodSleep

525pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

I provide two shell scripts, called wakeup.sh and goodnight.sh, that respectively let you wake up or go to sleep with fading music. While goodnight.sh works on its own, letting you decide how long do you want to listen before starting the fade, you are supposed to add wakeup.sh to cron.

In order to simplify matters a GUI is provided, called goodsleep.mc. To run it, first install BUC from http://buc.opensource.tk/, then run

buc goodsleep.mc

Remember to check that the two scripts have the executable flag, and don't change the script names! For more info and a complete list of options, run

wakeup.sh --help
goodnight.sh --help

Please note that you'll need "bc" to run those scripts

debian/ubuntu: apt-get install -y bc

wakeup.sh Edit

#!/bin/bash

# We set the default variables
startvolume="20"
endvolume="80"
fade="150" # The duration in seconds of the fading
changeplaylist="false"
random="true" # Toggles the random order
remote="false" # If true starts anyremote

remotecfg="$HOME""/Extra/Anyremote/Server-mode/mpd.cfg"
playlist="Sveglia"

# We cycle through the options and modify the variables accordingly

echo "$remotecfg"

displayhelp() {
	echo "Usage: wakeup.sh [OPTIONS]..."
	echo "Starts MPD with a gentle fading, which is useful to wake up nicely."
	echo "It should be added to cron to wake up at a fixed time."
	echo "By default it activates random playing in MPD (see below)."
	echo ""
	echo -e "-s, --startvolume \t\t VOL sets the initial volume to VOL (default=20)"
	echo -e "-e, --endvolume VOL \t\t sets the final volume to VOL (default=80)"
	echo -e "-f, --fade TIME \t\t sets the fading duration to TIME seconds (default=150)"
	echo -e "-p, --playlist PLAYLIST \t before starting MPD, replaces the current playlist with PLAYLIST. If no PLAYLIST is given, it uses the default \"Sveglia\""
	echo -e "-r, --remote CFG \t\t before starting MPD, activates anyremote with the config file CFG. If no CFG is given, it uses the default \$HOME/Extra/Anyremote/Server-mode/mpd.cfg"
	echo -e "-n, --norandom \t\t\t deactivates the MPD random mode"
	echo -e "-h, --help \t\t\t displays this help and exits"
}

while [ "$1" != "" ]; do
	case "$1" in
		-h | --help )
			displayhelp
			exit;;
		-s | --startvolume )
			shift
			startvolume="$1"
			shift;;
		-e | --endvolume )
			shift
			endvolume="$1"
			shift;;
		-f | --fade )
			shift
			fade="$1"
			shift;;
		-p | --playlist )
			shift
			changeplaylist="true"
			if [ ! -z "$1" ] && [ "$(echo "$1" | cut -b 1 )" != "-" ]; then
				playlist="$1"
				shift
			fi;;
		-r | --remote )
			shift
			remote="true"
			if [ ! -z "$1" ] && [ "$(echo "$1" | cut -b 1 )" != "-" ]; then
				remotecfg="$1"
				shift
			fi;;
		-n | --norandom )
			shift
			random="false";;
		* )
			shift;;
		esac
done

deltavol=$[endvolume-startvolume]
echo "$deltavol"
step=$(echo "scale=4; $fade/($deltavol)" | bc)

if [ $changeplaylist = "true" ]; then
	mpc clear
	mpc load "$playlist"
fi

if [ $random = "true" ]; then
	mpc random on
else
	mpc random off
fi

if [ "$remote" = "true" ]; then
	anyremote -f "$remotecfg" &
fi

mpc volume $startvolume
mpc play
i="0"
while [ "$i" -lt "$deltavol" ]; do
	sleep "$step"
	mpc volume +1
	i=$[i+1]
done

goodnight.sh Edit

#!/bin/bash

# We set the default variables
startvolume="60"
listeningtime="30" # How long to play music before starting to fade (in minutes)
fade="150" # The duration in seconds of the fading
changeplaylist="false"
random="true" # Toggles the random order
remote="false" # If true starts anyremote

remotecfg="$HOME""/Extra/Anyremote/Server-mode/mpd.cfg"
playlist="Sveglia"

displayhelp() {
	echo "Usage: goodnight.sh [OPTIONS]..."
	echo "Stops MPD with a gentle fading, which is useful to fall asleep nicely."
	echo "Before beginning to fade, it plays music for a fixed time."
	echo "By default it activates random playing in MPD (see below)."
	echo ""
	echo -e "-s, --startvolume \t\t VOL sets the initial volume to VOL (default=60)"
	echo -e "-f, --fade TIME \t\t sets the fading duration to TIME seconds (default=150)"
	echo -e "-l, --listen TIME \t\t sets the amount of time before starting to fade to TIME minutes (default=30)"
	echo -e "-p, --playlist PLAYLIST \t before starting MPD, replaces the current playlist with PLAYLIST. If no PLAYLIST is given, it uses the default \"Sveglia\""
	echo -e "-r, --remote CFG \t\t before starting MPD, activates anyremote with the config file CFG. If no CFG is given, it uses the default \$HOME/Extra/Anyremote/Server-mode/mpd.cfg"
	echo -e "-n, --norandom \t\t\t deactivates the MPD random mode"
	echo -e "-h, --help \t\t\t displays this help and exits"
}

# We cycle through the options and modify the variables accordingly

while [ "$1" != "" ]; do
	case "$1" in
		-h | --help )
			displayhelp
			exit;;
		-s | --startvolume )
			shift
			startvolume="$1"
			shift;;
		-f | --fade )
			shift
			fade="$1"
			shift;;
		-l | --listen )
			shift
			listeningtime="$1"
			shift;;
		-p | --playlist )
			shift
			changeplaylist="true"
			if [ ! -z "$1" ] && [ "$(echo "$1" | cut -b 1 )" != "-" ]; then
				playlist="$1"
				shift
			fi;;
		-r | --remote )
			shift
			remote="true"
			if [ ! -z "$1" ] && [ "$(echo "$1" | cut -b 1 )" != "-" ]; then
				remotecfg="$1"
				shift
			fi;;
		-n | --norandom )
			shift
			random="false";;
		* )
			shift;;
		esac
done

step=$(echo "scale=4; $fade/($startvolume)" | bc)

if [ "$changeplaylist" = "true" ]; then
	mpc clear
	mpc load "$playlist"
fi

if [ "$random" = "true" ]; then
	mpc random on
else
	mpc random off
fi

mpc volume "$startvolume"
mpc play
sleep $[ 60*$listeningtime ]

i="0"
while [ "$i" -lt "$startvolume" ]; do
	sleep "$step"
	mpc volume -1
	i=$[i+1]
done
mpc stop

goodsleep.mc Edit

 <?xml version="1.0"?>
<config>

	<global var="workingdir">
		dir1="$( locate -l 1 goodnight.sh | sed -e 's\goodnight.sh.*\\' )"
		dir2="$( locate -l 1 wakeup.sh | sed -e 's\wakeup.sh.*\\' )"
		if [ "$dir1" = "$dir2" ]
			then echo "$dir1"
			else echo "ERROR"
		fi
	</global>
	<global var="language">
		temp="$(locale | grep LANG= )"
		echo "$temp" | sed -e 's/.*=//' -e 's/_.*//'
	</global>

	<global var="title1">
		case "$language" in
		it) echo "Sveglia";;
		*) echo "Wake up";;
		esac
	</global>
	<global var="title2">
		case "$language" in
			it) echo "Buonanotte";;
			*) echo "Goodnight";;
		esac
	</global>

	<global var="string1">
		case "$language" in
			it) echo "Scegli la playlist (lascia vuoto per mantenere la playlist corrente)";;
			*) echo "Choose the playlist (leave empty to keep the current playlist)";;
		esac
	</global>
	<global var="string2">
		case "$language" in
			it) echo "Sveglia alle ore";;
			*) echo "Wake up at";;
		esac
	</global>
	<global var="string3">
		case "$language" in
			it) echo "Volume iniziale";;
			*) echo "Start volume";;
		esac
	</global>
	<global var="string4">
		case "$language" in
			it) echo "Volume finale";;
			*) echo "Final volume";;
		esac
	</global>
	<global var="string5">
		case "$language" in
			it) echo "Lascia in bianco i campi seguenti per utilizzare le scelte di default";;
			*) echo "Leave the following fields blank to use the defaults choices";;
		esac
	</global>
	<global var="string6">
		case "$language" in
			it) echo "Durata del fade in secondi";;
			*) echo "Fade duration in seconds";;
		esac
	</global>
	<global var="string7">
		case "$language" in
			it) echo "Quanto tempo vuoi ascoltare la musica in minuti";;
			*) echo "How long do you want to listen to music in minutes";;
		esac
	</global>
	<global var="string8">
		case "$language" in
			it) echo "Non ho trovato i file goodnight.sh e wakeup.sh. Controlla di avere questi file nella stessa cartella.";;
			*) echo "I didn't find the files goodnight.sh and wakeup.sh. Check that you have these files in the same folder.";;
		esac
	</global>
	<global var="string9">
		case "$language" in
			it) echo "Modalità casuale";;
			*) echo "Random mode";;
		esac
	</global>

	<tab>
		<title>
			echo "$title1"
		</title>
	
		<label>
			echo "$string1"
		</label>
		<combobox var="playlist">
			echo " "
			mpc lsplaylists
		</combobox>

		<label>
			echo "$string5"
		</label>
		<label>
			echo "$string3 (default 20)"
		</label>
		<text var="startvolume">
			echo ""
		</text>
		<label>
			echo "$string4 (default 80)"
		</label>
		<text var="endvolume">
			echo ""
		</text>
		<label>
			echo "$string6 (default 150)"
		</label>
		<text var="fadeduration">
			echo ""
		</text>

		<label>
			echo "$string9"
		</label>
		<combobox var="randommode">
			echo "On"
			echo "Off"
		</combobox>
	
		<label>
			echo "$string2"
		</label>
		<combobox var="hours">
			for i in $(seq 0 9)
				do echo "0$i"
			done
			seq 10 23
		</combobox>
		<combobox var="minutes">
			echo "00"
			echo "05"
			seq 10 5 55
		</combobox>
	
		<button title="OK">
			if [ "$playlist" != " " ]
				then play="-p \"$playlist\""
				else play=""
			fi
			if [ -z "$startvolume" ]
				then start=""
				else start="-s $startvolume"
			fi
			if [ -z "$endvolume" ]
				then end=""
				else end="-e $endvolume"
			fi
			if [ -z "$fadeduration" ]
				then fade=""
				else fade="-f $fadeduration"
			fi
			if [ "$randommode" = "On" ]
				then random=""
				else random="-n"
			fi
			if [ "$(locate "Server-mode/mpd.cfg" )" ]
				then remote="-r \"$(locate "Server-mode/mpd.cfg" )\""
				else remote=""
			fi
			command="$(echo "$workingdir""wakeup.sh" "$play" "$start" "$end" "$fade" "$random" "$remote")"
			if [ "$workingdir" != "ERROR" ]
				then echo "$command" | at "$hours:$minutes"
				else zenity --error --text="$string8"
			fi
			rm auxiliaryfile
		</button>
	</tab>

	<tab>
		<title>
			echo "$title2"
		</title>
	
		<label>
			echo "$string1"
		</label>
		<combobox var="playlist1">
			echo " "
			mpc lsplaylists
		</combobox>

		<label>
			echo "$string5"
		</label>
		<label>
			echo "$string3 (default 60)"
		</label>
		<text var="startvolume1">
			echo ""
		</text>
		<label>
			echo "$string7 (default 30)"
		</label>
		<text var="listeningtime">
			echo ""
		</text>
		<label>
			echo "$string6 (default 150)"
		</label>
		<text var="fadeduration1">
			echo ""
		</text>
	
		<label>
			echo "$string9"
		</label>
		<combobox var="randommode1">
			echo "On"
			echo "Off"
		</combobox>
	
		<button title="OK">
			if [ "$playlist1" != " " ]
				then play1="-p $playlist1"
				else play1=""
			fi
			if [ -z "$startvolume1" ]
				then start1=""
				else start1="-s $startvolume1"
			fi
			if [ -z "$listeningtime" ]
				then listen=""
				else listen="-l $listeningtime"
			fi
			if [ -z "$fadeduration1" ]
				then fade1=""
				else fade1="-f $fadeduration1"
			fi
			if [ "$randommode1" = "On" ]
				then random1=""
				else random1="-n"
			fi
			if [ "$(locate "Server-mode/mpd.cfg" )" ]
				then remote="-r \"$(locate "Server-mode/mpd.cfg" )\""
				else remote=""
			fi
			if [ "$workingdir" != "ERROR" ]
				then "$workingdir""goodnight.sh" $play1 $start1 $listen $fade1 $random1 $remote &
				else zenity --error --text="$string8"
			fi
		</button>
	</tab>
</config>

Also on Fandom

Random Wiki