Skip to content

Instantly share code, notes, and snippets.

@frostschutz
Created September 24, 2024 09:58
Show Gist options
  • Save frostschutz/0e88a14a4b3e2a17ca0b88407c18e5ad to your computer and use it in GitHub Desktop.
Save frostschutz/0e88a14a4b3e2a17ca0b88407c18e5ad to your computer and use it in GitHub Desktop.
Play ambient noise with random sounds in the background. Sounds different every time you play it.
#!/bin/bash
# Ambient background noise with randomized sounds.
# You can get suitable sounds from freesound.org, BBC SFX library, and other places.
whitenoise="white-noise.ogg"
soundsdir="sounds"
if [ ! -e "${whitenoise}" ]
then
echo "White noise '${whitenoise}' not found."
exit 1
fi
if [ ! -d "${soundsdir}" ]
then
echo "Soundsdir '${soundsdir}' not found."
exit 1
fi
# Permanent background noise:
mpv --quiet --loop-playlist "${whitenoise}" &
whitenoisepid=$!
# Generate random silence between tracks:
# (has to be in a subdir of soundsdir, because mpv --shuffle dir1/ dir2/ does not expand dir2/)
if [ ! -d "${soundsdir}/silence" ]
then
echo "Generating random silence in '${soundsdir}/silence'..."
mkdir "${soundsdir}"/silence &&
for i in $(seq 100)
do
sox --null --comment="" "${soundsdir}/silence/silence-${i}.ogg" trim 0.0 $((SRANDOM%60)).$((SRANDOM))
done
fi
# Four channels of random sound effects (bird, rain, silence, wind, cricket, ...)
(
for i in $(seq 4)
do
sleep 0.$RANDOM && # mpv sometimes uses the same random seed
mpv --quiet --shuffle "${soundsdir}/" &
done
wait # for mpv sounds
)
# All sounds played. Stop the white noise.
kill "${whitenoisepid}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment