Posted: . At: 10:12 AM. This was 11 months ago. Post ID: 18057
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to play interesting music using data from /dev/urandom.


It is possible to play music using random data from /dev/urandom. This is a nice shell trick.

┗━━━━━━━━━━┓ john@localhost ~
           ┗━━━━━━━━━━━━━╾ ╍▷ hexdump -e '/1 "%un"' /dev/urandom | \
           awk '{ split("0,2,4,5,7,8,10,12",a,",");for (i = 0; i < \
           1; i+= 0.0001) printf("%08Xn", 100*sin(1392*2**(a[$1 %8]/12)*i)) }'\
            | xxd -r -p | aplay -c 2 -f S32_LE -r 16000

This is the one-liner I used. This will play random tunes reminiscent of chiptunes from random data.

┗━━━━━━━━━━┓ john@localhost ~
           ┗━━━━━━━━━━━━━╾ ╍▷ hexdump -e '/1 "%un"' /dev/urandom | awk '{ split("0,2,4,5,7,8,10,12",a,",");for (i = 0; i < 1; i+= 0.0001) printf("%08Xn", 100*sin(1392*2**(a[$1 %8]/12)*i)) }' | xxd -r -p | aplay -c 2 -f S32_LE -r 16000
Playing raw data 'stdin' : Signed 32 bit Little Endian, Rate 16000 Hz, Stereo
^CAborted by signal Interrupt...
aplay: pcm_write:2127: write error: Interrupted system call

It is great to try out stuff like this and get it to work.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.