Raspberry Pi white noise machine
September 19, 2021, modified on November 11, 2022
A white noise machine can help find sleep or mask ambient noise or even tinnitus. It can also be used to muffle conversation from potential eavesdroppers. I wanted to experiment with a white noise machine for sleep purposes so I decided to build one with a Raspberry Pi I had lying around.
For this you will need:
- A Raspberry Pi equipped with a jack port and an OS installed (I used a Pi 3B with Raspbian OS)
- Speakers connected to the Pi
Actually a Pi 3 is a bit overkill for this. I have a Pi Zero lying around that could fit the bill except it needs a USB dongle to connect speakers and I don't have such a dongle.
Preparing the white noise
First you need to find a white noise sample, preferably a long one. I chose to extract the sound from a 3 hour long YouTube video of a waterfall. Of course, choose what suits you. If you want to extract the sound from a YouTube video, you could use one of the many online tools but I won't recommend that to you. Instead, you can use youtube-dl:
$ youtube-dl -x --audio-format mp3 https://www.youtube.com/watch?v=dQw4w9WgXcQ
Some explanations for the options used:
-x
asks youtube-dl to extract the audio from the video. As stated in youtube-dl's help, ffmpeg or avconv is required for this
--audio-format
should be self-explanatory
Creating a white noise service
Next we are going to create a service to play the white noise automatically at system startup. My Raspberry Pi is running raspbian so systemd is used to manage services. Create a /etc/systemd/system/whitenoise.service
file with the following contents:
[Unit]
Description=White noise machine
StartLimitIntervalSec=0
[Service]
Type=simple
User=pi
ExecStart=mpg123 --loop -1 /home/pi/whitenoise.mp3
[Install]
WantedBy=multi-user.target
You should adjust your username and path to the file. I used mpg123 because it's the simplest and lightest audio player I could find. Its loop option is handy to keep playing infinitely. It needs to be installed:
# apt install mpg123
Auto start, auto stop
To start automatically the service on system startup, you just need to enable the service on startup:
# systemctl enable whitenoise
To stop it, you can use a cron job set at the time you want it to stop:
55 6 * * * sudo systemctl stop whitenoise
This will stop the white noise at 6:55am. And that's it! Enjoy your homemade white noise machine!