History

[py] improve code style of midiconv.py

Paul Brossier committed 8 years ago

Commit 9374e571 parent 2c8ada61 file · +16 −11Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/lib/aubio/midiconv.py+16−11
@@ -1,11 +1,11 @@
1# -*- coding: utf-8 -*-1# -*- coding: utf-8 -*-
2""" utilities to convert midi note number to and from note names """2""" utilities to convert midi note number to and from note names """
3 3
4__all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq']
5
6import sys4import sys
7from ._aubio import freqtomidi, miditofreq5from ._aubio import freqtomidi, miditofreq
8 6
7__all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq']
8
9py3 = sys.version_info[0] == 39py3 = sys.version_info[0] == 3
10if py3:10if py3:
11 str_instances = str11 str_instances = str
@@ -14,6 +14,7 @@ else: · 2 unmodified lines
14 str_instances = (str, unicode)14 str_instances = (str, unicode)
15 int_instances = (int, long)15 int_instances = (int, long)
16 16
17
17def note2midi(note):18def note2midi(note):
18 """Convert note name to midi note number.19 """Convert note name to midi note number.
19 20
@@ -55,13 +56,14 @@ def note2midi(note): · 35 unmodified lines
55 --------56 --------
56 midi2note, freqtomidi, miditofreq57 midi2note, freqtomidi, miditofreq
57 """58 """
58 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}59 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7,
60 'A': 9, 'B': 11}
59 _valid_modifiers = {61 _valid_modifiers = {
60 u'𝄫': -2, # double flat62 u'𝄫': -2, # double flat
61 u'♭': -1, 'b': -1, '\u266d': -1, # simple flat63 u'♭': -1, 'b': -1, '\u266d': -1, # simple flat
62 u'♮': 0, '\u266e': 0, None: 0, # natural64 u'♮': 0, '\u266e': 0, None: 0, # natural
63 '#': +1, u'♯': +1, '\u266f': +1, # sharp65 '#': +1, u'♯': +1, '\u266f': +1, # sharp
64 u'𝄪': +2, # double sharp66 u'𝄪': +2, # double sharp
65 }67 }
66 _valid_octaves = range(-1, 10)68 _valid_octaves = range(-1, 10)
67 if not isinstance(note, str_instances):69 if not isinstance(note, str_instances):
@@ -93,12 +95,13 @@ def note2midi(note): · 25 unmodified lines
93 if octave not in _valid_octaves:95 if octave not in _valid_octaves:
94 raise ValueError("%s is not a valid octave" % octave)96 raise ValueError("%s is not a valid octave" % octave)
95 97
96 midi = 12 + octave * 12 + _valid_notenames[notename] \98 midi = (octave + 1) * 12 + _valid_notenames[notename] \
97 + _valid_modifiers[modifier]99 + _valid_modifiers[modifier]
98 if midi > 127:100 if midi > 127:
99 raise ValueError("%s is outside of the range C-2 to G8" % note)101 raise ValueError("%s is outside of the range C-2 to G8" % note)
100 return midi102 return midi
101 103
104
102def midi2note(midi):105def midi2note(midi):
103 """Convert midi note number to note name.106 """Convert midi note number to note name.
104 107
@@ -136,9 +139,10 @@ def midi2note(midi): · 31 unmodified lines
136 msg = "an integer between 0 and 127 is excepted, got {:d}"139 msg = "an integer between 0 and 127 is excepted, got {:d}"
137 raise ValueError(msg.format(midi))140 raise ValueError(msg.format(midi))
138 _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#',141 _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#',
139 'A', 'A#', 'B']142 'A', 'A#', 'B']
140 return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)143 return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)
141 144
145
142def freq2note(freq):146def freq2note(freq):
143 """Convert frequency in Hz to nearest note name.147 """Convert frequency in Hz to nearest note name.
144 148
@@ -162,6 +166,7 @@ def freq2note(freq): · 17 unmodified lines
162 nearest_note = int(freqtomidi(freq) + .5)166 nearest_note = int(freqtomidi(freq) + .5)
163 return midi2note(nearest_note)167 return midi2note(nearest_note)
164 168
169
165def note2freq(note):170def note2freq(note):
166 """Convert note name to corresponding frequency, in Hz.171 """Convert note name to corresponding frequency, in Hz.
167 172

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t