-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguitar_string.py
187 lines (144 loc) · 5.73 KB
/
guitar_string.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from equal_temperament import equal_temperament as et
class GuitarString:
"""
Represents a guitar string with various properties and methods.
Attributes:
tuning (str): The tuning note of the string.
note (int): The distance of the tuning note from A4.
"""
def __init__(self, tuning):
"""
Initializes a new instance of the GuitarString class.
Args:
tuning (str): The tuning note of the string.
Raises:
ValueError: If the tuning note is invalid.
"""
if tuning not in et.all_note_names:
raise ValueError(f"Invalid tuning note {tuning}")
self.tuning = tuning
self.note = self.distance_from_A4()
def note_name_at_fret(self, fret):
"""
Calculates the note name at a given fret on the string.
Args:
fret (int): The fret number.
Returns:
str: The note name at the specified fret.
Raises:
ValueError: If the fret number is invalid.
"""
if fret < 0:
raise ValueError(f"Invalid fret {fret}")
if fret == 0:
return self.tuning
index = (et.all_note_names.index(self.tuning) + fret) % len(et.note_names)
octave = (et.all_note_names.index(self.tuning) + fret) // len(et.note_names)
return f'{et.note_names[index][0]}{octave}'
def distance_from_A4(self):
"""
Calculates the distance of the tuning note from A4.
Returns:
int: The distance of the tuning note from A4.
"""
return et.all_note_names.index(self.tuning) - et.all_note_names.index('A4')
def frequency_at_fret(self, fret):
"""
Calculates the frequency of the note at a given fret on the string.
Args:
fret (int): The fret number.
Returns:
float: The frequency of the note at the specified fret.
"""
return et.frequency_at_fret(self.tuning, fret)
def frequency(self):
"""
Calculates the frequency of the open string.
Returns:
float: The frequency of the open string.
"""
return et.frequency(self.tuning[:-1], int(self.tuning[-1]))
def __str__(self):
"""
Returns a string representation of the GuitarString object.
Returns:
str: The string representation of the GuitarString object.
"""
return f"GuitarString({self.tuning})"
def __repr__(self):
"""
Returns a string representation of the GuitarString object.
Returns:
str: The string representation of the GuitarString object.
"""
return f"GuitarString({self.tuning})"
def __eq__(self, other):
"""
Checks if two GuitarString objects are equal.
Args:
other (GuitarString): The other GuitarString object to compare.
Returns:
bool: True if the two objects are equal, False otherwise.
"""
return self.tuning == other.tuning
def __hash__(self):
"""
Returns the hash value of the GuitarString object.
Returns:
int: The hash value of the GuitarString object.
"""
return hash(self.tuning)
def __lt__(self, other):
"""
Checks if the current GuitarString object is less than the other GuitarString object.
Args:
other (GuitarString): The other GuitarString object to compare.
Returns:
bool: True if the current object is less than the other object, False otherwise.
"""
return self.tuning < other.tuning
def __le__(self, other):
"""
Checks if the current GuitarString object is less than or equal to the other GuitarString object.
Args:
other (GuitarString): The other GuitarString object to compare.
Returns:
bool: True if the current object is less than or equal to the other object, False otherwise.
"""
return self.tuning <= other.tuning
def __gt__(self, other):
"""
Checks if the current GuitarString object is greater than the other GuitarString object.
Args:
other (GuitarString): The other GuitarString object to compare.
Returns:
bool: True if the current object is greater than the other object, False otherwise.
"""
return self.tuning > other.tuning
def __ge__(self, other):
"""
Checks if the current GuitarString object is greater than or equal to the other GuitarString object.
Args:
other (GuitarString): The other GuitarString object to compare.
Returns:
bool: True if the current object is greater than or equal to the other object, False otherwise.
"""
return self.tuning >= other.tuning
def __add__(self, other):
"""
Adds two GuitarString objects together.
Args:
other (GuitarString): The other GuitarString object to add.
Returns:
str: The result of adding the two GuitarString objects.
"""
return self.tuning + other.tuning
def __sub__(self, other):
"""
Subtracts one GuitarString object from another.
Args:
other (GuitarString): The other GuitarString object to subtract.
Returns:
str: The result of subtracting the two GuitarString objects.
"""
return self.tuning - other.tuning