Created
September 14, 2012 22:56
-
-
Save infertux/3725488 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This is a WTF Bash script licensed under the WTFPL license (what else?). | |
# WTF: | |
# 1. Watching http://youtu.be/lTx3G6h2xyA | |
# 2. "Hey, what a nice device..." | |
# 3. See http://amzn.com/B0046ZIZO8 | |
# 4. "Doh! $159! Way too expensive for a broke student... :/" | |
# 5. ... | |
# 6. "Everything is possible with Bash!" | |
# 7. End up with this script | |
# Usage: | |
# - no args: interactive mode | |
# - or, pass in a file as the first arg to read it char by char | |
# Examples: | |
# - ./pad.sh # then use your keyboard as a (kind of) pad | |
# - ./pad.sh /dev/random # then move your mouse like a psycho to get some entropy | |
# - ./pad.sh /dev/urandom # to produce some pseudo-random garbage sound | |
# - ./pad.sh pad.sh # how meta | |
# change to your sweet sounds directory | |
DIR=/usr/lib/libreoffice/share/gallery/sounds | |
# max simultaneous sounds playing | |
# (especially useful when input file is /dev/urandom for instance) | |
MAX=30 | |
### | |
set -eu | |
stty -echo | |
trap bye INT TERM | |
bye() { | |
local stopped=$(date +%s) | |
local time=$(($stopped - $started)) | |
echo "Played ${total} sounds for ${time} seconds." | |
exit 0 | |
} | |
playing() { | |
ps --no-headers -o pid --ppid=$$ | wc -w # number of children | |
} | |
### | |
declare -a sounds=($(ls -1 $DIR)) | |
count=${#sounds[@]} | |
started=$(date +%s) | |
total=0 | |
[[ $# -eq 1 && -r "$1" ]] && input=$1 || input= | |
[ "$input" ] && exec 3<"$input" || exec 3<&0 | |
while read -n 1 key; do | |
code=$(printf "%d" "'$key'") # ASCII code | |
code=$(($code % $count)) | |
sound=${sounds[$code]} | |
while [ $(playing) -ge $MAX ]; do sleep .1; done | |
echo "($(playing)) ${sound} [${key}]" | |
aplay --quiet "${DIR}/${sound}" & | |
total=$(($total + 1)) | |
done <&3 # this is not a smiley |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment