History

python/lib/aubio/midiconv.py: clean up, add some documentation

Paul Brossier committed 10 years ago

Commit 1827c491 parent cb89e511 file · +19 −17Browse files

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

Unified · Split

python/lib/aubio/midiconv.py+19−17
@@ -1,23 +1,26 @@
1# -*- coding: utf-8 -*-1# -*- coding: utf-8 -*-
2""" utilities to convert midi note number to and from note names """
2 3
3import sys4import sys
4PY3 = sys.version_info[0] == 35py3 = sys.version_info[0] == 3
5if PY3:6if py3:
6 string_types = [str]7 str_instances = [str]
8 int_instances = [int]
7else:9else:
8 string_types = [str, unicode]10 str_instances = [str, unicode]
11 int_instances = [int, long]
9 12
10def note2midi(note):13def note2midi(note):
11 " convert note name to midi note number, e.g. [C-1, G9] -> [0, 127] "14 " convert note name to midi note number, e.g. [C-1, G9] -> [0, 127] "
12 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}15 _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
13 _valid_modifiers = {None: 0, u'♮': 0, '#': +1, u'♯': +1, u'\udd2a': +2, 'b': -1, u'♭': -1, u'\ufffd': -2}16 _valid_modifiers = {None: 0, u'♮': 0, '#': +1, u'♯': +1, u'\udd2a': +2,
17 'b': -1, u'♭': -1, u'\ufffd': -2}
14 _valid_octaves = range(-1, 10)18 _valid_octaves = range(-1, 10)
15 if type(note) not in string_types:19 if isinstance(note, str_instances):
16 raise TypeError("a string is required, got %s (%s)" % (note, str(type(note)) ))20 raise TypeError("a string is required, got %s (%s)" % (note, str(type(note))))
17 if not (1 < len(note) < 5):21 if len(note) not in range(2, 5):
18 raise ValueError(22 raise ValueError("string of 2 to 4 characters expected, got %d (%s)" \
19 "string of 2 to 4 characters expected, got %d (%s)" %23 % (len(note), note))
20 (len(note), note))
21 notename, modifier, octave = [None]*324 notename, modifier, octave = [None]*3
22 25
23 if len(note) == 4:26 if len(note) == 4:
@@ -48,15 +51,14 @@ def note2midi(note): · 24 unmodified lines
48 51
49def midi2note(midi):52def midi2note(midi):
50 " convert midi note number to note name, e.g. [0, 127] -> [C-1, G9] "53 " convert midi note number to note name, e.g. [0, 127] -> [C-1, G9] "
51 if type(midi) != int:54 if isinstance(midi, int_instances):
52 raise TypeError("an integer is required, got %s" % midi)55 raise TypeError("an integer is required, got %s" % midi)
53 if not (-1 < midi < 128):56 if midi not in range(0, 128):
54 raise ValueError(57 raise ValueError("an integer between 0 and 127 is excepted, got %d" % midi)
55 "an integer between 0 and 127 is excepted, got %d" % midi)
56 midi = int(midi)
57 _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']58 _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
58 return _valid_notenames[midi % 12] + str( int(midi / 12) - 1 )59 return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)
59 60
60def freq2note(freq):61def freq2note(freq):
62 " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] "
61 from aubio import freqtomidi63 from aubio import freqtomidi
62 return midi2note(int(freqtomidi(freq)))64 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