Skip to content

Instantly share code, notes, and snippets.

@robhanlon22
Created March 17, 2023 03:42
Show Gist options
  • Save robhanlon22/cb9a2adacfe40e8d9473c677251d6be9 to your computer and use it in GitHub Desktop.
Save robhanlon22/cb9a2adacfe40e8d9473c677251d6be9 to your computer and use it in GitHub Desktop.
ChatGPT-generated bash script that generates an 808 clap from scratch
#!/bin/bash
duration=0.1
decay=0.3
volume=0.6
dd if=/dev/urandom bs=1 count=$((44100*duration)) 2>/dev/null | \
od -An -vtx1 | \
tr ' ' '\n' | \
while read hex; do
byte=$(printf "%d" "0x$hex")
if ((byte > 127)); then
byte=$((byte - 256))
fi
echo "$byte"
done > white_noise.raw
f1=2000
f2=20000
q=100
w0=$(echo "2*a(1)*($f2-$f1)/44100" | bc -l)
alpha=$(echo "s($w0)/(2*$q)" | bc -l)
cosw0=$(echo "c($w0)" | bc -l)
a0=1
a1=$(echo "-2*$cosw0" | bc -l)
a2=1
b0=$(echo "(1-$cosw0)/2" | bc -l)
b1=$(echo "1-$cosw0" | bc -l)
b2=$(echo "(1-$cosw0)/2" | bc -l)
x1=0
x2=0
y1=0
y2=0
sample_num=0
cat white_noise.raw | while read sample; do
x0=$sample
y0=$(echo "($b0/$a0)*$x0 + ($b1/$a0)*$x1 + ($b2/$a0)*$x2 - ($a1/$a0)*$y1 - ($a2/$a0)*$y2" | bc -l)
x2=$x1
x1=$x0
y2=$y1
y1=$y0
if ((sample_num < decay*44100)); then
echo $(echo "$volume*$y0/$decay" | bc -l)
else
echo "0"
fi
((sample_num++))
done > clap_processed.raw
max_val=$(cat clap_processed.raw | tr '\n' ' ' | tr -s ' ' | tr ' ' '\n' | sort -n | tail -n 1)
cat clap_processed.raw | while read sample; do
echo $(echo "$sample*32767/$max_val" | bc -l)
done > clap_normalized.raw
echo -ne "$(printf 'RIFF%.4sWAVE' '')$(printf 'fmt %.4s\x10\x00\x00\x00\x01\x00\x01\x00\x80\x3e\x00\x00\x00\x7d\x00\x00\x02\x00\x10\x00data%.4s' '')$(cat clap_normalized.raw | tr '\n' ' ' | tr -s ' ' | tr ' ' '\n' | while read sample; do echo -ne $(printf '\\x%02x\\x%02x' $((sample & 0xff)) $((sample >> 8))); done)" > clap.wav
rm white_noise.raw clap_processed.raw clap_normalized.raw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment