-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathmidiconvert.py
More file actions
executable file
·31 lines (22 loc) · 952 Bytes
/
midiconvert.py
File metadata and controls
executable file
·31 lines (22 loc) · 952 Bytes
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
NOTES = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
OCTAVES = list(range(11))
NOTES_IN_OCTAVE = len(NOTES)
errors = {
'program': 'Bad input, please refer this spec-\n'
'http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/program_change.htm',
'notes': 'Bad input, please refer this spec-\n'
'http://www.electronics.dit.ie/staff/tscarff/Music_technology/midi/midi_note_numbers_for_octaves.htm'
}
def number_to_note(number):
octave = number // NOTES_IN_OCTAVE
assert octave in OCTAVES, errors['notes']
assert 0 <= number <= 127, errors['notes']
note = NOTES[number % NOTES_IN_OCTAVE]
return note, octave
def note_to_number(note, octave):
assert note in NOTES, errors['notes']
assert octave in OCTAVES, errors['notes']
note = NOTES.index(note)
note += (NOTES_IN_OCTAVE * octave)
assert 0 <= note <= 127, errors['notes']
return note