summaryrefslogtreecommitdiffstats
path: root/src/fss.sh
blob: e4e21351dc222306c2814faa5b0a6d29d1c3b72f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/sh
#
# "Free Software Song" player
#
# Copyright (C) 2016  Patrick "P. J." McDermott
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

play()
{
	local octave_shift="${1}"
	local note_length="${2}"
	shift 2
	local note=
	local octave=
	local length=
	local freq=

	while [ ${#} -gt 0 ]; do
		note="${1%_*}"
		octave="${1%:*}"
		octave="${octave#*_}"
		length="${1#*:}"
		shift 1
		case "${note}" in
			C)     note=-9;;
			Cs|Df) note=-8;;
			D)     note=-7;;
			Ds|Ef) note=-6;;
			E)     note=-5;;
			F)     note=-4;;
			Fs|Gf) note=-3;;
			G)     note=-2;;
			Gs|Af) note=-1;;
			A)     note=0;;
			As|Bf) note=1;;
			B)     note=2;;
		esac
		# A note's frequency is:
		#        n/12
		#   f = 2     * 440 Hz
		# where n is the distance from A4.
		freq=$(awk -v octave_shift=${octave_shift} -v octave=${octave} \
			-v note=${note} '
			BEGIN {
				octaves = octave_shift + octave - 4 + note / 12;
				freq = 2 ^ octaves * 440;
				print(int(freq + 0.5));
			}
			')
		length=$(($length * $note_length))
		beep -f ${freq} -l ${length}
	done
}

# "Free Software Song"
#
# Time signature: 7/8
# Octaves: 4, 5
# Tempo: 252 BPM
# Duration: 0:20
#
# The notation for the "Free Software Song" is from
# <https://www.gnu.org/music/free-software-song.html>.

play 4 238 \
	D_1:2 C_1:1 B_0:2 A_0:2 \
	B_0:2 C_1:1 B_0:1 A_0:1 G_0:2 \
	G_0:3 A_0:3 B_0:1 \
	C_1:3 B_0:2 B_0:1 D_1:1 \
	A_0:3 A_0:4 \
	D_1:2 C_1:1 B_0:4 \
	\
	D_1:2 C_1:1 B_0:2 A_0:2 \
	B_0:2 C_1:1 B_0:1 A_0:1 G_0:2 \
	G_0:3 A_0:3 B_0:1 \
	C_1:3 B_0:2 B_0:1 D_1:1 \
	A_0:3 A_0:4 \
	A_0:7