Skip to content

Instantly share code, notes, and snippets.

@aweijnitz
Last active January 14, 2024 04:21
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aweijnitz/dfd44e86b617a235564d to your computer and use it in GitHub Desktop.
Save aweijnitz/dfd44e86b617a235564d to your computer and use it in GitHub Desktop.
Music from /dev/urandom on Mac OSX
# I really enjoyed "Bash One Liner - Compose Music From Entropy in /dev/urandom"
# From http://blog.robertelder.org/bash-one-liner-compose-music/
#
# This is a collection of one liners that work on Mac OSX
# You need sox
brew install sox
# Major scale
cat /dev/urandom | hexdump -v -e '/1 "%u\n"' | awk '{ split("0,2,4,5,7,9,11,12",a,","); for (i = 0; i < 1; i+= 0.0001) printf("%08X\n", 100*sin(1382*exp((a[$1 % 8]/12)*log(2))*i)) }' | xxd -r -p | sox -v 0.25 -traw -r16000 -b32 -e signed-integer - -tcoreaudio
# Minor scale
cat /dev/urandom | hexdump -v -e '/1 "%u\n"' | awk '{ split("0,2,3,5,7,8,10,12",a,","); for (i = 0; i < 1; i+= 0.0001) printf("%08X\n", 100*sin(1382*exp((a[$1 % 8]/12)*log(2))*i)) }' | xxd -r -p | sox -v 0.25 -traw -r16000 -b32 -e signed-integer - -tcoreaudio
# Pentatonic
cat /dev/urandom | hexdump -v -e '/1 "%u\n"' | awk '{ split("0,3,5,6,7,10,12",a,","); for (i = 0; i < 1; i+= 0.0001) printf("%08X\n", 100*sin(1382*exp((a[$1 % 8]/12)*log(2))*i)) }' | xxd -r -p | sox -v 0.25 -traw -r16000 -b32 -e signed-integer - -tcoreaudio
# Ryukyu (Okinawa) Japanese music scale
cat /dev/urandom | hexdump -v -e '/1 "%u\n"' | awk '{ split("4,5,7,11",a,","); for (i = 0; i < 1; i+= 0.0001) printf("%08X\n", 100*sin(1382*exp((a[$1 % 8]/12)*log(2))*i)) }' | xxd -r -p | sox -v 0.25 -traw -r16000 -b32 -e signed-integer - -tcoreaudio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment