-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvertNote.py
More file actions
executable file
·80 lines (72 loc) · 1.79 KB
/
convertNote.py
File metadata and controls
executable file
·80 lines (72 loc) · 1.79 KB
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
#!/bin/python3
import sys
# Usage: convertNote.py <Note Name> <Note Octave>
# Converts a note octave and name into midi number
# Converts Note into MIDI Number
def convertNote(note, octave):
val = -1
adj = 0 #Adjust the Octave in some edge cases
# Add support for a Testing "Space": note 0
if note == "_":
return 0
if note == "B#":
adj = 1
if note == "Cb":
adj = -1
if note == "C" or note == "B#":
val = 0
if note == "C#" or note == "Db":
val = 1
if note == "D":
val = 2
if note == "D#" or note == "Eb":
val = 3
if note == "E" or note == "Fb":
val = 4
if note == "F" or note == "E#":
val = 5
if note == "F#" or note == "Gb":
val = 6
if note == "G":
val = 7
if note == "G#" or note == "Ab":
val = 8
if note == "A":
val = 9
if note == "A#" or note == "Bb":
val = 10
if note == "B" or note == "Cb":
val = 11
val += (octave + 1 + adj) * 12
return val
def usage():
print("Usage: ./convertNote.py <Note Name> <Octave> [next, etc...]")
print("Note names: C, C#/Db, D, ... , A#/Bb, B, B#/C. Enharmonics are supported up to 1 half step.")
print(" Underscore Character \"_\" is used for a space (really 0 note with very low frequency)")
#Reads Command Args
def readArgs():
argnum = -1
hasNote = False
note = "X"
if len(sys.argv) <= 2:
usage()
return
for arg in sys.argv:
argnum += 1
#print(str(argnum) + ":" + str(arg))
#Ignore the command itself
if argnum == 0:
continue
else:
if not hasNote:
note = str(arg)
hasNote = True
else:
hasNote = False
val = convertNote(note, int(arg))
if val < 0:
print("ERR: Incorrect Value! " + note + " " + arg)
usage()
return
print(val)
readArgs()