Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # RandOm Music Player #########################################################
- # v.1
- # by mahatman2
- #{{{ FEATURES ROADMAP #########################################################
- # Current:
- # - definable begin path
- # - descend directory tree, play music files with `play` (from SoX)
- # - debug stuff
- # - exit playing with ^C
- # Future:
- # - shuffle music files while playing (repeat/no-repeat)
- # - begin on track
- # > Shortcut Keys
- # - skip to next track
- # - pause/play
- #}}} ##########################################################################
- ## config variables {{{
- exclude=""
- ls_opts="-l" # just list files, no hiddens
- player="$(which play)"
- file_formats="au\|snd\|a?iff?\|mp2\|mp3\|ra\|sf\|smp\|voc\|wav\|wve\|ogg\|wma\|m4a\|flac"
- debug=0
- ## }}}
- ## functions {{{
- Help() {
- cat <<EOF
- usage: $0 -[xhdb] [folder]
- -x [pattern]: Exclude this pattern in file names.
- -b [track]: Begin on track [track]
- -h: Show this help.
- -d: Debug.
- [folder]: Play files in this folder.
- ----DEFAULT: use current folder.
- EOF
- exit 0
- }
- Debug() { # <message>
- if [[ $debug == 1 ]]; then
- echo "$@" >&2;
- 2>&1;
- fi
- }
- GetRandom() { # [lo=0] <hi> #TODO:[randosity]
- if [[ -z $1 ]]; then
- Debug "You need at least one argument!"
- return -1
- fi
- if [[ -z $2 ]]; then
- lo=0; hi=$1;
- Debug "GetRandom: lo defaulting to 0; hi = $hi"
- else
- lo=$1; hi=$2;
- Debug "GetRandom: lo = $lo; hi = $hi"
- fi
- # returns a value: {lo..hi}
- rand_number=$[(RANDOM % (hi+1 - lo)) + lo]
- Debug "GetRandom: number returned: $rand_number"
- return $rand_number
- }
- Branch() { # return 0 for continuing down, return # for stop
- number_dirs=$(ls -l | grep '^d' | wc -l)
- Debug "Branch: number_dirs = $number_dirs"
- # if no more branches, return 1
- if [[ $number_dirs == 0 ]]; then
- Debug "Branch: Directory at the end of tree. Return 1."
- return 1
- fi
- GetRandom $number_dirs
- random_dir_number=$?
- # if the random number is 0, stay here
- if [[ $random_dir_number == 0 ]]; then
- Debug "Branch: random_dir is this one."
- return 2
- fi
- random_dir="$(ls $ls_opts|\
- awk -v n=$random_dir_number '/^d/{i++;if(i==n){for(j=9;j<=NF;j++){printf "%s ",$j} }}')"
- # if it's something we don't want, go again
- if [[ "$exclude" =~ "*$random_dir*" ]]; then
- Debug "Branch: The directory "$random_dir" is in the \$exclude list. Retrying.."
- Branch
- fi
- # else, cd to the dir && return 0
- Debug "Branch: echo \"$random_dir\"; return 0"
- echo $random_dir; return 0
- }
- ## }}}
- ## option parsing {{{
- while getopts 'hdx:b:' opt; do
- case $opt in
- h) Help ;;
- d) debug=1 ;;
- x)
- exclude="$OPTARG"
- Debug "$exclude will be excluded from path."
- ;;
- b)
- begin_on="$OPTARG"
- Debug "Begginning on track ${begin_on}..."
- ;;
- \?) echo "Invalid options: -$OPTARG" >&2 ;;
- esac
- done
- shift $((OPTIND-1))
- # cd to folder passed after options (if exists)
- if [[ -n "$1" ]]; then
- Debug "Romping from $(realpath ${1}).."
- cd ${1}
- fi
- ##}}}
- ## Climb the tree {{{
- Debug "Climb: Begin tree-climbing.."
- while true; do
- path="$(Branch)";
- if [[ $? -ne 0 ]]; then
- Debug "Climb: Finished climbing the tree!"
- Debug "PWD = $PWD"
- # Make sure there are music files in this folder
- ls -l | grep -v '^d\|^total' && break || Debug "Oops, I lied! This folder has no music."
- fi
- cd "$path";
- Debug "Climb: PWD = $PWD"
- done
- ##}}}
- ## Play music in this folder {{{
- currentSong=1
- IFS=$'\n'
- list_Songs=( $(ls -1 | grep -i $file_formats) )
- Debug "List of songs: ${list_Songs[@]}"
- trap "exit" SIGINT
- for song in ${list_Songs[@]}; do
- # TODO: enable 'skip'
- #trap "continue" SIGTSTP
- if [[ $begin_on -gt 1 ]]; then
- ((begin_on-=1))
- ((currentSong+=1))
- Debug "Skipping \"$song\" (-b flag; $begin_on more to skip)"
- continue
- fi
- [[ $debug -ne 0 ]] || clear
- echo "Romping in $PWD"
- echo "==============================================="
- echo "Playing: $song"
- echo "( $currentSong / ${#list_Songs[@]} )"
- echo "+++++++++++++++++++++++++++++++++++++++++++++++"
- $player "$song" || (Debug "Skipping \"$song\" (unrecognized format)"; continue)
- ((currentSong+=1))
- done
- #}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement