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

Measure feature #377

Merged
merged 8 commits into from
Nov 5, 2024
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
40 changes: 40 additions & 0 deletions partitura/musicanalysis/note_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,46 @@ def metrical_strength_feature(na, part, **kwargs):

return W, names

def measure_feature(na, part, **kwargs):
"""Measure feature

This feature encodes the measure each note is in.

"""
notes_list = part.notes_tied if not np.all(na["pitch"] == 0) else part.rests
notes = {n.id:n for n in notes_list}
bm = part.beat_map

global_start = bm(part.first_point.t)
global_end = bm(part.last_point.t)
sildater marked this conversation as resolved.
Show resolved Hide resolved
manoskary marked this conversation as resolved.
Show resolved Hide resolved
global_number = 0 # default global measure number

names = [
"measure_number",
"measure_start_beat",
"measure_end_beat",
]
W = np.zeros((len(notes), 3))

for i, na_n in enumerate(na):
n = notes[na_n["id"]]
measure = next(n.start.iter_prev(score.Measure, eq=True), None)

if measure:
start = bm(measure.start.t)
end = bm(measure.end.t)
sildater marked this conversation as resolved.
Show resolved Hide resolved
manoskary marked this conversation as resolved.
Show resolved Hide resolved
number = measure.number
else:
start = global_start
end = global_end
number = global_number

W[i, 0] = number
W[i, 1] = start
W[i, 2] = end

return W, names


def time_signature_feature(na, part, **kwargs):
"""TIme Signature feature
Expand Down
4 changes: 2 additions & 2 deletions tests/data/musicxml/test_note_features.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</score-part>
</part-list>
<part id="P1">
<measure number="1" width="511.52">
<measure number="4" width="511.52">
<print>
<system-layout>
<system-margins>
Expand Down Expand Up @@ -138,7 +138,7 @@
</notations>
</note>
</measure>
<measure number="2" width="467.05">
<measure number="4a" width="467.05">
<direction placement="below">
<direction-type>
<dynamics default-x="4.70" default-y="-40.00" relative-y="-25.00">
Expand Down
16 changes: 16 additions & 0 deletions tests/test_note_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ def test_slur_grace_art_dyn_orn(self):
self.assertTrue(np.all(dyntest), "forte feature does not match")
self.assertTrue(np.all(slurtest), "slur feature does not match")

def test_measure_feature(self):
for fn in MUSICXML_NOTE_FEATURES:
score = load_musicxml(fn, force_note_ids=True)
feats = [
"measure_feature"
]
na = compute_note_array(score[0], feature_functions=feats)

numtest = na["measure_feature.measure_number"] == np.array([1, 1, 1, 2, 2, 2])
starttest = na["measure_feature.measure_start_beat"] == np.array([0, 0, 0, 4, 4, 4])
endtest = na["measure_feature.measure_end_beat"] == np.array([4, 4, 4, 8, 8, 8])
self.assertTrue(np.all(numtest), "measure number feature does not match")
self.assertTrue(np.all(starttest), "measure start feature does not match")
self.assertTrue(np.all(endtest), "measure end feature does not match")



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