Skip to content

Instantly share code, notes, and snippets.

@turbq
Created October 17, 2023 10:31
Show Gist options
  • Save turbq/bfb26b7017740a2a2b5039cd8c886c99 to your computer and use it in GitHub Desktop.
Save turbq/bfb26b7017740a2a2b5039cd8c886c99 to your computer and use it in GitHub Desktop.
Play doom theme through aplay without external files
#Idea based on https://tldp.org/LDP/abs/html/devref1.html#MUSICSCR
#default = 8000 frames per second, 8 bits per frame (1 byte),
#1 channel (mono)
duration=1000 # If 8000 bytes = 1 second, then 1000 = 1/8 second.
volume=$'\xff' # Max volume = \xff (or \x00).
mute=$'\x80' # No volume = \x80 (the middle).
function mknote () # $1=Note Hz in bytes (e.g. A = 440Hz ::
{ #+ 8000 fps / 440 = 16 :: A = 16 bytes per second)
for t in `seq 0 $2`
do
test $(( $t % $1 )) = 0 && echo -n $volume || echo -n $mute
done
}
function mk2note () # here I'm trying several notes at one time
{
for t in `seq 0 $3`
do
test $(( $t % $1 )) = 0 && echo -n $volume ||
test $(( $t % $2 )) = 0 && echo -n $volume || echo -n $mute
done
}
function mk4note ()
{
for t in `seq 0 $5`
do
test $(( $t % $1 )) = 0 && echo -n $volume ||
test $(( $t % $2 )) = 0 && echo -n $volume ||
test $(( $t % $3 )) = 0 && echo -n $volume ||
test $(( $t % $4 )) = 0 && echo -n $volume || echo -n $mute
done
}
#two notes at same time
E2B2=`mk2note 97 65 $duration`
E3B2=`mk2note 48 65 $duration`
E3Fd3=`mk2note 48 43 $duration`
Ad3E3=`mk2note 69 48 4000`
Ad3F3=`mk2note 34 46 $duration`
Ad3F3E3B2=`mk4note 34 46 48 65 4000`
A3E4=`mk2note 36 24 $duration`
C4G3=`mk2note 30 41 $duration`
D4A3=`mk2note 27 36 $duration`
E4B3=`mk2note 24 32 $duration`
E2=`mknote 97 $duration`
E3=`mknote 48 $duration`
F3=`mknote 46 $duration`
G3=`mknote 41 $duration`
A3=`mknote 36 $duration`
Ad3=`mknote 34 $duration`
B3=`mknote 32 $duration`
C4=`mknote 30 $duration`
D4=`mknote 27 $duration`
E4=`mknote 24 $duration`
A4=`mknote 36 $duration`
#short notes
sDd4=`mknote 26 500`
sE4=`mknote 24 500`
sF4=`mknote 23 500`
sG4=`mknote 20 500`
sA4=`mknote 18 500`
sB4=`mknote 16 500`
#blank note
n=`mknote 32767 $duration`
echo -n "$E2$E3$E4$E3 $E3$D4$E3$E3 \
$C4$E3$E3$Ad3 $E3$E3$B3$C4 \
$E2$E3$E4$E3 $E3$D4$E3$E3 \
$C4$E3$E3$Ad3 $Ad3E3 \
$E2$E3$E4$E3 $E3$D4$E3$E3 \
$C4$E3$E3$Ad3 $E3$E3$B3$C4 \
$E2$E3$E4$E3 $E3$D4$E3$E3 \
$C4$E3$E3$Ad3 $Ad3E3 \
$E2B2$E3$E4B3$E3 $E3B2$A3E4$E3$E3B2 \
$C4G3$E3$E3B2$Ad3F3 $E3$E3B2$B3Fd3$C4G3 \
$E2$E3$E4B3$E3 $E3B2$D4A3$E3$E3B2 \
$C4G3$E3$E3B2$Ad3F3 $Ad3F3E3B2 \
$E2B2$E3$E4B3$E3 $E3B2$A3E4$E3$E3B2 \
$C4G3$E3$E3B2$Ad3F3 $E3$E3B2$B3Fd3$C4G3 \
$E2$E3$E4B3$E3 $E3B2$D4A3$E3$E3B2 \
$sF4$sE4$sDd4$sF4 $sA4$sG4$sF4$sDd4 $sF4$sG4$sA4$sB4 $sA4$sG4$sF4$sDd4\
" | aplay
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment