Last active
January 3, 2023 09:41
-
-
Save 27Bslash6/d145cc0fe698fa2588931558e38189a5 to your computer and use it in GitHub Desktop.
OSX Bash script that beeps a pure sinewave, variable by number of beeps, delay and volume
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#shellcheck disable=SC2034 | |
# beep | |
# ********************************************************* | |
# Generate a beep shell command for OSX 10.7.5+ | |
# Original idea (C)2012, B.Walker, G0LCU. | |
# Now issued as Public Domain, CC0. You may do with it as you please. | |
repeat=${1:-1} | |
volume=${2:-.05} | |
delay=${3:-.3} | |
function usage { | |
echo "Usage: $(basename $0) [OPTION|OPTION2|...] [repeat] [volume] [delay] | |
Build and test artifacts in this repository | |
EXAMPLE | |
# Beep 3 times, volume 0.05, with a delay of 0.1 seconds between beeps, | |
$(basename $0) -d 0.5 -r 3 -v 0.01 | |
# or | |
$(basename $0) 3 0.05 0.1 | |
OPTIONS | |
-d Delay between beeps in seconds | |
-h Help information (this text) | |
-r Repeat beeps n times, eg | |
$(basename $0) -r 5 | |
-v Volume (range 0.01 ... 1) | |
" | |
} | |
OPTIONS=':d:hr:v:' | |
while getopts $OPTIONS option | |
do | |
case $option in | |
d ) delay=$OPTARG;; | |
h ) usage | |
exit;; | |
r ) repeat=$OPTARG;; | |
v ) volume=$OPTARG | |
[[ $volume -gt 1 ]] && volume=1 | |
;; | |
* ) echo "Unkown option: ${OPTARG}" | |
usage | |
exit 1;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
true > /tmp/sinewave.wav | |
printf "\x52\x49\x46\x46\x64\x1F\x00\x00\x57\x41\x56\x45\x66\x6D\x74\x20\x10\x00\x00\x00\x01\x00\x01\x00\x40\x1F\x00\x00\x40\x1F\x00\x00\x01\x00\x08\x00\x64\x61\x74\x61\x40\x1F\x00\x00" >> /tmp/sinewave.wav | |
for i in {0..500} | |
do | |
printf "\x80\x26\x00\x26\x7F\xD9\xFF\xD9" >> /tmp/sinewave.wav | |
done | |
for j in $(seq 1 $repeat) | |
do | |
# The line below uses various Linux flavours, "aplay"... | |
# aplay /tmp/sinewave.wav | |
# Use the OSX default basic command line audio player, "/usr/bin/afplay". | |
afplay -v ${volume} /tmp/sinewave.wav | |
# "afinfo" is OSX also... | |
# afinfo /tmp/sinewave.wav | |
sleep $delay | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment