Notation
ci / checks (push) Successful in 38m51s

This commit is contained in:
khodowany
2026-09-05 16:18:34 -05:00
parent 6da21a2deb
commit ca285d63d4
6 changed files with 1463 additions and 238 deletions
+187
View File
@@ -1,6 +1,11 @@
#include <test.hpp>
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <limits>
#include <string>
#include <string_view>
#include <utility>
@@ -12,6 +17,8 @@
namespace {
constexpr auto layout_any_index = std::numeric_limits<std::size_t>::max();
auto pitch(std::char_t letter, std::string accidental, std::int32_t octave = 4) -> vamp_zero::score_pitch {
return {
.letter = letter,
@@ -37,6 +44,13 @@ auto note(
};
}
auto note_with_ticks(std::initializer_list<vamp_zero::score_pitch> pitches, std::int64_t ticks) -> vamp_zero::score_note {
auto value = note(pitches);
value.duration_ticks = ticks;
value.display_ticks = ticks;
return value;
}
auto rest(std::initializer_list<vamp_zero::score_pitch> pitches) -> vamp_zero::score_note {
return {
.kind = "rest",
@@ -79,6 +93,71 @@ void expect_marks(std::vector<vamp_zero::score_accidental_mark> const& marks, st
}
}
auto score_with_measure(vamp_zero::score_measure measure, std::string key_signature = "C", std::string time_signature = "4/4", std::string clef = "treble") -> vamp_zero::score_document {
if (measure.key_signature.empty()) {
measure.key_signature = key_signature;
}
if (measure.time_signature.empty()) {
measure.time_signature = time_signature;
}
return {
.title = "Layout Tune",
.composer = "Composer",
.key_signature = std::move(key_signature),
.time_signature = std::move(time_signature),
.clef = std::move(clef),
.part = "Lead",
.measures = {std::move(measure)},
};
}
auto score_with_notes(std::vector<vamp_zero::score_note> notes, std::string key_signature = "C", std::string time_signature = "4/4", std::string clef = "treble") -> vamp_zero::score_document {
auto measure = vamp_zero::score_measure{
.time_signature = time_signature,
.key_signature = key_signature,
.notes = std::move(notes),
};
return score_with_measure(std::move(measure), std::move(key_signature), std::move(time_signature), std::move(clef));
}
auto layout_items(
vamp_zero::score_page_layout const& layout,
vamp_zero::score_layout_item_role role,
std::size_t system_index = layout_any_index,
std::size_t measure_index = layout_any_index,
std::size_t note_index = layout_any_index
) -> std::vector<vamp_zero::score_layout_item const*> {
auto result = std::vector<vamp_zero::score_layout_item const*>{};
for (auto const& item : layout.items) {
if (item.role != role) {
continue;
}
if (system_index != layout_any_index && item.system_index != system_index) {
continue;
}
if (measure_index != layout_any_index && item.measure_index != measure_index) {
continue;
}
if (note_index != layout_any_index && item.note_index != note_index) {
continue;
}
result.push_back(&item);
}
return result;
}
auto center_x(vamp_zero::score_layout_item const& item) -> std::float32_t {
return item.bounds.x + item.bounds.width * 0.5F;
}
auto center_y(vamp_zero::score_layout_item const& item) -> std::float32_t {
return item.bounds.y + item.bounds.height * 0.5F;
}
auto near(std::float32_t left, std::float32_t right) -> std::bool_t {
return std::abs(left - right) < 0.02F;
}
}
BOOST_AUTO_TEST_CASE(score_parser_preserves_notation_preamble) {
@@ -110,6 +189,7 @@ BOOST_AUTO_TEST_CASE(score_parser_preserves_notation_preamble) {
"id": "note-1",
"kind": "note",
"duration_ticks": 4096,
"tuplet_id": "triplet-1",
"pitches": [
{
"letter": "b",
@@ -118,6 +198,13 @@ BOOST_AUTO_TEST_CASE(score_parser_preserves_notation_preamble) {
}
]
}
],
"tuplets": [
{
"id": "triplet-1",
"note_count": 3,
"space_count": 2
}
]
}
]
@@ -138,6 +225,12 @@ BOOST_AUTO_TEST_CASE(score_parser_preserves_notation_preamble) {
BOOST_REQUIRE_EQUAL(parsed->measures.size(), 1U);
BOOST_TEST(parsed->measures.front().key_signature == "Eb");
BOOST_TEST(parsed->measures.front().time_signature == "3/4");
BOOST_REQUIRE_EQUAL(parsed->measures.front().notes.size(), 1U);
BOOST_TEST(parsed->measures.front().notes.front().tuplet_id == "triplet-1");
BOOST_REQUIRE_EQUAL(parsed->measures.front().tuplets.size(), 1U);
BOOST_TEST(parsed->measures.front().tuplets.front().id == "triplet-1");
BOOST_TEST(parsed->measures.front().tuplets.front().note_count == 3);
BOOST_TEST(parsed->measures.front().tuplets.front().space_count == 2);
}
BOOST_AUTO_TEST_CASE(accidentals_are_suppressed_when_the_key_signature_already_covers_the_pitch) {
@@ -216,3 +309,97 @@ BOOST_AUTO_TEST_CASE(tied_rests_hidden_and_slash_notes_do_not_emit_accidentals)
};
expect_marks(vamp_zero::notated_accidentals_for_measure(measure, "C"), {"none", "flat"});
}
BOOST_AUTO_TEST_CASE(score_layout_orders_preamble_on_staff_baselines) {
auto score = score_with_notes({note({pitch('e', "n")})}, "F", "4/4", "treble");
auto layout = vamp_zero::layout_score_page({.x = 0.0F, .y = 0.0F, .width = 976.0F, .height = 484.0F}, score, 0U);
auto clefs = layout_items(layout, vamp_zero::score_layout_item_role::CLEF, 0U);
auto keys = layout_items(layout, vamp_zero::score_layout_item_role::KEY_SIGNATURE, 0U);
auto times = layout_items(layout, vamp_zero::score_layout_item_role::TIME_SIGNATURE, 0U);
auto staff_lines = layout_items(layout, vamp_zero::score_layout_item_role::STAFF_LINE, 0U);
BOOST_REQUIRE_EQUAL(clefs.size(), 1U);
BOOST_REQUIRE(!keys.empty());
BOOST_REQUIRE(!times.empty());
BOOST_REQUIRE_GE(staff_lines.size(), 5U);
BOOST_TEST(clefs.front()->bounds.x < keys.front()->bounds.x);
BOOST_TEST(keys.front()->bounds.x < times.front()->bounds.x);
BOOST_TEST(near(clefs.front()->origin.y, staff_lines.front()->from.y + layout.staff_spacing * 3.0F));
}
BOOST_AUTO_TEST_CASE(score_layout_attaches_stems_to_notehead_anchors) {
auto score = score_with_notes({note({pitch('d', "n")})});
auto layout = vamp_zero::layout_score_page({.x = 0.0F, .y = 0.0F, .width = 976.0F, .height = 484.0F}, score, 0U);
auto heads = layout_items(layout, vamp_zero::score_layout_item_role::NOTEHEAD, 0U, 0U, 0U);
auto stems = layout_items(layout, vamp_zero::score_layout_item_role::STEM, 0U, 0U, 0U);
BOOST_REQUIRE_EQUAL(heads.size(), 1U);
BOOST_REQUIRE_EQUAL(stems.size(), 1U);
BOOST_TEST(near(stems.front()->from.x, heads.front()->origin.x + layout.staff_spacing * 1.336F));
BOOST_TEST(near(stems.front()->from.y, heads.front()->origin.y - layout.staff_spacing * 0.288F));
BOOST_TEST(stems.front()->to.y < stems.front()->from.y);
}
BOOST_AUTO_TEST_CASE(score_layout_displaces_chord_seconds_and_columns_accidentals) {
auto score = score_with_notes({note({pitch('c', "#"), pitch('d', "#")})});
auto layout = vamp_zero::layout_score_page({.x = 0.0F, .y = 0.0F, .width = 976.0F, .height = 484.0F}, score, 0U);
auto heads = layout_items(layout, vamp_zero::score_layout_item_role::NOTEHEAD, 0U, 0U, 0U);
auto accidentals = layout_items(layout, vamp_zero::score_layout_item_role::ACCIDENTAL, 0U, 0U, 0U);
BOOST_REQUIRE_EQUAL(heads.size(), 2U);
BOOST_REQUIRE_EQUAL(accidentals.size(), 2U);
auto const* lower_head = *std::max_element(heads.begin(), heads.end(), [](auto const* left, auto const* right) {
return center_y(*left) < center_y(*right);
});
auto const* upper_head = *std::min_element(heads.begin(), heads.end(), [](auto const* left, auto const* right) {
return center_y(*left) < center_y(*right);
});
BOOST_TEST(center_x(*upper_head) > center_x(*lower_head) + layout.staff_spacing * 0.30F);
auto const* leftmost_head = *std::min_element(heads.begin(), heads.end(), [](auto const* left, auto const* right) {
return left->bounds.x < right->bounds.x;
});
auto const* leftmost_accidental = *std::min_element(accidentals.begin(), accidentals.end(), [](auto const* left, auto const* right) {
return left->bounds.x < right->bounds.x;
});
auto const* rightmost_accidental = *std::max_element(accidentals.begin(), accidentals.end(), [](auto const* left, auto const* right) {
return left->bounds.x < right->bounds.x;
});
for (auto const* accidental : accidentals) {
BOOST_TEST(accidental->bounds.x + accidental->bounds.width < leftmost_head->bounds.x);
}
BOOST_TEST(rightmost_accidental->bounds.x - leftmost_accidental->bounds.x > layout.staff_spacing * 0.50F);
}
BOOST_AUTO_TEST_CASE(score_layout_beams_eighth_tuplets_and_labels_them) {
auto first = note_with_ticks({pitch('c', "n")}, 2048);
auto second = note_with_ticks({pitch('d', "n")}, 2048);
auto third = note_with_ticks({pitch('e', "n")}, 2048);
first.tuplet_id = "triplet-1";
second.tuplet_id = "triplet-1";
third.tuplet_id = "triplet-1";
auto score = score_with_measure({
.time_signature = "4/4",
.key_signature = "C",
.notes = {std::move(first), std::move(second), std::move(third)},
.tuplets = {{
.id = "triplet-1",
.note_count = 3,
.space_count = 2,
}},
});
auto layout = vamp_zero::layout_score_page({.x = 0.0F, .y = 0.0F, .width = 976.0F, .height = 484.0F}, score, 0U);
auto beams = layout_items(layout, vamp_zero::score_layout_item_role::BEAM, 0U, 0U);
auto flags = layout_items(layout, vamp_zero::score_layout_item_role::FLAG, 0U, 0U);
auto tuplets = layout_items(layout, vamp_zero::score_layout_item_role::TUPLET, 0U, 0U);
BOOST_REQUIRE_EQUAL(beams.size(), 1U);
BOOST_TEST(flags.empty());
BOOST_REQUIRE_EQUAL(tuplets.size(), 1U);
BOOST_TEST(tuplets.front()->text == "3");
}