Skip to content

Instantly share code, notes, and snippets.

@plugnburn
Last active March 3, 2018 06:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugnburn/af0aabd434c10ad66e6d98f9e4e8f4a0 to your computer and use it in GitHub Desktop.
Save plugnburn/af0aabd434c10ad66e6d98f9e4e8f4a0 to your computer and use it in GitHub Desktop.
SoX harp - simplest cross-platform musical instrument in a few lines of Bash (depends on SoX)
#!/bin/bash
function idx() {
local t=${1%%$2*}
[[ "$1" == "$t" ]] && echo "-1" || echo ${#t}
}
oct=0
while IFS= read -r -s -n1 i; do
[[ "$i" == "a" ]] && (( oct > -3 )) && oct=$((oct - 1)) && echo "Octave: $((oct + 3))"
[[ "$i" == "1" ]] && (( oct < 3 )) && oct=$((oct + 1)) && echo "Octave: $((oct + 3))"
noteIdx=$(idx 'zsxdcvgbhnjmq2w3er5t6y7ui9o0p[=]' "$i")
(( noteIdx > -1 )) && play -q -n synth 3 pluck %$(( noteIdx - 12 + 12 * oct )) &
done
@minerscale
Copy link

Pretty dang cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment