-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalcFreq.py
More file actions
executable file
·116 lines (103 loc) · 2.94 KB
/
calcFreq.py
File metadata and controls
executable file
·116 lines (103 loc) · 2.94 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/python3
import sys
# This script will calculate the frequency of a note using various tuning methods
# Parameters will be: [tuning_method] [truncate] note_number [note_number2 ...]
# Valid note_number will range from 0 to whatever, really.
# tuning methods will be optional (default to equal temperment), but will also have others in the future.
# truncate option will cut off any decimal portion for ease of use.
# Base note is A 440, which is A4.
base_freq = 440
base_note = 69
base_note_name = "A"
# Returns the Friendly name of the note
def name(note):
toRet = ""
n = note % 12
if n == 0:
toRet = "C"
if n == 1:
toRet = "C#/Db"
if n == 2:
toRet = "D"
if n == 3:
toRet = "D#/Eb"
if n == 4:
toRet = "E"
if n == 5:
toRet = "F"
if n == 6:
toRet = "F#/Gb"
if n == 7:
toRet = "G"
if n == 8:
toRet = "G#/Ab"
if n == 9:
toRet = "A"
if n == 10:
toRet = "A#/Bb"
if n == 11:
toRet = "B"
return toRet
# Returns the Octave of a note
def octave(note):
return (note // 12) - 1 #want Integer Division Here
# Calculates with Equal Temperment
def equalTemperment(note):
a = pow(2, 1/12)
#print(str(a) + " " + str(1/12) + " " + str(a * 12))
return base_freq * pow(a, note - base_note)
# Returns the frequency of a note using a specified tuning method
def freq(note, tuningMethod):
tm = tuningMethod.lower()
if tm == "et":
return equalTemperment(note)
else:
print("ERR: Unsupported!")
return -1
#Display Help
def displayHelp():
print("Usage: ./calcFreq.py [tuning_method] [-t (Truncate Frequency)] note_number [note_number ...]")
print("[tuning_method] => -T:{et:just:test}")
print("-t Truncate Frequency")
print("Base note is " + str(base_note_name) + " where Note is " + str(base_note) + " and frequency is " + str(base_freq))
# Testing for Sanity
def _selfTest():
print("Sanity Test: Note 81 should be A in Octave 5 with Frequency 880")
testNote = 81
print("Results: Name: " + str(name(testNote)) + " Octave: " + str(octave(testNote)) + " Frequency: " + str(freq(testNote, "et")) + " Truncated: " + str(int(freq(testNote, "et"))))
#Reads Command Args
def readArgs():
note = ""
tuningMethod = "et"
truncate = False
argnum = -1
if len(sys.argv) <= 1:
displayHelp()
return
for arg in sys.argv:
argnum += 1
#print(str(argnum) + ":" + str(arg))
#Ignore the command itself
if argnum == 0:
continue
# Can be either -tr or -t or a note number:
else:
if arg.find("-T:") != -1:
tuningMethod = arg[3:]
elif arg.find("-t") != -1:
truncate = True
# this should be a note number
elif arg.isdigit():
res = freq(int(arg), tuningMethod)
if truncate == False:
print("{0:.3f}".format(res))
else:
print("{0:.0f}".format(res))
else:
print("Invalid Syntax: " + arg)
displayHelp()
return()
# Code Here
#displayHelp()
#_selfTest()
readArgs()