Skip to content

Instantly share code, notes, and snippets.

@thebiss
Created April 13, 2021 00:30
Show Gist options
  • Save thebiss/5ab190289a8a4bb7243ca1b0b0df5525 to your computer and use it in GitHub Desktop.
Save thebiss/5ab190289a8a4bb7243ca1b0b0df5525 to your computer and use it in GitHub Desktop.
Adaptation of Antonio Macchi's music player, for freebsd sh (from https://tldp.org/LDP/abs/html/devref1.html#MUSICSCR)
#!/bin/sh
# music.sh
# Music without external files
# Author: Antonio Macchi
# Used in ABS Guide with permission.
#
# revised for SH by BB
# 1. Some echo's don't support -e -n, so use printf instead
# 2. Some printf's don't support hex (\x), only octal escapes (\000) with %b format for binary data.
# /dev/dsp default = 8000 frames per second, 8 bits per frame (1 byte),
#+ 1 channel (mono)
duration=2000 # If 8000 bytes = 1 second, then 2000 = 1/4 second.
#volume='\xc0' # Max volume = \xff (or \x00).
volumeOctal='\0300'
#mute='\x80' # No volume = \x80 (the middle).
muteOctal='\0200'
#
# $1=Note Hz in bytes
# (e.g. A4 = 440Hz ::
# 8000 fps / 440 = 18 :: A = 18 bytes per second, harmonics on multiples of this.)
#
mknote ()
{
for t in $(seq 0 $duration)
do
test $(( t % $1 )) = 0 && printf "%b" "$volumeOctal" || printf "%b" "$muteOctal"
done
}
a4=$(mknote 18)
e=$(mknote 49)
g=$(mknote 41)
a=$(mknote 36)
b=$(mknote 32)
c=$(mknote 30)
cis=$(mknote 29)
d=$(mknote 27)
e2=$(mknote 24)
n=$(mknote 32767)
# European notation.
printf "$g$e2$d$c$d$c$a$g$n$g$e$n$g$e2$d$c$c$b$c$cis$n$cis$d$n$g$e2$d$c$d$c$a$g$n$g$e$n$g$a$d$c$b$a$b$c" > /dev/dsp
# dsp = Digital Signal Processor
exit # A "bonny" example of an elegant shell script!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment