Source code for musif.musicxml.scoring

from typing import List, Optional, Tuple

from music21.stream.base import Part
from roman import fromRoman, toRoman

from musif.config import ExtractConfiguration

# last tokens of music21 instrumentSound values that qualify an instrument
# rather than name one (e.g. 'wind.reed.saxophone.alto')
VARIANT_QUALIFIERS = {
    "alto",
    "tenor",
    "soprano",
    "sopranino",
    "baritone",
    "bass",
    "contrabass",
    "acoustic",
    "electric",
    "piccolo",
}

ROMAN_NUMERALS_FROM_1_TO_20 = [toRoman(i).upper() for i in range(1, 21)]

[docs] def to_abbreviation(part: Part, parts: List[Part], cfg: ExtractConfiguration) -> str: """ Returns abbreviation name for a specific part based on the sound name Parameters ---------- part: str Part to get abbreviaton from parts: List[part] List of parts in the score cfg: ExtractConfiguration ExtractConfiguration object """ sound = extract_sound(part, cfg) return list(_extract_abbreviated_part(sound, part, parts, cfg))[0]
[docs] def extract_sound(part: Part, config: ExtractConfiguration) -> str: """ Returns sound name for a specific part based on the sound name Part: VnI -> Sound name: Vn Parameters ---------- part: str Part to get abbreviaton from cfg: ExtractConfiguration ExtractConfiguration object """ instrument = part.getInstrument(returnDefault=False) sound_name = None if instrument is None or instrument.instrumentSound is None: sound_name = part.partName.strip().split(' ')[0] if sound_name not in config.sound_to_abbreviation: sound_name = _replace_naming_exceptions(sound_name, part) sound_name = sound_name if sound_name[-1] != 's' else sound_name[:-1] else: tokens = [ token for token in instrument.instrumentSound.split(".") if "flat" not in token and "sharp" not in token and len(token) > 2 ] if tokens: sound_name = tokens[-1] # in sounds like 'saxophone.alto', 'clarinet.bass' or # 'guitar.acoustic' the last token is a variant qualifier, not the # instrument: prefer the preceding token when it is a known sound. # EXCEPT when both tokens name sounds of the same family # ('wind.flutes.flute.piccolo'): there the last token is the more # specific instrument, not a qualifier if ( len(tokens) > 1 and sound_name in VARIANT_QUALIFIERS and tokens[-2] != "voice" and tokens[-2].replace("-", " ").lower() in config.sound_to_abbreviation ): last_family = config.sound_to_family.get( sound_name.replace("-", " ").lower() ) prev_family = config.sound_to_family.get( tokens[-2].replace("-", " ").lower() ) if last_family is None or last_family != prev_family: sound_name = tokens[-2] if sound_name: sound_name = sound_name.replace('-', ' ').lower() sound_name = _replace_naming_exceptions(sound_name, part) return sound_name else: return ''
def _extract_abbreviated_part(sound: str, part: Part, parts: List[Part], config: ExtractConfiguration) -> Tuple[str, str, int]: if sound not in config.sound_to_abbreviation: abbreviation = part.partAbbreviation # may contain I, II or whatever abbreviation_parts = abbreviation.split(" ") abbreviation = abbreviation_parts[0].lower().replace(".", "") + ( abbreviation_parts[1] if len(abbreviation_parts) > 1 else "" ) else: abbreviation = config.sound_to_abbreviation[sound] # Only a numeral written in the part's own name numbers it ("Violino # II", "Ob. I"). Staves left with the same abbreviation are one logical # part: two sopranos of a duet are `sop`, pooled. (A numbering by # position among "the same instruments" used to be tried as well. It # compared music21 instruments, which are equal whenever they are of the # same class, and it only ran with the cache off: there it numbered a # soprano and a tenor as `sopI` and `tenII`, so the names of the columns # depended on a setting that should not touch them.) part_roman_number = _get_part_roman_number(part) other_number = _get_part_normal_number(part) part_number = fromRoman(part_roman_number) if part_roman_number else (other_number if other_number else 0) sound_abbreviation = abbreviation.split('(')[0] return abbreviation + (part_roman_number or ""), sound_abbreviation, part_number def _get_part_normal_number(part: Part) -> Optional[str]: if '(' and ')' in part.partAbbreviation: return part.partAbbreviation.split('(')[1].split(')')[0] return None def _get_part_roman_number(part: Part) -> Optional[str]: for number in ROMAN_NUMERALS_FROM_1_TO_20: if part.partAbbreviation.endswith(f". {number}") or part.partName.endswith( f" {number}" ): return number return None def _replace_naming_exceptions(sound: str, part: Part) -> str: if 'da caccia' in sound: sound = sound.replace('da caccia', '') if 'tromba' in sound: sound = 'horn' if 'bass' == sound: # determines if voice or string instrument if len(part.lyrics()) == 0: sound = "basso continuo" if "french" in sound and "horn" in sound: sound = "horn" if "cello" in sound and "bass" in part.partName.lower(): sound = "basso continuo" return sound