Register
It is currently Thu Feb 22, 2018 10:35 am

Automatic DJ, BASH version


All times are UTC


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Thu Jul 16, 2009 3:24 am   
Advanced Member
User avatar

Joined: Fri Jul 18, 2008 4:54 pm
Posts: 608
Location: South Portland, Maine, USA, Earth, Sol System
Did you know that with MPD, icecast, cron and a simple bash script you too can have an automatic DJ. Capable of handling music libraries of up to 10,000,000,000 songs and boasting remarkably good randomness. It uses some features of bash only available in bash versions 3.XX, but let's face it who hasn't upgraded to at least 3.0 by now?

And without further ado, here's the code:
Code:
#!/bin/bash

# Defines
#

# which host to connect to
MPD_HOST=127.0.0.1;
MPD_PORT=6600;
MPD_DATABASE_LOCATION="/var/lib/mpd/database";
#Don't set this so low that playlist could complete before cron task fires again
MPD_PLAYLIST_MAX_LENGTH=20;

################################################################################
#                       DO NOT EDIT BELOW THIS LINE!                           #
################################################################################
MPD_DATABASE=( );
MPD_DATABASE_COUNT=0;

function mpd_get_random ()
{
    local MPD_RANDOM=$(head -c4 /dev/random| od -N4 -tu4 | sed -ne '1s/.* //p');
    if [ "$1" ]; then
   if echo $1 | grep -v "[^0-9]" > /dev/null; then
       expr ${MPD_RANDOM} % $1
   else
       echo $MPD_RANDOM;
   fi
    else
   echo $MPD_RANDOM;
    fi
}

function mpd_connect()
{
    local MPD_CONNECT_COMMAND="env"
    if [ "$MPD_HOST" ]; then
   MPD_CONNECT_COMMAND=${MPD_CONNECT_COMMAND}' MPD_HOST='${MPD_HOST};
    fi
    if [ "$MPD_PORT" ]; then
   MPD_CONNECT_COMMAND=${MPD_CONNECT_COMMAND}' MPD_PORT='${MPD_PORT};
    fi
    if [ "${MPD_CONNECT_COMMAND}" != "env" ]; then
   echo "${MPD_CONNECT_COMMAND} mpc";
    else
   echo "mpc";
    fi
}
function mpd_playlist_current_song ()
{
    local MPD_PLAYLIST=$($(mpd_connect) playlist|tr ' ' '_');
    local MPD_CURRENT=1
    for MPD_SONG in ${MPD_PLAYLIST}; do
   if $(echo "${MPD_SONG}"|egrep "^>" >/dev/null); then
       break;
   fi
   (( MPD_CURRENT++ ));
    done
    echo ${MPD_CURRENT};

}
function mpd_playlist_length ()
{
    local MPC_PLAYLIST=$($(mpd_connect) playlist|tr ' ' '_');
    local MPD_CURRENT=0
    for MPD_SONG in ${MPC_PLAYLIST}; do
   (( MPD_CURRENT++ ));
    done
    echo ${MPD_CURRENT};
}

function mpd_add_song ()
{
    echo $1;
    if [ "$1" ]; then
   $(mpd_connect) add "$1" &>/dev/null;
   return $?
    fi
    return -1;
}
function mpd_del_song ()
{
    if [ $1 ]; then
   $(mpd_connect) del "$1"  &> /dev/null
   return $?
    fi
    return -1;
}
function mpd_songs_to_delete ()
{
    local MPD_CURRENT="$(mpd_playlist_current_song)";
    (( MPD_CURRENT-- ));
    if [ $MPD_CURRENT -ge 2 ]; then
   echo "1-${MPD_CURRENT}"
    fi;
   
}
function mpd_del_old_songs()
{
    local MPD_TO_DELETE="$(mpd_songs_to_delete)"
    if [ ${MPD_TO_DELETE} ]; then
   mpd_del_song ${MPD_TO_DELETE}
    fi
}
function mpd_add_new_songs ()
{
    local MPD_LENGTH="$(mpd_playlist_length)";
    while [ ${MPD_LENGTH} -lt ${MPD_PLAYLIST_MAX_LENGTH} ]; do
   if mpd_add_song "$(mpd_get_rand_song)"; then
       (( MPD_LENGTH++ ))
   fi
    done;
}
function mpd_get_rand_song ()
{
    echo "${MPD_DATABASE[$(mpd_get_random ${MPD_DATABASE_COUNT})]}";
}
function mpd_get_database ()
{
    MPD_TEMP_FILE=$(mktemp);
    egrep "^file:" "${MPD_DATABASE_LOCATION}"| cut -d' ' -f 2- > "${MPD_TEMP_FILE}"
    while read MPD_FILE; do
   MPD_DATABASE[${MPD_DATABASE_COUNT}]="${MPD_FILE}"
   (( MPD_DATABASE_COUNT++ ));
    done < "${MPD_TEMP_FILE}"
    rm  "${MPD_TEMP_FILE}"

}

mpd_del_old_songs
mpd_get_database
mpd_add_new_songs
exit 0;


What? So i get bored easily! sheesh.....

_________________
ArchLinux x86_64 - Custom Built Desktop
ArchLinux x86_64 - Compaq CQ50 Laptop
ArchLinux i686 - Acer Aspire One Netbook
ArchLinux i686 - Dell Presario ze2000 (w/ shattered LCD)

PuppyLinux, CloneZilla, PartedMagic, DBAN - rescue thumbdrives
Windows 7 (x86_64 desktop alternate boot)


Top
 Profile WWW  
 PostPosted: Thu Jul 16, 2009 10:48 pm   
Linux Guru
User avatar

Joined: Wed Jul 07, 2004 3:30 am
Posts: 7393
Location: Central Montana & Eastern Tennessee
That looks like it would be pretty cool, now to get a music library :? I'm just not sure there are 10,000,000,000 Bluegrass songs :roll:

:lol: :lol:

_________________
BYOBox - Dell E6400 7w/Debian 9.3.0 64
BrewBox - D610 7w/antix17 base
PlayBox - antiX 17 Testing


Top
 Profile  
 PostPosted: Fri Jul 17, 2009 12:06 am   
Advanced Member
User avatar

Joined: Fri Jul 18, 2008 4:54 pm
Posts: 608
Location: South Portland, Maine, USA, Earth, Sol System
So it's a little future proofed....

-shrug-

$RANDOM only generates a signed 16 bit random number. thats only 32767 songs. And I already have more than that in my library. ender /dev/random and a little magic with od and sed...

_________________
ArchLinux x86_64 - Custom Built Desktop
ArchLinux x86_64 - Compaq CQ50 Laptop
ArchLinux i686 - Acer Aspire One Netbook
ArchLinux i686 - Dell Presario ze2000 (w/ shattered LCD)

PuppyLinux, CloneZilla, PartedMagic, DBAN - rescue thumbdrives
Windows 7 (x86_64 desktop alternate boot)


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


Powered by phpBB © 2012 phpBB Group
© 2003 - 2012 USA LINUX USERS GROUP