USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

Automatic DJ, BASH version

 
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming
View previous topic :: View next topic  
Author Message
Lord.DragonFly.of.Dawn
Advanced Member


Joined: 18 Jul 2024
Posts: 607
Location: South Portland, Maine, USA, Earth, Sol System

PostPosted: Thu Jul 16, 2024 3:24 am    Post subject: Automatic DJ, BASH version Reply with quote

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)
Back to top
View user's profile Send private message Visit poster's website
JP
Linux Guru


Joined: 07 Jul 2024
Posts: 6670
Location: Central Montana

PostPosted: Thu Jul 16, 2024 10:48 pm    Post subject: Reply with quote

That looks like it would be pretty cool, now to get a music library Confused I'm just not sure there are 10,000,000,000 Bluegrass songs Rolling Eyes

Laughing Laughing



_________________
Dell Box - Arch Linux
Dell Lappy - DreamLinux 3.5 - Default OS
Mepis 8.0 - Backup
Back to top
View user's profile Send private message Visit poster's website
Lord.DragonFly.of.Dawn
Advanced Member


Joined: 18 Jul 2024
Posts: 607
Location: South Portland, Maine, USA, Earth, Sol System

PostPosted: Fri Jul 17, 2024 12:06 am    Post subject: Reply with quote

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)
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming All times are GMT
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum
All content © 2024-2009 - Usa Linux Users Group
This forum is powered by phpBB. © 2024-2009 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse