Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it work on Mac OS #2

Merged
merged 3 commits into from Apr 1, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
100 changes: 60 additions & 40 deletions astley.sh
@@ -1,7 +1,6 @@
#!/bin/bash
# Rick Astley in your Terminal. Prefers 256-color.
# 2013.03.04
# ~ keroserene <3
# Rick Astley in your Terminal.
# By Serene Han and Justine Tunney <3
version='1.0'
my_head=${BASH_SOURCE[0]}
base_dir=$(dirname $my_head)
Expand All @@ -16,14 +15,12 @@ NEVER_GONNA_TELL_A_LIE="INT TERM EXIT STOP SUSPEND" # For evil mode.
AND_HURT_YOU="$HOME/.bashrc"

# Terminal helpers (Assumes 256 colors).
red='\e[38;5;9m'
purp='\e[38;5;171m'
yell='\e[38;5;216m'
green='\e[38;5;10m'
gr=$(which grep)
NEVER_GONNA_GIVE="cat" # Mreow!
red='\x1b[38;5;9m'
purp='\x1b[38;5;171m'
yell='\x1b[38;5;216m'
green='\x1b[38;5;10m'

echo -e '\e[s' # Save cursor
echo -e '\x1b[s' # Save cursor

usage () {
echo -e "${green}Rick Astley performs ♪ Never Gonna Give You Up ♪ on STDOUT."
Expand Down Expand Up @@ -51,9 +48,9 @@ clean() {
}

quit() {
echo -en "\e[?25h \e[0m" # Reset cursor
[[ -n $save_term ]] || echo -e "\e[2J \e[H <3"
[[ -n $return_mode ]] && return 0 || exit 0
echo -en "\x1b[?25h \x1b[0m" # Reset cursor
[[ -n $save_term ]] || echo -e "\x1b[2J \x1b[H <3"
[[ -n $return_mode ]] && return 0 || exit 0
}

nanosec() {
Expand Down Expand Up @@ -125,26 +122,32 @@ never_gonna=(
"Never gonna say goodbye"
"Never gonna tell a lie and hurt you")
give=0
audpid=0
# Emit a rick roll lyric.
oooh() {
[[ "$give" -gt "${#never_gonna[@]}" ]] && give=0
echo -e "\e[2J\e[35;3H\e[0m${never_gonna[$give]}\e[H"
echo -e "\x1b[2J\x1b[35;3H\x1b[0m${never_gonna[$give]}\x1b[H"
(( give++ ))
kill -CONT $audpid
(( audpid > 1 )) && kill -CONT $audpid
# pids=$(jobs -p)
pids=$(jobs -l)
kill -CONT $pids
echo "$pids lulz $vidpid" >> ~/roflzz
}
cleanup() {
(( audpid > 1 )) && kill $audpid
}
if [[ $evil ]]; then
# ... you know the rules, and so do I
trap - $NEVER_GONNA_TELL_A_LIE
trap "oooh" 1 2 5 9 15 17 19 20 23 24
else
trap "cleanup" INT
fi
trap "quit" EXIT

# we know the game and we're gonna play it!
echo -e "\e[2J"
echo -e "\x1b[2J"

# Agnostic to curl or wget availability.
obtainium() {
Expand All @@ -158,27 +161,44 @@ obtainium() {
# Bean streamin'
bean="http://bean.vixentele.com/~keroserene"
remote="$bean/astley80.full.bz2"
audio="$bean/roll.s16"

# Prepare FPS sync state.
fps=25; ns_per_frame=$((1000000000/fps)); line=0
frame=0; next_frame=0
begin=$(nanosec)

# obtainium $audio | aplay -q -f S16_LE -r 22050 &
obtainium $audio | aplay -q -f S16_LE -r 8000 &
while read p; do
((line++))
if [[ $line == 32 ]]; then # Adjust for FPS timing per frame.
line=0
((frame++))
now=$(nanosec)
elapsed=$((now - begin))
next_frame=$((elapsed / ns_per_frame))
repose=$((frame * ns_per_frame - elapsed))
((repose > 0)) && sleep $(printf 0.%09d $repose)
fi
# Only print if no frame skips are necessary.
(( frame >= next_frame )) && echo -e "$p"
done < <(obtainium $remote | bunzip2 -q 2> /dev/null)
echo -e '\e[u' # Restore cursor position.

if hash afplay 2>/dev/null; then
# On Mac OS we pre-fetch compressed audio and use afplay.
#remote="$bean/astley80.mac.bz2"
echo "downloading audio..."
obtainium $bean/roll.gsm.wav >/tmp/roll.gsm.wav
afplay /tmp/roll.gsm.wav &
audpid=$!
elif hash afplay 2>/dev/null; then
# On Linux if the aplay command is available, we stream raw sound.
obtainium $bean/roll.s16 | aplay -q -f S16_LE -r 8000 &
audpid=$!
fi

# Print out frames, inserting pauses and skipping frames if necessary to stay
# as close as possible to real time.
python <(cat <<EOF
import sys
import time
fps = 25
time_per_frame = 1.0 / fps
begin = time.time()
frame = 0
try:
for i, line in enumerate(sys.stdin):
if i % 32 == 0:
frame += 1
now = time.time()
elapsed = now - begin
next_frame = elapsed / time_per_frame
repose = frame * time_per_frame - elapsed
if repose > 0.0:
time.sleep(repose)
if frame >= next_frame:
sys.stdout.write(line)
except KeyboardInterrupt:
pass
EOF
) < <(obtainium $remote | bunzip2 -q 2> /dev/null)

echo -e '\x1b[u' # Restore cursor position.