Advertisement
Guest User

romp - random music player

a guest
Sep 10th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.61 KB | None | 0 0
  1. #!/bin/bash
  2. # RandOm Music Player #########################################################
  3. # v.1
  4. # by mahatman2
  5. #{{{ FEATURES ROADMAP #########################################################
  6. # Current:
  7. #   - definable begin path
  8. #   - descend directory tree, play music files with `play` (from SoX)
  9. #   - debug stuff
  10. #   - exit playing with ^C
  11. # Future:
  12. #   - shuffle music files while playing (repeat/no-repeat)
  13. #   - begin on track
  14. #   > Shortcut Keys
  15. #      - skip to next track
  16. #      - pause/play
  17. #}}} ##########################################################################
  18. ## config variables {{{
  19. exclude=""
  20. ls_opts="-l" # just list files, no hiddens
  21. player="$(which play)"
  22. file_formats="au\|snd\|a?iff?\|mp2\|mp3\|ra\|sf\|smp\|voc\|wav\|wve\|ogg\|wma\|m4a\|flac"
  23. debug=0
  24. ## }}}
  25. ## functions {{{
  26. Help() {
  27.     cat <<EOF
  28.     usage: $0 -[xhdb] [folder]
  29.     -x [pattern]:   Exclude this pattern in file names.
  30.     -b [track]:     Begin on track [track]
  31.     -h:             Show this help.
  32.     -d:             Debug.
  33.     [folder]:       Play files in this folder.
  34.                     ----DEFAULT: use current folder.
  35. EOF
  36. exit 0
  37. }
  38. Debug() { # <message>
  39.     if [[ $debug == 1 ]]; then
  40.         echo "$@" >&2;
  41.         2>&1;
  42.     fi
  43. }
  44. GetRandom() { # [lo=0] <hi> #TODO:[randosity]
  45.     if [[ -z $1 ]]; then
  46.         Debug "You need at least one argument!"
  47.         return -1
  48.     fi
  49.     if [[ -z $2 ]]; then
  50.         lo=0; hi=$1;
  51.         Debug "GetRandom: lo defaulting to 0; hi = $hi"
  52.     else
  53.         lo=$1; hi=$2;
  54.         Debug "GetRandom: lo = $lo; hi = $hi"
  55.     fi
  56.     # returns a value: {lo..hi}
  57.     rand_number=$[(RANDOM % (hi+1 - lo)) + lo]
  58.     Debug "GetRandom: number returned: $rand_number"
  59.     return $rand_number
  60. }
  61. Branch() { # return 0 for continuing down, return # for stop
  62.     number_dirs=$(ls -l | grep '^d' | wc -l)
  63.     Debug "Branch: number_dirs = $number_dirs"
  64.     # if no more branches, return 1
  65.     if [[ $number_dirs == 0 ]]; then
  66.         Debug "Branch: Directory at the end of tree. Return 1."
  67.         return 1
  68.     fi
  69.  
  70.     GetRandom $number_dirs
  71.     random_dir_number=$?
  72.     # if the random number is 0, stay here
  73.     if [[ $random_dir_number == 0 ]]; then
  74.         Debug "Branch: random_dir is this one."
  75.         return 2
  76.     fi
  77.  
  78.     random_dir="$(ls $ls_opts|\
  79.        awk -v n=$random_dir_number '/^d/{i++;if(i==n){for(j=9;j<=NF;j++){printf "%s ",$j} }}')"
  80.  
  81.     # if it's something we don't want, go again
  82.     if [[ "$exclude" =~ "*$random_dir*" ]]; then
  83.         Debug "Branch: The directory "$random_dir" is in the \$exclude list. Retrying.."
  84.         Branch
  85.     fi
  86.  
  87.     # else, cd to the dir && return 0
  88.     Debug "Branch: echo \"$random_dir\"; return 0"
  89.     echo $random_dir; return 0
  90. }
  91. ## }}}
  92. ## option parsing {{{
  93. while getopts 'hdx:b:' opt; do
  94.     case $opt in
  95.         h)  Help                                    ;;
  96.         d)  debug=1                                 ;;
  97.         x)
  98.             exclude="$OPTARG"
  99.             Debug "$exclude will be excluded from path."
  100.             ;;
  101.         b)  
  102.             begin_on="$OPTARG"
  103.             Debug "Begginning on track ${begin_on}..."
  104.             ;;
  105.         \?) echo "Invalid options: -$OPTARG" >&2    ;;
  106.     esac
  107. done
  108. shift $((OPTIND-1))
  109. # cd to folder passed after options (if exists)
  110. if [[ -n "$1" ]]; then
  111.     Debug "Romping from $(realpath ${1}).."
  112.     cd ${1}
  113. fi
  114. ##}}}
  115. ## Climb the tree {{{
  116. Debug "Climb: Begin tree-climbing.."
  117. while true; do
  118.     path="$(Branch)";
  119.     if [[ $? -ne 0 ]]; then
  120.         Debug "Climb: Finished climbing the tree!"
  121.         Debug "PWD = $PWD"
  122.         # Make sure there are music files in this folder
  123.         ls -l | grep -v '^d\|^total' && break || Debug "Oops, I lied! This folder has no music."
  124.     fi
  125.     cd "$path";
  126.     Debug "Climb: PWD = $PWD"
  127. done
  128. ##}}}
  129. ## Play music in this folder {{{
  130. currentSong=1
  131. IFS=$'\n'
  132. list_Songs=( $(ls -1 | grep -i $file_formats) )
  133. Debug "List of songs: ${list_Songs[@]}"
  134. trap "exit" SIGINT
  135. for song in ${list_Songs[@]}; do
  136.     # TODO: enable 'skip'
  137.     #trap "continue" SIGTSTP
  138.     if [[ $begin_on -gt 1 ]]; then
  139.         ((begin_on-=1))
  140.         ((currentSong+=1))
  141.         Debug "Skipping \"$song\" (-b flag; $begin_on more to skip)"
  142.         continue
  143.     fi
  144.     [[ $debug -ne 0 ]] || clear
  145.     echo "Romping in $PWD"
  146.     echo "==============================================="
  147.     echo "Playing: $song"
  148.     echo "( $currentSong / ${#list_Songs[@]} )"
  149.     echo "+++++++++++++++++++++++++++++++++++++++++++++++"
  150.     $player "$song" || (Debug "Skipping \"$song\" (unrecognized format)"; continue)
  151.     ((currentSong+=1))
  152. done
  153. #}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement