"white noise" generator with sox for Linux
I am a light sleeper. So a while back I was thinking about getting a "white noise" generator for my bedroom.
Then I remembered that my computer sits right across from my bed. Certainly there must be a software option...
Sox is "the swiss army knife of sound processing programs". It comes standard on most GNU/Linux distributions and is available for other platforms.
It includes sound generation capabilties, for pure tones and for white noise. More agreeable for my purpose, though, is "pink noise", which is also in sox's bag of tricks.
After a bit of experimentation, I found the following shell script produced agreeable results:
pinknoise:
#!/bin/sh len='7:00:00' if [ "$1" != '' ]; then len=$1 fi sox -t sl - -t sl - synth $len pinknoise < /dev/zero |\ sox -t sl - -t ossdsp /dev/dsp band -n 1200 200 vibro 20 .1
The script takes an optional argument providing the length of time to run and produce sound (hh:mm:ss format); it defaults to seven hours, where its output will mask minor noises to allow a decent night's sleep. The first sox invocation generates the noise, while the second applies a filter and a light vibrato effect that I find a little more pleasant than the raw noise.
You may have to change the "-t ossdsp" and/or the "/dev/dsp" depending on your audio setup. man sox to discover other options. Share and enjoy.
September 2009 update
The latest sox renamed the "vibro" effect to "tremolo", breaking my script when I updated. That was annoying. I've also reworked the thing so the pipe is no longer needed. Here's the revised, updated version:
#!/bin/sh
len='7:00:00'
if [ "$1" != '' ]; then
len=$1
fi
play -t sl - synth $len pinknoise \
band -n 1200 200 tremolo 20 .1 < /dev/zero
Thanks all for your interest.



Comments
I like it!
This beats the heck out of "cat /dev/urandom > /dev/dsp". The band filter is nice to take out the pops.
Groovy
I will probably keep looking at other options such as Broodle to see what is out there. But this is a really cool one liner. I had no idea that sox could do this! Should somebody be looking for such a thing I'll be sure and point them to this page.
Thanks a lot.
Updates
With recent versions of sox, things are a little simpler:
play -n synth 60:00 brownnoiseproduces brown noise for an hour. (Replace brown with pink/white if you prefer. My baby sleeps best with brown).
brown noise...
Have to say I thought this was a "brown note" joke, but the "brown" in "brown noise" means Brownian motion. It's also called red noise. I learned something today, hooray!
Tom Swiss - proprietor, unreasonable.org
thanks -- this works great!
This tip is a godsend to me. Just switching to Ubuntu on my new netbook and this works like a charm. I was missing the app Noise, which I use on the Mac. I wonder if this would work using Terminal on the Mac? Anyway, thanks so much.
vibro effect renamed
I'm still working out how to do this using a single command, I.E. without the pipe, but the 'vibro' effect is now called tremolo in the latest version of Sox.
Thanks for posting this anyway, I've had much more luck in 5 minutes with this than with Boodle.
vibro -> tremolo
I just figured out the rename (it was driving me nuts that it broke after I upgraded my box) and came over to update the page, only to find your comment.
The two sox invocations can be replaced with one call to play:
play -t sl - synth $len pinknoise band -n 1200 200 tremolo 20 .1 < /dev/zero
Tom Swiss - proprietor, unreasonable.org
Even simpler
You can replace the "-t sl -" and "< /dev/zero" parts with the "-n" option, so your sox invocation becomes:
play -n synth $len pinknoise band -n 1200 200 tremolo 20 .1
So how do I combine it with sine-wave generation?
Suppose I want to do Brainwave Entrainment, so I want to play a 400hz pure tone in the left ear, and a 410hz tone in the right ear, and then I wanted to merge with that the pink or brown noise at 1/4th the volume. Can sox actually generate all that in a single command line?
And can sox do frequency shifts?
That would be awesome if it could! :)
There's an app for that
Have a look at http://gnaural.sourceforge.net/
LENGTH='5:00' sox -m "|sox -n
LENGTH='5:00'
sox -m "|sox -n -c 2 -p synth $LENGTH brown gain -5" "|sox -n -p synth $LENGTH sine 400 sine 410 gain -3" -t alsa fade h 5
# (you may need to change 'alsa' to fit your sound setup)
# varying frequencies example
play -n synth 2 square 440-880 fade h 2 gain -5 : synth 2 square 880-660 gain -5
App
Is there a way to turn this script into a program for Windows or even a mobile phone?
Thanks
The brown noise sounds the best in my opinion.
I am sure sox can output
I am sure sox can output into a file, which could be looped on the mobile device, but it brings a few aches, like large memory consumption, pops at the end, and such, but... It works :)
Thanks a million
Thanks a lot for this. I had previously been using boodler's noise libraries but this method is far superior since you can adjust the bandpass just how you like it right from the command line. I have been using something like this:
play -r 32000 -t sl - synth $len pinknoise band -n 600 400 tremolo 20 .1 < /dev/zero
Hey,
Thanks a lot for this.like large memory consumption, pops at the end but works .I'm still working out how to do this using a single command
Overnoising noisy neighbors
I adapted the line to a "my neighbor is having a party and I need to study" situation:
play -c 2 -n synth pinknoise band -n 2500 4000 reverb 20
The band pass is centered on human voice frequencies and wide enough to also cover most of the musical frequency range, without producing annoying high-pitched noise. The slight reverb adds a background/ambient quality for less distraction.
Documented the code, bonus: 95% reduction in CPU usage
Thanks for the excellent article, and useful comments. Learned alot about the sox package and signal processing. I documented and modified the one-liner to a full script with detailed references:
http://gist.github.com/1209835
The CPU usage was high if the process was running for a long time, so I found a way to reduce it by 95%.
TODO [ ] - could add volume oscillation (amplitude modulation) to simulate waves or breathing pattern -- any suggestions for accomplishing this ??
Post new comment