Generate and listen to random sounds

February 9, 2022    Article    277 words    2 mins read

Let’s generate some random sounds (tuned a bit with the help of awk) for those lonely summer nights when you can’t sleep and want to listen to something that comes straight out of old Nintendo games. Don’t expect Bach but probably it’s better than the music of the last few years.

Caution, might induce weird feelings and the urge to eat a lot of chocolate or sushi. If the Russian president listens to this and invades Ukraine as a result, I shall not be held responsible.

Make sure you have SoX installed (Linux and macOS), if not, you can use aplay (Linux-only) but with different parameters.


$ brew install sox

Debian-based Linux:

$ sudo apt install sox


Never copy-paste stuff into your computer’s terminal unless you really understand what those commands do. The North Koreans are already in your terminal. Always.
$ 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(1024 * exp((a[$1 % 8] / 12) * log(2)) * i)) \
		}' | \
	xxd -r -p | \
	play -c2 -b8 -eunsigned -traw -r32k -

or as a one-liner:

$ 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(1024*exp((a[$1%8]/12)*log(2))*i)) }' | xxd -r -p | play -c2 -b8 -eunsigned -traw -r32k -

If you use aplay, replace the last part with:

aplay -c 2 -f S32_LE -r 32000

Feel free to play with the awk part that generates the sound; the outcome should sound similar to this: