2929NOTE_BASE = ["C" , "C#" , "D" , "D#" , "E" , "F" , "F#" , "G" , "G#" , "A" , "A#" , "B" ]
3030
3131
32- def note_or_name (input ):
32+ def note_or_name (value ):
3333 """Bidirectionally translates a MIDI sequential note value to a note name
3434 or a note name to a MIDI sequential note value. Note values are
3535 of integer type in the range of 0 to 127 (inclusive). Note names are
@@ -38,17 +38,16 @@ def note_or_name(input):
3838 'F#9' (note value 127). If the input value is outside of the note
3939 value or name range, the value of None is returned.
4040
41- :param union(int, str) input : The note name or note value input. Note value
41+ :param union(int, str) value : The note name or note value input. Note value
4242 is an integer; note name is a string. No default value.
4343 """
44- if type ( input ) == str :
44+ if isinstance ( value , str ) :
4545 # Input is a string, so it's a note name
46- return name_to_note (input )
47- elif type ( input ) == int :
46+ return name_to_note (value )
47+ if isinstance ( value , int ) :
4848 # Input is an integer, so it's a note value
49- return note_to_name (input )
50- else :
51- return None # Invalid input parameter type
49+ return note_to_name (value )
50+ return None # Invalid input parameter type
5251
5352
5453def note_to_name (note ):
@@ -60,23 +59,22 @@ def note_to_name(note):
6059 the value of None is returned.
6160
6261 :param int note: The note value input in the range of 0 to 127 (inclusive).
63- No default.
62+ No default value .
6463 """
6564 if 0 <= note <= 127 :
6665 return NOTE_BASE [note % 12 ] + str ((note // 12 ) - 1 )
67- else :
68- return None # Note value outside valid range
66+ return None # Note value outside valid range
6967
7068
7169def name_to_note (name ):
7270 """Translates a note name to a MIDI sequential note value. Note names are
73- character strings expressed in the format NoteOctave, such as
71+ character strings expressed in the NoteOctave format , such as
7472 'C4' or 'G#7'. Note names can range from 'C-1' (note value 0) to
7573 'F#9' (note value 127). Note values are of integer type in the range
7674 of 0 to 127 (inclusive). If the input value is outside of that range,
7775 the value of None is returned.
7876
79- :param str name: The note name input in NoteOctave format. No default.
77+ :param str name: The note name input in NoteOctave format. No default value .
8078 """
8179 name = name .upper () # Convert lower to uppercase
8280 if (name [:1 ] or name [:2 ]) in NOTE_BASE :
@@ -90,8 +88,7 @@ def name_to_note(name):
9088 note = NOTE_BASE .index (name [0 ])
9189 octave = int (name [1 :])
9290 return note + (12 * (octave + 1 )) # Calculated note value
93- else :
94- return None # Input string is not in NOTE_BASE
91+ return None # Input string is not in NOTE_BASE
9592
9693
9794def note_to_frequency (note ):
@@ -100,18 +97,17 @@ def note_to_frequency(note):
10097 127 (inclusive). Frequency values are floating point. If the input
10198 note value is less than 0 or greater than 127, the input is
10299 invalid and the value of None is returned. Ref: MIDI Tuning Standard
103- formula.
100+ formula: https://en.wikipedia.org/wiki/MIDI_tuning_standard
104101
105- :param int note: The note value input in the range of 0 to 127 (inclusive).
106- No default.
102+ :param int note: The MIDI note value input in the range of 0 to 127
103+ (inclusive). No default.
107104 """
108105 if 0 <= note <= 127 :
109106 return pow (2 , (note - 69 ) / 12 ) * 440
110- else :
111- return None # note value outside valid range
107+ return None # note value outside valid range
112108
113109
114- def frequency_to_note (freq ):
110+ def frequency_to_note (frequency ):
115111 """Translates a frequency in Hertz (Hz) to the closest MIDI sequential
116112 note value. Frequency values are floating point. Note values are of
117113 integer type in the range of 0 to 127 (inclusive). If the input
@@ -120,12 +116,11 @@ def frequency_to_note(freq):
120116 is returned. Ref: MIDI Tuning Standard formula:
121117 https://en.wikipedia.org/wiki/MIDI_tuning_standard
122118
123- :param float freq : The frequency value input. No default.
119+ :param float frequency : The frequency value input in Hz . No default.
124120 """
125- if (pow (2 , (0 - 69 ) / 12 ) * 440 ) <= freq <= (pow (2 , (127 - 69 ) / 12 ) * 440 ):
126- return int (69 + (12 * log (freq / 440 , 2 )))
127- else :
128- return None # Frequency outside valid range
121+ if (pow (2 , (0 - 69 ) / 12 ) * 440 ) <= frequency <= (pow (2 , (127 - 69 ) / 12 ) * 440 ):
122+ return int (69 + (12 * log (frequency / 440 , 2 )))
123+ return None # Frequency outside valid range
129124
130125
131126# Controller descriptions -- no list offset
@@ -265,5 +260,8 @@ def frequency_to_note(freq):
265260def cc_code_to_description (cc_code ):
266261 """Provides a controller description decoded from a Control Change code value.
267262 https://www.midi.org/specifications-old/item/table-3-control-change-messages-data-bytes-2
263+
264+ :param int cc_code: The Control Change code value in the range of 0 to 127.
265+ No default value.
268266 """
269267 return CONTROLLERS [cc_code ]
0 commit comments