Skip to content

Instantly share code, notes, and snippets.

@clort81
Last active November 8, 2021 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clort81/cc01e4376a25d5103a212fa223670197 to your computer and use it in GitHub Desktop.
Save clort81/cc01e4376a25d5103a212fa223670197 to your computer and use it in GitHub Desktop.
Bash Scripting Sound Audio Beep pcspkr
#!/bin/bash
# 255 mysmps SIMPLE SAWTOOTH SOUND
# <-----------------------> \/\/\/\/\/\/\/\/\/\/\
# . . A beep for bash shell
# | mypeak | for machines without
# \ | /\ pc speaker beep \007.
# \ | / \
# \ | / \ It requires alsa and
# \ | / \ 'play' via alsautils.
# \ | / \
# \ | / \
#126 ---------------------|---------------------------------------------------------
# \ | / \
# \ | / \ /
# \ | / \ /
# \ | / When invoking mynote() \ /
# \ / mysmps * myslop may not \ /
# \/ exceed 255. We entrust \/
# mytrou you to use mynote with
# care & respect for math.
#
# 1
mynote() {
mysmps=$1 # number of samples before wave reverses (1/2 cycle)
myslop=$2 # vertical step increment of the slope (steepness)
mycycl=$3 # number of times to repeat (duration of beep)
mypeak=$((126 + mysmps))
mytrou=$((126 - mysmps))
for ((i = 1; i <= mycycl; i++)); do # outer loop: cycles
for ((j = mypeak; j >= mytrou; j -= myslop)); do # falling slope
printf -v hex %x "$j" # hexen magic
printf "\x$hex"
done
for ((j = mytrou; j <= mypeak; j += myslop)); do # rising slope
printf -v hex %x "$j"
printf "\x$hex"
done
done
}
bignote=$(mynote 24 4 300)
#echo -ne "$bignote" > /tmp/beep.raw # Save for visual inspection or
#od -tx1 /tmp/beep.raw # further adventures in beeping.
for i in {1..50}; do # Repeat for beeping glory.
for i in {1..3}; do
echo -ne "$bignote" | aplay -t raw -q -f U8 --channels=2 --rate=48000
sleep 0.7s
done
sleep 3s
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment