2 min read

Generate hours of white, pink and brown noise in R

Many studies suggest that white, pink and brown noise can improve focus, help with relaxation and sleep and even reduce crying in babies. Due to these potential benefits many options are available to generate color noise, including sound machines, phone apps and online clips.

R also provides tools to generate white, pink and brown noise: you can create your own sound clip of a desired length and save it to a local file. A helpful package in this process is tuneR, which offers many useful functions to process and generate sounds and write WAVE files.

Let’s start by loading the tuneR package and setting our default player for WAVE files (on my computer this is aplay):

library(tuneR)
setWavPlayer("aplay")

We can now easily generate multiple types of sounds via the noise function. The code below constructs a Wave object that holds 4 seconds of stereo brown noise:

brown_noise <- noise(kind = "red", duration = 4, xunit = "time", stereo = TRUE)
# kind = "red": brown noise
# kind = "white": white noise
# kind = "pink": pink noise

We can plot the two channels of this Wave object (both channels are generated independently):

plot(brown_noise)

And finally we can play the sound clip directly from R or save it to a local file:

play(brown_noise)
writeWave(brown_noise, filename = "brown_noise_sample.wav")

Pre-recorded loops of white, pink and brown noise

As a bonus I am including a pre-recorded loop for each noise type that you can listen to below and compare (if you are reading this post via RSS feed then visit my original page to access the audio).

White noise loop:
Pink noise loop:
Brown noise loop:

For a full collection of R programming tutorials and exercises visit my website at codeRtime.org and the codeRtime YouTube channel.