taellinglin's picture
MIDI patches: a colour per channel, a timbre per patch family
90dd634 verified
Raw History Blame Contribute Delete
4.2 kB
"""General MIDI patches: names, instrument families, a timbre for each family, and a colour for each channel.
A note is [start s, length s, pitch, velocity 0..1, channel, program]; the program (0..127, or 128 for the drum kit on channel 10) is the patch
that was selected on its channel when it sounded. Notes found by the FFT have no sixth field: they count as program 0.
"""
from ling_hatch import KEYS
NAMES = (
'Acoustic Grand Piano,Bright Acoustic Piano,Electric Grand Piano,Honky-tonk Piano,Electric Piano 1,Electric Piano 2,Harpsichord,Clavinet,'
'Celesta,Glockenspiel,Music Box,Vibraphone,Marimba,Xylophone,Tubular Bells,Dulcimer,'
'Drawbar Organ,Percussive Organ,Rock Organ,Church Organ,Reed Organ,Accordion,Harmonica,Tango Accordion,'
'Nylon Guitar,Steel Guitar,Jazz Guitar,Clean Guitar,Muted Guitar,Overdriven Guitar,Distortion Guitar,Guitar Harmonics,'
'Acoustic Bass,Finger Bass,Pick Bass,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,Synth Strings 1,Synth Strings 2,Choir Aahs,Voice Oohs,Synth Voice,Orchestra Hit,'
'Trumpet,Trombone,Tuba,Muted Trumpet,French Horn,Brass Section,Synth Brass 1,Synth Brass 2,'
'Soprano Sax,Alto Sax,Tenor Sax,Baritone Sax,Oboe,English Horn,Bassoon,Clarinet,'
'Piccolo,Flute,Recorder,Pan Flute,Blown Bottle,Shakuhachi,Whistle,Ocarina,'
'Square Lead,Saw Lead,Calliope Lead,Chiff Lead,Charang Lead,Voice Lead,Fifths Lead,Bass and Lead,'
'New Age Pad,Warm Pad,Polysynth Pad,Choir Pad,Bowed Pad,Metallic Pad,Halo Pad,Sweep Pad,'
'Rain,Soundtrack,Crystal,Atmosphere,Brightness,Goblins,Echoes,Sci-Fi,'
'Sitar,Banjo,Shamisen,Koto,Kalimba,Bagpipe,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').split(',')
FAMILIES = ['Piano', 'Chromatic percussion', 'Organ', 'Guitar', 'Bass', 'Strings', 'Ensemble', 'Brass', 'Reed', 'Pipe', 'Synth lead', 'Synth pad',
'Synth effects', 'Ethnic', 'Percussive', 'Sound effects']
# per family: (wave, attack s, decay time constant s (0: held), release s, lowpass Hz (0: none))
TIMBRES = [('triangle', 0.005, 1.6, 0.25, 0), ('sine', 0.002, 0.6, 0.3, 0), ('square', 0.01, 0.0, 0.05, 2400), ('sawtooth', 0.004, 0.9, 0.2, 2200),
('sawtooth', 0.008, 1.2, 0.15, 700), ('sawtooth', 0.12, 0.0, 0.3, 3200), ('sawtooth', 0.15, 0.0, 0.35, 2600), ('sawtooth', 0.05, 0.0, 0.12, 2400),
('square', 0.03, 0.0, 0.12, 1800), ('sine', 0.06, 0.0, 0.15, 0), ('square', 0.01, 0.0, 0.08, 3500), ('sawtooth', 0.35, 0.0, 0.5, 1500),
('sawtooth', 0.2, 0.0, 0.6, 1200), ('triangle', 0.005, 1.0, 0.2, 0), ('square', 0.001, 0.25, 0.05, 1500), ('sine', 0.1, 0.8, 0.4, 0)]
def name(program: int) -> str:
return 'Drum kit' if program >= 128 else NAMES[max(0, min(127, int(program)))]
def program_of(note) -> int:
return 128 if note[4] == 9 else (int(note[5]) if len(note) > 5 else 0)
def timbre(program: int):
return TIMBRES[(min(127, max(0, int(program)))) >> 3]
def channel_hues(notes) -> dict:
"""channel -> which of the seven key colours (an index into KEYS). The drum channel is the first colour, red; the other channels follow
in the order they first sound, one colour each, going round the seven."""
first = {}
for n in notes: first.setdefault(int(n[4]), n[0])
hues = {}; k = 0
for ch in sorted(first, key=lambda c: (c != 9, first[c])):
if ch == 9: hues[ch] = 0
else: k += 1; hues[ch] = k % 7
return hues
def channels(notes) -> list:
"""The channels of a tune, for a legend: [{'channel', 'hue' (index into KEYS), 'key', 'program', 'patch', 'notes'}]"""
hues = channel_hues(notes); prog = {}; count = {}
for n in notes:
c = int(n[4]); count[c] = count.get(c, 0) + 1; prog.setdefault(c, program_of(n))
return [{'channel': c + 1, 'hue': hues[c], 'key': KEYS[hues[c]], 'program': prog[c], 'patch': name(prog[c]), 'notes': count[c]} for c in sorted(hues)]