Need to play high-fidelity white noise? Listen to /dev/urandom on OSX!

At the FSFE Berlin meet yesterday, one recently-become-father fellow showed us he Chromadoze app on his android phone. This app worked wonders in putting his baby to sleep by playing white noise across his android phone’s speaker. apparently, there’s a lot of people that use white noise either to fall asleep, or to reach a state of utter concentration. (Useful if you want to build a multi-threaded operating system in two hours, for example.)

This got us started on a small discussion on how to do this on desktop computers and tablets. Of course, /dev/random on Linux is perfect for exactly this kind of noise generation. OSX has something similar, called /dev/urandom/ and I wanted to share a snippet with you guys so that you, too, can enjoy the soothing glory of high-fidelity white noise during your next coding spree.

First install sox

Natively, OSX has no /dev/audio or something like it. So you need the sox package, a command-line interface to OSX’ Core Audio. You can get that in various ways, but I’ve found installing it using Homebrew is easiest. First install Homebrew, then you can do brew install sox

The code

cat /dev/urandom | sox -traw -r44100 -b16 -e unsigned-integer - -tcoreaudio

Explanation

First, we take the contents of the random generator by doing cat /dev/urandom/, after which we pipe them to sox with | sox.

Sox takes a couple of options to tell it what kind of data it’s receiving:

-traw Tells sox to consider incoming data as raw, uncompressed audio.
-r44100 Sets the input sampling rate to 44.1 Kiloherz.
-b16 Tells sox the bit-depth for this audio stream is 16 bits.
-e unsigned-integer Says that the raw data should be considere an unsigned integer. (Which are the kind of numbers /dev/urandom outputs.)
- This stray minus sign is very important! It tells sox to take the stream that was piped to it as a source.
-tcoreaudio tells sox to use the ‘coreaudio’ device for output.

Let me know what you do with this!