-
Notifications
You must be signed in to change notification settings - Fork 0
/
grub_init_gen.py
executable file
·104 lines (80 loc) · 2.68 KB
/
grub_init_gen.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
import argparse
import sys
from frequencies import FREQ_TABLE
from duration import DURATION_TABLE
def print_grub_init(tempo, notes):
output = [tempo]
for note in notes:
parts = note.split(" ")
if len(parts) != 2:
raise ValueError("Incorrect format for input")
note, duration = parts[0].upper(), parts[1]
try:
output.append(FREQ_TABLE[note])
except KeyError:
raise ValueError(f"{note} not a valid value, aborting")
try:
output.append(DURATION_TABLE[duration])
except KeyError:
raise ValueError(f"{duration} not a valid value, aborting")
for duration in [64, 32, 16, 8]:
if str(duration) in output:
adjust_output(duration, output)
break
print(" ".join(output))
def adjust_output(factor, output):
output[0] = str(int(output[0]) * (factor // 4))
duration_values = set(DURATION_TABLE.values())
for index, note in enumerate(output[1:], start=1):
if note in duration_values:
if note == "1":
output[index] = str(factor)
else:
output[index] = str(factor // int(note))
def parse_args(argv):
parser = argparse.ArgumentParser(prog="grub_init_gen",
description="GRUB_INIT_TUNE generator")
parser.add_argument(
"-q",
"--quiet",
help="Prints only the resulting code, useful for inputs from files",
action="store",
nargs="*",
)
return parser.parse_args(argv)
def main(argv):
args = parse_args(argv)
quiet = args.quiet is not None
if not quiet:
tempo = input("What's the BPM?")
print("Notes are typed as follows:")
print("NOTE DURATION [with a '.' after it if it's a dotted note]")
print("If it's a rest, REST DURATION")
print("You can also type in comments starting with #, which will be "
"ignored (useful if you're passing the notes from a file).\n")
prompt = (
"What is the next note and its duration? (NOTE DURATION[.])\n"
"If you want to end the melody, type END\n")
else:
tempo = input()
prompt = ""
notes = []
while True:
note = input(prompt)
if note.upper() == "END":
break
elif note.startswith("#"):
continue
notes.append(note)
try:
print_grub_init(tempo, notes)
except ValueError as e:
print(e)
sys.exit(1)
if __name__ == "__main__":
try:
main(sys.argv[1:])
except KeyboardInterrupt:
print("\nScript aborted")
sys.exit(1)