Skip to content

Instantly share code, notes, and snippets.

@devxpy
Last active May 22, 2024 06:29
Show Gist options
  • Save devxpy/063968e0a2ef9b6db0bd6af8079dad2a to your computer and use it in GitHub Desktop.
Save devxpy/063968e0a2ef9b6db0bd6af8079dad2a to your computer and use it in GitHub Desktop.
A python script that converts MIDI message numbers to notes and instruments (and vice-versa)
INSTRUMENTS = [
'Acoustic Grand Piano',
'Bright Acoustic Piano',
'Electric Grand Piano',
'Honky-tonk Piano',
'Electric Piano 1',
'Electric Piano 2',
'Harpsichord',
'Clavi',
'Celesta',
'Glockenspiel',
'Music Box',
'Vibraphone',
'Marimba',
'Xylophone',
'Tubular Bells',
'Dulcimer',
'Drawbar Organ',
'Percussive Organ',
'Rock Organ',
'Church Organ',
'Reed Organ',
'Accordion',
'Harmonica',
'Tango Accordion',
'Acoustic Guitar (nylon)',
'Acoustic Guitar (steel)',
'Electric Guitar (jazz)',
'Electric Guitar (clean)',
'Electric Guitar (muted)',
'Overdriven Guitar',
'Distortion Guitar',
'Guitar harmonics',
'Acoustic Bass',
'Electric Bass (finger)',
'Electric Bass (pick)',
'Fretless Bass',
'Slap Bass 1',
'Slap Bass 2',
'Synth Bass 1',
'Synth Bass 2',
'Violin',
'Viola',
'Cello',
'Contrabass',
'Tremolo Strings',
'Pizzicato Strings',
'Orchestral Harp',
'Timpani',
'String Ensemble 1',
'String Ensemble 2',
'SynthStrings 1',
'SynthStrings 2',
'Choir Aahs',
'Voice Oohs',
'Synth Voice',
'Orchestra Hit',
'Trumpet',
'Trombone',
'Tuba',
'Muted Trumpet',
'French Horn',
'Brass Section',
'SynthBrass 1',
'SynthBrass 2',
'Soprano Sax',
'Alto Sax',
'Tenor Sax',
'Baritone Sax',
'Oboe',
'English Horn',
'Bassoon',
'Clarinet',
'Piccolo',
'Flute',
'Recorder',
'Pan Flute',
'Blown Bottle',
'Shakuhachi',
'Whistle',
'Ocarina',
'Lead 1 (square)',
'Lead 2 (sawtooth)',
'Lead 3 (calliope)',
'Lead 4 (chiff)',
'Lead 5 (charang)',
'Lead 6 (voice)',
'Lead 7 (fifths)',
'Lead 8 (bass + lead)',
'Pad 1 (new age)',
'Pad 2 (warm)',
'Pad 3 (polysynth)',
'Pad 4 (choir)',
'Pad 5 (bowed)',
'Pad 6 (metallic)',
'Pad 7 (halo)',
'Pad 8 (sweep)',
'FX 1 (rain)',
'FX 2 (soundtrack)',
'FX 3 (crystal)',
'FX 4 (atmosphere)',
'FX 5 (brightness)',
'FX 6 (goblins)',
'FX 7 (echoes)',
'FX 8 (sci-fi)',
'Sitar',
'Banjo',
'Shamisen',
'Koto',
'Kalimba',
'Bag pipe',
'Fiddle',
'Shanai',
'Tinkle Bell',
'Agogo',
'Steel Drums',
'Woodblock',
'Taiko Drum',
'Melodic Tom',
'Synth Drum',
'Reverse Cymbal',
'Guitar Fret Noise',
'Breath Noise',
'Seashore',
'Bird Tweet',
'Telephone Ring',
'Helicopter',
'Applause',
'Gunshot'
]
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 instrument_to_program(instrument: str) -> int:
assert instrument in INSTRUMENTS, errors['program']
return INSTRUMENTS.index(instrument) + 1
def program_to_instrument(program: int) -> str:
assert 1 <= program <= 128, errors['program']
return INSTRUMENTS[program - 1]
def number_to_note(number: int) -> tuple:
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: str, octave: int) -> int:
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
@devxpy
Copy link
Author

devxpy commented Feb 3, 2018

USAGE

>> note_to_number('C#', 5)
61

>> number_to_note(61)
('C#', 5)

>> instrument_to_program('Violin')
41

>> program_to_instrument(41)
'Violin'

@after5cst
Copy link

Thank you.

@codelif
Copy link

codelif commented Jan 18, 2022

Thank you!

@niheibu
Copy link

niheibu commented Jul 11, 2022

WOW! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment