Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matchfile v1.0.0 voice parsing fix #331

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions partitura/io/importmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
from partitura.io.matchfile_utils import (
Version,
number_pattern,
vnumber_pattern,
MatchTimeSignature,
MatchKeySignature,
format_pnote_id,
Expand Down Expand Up @@ -629,15 +630,19 @@ def part_from_matchfile(
except (TypeError, ValueError):
# no staff attribute, or staff attribute does not end with a number
note_attributes["staff"] = None

if "s" in note.ScoreAttributesList:
note_attributes["voice"] = 1
elif any(a.startswith("v") for a in note.ScoreAttributesList):
note_attributes["voice"] = next(
(int(a[1:]) for a in note.ScoreAttributesList if vnumber_pattern.match(a)),
None,
)
else:
note_attributes["voice"] = next(
(int(a) for a in note.ScoreAttributesList if number_pattern.match(a)),
None,
)

# get rid of this if as soon as we have a way to iterate over the
# duration components. For now we have to treat the cases simple
# and compound durations separately.
Expand Down Expand Up @@ -692,9 +697,7 @@ def part_from_matchfile(

else:
part_note = score.Note(**note_attributes)

part.add(part_note, onset_divs, offset_divs)

# Check if the note is tied and if so, add the tie information
if is_tied:
found = False
sildater marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -717,7 +720,6 @@ def part_from_matchfile(
part_note.id
)
)

# add time signatures
for ts_beat_time, ts_bar, tsg in ts:
ts_beats = tsg.numerator
Expand All @@ -729,7 +731,6 @@ def part_from_matchfile(
else:
bar_start_divs = 0
part.add(score.TimeSignature(ts_beats, ts_beat_type), bar_start_divs)

# add key signatures
for ks_beat_time, ks_bar, keys in mf.key_signatures:
if ks_bar in bar_times.keys():
Expand All @@ -742,7 +743,7 @@ def part_from_matchfile(
# * use key estimation if there are multiple defined keys
# fifths, mode = key_name_to_fifths_mode(key_name)
part.add(score.KeySignature(keys.fifths, keys.mode), ks_bar)

add_staffs(part)
# add_clefs(part)

Expand All @@ -754,11 +755,17 @@ def part_from_matchfile(
score.add_measures(part)
score.tie_notes(part)
score.find_tuplets(part)

if not all([n.voice for n in part.notes_tied]):

n_voices = set([n.voice for n in part.notes])
if len(n_voices) == 1 and None in n_voices:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only adds voices equal to one in case all voices are None. Maybe something like

n_voices_num = [n.voice for n in part.notes if n is not None]
n_voices_none = [n.voice for n in part.notes if n is None]
new_voice = 0
if len(n_voices_num) > 0:
    new_voice = max(n_voices_num ) + 1
if len(n_voices_num):
     -> the loop

for note in part.notes_tied:
if note.voice is None:
note.voice = 1
elif len(n_voices) > 1 and None in n_voices:
n_voices.remove(None)
for note in part.notes_tied:
if note.voice is None:
note.voice = max(n_voices) + 1

return part

Expand Down
1 change: 1 addition & 0 deletions partitura/io/matchfile_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
pitch_class_pattern = re.compile("(?P<step>[A-Ga-g])(?P<alter>[#bn]*)")

number_pattern = re.compile(r"\d+")
vnumber_pattern = re.compile(r"v\d+")

# For matchfiles before 1.0.0.
old_version_pattern = re.compile(r"^(?P<minor>[0-9]+)\.(?P<patch>[0-9]+)")
Expand Down
6 changes: 5 additions & 1 deletion tests/test_match_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def test_load_match(self):
)

sna_musicxml = score_musicxml.note_array()
assert np.all(sna_match['voice'] == sna_musicxml['voice'])

for note in alignment:

Expand Down Expand Up @@ -564,7 +565,7 @@ def test_stimeptime_lines(self):
self.assertTrue(True)

def test_snote_lines(self):

snote_lines = [
"snote(n1,[B,n],3,0:2,1/8,1/8,-0.5000,0.0000,[v1])",
"snote(n3,[G,#],3,1:1,0,1/16,0.0000,0.2500,[v3])",
Expand Down Expand Up @@ -1992,3 +1993,6 @@ def test_match_key_signature(self):
for component in ks.other_components:
key_name = fifths_mode_to_key_name(component.fifths, component.mode)
self.assertTrue(str(component).startswith(key_name))

if __name__ == "__main__":
unittest.main()