History

[py] [docs] add documentation for note2midi, midi2note, and freq2note

Paul Brossier committed 8 years ago

Commit 7165fc51 parent fb1c5e21 file · +89 −3Browse files

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

Unified · Split

python/lib/aubio/midiconv.py+89−3
@@ -13,7 +13,46 @@ else: · 12 unmodified lines
13 int_instances = (int, long)13 int_instances = (int, long)
14 14
15def note2midi(note):15def note2midi(note):
16 " convert note name to midi note number, e.g. [C-1, G9] -> [0, 127] "16 """Convert note name to midi note number.
17
18 Input string `note` should be composed of one note root
19 and one octave, with optionally one modifier in between.
20
21 List of valid components:
22
23 - note roots: `C`, `D`, `E`, `F`, `G`, `A`, `B`,
24 - modifiers: `b`, `#`, as well as unicode characters
25 `𝄫`, `♭`, `♮`, `♯` and `𝄪`,
26 - octave numbers: `-1` -> `11`.
27
28 Parameters
29 ----------
30 note : str
31 note name
32
33 Returns
34 -------
35 int
36 corresponding midi note number
37
38 Examples
39 --------
40 >>> aubio.note2midi('C#4')
41 61
42 >>> aubio.note2midi('B♭5')
43 82
44
45 Raises
46 ------
47 TypeError
48 If `note` was not a string.
49 ValueError
50 If an error was found while converting `note`.
51
52 See Also
53 --------
54 midi2note, freqtomidi, miditofreq
55 """
17 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}56 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
18 _valid_modifiers = {57 _valid_modifiers = {
19 u'𝄫': -2, # double flat58 u'𝄫': -2, # double flat
@@ -57,7 +96,36 @@ def note2midi(note): · 37 unmodified lines
57 return midi96 return midi
58 97
59def midi2note(midi):98def midi2note(midi):
60 " convert midi note number to note name, e.g. [0, 127] -> [C-1, G9] "99 """Convert midi note number to note name.
100
101 Parameters
102 ----------
103 midi : int [0, 128]
104 input midi note number
105
106 Returns
107 -------
108 str
109 note name
110
111 Examples
112 --------
113 >>> aubio.midi2note(70)
114 'A#4'
115 >>> aubio.midi2note(59)
116 'B3'
117
118 Raises
119 ------
120 TypeError
121 If `midi` was not an integer.
122 ValueError
123 If `midi` is out of the range `[0, 128]`.
124
125 See Also
126 --------
127 note2midi, miditofreq, freqtomidi
128 """
61 if not isinstance(midi, int_instances):129 if not isinstance(midi, int_instances):
62 raise TypeError("an integer is required, got %s" % midi)130 raise TypeError("an integer is required, got %s" % midi)
63 if midi not in range(0, 128):131 if midi not in range(0, 128):
@@ -66,6 +134,24 @@ def midi2note(midi): · 2 unmodified lines
66 return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)134 return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)
67 135
68def freq2note(freq):136def freq2note(freq):
69 " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] "137 """Convert frequency in Hz to nearest note name.
138
139 Parameters
140 ----------
141 freq : float [0, 23000[
142 input frequency, in Hz
143
144 Returns
145 -------
146 str
147 name of the nearest note
148
149 Example
150 -------
151 >>> aubio.freq2note(440)
152 'A4'
153 >>> aubio.freq2note(220.1)
154 'A3'
155 """
70 from aubio import freqtomidi156 from aubio import freqtomidi
71 return midi2note(int(freqtomidi(freq)))157 return midi2note(int(freqtomidi(freq)))

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