Hacker News new | past | comments | ask | show | jobs | submit login
'Music' from /dev/urandom (robertelder.org)
175 points by signa11 on Mar 7, 2016 | hide | past | web | favorite | 56 comments



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


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


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.


Oops, I'm sorry. Looks better now?


I have this version and the authors "sad" version playing together. It's weird because after about 10 or so minutes they stop sounding random and become almost like chiptune jazz.


80s/90s computer game soundtrack...


This sounds better than my version.


It really does! At twice the sampling rate it's pretty interesting, too.

Incidentally, this little idea rocks. It's amazing what's buried down there in Unix command-line utilities.


I received an email suggestion yesterday of a variant of this command that places it inside a function, so that you could play multiple instruments at once with function calls. I guess you could make an entire music composing utility in bash if you wanted to.


anyone got a sample?



Here is a recording created by someone on reddit:

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/linux/comments/48xj46/bash_one_line...

https://www.reddit.com/r/coding/comments/48zvn4/bash_one_lin...

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.


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


> 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/

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

Edit: changed http://security.stackexchange.com/questions/3936/is-a-rand-f... to the current link, since the current link is more to the point.


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


Back in the day you could just do:

  cat /dev/urandom > /dev/audio
http://everything2.com/title/catting+weird+things+to+%252Fde...


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


Yeah. It is.


It's great! Using:

    </dev/urandom hexdump -v -e '/1 "%u\n"' | ...
save you from spawning one process.


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


> 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.


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?


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.


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.


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.


Part of this exercise was to leverage highly portable and backward compatible utilities as much as possible, so I expect stuff like cat and awk to work mostly the same everywhere. 'aplay' is the one I was not sure about. If I recall correctly, it was even necessary to explicitly 'sudo apt-get install' on older versions of Ubuntu.


aplay is part of ALSA, which is old enough that I wouldn't expect any differences between it on Ubuntu & on other Linuxes.

FWIW, I really dig the demo: it's cool.


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


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...


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.


A triangle/sawtooth wave sounds nicer without any filtering or other effects. It's the only "elementary" waveform that I know of that doesn't sound earpiercingly harsh.


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


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...


Heads up, that is incredibly loud.


I did, install sox and use:

> ... | play -c 2 -r 16000 -e signed -L -b 32 -t raw -


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

Preview: https://soundcloud.com/glowdon/malloc-read-hook-self-compile


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/

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


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.


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

http://countercomplex.blogspot.fi/2011/10/algorithmic-sympho...


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


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.


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.


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.


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.


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


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

http://thejh.net/misc/website-terminal-copy-paste


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.


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.


you mean js?


I mean CSS, as explained by the link I posted.


It was an excellent share you although I don't quite think "turning off CSS" would be my preferred defense.

You can paste to an intermediate area to be sure you didn't catch something hidden from whichever site you don't trust (which allows users to inline arbitrary HTML elements with arbitrary styles).

This is a habit one already might be in if they want to avoid being tracked from clicking on, say, Google search results. You'll avoid some of their tracking features if you copy the link you want and paste it to some text-holding zone first. (This also avoids the site you land on knowing your "referer".)


If you try to copy a search result, it turns into a Google url that redirects to the result.

On iOS this doesn't happen for some reason.


I knew it would take a few bare minutes before someone would come to make that distinction about how Google search links work.

Then I would have to say "okay copy the domain and path from your search result, which isn't a link, and which Google probably hides already (so this new advice won't work any more) or they will hide it some day, so the system doesn't work as literally as I described it."

So if Google has found more and more ways to avoid people avoiding their tracking, that's fine, and what I said literally doesn't apply if you take it 100% literally, but at least you understand what I'm saying about how to avoid the naive copy paste surprise.


The onclick attribute of the search result does it, that may have something to do with that.


It does happen on Android. So apparently they treat onclick differently.


There are extensions to fix this




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: