Music Player Daemon Community Wiki


525pages on
this wiki
Add New Page
Add New Page Talk1

The script can be used to play an alarm at a specific time (for instance at the start of the lunch break).

This script does the following:

  • wait untill the current song has finished
  • play a specific mp3 file (for instance: alarm.mp3) at maximum volume
  • restore volume level and continue playing the songs in the playlist

The script is intended to be started from crontab. As this script uses the bash shell and crontab uses the sh shell, you should use an intermediate script, like the following:

bash /home/username/

Now, this is the script. It is based on the stop_after_current script:

# MPD script to play an alarm after the current track.
# It is based on the script by Harun Vos.
# After the current song, the playlist is paused, the alarm
# track is played (at maximum volume) and the playlist is 
# resumed.

# Requires mpc - set the path here.
# Define which file to use as alarm.
# Note: it should be present in the MPD database.

# Extract the interesting line from the mpc output.
# There must be a nicer way of dealing with multi-line shell variables.
function getDetailsLine()
  local d=$($MPC --format "no file name")
  d=$(tail -1 <<EOL
$(head -2 <<EOL2
  echo $d

# Get the playing/paused status.
function getStateFromLine()
  echo $*|cut -d' ' -f1

# Get the number of the currently playing song.
function getSongFromLine()
  echo $*|cut -d' ' -f2|cut -d'/' -f1|cut -d'#' -f2

# Get the number of the last song / the length of the playlist.
function getLastSongFromLine()
  echo $*|cut -d' ' -f2|cut -d' ' -f1|cut -d'/' -f2

# Convert the mm:ss format to the number of seconds.
# This 10#stuff forces the numbers into base 10, even if they start with a 0.
function minToSec()
  echo $((10#$(echo $1|cut -d':' -f1) * 60 + 10#$(echo $1|cut -d':' -f2)))

# Find out how long we should wait for the end of the current song.
function getTimeRemainingFromLine()
  local current=$(echo $*|cut -d' ' -f3|cut -d'/' -f1)
  local total=$(echo $*|cut -d' ' -f3|cut -d'/' -f2)
  current=$(minToSec $current)
  total=$(minToSec $total)
  echo $(($total - $current))

# Get the current volume.
function getCurrentVolume()
  volumeLine=$MPC volume
  echo $($MPC volume | sed 's/volume: //g' | sed 's/%//g')

# Dont do anything if we arent playing.
currState=$(getStateFromLine $details)
if [ "$currState" != "[playing]" ]; then exit; fi

currSong=$(getSongFromLine $details)
timeRemaining=$(getTimeRemainingFromLine $details)
# Wait till almost the end of the current song.
# Try to stop before the end of the current song, rather than after the start of the next one.
if [ $timeRemaining -gt 1 ]; then
  sleep $(($timeRemaining - 1))

# Make sure it is still playing.
currState=$(getStateFromLine $details)
if [ "$currState" != "[playing]" ]; then exit; fi

# And make sure it is still the same song.
finalSong=$(getSongFromLine $details)
if [ "$finalSong" != "$currSong" ]; then exit; fi

# Add the alarm file

# Get current, next and last (alarm) song.
currSong=$(getSongFromLine $details)
lastSong=$(getLastSongFromLine $details)
let nextSong=currSong+1

$ Get current volume, to reset after playing the alarm.

# Play the alarm
$MPC volume 100
$MPC play $lastSong

# Wait till almost the end of the current song.
timeRemaining=$(getTimeRemainingFromLine $details)
if [ $timeRemaining -gt 1 ]; then
  sleep $(($timeRemaining - 1))

# Resume playlist
$MPC volume $origVol
$MPC play $nextSong

# Delete alarm file
$MPC del $lastSong

Also on Fandom

Random Wiki