Guest User

MrSkinny

a guest
Jan 8th, 2010
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.52 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ###############################################################################################
  4. # HISTORY
  5. # V1.0 Complete, Stuart Graham 24/12/09.
  6. #
  7. #
  8. # BRIEF DESCRIPTION - resumes mocp to last track and position in track
  9. #
  10. #
  11. # WHY I WROTE IT
  12. # I am a big fan of moc but, the only missing feature I found was it's inability to remember
  13. # the track and position of track since the last time I ran it. I tend to listen to a lot
  14. # of podcasts & audio books on moc that I stream to my cordless headphones from a server.
  15. # I control my server remotely using my Nokia N810 internet tablet, the screen and keyboard
  16. # of the N810 is a little small, it makes things a lot easier now that my script allows moc
  17. # to resume from from where it left off.
  18. #
  19. # The script tries to match each track in the playlist with the one stored in a settings file.
  20. # It takes moc a second or so to check a track in the playlist, this means the script works best
  21. # if the track you are trying to find is near the top of your playlist. I've also limited it to
  22. # only search the first 30 tracks on the playlist, although this can be easily changed by
  23. # changing the value of $count in function RESUME.
  24. #
  25. #
  26. # HOW TO USE IT
  27. # You must first have a fairly new version of moc installed on your system.
  28. # I am currently running Moc 2.5.0-alpha2 Build: Oct 28 2007 11:11:40, which was available in the
  29. # Ubuntu Hardy repo. eg run "sudo apt-get install moc" to install moc
  30. # or use Synaptic Package Manager and search for "moc"
  31. # Copy the script into somewhere the system can find it, maybe /usr/bin, I have a scripts dir in
  32. # my home dir which is included in my enviroment variable.
  33. # Create the settings file somewhere handy, use command "touch settings"
  34. # modify the values of the # GLOBAL CONSTANTS to suit your needs
  35. # eg modfy the values of "fpath" and "fname" then you should be ready to go.
  36. # good luck, I hope you find this useful :)
  37. #
  38. #
  39. # OPERATION OF SCRIPT
  40. # The script checks to see if the settings file "$fname" exists, if it doesn't the script exits.
  41. #
  42. # It checks to see if the moc server is playing or paused, if it is then it runs the moc GUI as
  43. # normal. If the moc server is stopped it loads the previous played track name and position info
  44. # from the settings file "$fname".
  45. #
  46. # If the title stored in the settings file "fname" is blank then the moc GUI is run as normal.
  47. #
  48. # If the title stored in the settings file "fname" is NOT blank then the script diplays a
  49. # "PLAY OPTIONS" menu.
  50. #
  51. # The "PLAY OPTIONS" are (R)esume, (C)ontinue or (Q)uit.
  52. #
  53. # If the (Q)uit option is chosen the script exits normally.
  54. # If the (C)ontinue option is chosen then the script runs the moc GUI as normal.
  55. # If the (R)esume option is chosen then the script will attempt to resumes moc to it's
  56. # previous run track within the playlist.
  57. # It will also attempt to resume to the previous track position.
  58. #
  59. #
  60. # SHORTCOMMINGS
  61. # if the track has a blank title in the ID tag info then resume will not work as this is how the
  62. # script resumes to the correct track.
  63. #
  64. # Script will try 30 times to find the track to resume from. If the track is further down than 30
  65. # tracks then the script gives up.
  66. #
  67. # The script will exit if the settings file doesn't exist, it will not create a blank settings
  68. # file for you.
  69. #
  70. #
  71. # POSSIBLE IMROVEMENTS
  72. # Script could recognise when the end of the playlist is reached, this could be done by moving
  73. # down one track then comparing the current track name with the previous track name, if the
  74. # track names is unchanged then it can be assumed that we reached the end of the playlist.
  75. # For me, this is a low priority improvment since I wrote this script to listen to audio books
  76. # & podcasts, which I delete from the playlist once listend to.
  77. # The script is not intended to be used to search through large playlists as it would take
  78. # prohibitively long to locate the track to resume.
  79. #
  80. # Another improvement would be to create an empty settings file rather than just exit if it
  81. # doesn't exist.
  82. ###############################################################################################
  83.  
  84.  
  85.  
  86. # GLOBAL CONSTANTS
  87.     readonly fpath="~/" # path to settings file
  88.     readonly fname="resume-mocp-settings" # name of file that holds settings
  89.     readonly mocp="mocp $@" # allows you to run mocp with options if required
  90.     readonly recap="30" # amount of seconds to recap when moc resumes
  91.  
  92.  
  93.  
  94. # GLOBAL VAIABLES
  95.     # Values gathered from current running mocp
  96.     mocState="" # current moc state, eg PLAY, PAUSE, STOP
  97.     mocCurrentSec="" # current seconds into track
  98.     mocCurentTime="" # current time into track eg "hours:minutes"
  99.     mocTitle="" # Title of current song
  100.  
  101.     # Values from mocp settings file gathered from previous run of mocp
  102.     resumeState="" # saved moc state, eg PLAY, PAUSE, STOP
  103.     resumeCurrentSec="" # saved position in track in seconds
  104.     resumeCurentTime="" # saved position in track in "hours:minutes"
  105.     resumeTitle="" # saved Title of song
  106.  
  107.  
  108.  
  109. function GET_MOC_STATE ()
  110. # Gets the current running moc state
  111. {
  112.     mocState="$($mocp --info | grep "^State:" | awk '{print $2}')"
  113.     mocCurrentSec="$($mocp --info | grep  "^CurrentSec:" |  awk '{print $2}')"
  114.     mocCurrentTime="$($mocp --info | grep "^CurrentTime:" | awk '{print $2}')"    
  115.     mocTitle="$($mocp --info | grep "^Title:" | cut -c8-)"
  116. }
  117.  
  118.  
  119.  
  120. function GET_RESUME_STATE ()
  121. # Get old moc resume state from disk and pass the values to the global
  122. # resume variables
  123. {
  124. # LOCAL VARIABLES
  125. settings="" # stores the settings read from file "$fname"
  126.  
  127.     # read settings from disk from $fname and store in variable $settings
  128.     # use awk to chop up the text string held in $settings into fields
  129.     # then assign these values to the global resume variables
  130.     while read settings
  131.     do
  132.         # eval used here so that all aurguments are concatenated together into a single
  133.         # command before being evaluated by the shell this allows the value of $1, $2,
  134.         # $3 and $4 to be assinged to the appropriate resume variables.
  135.         eval $(echo $settings | \
  136.         awk -F\| '{print "resumeState=\""$1"\"; resumeCurrentSec=\""$2"\"; resumeCurrentTime=\""$3"\"; resumeTitle=\""$4"\""}')
  137.     done < "${fpath}${fname}" # path and file name of settings file
  138. }
  139.  
  140.  
  141.  
  142. function RUN_MOC ()
  143. # Runs mocp then when mocp GUI exits gets current mocp state and writes it to disk
  144. {
  145.     $mocp # runs mocp
  146.     GET_MOC_STATE
  147.     # write current seek position, time and track name to disk
  148.     echo "$mocState|$mocCurrentSec|$mocCurrentTime|$mocTitle" > "${fpath}${fname}"
  149. }
  150.  
  151.  
  152.  
  153. function RESUME ()
  154. # Resume Mocp to it's previous state from values stored in $fname file
  155. # Will try $count times (30) to match title stored on disk with one of the titles
  156. # within the laylist
  157. {
  158. # LOCAL VARIABLES
  159. count="30" # Number of tracks to move through while trying to find resumed track.
  160.  
  161. eval "$mocp --play" # start playing from 1st track in playlist
  162. echo "Waiting for mocp to settle..."
  163. sleep "2" # give moc time to settle before checking first track
  164. echo "Trying $count times to find track, please wait..."
  165. while [ "$count" -ge 1 ] ; do
  166.     echo "  ${count}..."
  167.     GET_MOC_STATE
  168.     if [[ "$mocTitle" = "$resumeTitle" ]] ; then # Check to see if moc is playing the correct resume track
  169.     if [[ "$resumeCurrentSec" -gt "$recap" ]] ; then # Recap if far enough into track
  170.         eval "$mocp --seek $(($resumeCurrentSec - $recap))" # Seek to resume part of track minus recap value
  171.     else
  172.         eval "$mocp --seek $resumeCurrentSec" # Seek to resume part of track
  173.     fi
  174.     sleep "1" # delay introduced so there is time for pause to work
  175.     eval "$mocp --pause" #pause mocp playing
  176.     RUN_MOC
  177.     exit 0
  178.     else
  179.     eval "$mocp --next" # move to next track to try again to match track
  180.     fi
  181.     count="$(($count - 1))"
  182. done
  183. # If track not found then pause mocp
  184. eval "$mocp --previous" # Go back one track to last track title checked
  185. sleep "1" # delay introduced so there is time for pause to work
  186. eval "$mocp --pause" #pause mocp playing
  187.  
  188. # Error mesages sent to standard err
  189. clear
  190. echo
  191. echo "***************************** ERROR ***********************"
  192. echo "Could not find track!" >&2
  193. echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  194. echo "STOPPED AT.......$mocTitle" >&2
  195. echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  196. echo "TRYING TO FIND...$resumeTitle" >&2
  197. echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  198. echo "Run $mocp to see where you are"
  199. echo "***********************************************************"
  200. echo
  201. exit 1
  202. }
  203.  
  204.  
  205.  
  206. function MAIN_PROGRAM ()
  207. # Exit if settings file $fname does not exist
  208. # Get current running moc state. If moc is playing or paused then run moc gui.
  209. # If moc is in stopped state then load moc resume settings from disk. If the resume
  210. # title is blank run moc gui. If resume title is NOT blank then display "PLAY OPTION"
  211. # menu. If user picks (R)esume then resume moc to previous track and postion. If user
  212. # picks (C)ontinue then run moc gui. If user picks (Q)uit then exit script.
  213. {
  214. # LOCAL VARIABLES
  215. answer="" # answer from user input in PLAY OPTIONS menu
  216.  
  217.     # Exit if settings file not found
  218.     if [[ ! -f "${fpath}${fname}" ]] ; then
  219.     echo
  220.     echo "  **** Resume settings file ${fpath}${fname} NOT FOUND! ****" >&2
  221.     echo
  222.     exit 2
  223.     fi
  224.  
  225.     GET_MOC_STATE
  226.     # for some reason wont work with [[ if using -o, ie tesing for one thing OR another
  227.     if [ "$mocState" = "PLAY" -o "$mocState" = "PAUSE" ] ; then
  228.         RUN_MOC # run moc
  229.         exit 0
  230.     else
  231.         # mocpState must be STOPPED
  232.     GET_RESUME_STATE # loads resume settings from disk
  233.     # If resumeTitle is blank then run mocp
  234.     if [[ "$resumeTitle" = "" ]] ; then
  235.         RUN_MOC
  236.         exit 0
  237.     fi
  238.     clear # clear screen
  239.     echo "~~~~~~~~~~~~~~~~~~~~~~~ PLAY OPTIONS ~~~~~~~~~~~~~~~~~~~~~~~~"
  240.     echo "(R)esume"
  241.     # Could use colours here to improve readablility
  242.     echo "  ${resumeCurrentTime}|${resumeTitle}"
  243.     echo
  244.     echo
  245.     echo "(C)ontinue without resume"
  246.     echo
  247.     echo
  248.     echo "(Q)uit"
  249.     echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
  250.     echo -n "CHOOSE OPTION: "
  251.     #echo "============================================================"
  252.     read "answer"
  253.     case "$answer" in
  254.         r|R)
  255.         # move to resume point and run mocp
  256.         RESUME
  257.         ;;
  258.        
  259.         c|C)
  260.         # Continue and run moc as normal
  261.         RUN_MOC
  262.         exit 0
  263.         ;;
  264.        
  265.         q|Q)
  266.         # Quit script with exit code 0
  267.         exit 0
  268.         ;;
  269.            
  270.         *)
  271.         echo
  272.         echo "INVALID OPTION...!"
  273.         sleep "1"
  274.         MAIN_PROGRAM
  275.         ;;
  276.     esac       
  277.     fi
  278. }
  279.  
  280. # Runs MAIN_PROGRAM
  281. MAIN_PROGRAM
Advertisement
Add Comment
Please, Sign In to add comment