fridayrc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env bash
  2. trap "cleanup" INT
  3. trap "cleanup" EXIT
  4. audpid=0
  5. host="http://tylercipriani.s3.amazonaws.com/fridayrc"
  6. video="$host/friday.bz2"
  7. audio_wav="$host/friday.gsm.wav"
  8. local_wav="/tmp/friday.gsm.wav"
  9. audio_raw="$host/friday.raw"
  10. error() {
  11. printf "[$(tput setaf 1) ! $(tput sgr0)] ERROR: %s" "$1"
  12. }
  13. has?() {
  14. command -v "$1" > /dev/null 2>&1
  15. }
  16. pythonian() {
  17. if has? python2;
  18. then python2 "$@"
  19. elif has? python;
  20. then python "$@"
  21. else
  22. error "Cannot haz pythons :(" && exit 1
  23. fi
  24. }
  25. obtainium() {
  26. if has? curl;
  27. then curl -s $1
  28. elif has? wget;
  29. then wget -q -O - $1
  30. else
  31. error "Cannot haz internets. :(" && exit 1
  32. fi
  33. }
  34. cleanup() {
  35. (( audpid > 1 )) && kill $audpid 2> /dev/null;
  36. if [ -f "$local_wav" ]; then
  37. rm "$local_wav"
  38. fi
  39. printf "$(tput clear) $(tput cnorm)\n"
  40. }
  41. clearscreen() {
  42. printf "$(tput civis) $(tput clear)"
  43. }
  44. clearscreen
  45. printf "$(tput setaf 1)LOADING...$(tput sgr0)\n"
  46. if has? afplay; then
  47. # On Mac OS
  48. obtainium "$audio_wav" > "$local_wav"
  49. afplay /tmp/friday.gsm.wav &
  50. elif has? aplay; then
  51. # On Linux
  52. obtainium "$audio_raw" | aplay -q -f S16_LE -r 8000 &
  53. elif has? play; then
  54. # On Cygwin
  55. obtainium "$audio_wav" > "$local_wav"
  56. play -q "$local_wav" &
  57. fi
  58. audpid=$!
  59. # Sync FPS to reality as best as possible. Mac's freebsd version of date cannot
  60. # has nanoseconds so inject python. :/
  61. pythonian <(cat <<EOF
  62. import sys
  63. import time
  64. fps = 16.5
  65. time_per_frame = 1.0 / fps
  66. buf = ''
  67. frame = 0
  68. next_frame = 0
  69. begin = time.time()
  70. try:
  71. for i, line in enumerate(sys.stdin):
  72. if i % 32 == 0:
  73. frame += 1
  74. sys.stdout.write(buf); buf = ''
  75. elapsed = time.time() - begin
  76. repose = (frame * time_per_frame) - elapsed
  77. if repose > 0.0:
  78. time.sleep(repose)
  79. next_frame = elapsed / time_per_frame
  80. if frame >= next_frame:
  81. buf += line
  82. except KeyboardInterrupt:
  83. pass
  84. EOF
  85. ) < <(obtainium $video | bunzip2 -q 2> /dev/null)