from collections import namedtuple # taken from https://imslp.org/wiki/IMSLP:Abbreviations_for_Instruments ABBREVIATIONS = """ score Score acc Accordion afl Alto flute alt Alto (voice) (contralto) arp Arpeggione bag Bagpipe bar Baritone (voice) bass Bass (voice) bbar Bass baritone (voice) bc Continuo (Basso continuo) bcl Bass clarinet bell Bell (Chimes) bfl Bass flute bgtr Bass guitar bjo Banjo bn Bassoon bob Bass oboe (Baritone oboe) br Brass instruments bryt Baryton bstcl Basset clarinet bsthn Basset horn bug Bugle cbcl Contrabass clarinet cbn Contrabassoon cch Children's chorus cel Celesta ch Mixed chorus cimb Cimbalom cit Cittern cl Clarinet clvd Clavichord cm Chalumeau conc Concertina crh Crumhorn crt Cornet crtt Cornett (Zink) cv Child's voice db Double Bass dlcn Dulcian dom Domra dulc Dulcimer egtr Electric guitar eh English horn (Cor anglais) elec Electronic Instruments epf Electric piano eq Equal voices erhu Erhu euph Euphonium fch Female chorus fda Flute d'amore (Tenor flute) fgh Flugelhorn fife Fife fl Flute flag Flageolet ghca Glass harmonica (Bowl organ) gl Glockenspiel gtr Guitar harm Harmonium hca Harmonica (Mouth Organ) heck Heckelphone hn Horn hp Harp hpd Harpsichord kbd Keyboard instrument lute Lute lyre Lyre mand Mandolin mar Marimba mch Male chorus mez Mezzo-soprano mus Musette nar Narrator (Reciter) ob Oboe oca Ocarina oda Oboe d'amore om Ondes Martenot oph Ophicleide orch Orchestra org Organ oud Oud pan Pan flute (Pan-pipes) perc Percussion pf Piano pf3h Piano 3 hands pf4h Piano 4 hands pf5h Piano 5 hands pf6h Piano 6 hands pflh Piano left hand pfped Pedal piano pfrh Piano right hand picc Piccolo pipa Pipa pk Timpani ptpt Piccolo trumpet reb Rebec rec Recorder sar Sarrusophone sax Saxophone sheng Sheng shw Shawm sit Sitar skbt Sackbut sop Soprano (voice) srp Serpent stpt Slide trumpet str String instruments sxh Saxhorn syn Synthesizer tba Tuba tbn Trombone ten Tenor thrm Theremin timp Timpani tpt Trumpet uch Unison chorus uke Ukelele (Ukulele) v Voice (solo) va Viola vap Viola pomposa vc Cello vda Viola d'amore vib Vibraphone vie Vielle (Hurdy-Gurdy) viol Viol (Viola da gamba) vlne Violone vn Violin vuv Vuvuzela vv Voices (multiple soloists) wag Wagner tuba ww Woodwind instruments xiao Xiao xyl Xylophone zith Zither """ INSTRUMENTS = [] for line in ABBREVIATIONS.split('\n'): parts = line.strip().split(maxsplit=1) if len(parts) < 2: continue name, _, _ = parts[1].partition('(') INSTRUMENTS.append((parts[0], name)) INSTRUMENT_LOOKUP = dict(INSTRUMENTS) TAG_LOOKUP = dict( ( (x[1].lower(), x[0]) for x in INSTRUMENTS ) ) class Instrument(namedtuple('Instrument', ('name', 'variant'), defaults=[None])): @classmethod def from_tag(cls, tag): """ >>> Instrument.from_tag('vn-1') Instrument(name='Violin', variant='1') >>> Instrument.from_tag('db') Instrument(name='Double Bass', variant=None) >>> Instrument.from_tag('Jaws Harp') Instrument(name='Jaws Harp', variant=None) """ abbr, _, variant = tag.partition('-') name = INSTRUMENT_LOOKUP.get(abbr.lower(), abbr) if variant: return cls(name, variant) return cls(name, None) def abbreviate(self): """ >>> Instrument('Violin', 1).abbreviate() 'vn-1' >>> Instrument('Double Bass').abbreviate() 'db' """ tag = TAG_LOOKUP.get(self.name.lower()) if self.variant: tag = f"{tag}-{self.variant}" return tag def __str__(self): """ >>> str(Instrument('Violin', 1)) 'Violin 1' >>> str(Instrument('Double Bass')) 'Double Bass' """ if self.variant: return f"{self.name} {self.variant}" return self.name if __name__ == "__main__": import doctest print(doctest.testmod())