Source code for musif.musicxml.tempo

import re
import xml.etree.ElementTree as ET
from enum import Enum
from typing import Optional

import pandas as pd


[docs] class TempoGroup2(Enum): """ Contains constants refering to grouped tempo on the second level that will be used by the tempo module. """ NA = "NA" SLOW = "Slow" MODERATE = "Moderate" FAST = "Fast"
[docs] def get_time_signature_type(time_signature: str) -> str: """ Given a time signature, it returns the correspondent type. Parameters ---------- time_signature: str Time signature in the form of string separated by '/' to obtain the time signature type. """ if time_signature in [ "1/2", "1/4", "1/8", "1/16", "2/2", "2/4", "2/8", "2/16", "4/4", "C", "4/2", "4/8", "4/16", "8/2", "8/4", "8/8", "8/16", ]: return "simple duple" elif time_signature in ["6/8", "6/2", "6/4", "6/16"]: return "compound duple" elif time_signature in ["12/2", "12/4", "12/8", "12/16"]: return "compound quadruple" # 3/8 is simple triple, as 3/4 and 3/16 are (it used to be classed as # compound duple). NumberOfBeats still counts one beat per bar in 3/8. elif time_signature in ["3/2", "3/4", "3/8", "3/16"]: return "simple triple" elif time_signature in ["9/2", "9/4", "9/8", "9/16"]: return "compound triple" else: return "other"
[docs] def get_tempo_grouped_1(tempo: str) -> str: """ Returns a 1st level of grouping for the tempo markings, removing secondary labels and diminutive endings. Parameters ---------- tempo: str Tempo string in italian to be extract the group from. """ tempo = re.sub("\\W+", " ", tempo) # removes eventual special characters # spellings found in sources: French and shortened forms of the minuet, # doubled consonants, and diminutives the endings below do not cover for spelling, standard in ( (r"\b(menuett?|minuet)\b", "minuetto"), (r"\badagg", "adag"), (r"\ballegrino\b", "allegretto"), (r"\bcomodetto\b", "comodo"), ): tempo = re.sub(spelling, standard, tempo, flags=re.IGNORECASE) replacements = [ (w, "") for w in [ "molto", "poco", "un poco", "tanto", "un tanto", "assai", "meno", "più", "piuttosto", ] ] if not tempo: return "NA" for r in replacements: tempo = tempo.replace(*r) tempo = ( tempo.replace("Con brio", "brio") .replace("con brio", "brio") .replace("Con spirito", "spiritoso") .replace("con spirito", "spiritoso") ) base_important_words = [ "adagio", "allegro", "andante", "andantino", "largo", "lento", "presto", "vivace", "minueto", "minuetto", "menuetto", "marcia", ] # if the tempo marking ends in -ietto and or -issimo, it retuns the marking without that ending important_words = ( base_important_words + [w[0:-1] + "etto" for w in base_important_words] + [w[0:-1] + "ietto" for w in base_important_words] + [w[0:-1] + "issimo" for w in base_important_words] + [w[0:-1] + "ssimo" for w in base_important_words] + [w[0:-1] + "hetto" for w in base_important_words] ) last_words = "" for word in tempo.split(" "): if word.lower() in important_words and ( "ma non" not in last_words or " ma " not in last_words or "Tempo di" in last_words ): return word.capitalize() last_words += " " + word ################ SECOND IMPORTANT WORDS ################ relevant_words = [ "amoroso", "affettuoso", "agitato", "arioso", "cantabile", "comodo", "brio", "spiritoso", "espressivo", "fiero", "furioso", "brillante", "gaio", "giusto", "grave", "grazioso", "gustoso", "maestoso", "moderato", "risoluto", "sostenuto", "spiritoso", "tempo", # headings that name a manner rather than a speed; classed Moderate "parlante", "arbitrio", ] words_contained = [] for word in tempo.split(" "): if word.lower() in relevant_words: words_contained.append(word) # "tempo" alone stands for "a tempo"; next to a word that says which # tempo ("Tempo giusto", "Tempo comodo") it is that word that counts if len(words_contained) > 1: described = [w for w in words_contained if w.lower() != "tempo"] if described: words_contained = described ## FINAL DECISION (Two exceptions: tempo and con brio) ## if len(words_contained) == 1: if words_contained[0].lower() not in ["tempo", "brio"]: return words_contained[0].capitalize() elif words_contained[0].lower() == "tempo": return "A tempo" else: return "Con brio" elif len(words_contained) > 1: for w in words_contained: if w[0].isupper(): if w.lower() == "tempo": return "A tempo" elif w.lower() == "brio": return "Con brio" return w # returns the capitalized term first = words_contained[0].lower() if first == "tempo": return "A tempo" # or the first one elif first == "brio": return "Con brio" return words_contained[0].capitalize() return "NA"
[docs] def get_tempo_grouped_2(tempo_grouped_1: str) -> str: """ Returns a 2nd level of grouping for the tempo markings. Parameters ---------- tempo_grouped_1: str Tempo string got from get_tempo_grouped_1 """ if tempo_grouped_1 is None or tempo_grouped_1.upper() == TempoGroup2.NA.value: return TempoGroup2.NA.value possible_terminations = ["ino", "etto", "ietto", "ssimo", "issimo", "hetto"] slow_basis = [ "Adagio", "Affettuoso", "Grave", "Sostenuto", "Largo", "Lento", "Sostenuto", ] slow = slow_basis + [w[:-1] + t for w in slow_basis for t in possible_terminations] moderate_basis = [ "Andante", "Arioso", "Comodo", "Cantabile", "Comodo", "Giusto", "Espressivo", "Grazioso", "Gustoso", "Maestoso", "Minueto", "Moderato", "Marcia", "Minuetto", "Menuetto", "Amoroso", ] moderate = moderate_basis + [ w[:-1] + t for w in moderate_basis for t in possible_terminations ] fast_basis = [ "Agitato", "Allegro", "Con brio", "Spiritoso", "Fiero", "Furioso", "Brillante", "Gaio", "Presto", "Risoluto", "Vivace", ] fast = fast_basis + [w[:-1] + t for w in fast_basis for t in possible_terminations] if tempo_grouped_1 in slow: return TempoGroup2.SLOW.value elif tempo_grouped_1 in moderate: return TempoGroup2.MODERATE.value elif tempo_grouped_1 in fast: return TempoGroup2.FAST.value # A heading that names no speed ("A tempo", "Parlante", "Ad arbitrio", # "Tempo spazioso") is classed Moderate; only the absence of a heading # (NA, handled above) has no class. return TempoGroup2.MODERATE.value
[docs] def get_quarter_length(time_signature: str) -> float: """ Returns the length of a measure in quarter notes for a time signature: 4 for 4/4 and for 2/2, 1.5 for 3/8, 3 for 6/8. Unlike the number of beats, it does not depend on which note value the signature takes as the beat, so rates per quarter note are comparable between metres. It follows `get_number_of_beats` for the special values: 4 for `"C"`, 1 for an empty signature, and `pd.NA` for `"NA"`. Parameters ---------- time_signature: str Time signature in the form of string separated by '/'. """ time_signature = time_signature.split(",")[0] if time_signature == "C": return 4.0 elif time_signature == "": return 1.0 elif time_signature == "NA": return pd.NA num, den = time_signature.split("/") return int(num) * 4 / int(den)
[docs] def get_number_of_beats(time_signature: str) -> int: """ Returns the number of beats corresponding to a time signature. By default, this returns the numerator, with the exceptions when the numerator is 6, 9, or 12 and when the time signature is C or 3/8. If it is empty, it returns 1. If it is `"NA"`, it returns `pd.NA`. Parameters ---------- time_signature: str Time signature in the form of string separated by '/' to obtain the time signature type. """ # we only take the first time signature in case we receive more than one time_signature = time_signature.split(",")[0] if time_signature == "C": return 4 elif time_signature == "": return 1 elif time_signature == "NA": return pd.NA num, den = time_signature.split("/") if num == "3" and den == "8": return 1 elif num == "6": return 2 elif num == "9": return 3 elif num == "12": return 4 else: return int(num)
# raise ValueError(f"The {time_signature} is not a known time signature") _REPRISE_WORDS = re.compile(r"^\W*(da\s+capo|dal\s+segno|d\.\s*c\.?|d\.\s*s\.?)(\W|$)", re.IGNORECASE)
[docs] def extract_reprise_instruction(file_path: str): """ Whether a MusicXML file carries a "da capo" or "dal segno" instruction: 1 if it does, 0 if it does not, `"NA"` if the file cannot be read. Such a score does not write its reprise out: the opening section is on the page once and is performed twice. Every proportion and mean computed from the page therefore weighs that section once, where a score with a written-out reprise weighs it twice. Parameters ---------- file_path: str Path to xml file to look into. """ try: root = ET.parse(file_path).getroot() except Exception: return "NA" for sound in root.iter("sound"): if sound.get("dacapo") is not None or sound.get("dalsegno") is not None: return 1 for words in root.iter("words"): if words.text and _REPRISE_WORDS.match(words.text.strip()): return 1 return 0
# the tempo that notation software writes when nobody sets one: in a score it # cannot be told from a tempo chosen by the engraver, so it is not reported DEFAULT_SOFTWARE_TEMPO = 120
[docs] def extract_numeric_tempo(file_path: str) -> Optional[int]: """ Finds the numeric tempo in a musixml file: the first tempo stated in the first measure of the first part (`<sound tempo="...">`, in a direction or directly in the measure). It is `"NA"` when that measure states none, and also when it states 120, the default of notation software. Parameters ---------- file_path: str Path to xml file to get the tempo from. """ try: tree = ET.parse(file_path) except ET.ParseError as e: return "NA" root = tree.getroot() try: sound = root.find("part").find("measure").find(".//sound[@tempo]") tempo = int(round(float(sound.get("tempo")))) except Exception: return "NA" if tempo == DEFAULT_SOFTWARE_TEMPO: return "NA" return tempo