
'Music' from /dev/urandom - signa11
http://blog.robertelder.org/bash-one-liner-compose-music/
======
ckaygusu
Here is one with the blues scale. From time to time it grooves really nice.

    
    
      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 | aplay -c 2 -f S32_LE -r 24000

~~~
unsignedint
Here's 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(1046*exp((a[$1 % 8]/12)*log(2))*i)) }' | xxd -r -p | aplay -c 2 -f S32_LE -r 24000﻿

~~~
laumars
Unfortunately markdown formatting has broken your code. I can make a guess
based on where the italics starts and ends, but you might want to indent that
code.

~~~
unsignedint
Oops, I'm sorry. Looks better now?

------
robertelder
Here is a recording created by someone on reddit:

[http://mirror.xwl.me/bash_music_aac.mp4](http://mirror.xwl.me/bash_music_aac.mp4)

The above is close to what it should sound like.

Here are the reddit comment threads, some of which contain instructions for
OSX:

[https://www.reddit.com/r/programming/comments/48xf6k/bash_on...](https://www.reddit.com/r/programming/comments/48xf6k/bash_one_liner_compose_music_from_entropy_in/)

[https://www.reddit.com/r/linux/comments/48xj46/bash_one_line...](https://www.reddit.com/r/linux/comments/48xj46/bash_one_liner_compose_music_from_entropy_in/)

[https://www.reddit.com/r/coding/comments/48zvn4/bash_one_lin...](https://www.reddit.com/r/coding/comments/48zvn4/bash_one_liner_compose_music_from_entropy_in/)

Someone recorded what they got working on OSX, and it sounded terrible (not
how it sounds on my Ubuntu machine). I haven't tested this on a mac since I
don't have one.

I was surprised that many people suggested listening to the music for pleasure
or inspiration. There are a few things I could do that would probably improve
the sounds quality: One is experimenting with the sampling rate of the sin
wave, and another is adjusting the for loop to make sure that it ends on the
end of the sine curve close to y = 0. Right now, for some notes it will just
stop halfway through close to y=1, and the next note will start at y=0. I
think that is the source of the 'poping' sound between notes.

~~~
ckaygusu
Those popping sounds give me the impression of a percussion instrument
playing, which I find desirable.

------
jeffreyrogers
> If you're a level 500 elite hacker like I am, you will note that the
> cryptographic quality of numbers from /dev/urandom is not the same as
> numbers from /dev/random. In our case, we don't want this operation to
> block, so we use /dev/urandom.

This is wrong. See [http://www.2uo.de/myths-about-
urandom/](http://www.2uo.de/myths-about-urandom/)

(The part about /dev/urandom not blocking is right though)

Edit: changed [http://security.stackexchange.com/questions/3936/is-a-
rand-f...](http://security.stackexchange.com/questions/3936/is-a-rand-from-
dev-urandom-secure-for-a-login-key) to the current link, since the current
link is more to the point.

------
noinsight
For those moments when you need cryptographically secure background noise.

You can also pipe it directly to pcspkr for a real wake-up call:

    
    
        cat /dev/urandom > /dev/pcspkr

------
sbierwagen
Back in the day you could just do:

    
    
      cat /dev/urandom > /dev/audio
    

[http://everything2.com/title/catting+weird+things+to+%252Fde...](http://everything2.com/title/catting+weird+things+to+%252Fdev%252Faudio)

------
blinkme
Is... is that blinking text at the bottom?

~~~
tedks
Yeah. It is.

------
Gnouc
It's great! Using:

    
    
        </dev/urandom hexdump -v -e '/1 "%u\n"' | ...
    

save you from spawning one process.

------
mpnordland
Almost could replace Pandora for me. All I want is some somewhat melodious
tones in the background anyway.

------
zeveb
> I only expected this to work on Ubuntu

And yet here's the command:

    
    
        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 | aplay -c 2 -f S32_LE -r 16000
    

There's nothing Ubuntu-specific there: it works fine on other Linuxes.

It's gotten more than a little annoying, this widespread equivalence between
Ubuntu and Linux. Ubuntu is _a_ Linux distro, albeit hardly the best, most
idiomatic or most traditional. It's just a single distro, out of many.

~~~
JoeAltmaier
But lets admit that distros of Linux _have_ become idiomatic. Its reasonable
to give a caveat when describing any Linux procedure. This is not an Ubuntu
issue at all.

In fact, what's the point of different distros if they _aren 't_ idiomatic?

~~~
cyphar
It's incorrect to assume that a shell script that uses tools like _xxd_ and
__awk __is distro-specific. Almost every distribution ships the GNU coreutils
and other GNU base tools. It 's going to work on basically every distribution.
What makes distributions unique is their package manager, release and security
philosophy, amount of cruft, etc.

~~~
JoeAltmaier
Even the same distro line but different versions may have significant issues.
This is why every build on top of Linux (e.g. Android) calls out a particular
kernel version. Even a particular version for the _build machine_. Because
things like command line tools differ over time. Versionology is a whole
universe of hurt on Linux.

~~~
cyphar
That's the cost of supporting code that is constantly being updated, you need
to start freezing versions and backporting fixes. Also, I'm not sure what
command line tools you're referring to as "differing over time". Kernel
version freezing is mainly fear of new code (or out-of-tree vendor patchsets
that depend very strongly on the kernel ABI which changes every few months --
but that's not user visible). Sure, if your system never changes anything you
don't need to worry about software versions. But there are many other problems
with that.

------
Tepix
Is there a way to make it sounds more pleasant? The sine wave is a bit
harsh...

~~~
kittxkat
The example from /r/coding[1] is very nice to listen to:

    
    
        awk 'function wl() {
            rate=64000;
            return (rate/160)*(0.87055^(int(rand()*10)))};
        BEGIN {
            srand();
            wla=wl();
            while(1) {
                wlb=wla;
                wla=wl();
                if (wla==wlb)
                    {wla*=2;};
                d=(rand()*10+5)*rate/4;
                a=b=0; c=128;
                ca=40/wla; cb=20/wlb;
                de=rate/10; di=0;
                for (i=0;i<d;i++) {
                    a++; b++; di++; c+=ca+cb;
                    if (a>wla)
                        {a=0; ca*=-1};
                    if (b>wlb)
                        {b=0; cb*=-1};
                    if (di>de)
                        {di=0; ca*=0.9; cb*=0.9};
                    printf("%c",c)};
                c=int(c);
                while(c!=128) {
                    c<128?c++:c--;
                    printf("%c",c)};};}' | sox -t raw -r 64k -c 1 -e unsigned -b 8 - -d
    

[1]:
[https://www.reddit.com/r/coding/comments/48zvn4/bash_one_lin...](https://www.reddit.com/r/coding/comments/48zvn4/bash_one_liner_compose_music_from_entropy_in/)

~~~
geographomics
That's quite a delight for the ears. Reminds me of Stéphane Picq's work on the
Dune computer game soundtrack, back in the early 90s. It had some similar
sounds.

------
wookoouk
Anyone got this working on OSX afplay does not accept the same input :(

~~~
balducien
Another solution: [1]

    
    
        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 -traw -r44100 -b16 -e unsigned-integer - -tcoreaudio
    

1:
[https://www.reddit.com/r/programming/comments/48xf6k/bash_on...](https://www.reddit.com/r/programming/comments/48xf6k/bash_one_liner_compose_music_from_entropy_in/d0nu6ta)

~~~
pavel_lishin
Heads up, that is incredibly loud.

------
tinix
This reminds me of something similar, music from malloc, and file reads... by
hooking with ld_preload.

Code: [https://github.com/gordol/ld_preload-
sounds](https://github.com/gordol/ld_preload-sounds)

Preview: [https://soundcloud.com/glowdon/malloc-read-hook-self-
compile](https://soundcloud.com/glowdon/malloc-read-hook-self-compile)

------
kroger
I wrote something similar (but using Python), on how one can generate music
using mathematical sequences such as Fibonacci's:

[http://pedrokroger.net/pascals-triangle-
sound/](http://pedrokroger.net/pascals-triangle-sound/)

I used to spend a lot of time on The On-Line Encyclopedia of Integer Sequences
;-)

------
kerny
I don't know if I'm the only one who have noticed that, but on 64 bit machines
you have to change "%08X\n" to "%016X\n". Although using %08X sounds
interesting, the result is not a major scale and probably not the original
intent of the code.

------
visy
Something a bit less random but on the same realm of things:

[http://countercomplex.blogspot.fi/2011/10/algorithmic-
sympho...](http://countercomplex.blogspot.fi/2011/10/algorithmic-symphonies-
from-one-line-of.html)

------
phatbyte
I was listening to Esbjron Svensson Trio, and then I tried this...my ears...

~~~
logicrook
If you're into jazz, you can check the label Rune Grammofon; you can start
bands that are easy to listen to, such as Elephant9 (just listen to Fugl Fønix
and try to resist the groove), and then get into the rest (woohoo, Shining).
After that, you shouldn't find these bleep-bloops that hard.

------
michaelmior
Handy smoke test for a good RNG? Not sure I see any case where it would
actually be useful for this in practice, but I find the idea amusing in
theory.

~~~
jerf
You may be able to hear or see that a _very_ bad RNG is bad (there are
certainly sufficiently bad ones for which you can), but you can not hear or
see that one is good.

~~~
michaelmior
My thoughts exactly. You certainly wouldn't want to rely on this to identify a
good RNG, but you might be able to catch a bad one.

------
bipin_nag
Who knew chaos sounds so melodious. You sir are a genius.

------
chei0aiV
I hope everyone is turning off CSS before copy-pasting this command-line into
their terminal :)

[http://thejh.net/misc/website-terminal-copy-
paste](http://thejh.net/misc/website-terminal-copy-paste)

~~~
Raphmedia
Always paste to the browser url bar and then copy again. This clears any extra
tags you might carry over and will display anything that might be hidden. You
could copy and paste to notepad, but the browser bar is closer.

~~~
chei0aiV
Great point, however the browser bar is only a single line. I guess this is
where a global shortcut to start a text editor would be useful.

