Generate hours of white, pink and brown noise in R

[This article was first published on R programming tutorials and exercises for data science and mathematics, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

To leave a comment for the author, please follow the link and comment on their blog: R programming tutorials and exercises for data science and mathematics.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)