Skip to content

Commit

Permalink
Merge pull request #370 from CPJKU/kern_fixes
Browse files Browse the repository at this point in the history
Kern fixes (parsing editorials)
  • Loading branch information
manoskary authored Oct 2, 2024
2 parents 00f6329 + ee8dfd3 commit 1ca009c
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions partitura/io/importkern.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@
"256": {"type": "256th"},
}

class KernElement(object):
def __init__(self, element):
self.editorial_start = True if "ossia" in element else False
self.editorial_end = True if "Xstrophe" in element else False
self.voice_end = True if "*v" in element else False
self.voice_start = True if "*^" in element else False
self.element = element.replace("*", "")


def add_durations(a, b):
return a * b / (a + b)
Expand Down Expand Up @@ -102,8 +110,10 @@ def parse_by_voice(file: list, dtype=np.object_):
data[line, voice] = file[line][voice]
data = data.T
if num_voices > 1:
# Copy global lines from the first voice to all other voices
# Copy global lines from the first voice to all other voices unless they are the string "*S/ossia"
cp_idx = np.char.startswith(data[0], "*")
un_idx = np.char.startswith(data[0], "*S/ossia")
cp_idx = np.logical_and(cp_idx, ~un_idx)
for i in range(1, num_voices):
data[i][cp_idx] = data[0][cp_idx]
# Copy Measure Lines from the first voice to all other voices
Expand Down Expand Up @@ -171,10 +181,16 @@ def element_parsing(
):
divs_pq = part._quarter_durations[0]
current_tl_pos = 0
editorial = False
measure_mapping = {m.number: m.start.t for m in part.iter_all(spt.Measure)}
for i in range(elements.shape[0]):
element = elements[i]
if element is None:
if isinstance(element, KernElement):
if element.editorial_start:
editorial = True
if element.editorial_end:
editorial = False
if element is None or editorial:
continue
if isinstance(element, spt.GenericNote):
if total_duration_values[i] == 0:
Expand Down Expand Up @@ -250,9 +266,6 @@ def load_kern(
)
# Get Splines
splines = file[1:].T[note_parts]
# Inverse Order
splines = splines[::-1]
parsing_idxs = parsing_idxs[::-1]
prev_staff = 1
has_instrument = np.char.startswith(splines, "*I")
# if all parts have the same instrument, then they are the same part.
Expand Down Expand Up @@ -326,6 +339,7 @@ def load_kern(
for part in copy_partlist:
part.set_quarter_duration(0, divs_pq)


for part, elements, total_duration_values, same_part in zip(
copy_partlist, elements_list, total_durations_list, part_assignments
):
Expand All @@ -351,7 +365,8 @@ def load_kern(
)

doc_name = get_document_name(filename)
score = spt.Score(partlist=partlist, id=doc_name)
# inversing the partlist results to correct part order and visualization for exporting musicxml files
score = spt.Score(partlist=partlist[::-1], id=doc_name)
return score


Expand Down Expand Up @@ -503,6 +518,9 @@ def meta_tandem_line(self, line: str):
return self.process_key_line(rest)
elif line.startswith("*-"):
return self.process_fine()
else:
return KernElement(element=line)


def process_tempo_line(self, line: str):
return spt.Tempo(float(line))
Expand Down

0 comments on commit 1ca009c

Please sign in to comment.