@@ -0,0 +1,347 @@
|
||||
#include <vamp_zero/notation.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <filesystem>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <numeric>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <boost/json.hpp>
|
||||
#include <boost/rational.hpp>
|
||||
#include <boost/multiprecision/cpp_int.hpp>
|
||||
#include <vamp_zero/errors.hpp>
|
||||
|
||||
namespace {
|
||||
using object = boost::json::object;
|
||||
using value = boost::json::value;
|
||||
using exact_time = boost::rational<boost::multiprecision::cpp_int>;
|
||||
auto exact(vamp_zero::score_time t) -> exact_time { return {t.numerator, t.denominator}; }
|
||||
constexpr auto types = std::array<std::string_view, 14>{"maxima", "long", "breve", "whole", "half", "quarter", "eighth", "16th", "32nd", "64th", "128th", "256th", "512th", "1024th"};
|
||||
|
||||
auto notation_error(std::string const& message) -> void { throw std::invalid_argument(message); }
|
||||
auto text(object const& o, char const* key, std::string fallback = {}) -> std::string {
|
||||
auto p = o.if_contains(key);
|
||||
return p == nullptr ? fallback : std::string{p->as_string()};
|
||||
}
|
||||
auto integer(value const& v) -> std::int64_t { return boost::json::value_to<std::int64_t>(v); }
|
||||
auto integer(object const& o, char const* key, std::int64_t fallback = 0) -> std::int64_t {
|
||||
auto p = o.if_contains(key);
|
||||
return p == nullptr ? fallback : integer(*p);
|
||||
}
|
||||
auto boolean(object const& o, char const* key) -> bool {
|
||||
auto p = o.if_contains(key);
|
||||
return p != nullptr && p->as_bool();
|
||||
}
|
||||
auto time(value const& v) -> vamp_zero::score_time {
|
||||
auto const& o = v.as_object();
|
||||
auto n = integer(o.at("numerator"));
|
||||
auto d = integer(o.at("denominator"));
|
||||
if (o.size() != 2 || n < 0 || d <= 0 || std::gcd(n, d) != 1) notation_error("invalid or unreduced musical time");
|
||||
return {n, d};
|
||||
}
|
||||
auto written(value const& v) -> vamp_zero::score_written_duration {
|
||||
auto const& o = v.as_object();
|
||||
auto name = text(o, "type");
|
||||
auto dots = integer(o, "dots");
|
||||
if (std::find(types.begin(), types.end(), name) == types.end() || dots < 0 || dots > 16) notation_error("invalid written duration");
|
||||
auto result = vamp_zero::score_written_duration{.type = name, .dots = static_cast<std::int32_t>(dots)};
|
||||
if (auto ratio = o.if_contains("ratio")) {
|
||||
result.ratio = time(*ratio);
|
||||
if (result.ratio.numerator == 0) notation_error("duration ratio must be positive");
|
||||
}
|
||||
if (auto normal = o.if_contains("normal")) {
|
||||
auto parsed = written(*normal);
|
||||
result.normal_type = parsed.type;
|
||||
result.normal_dots = parsed.dots;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
auto metric_length(vamp_zero::score_written_duration const& written) -> exact_time {
|
||||
auto index = std::distance(types.begin(), std::find(types.begin(), types.end(), written.type));
|
||||
auto dots = std::int64_t{1} << written.dots;
|
||||
return exact_time{32, std::int64_t{1} << index} * exact_time{2*dots-1, dots} * exact(written.ratio);
|
||||
}
|
||||
auto clef(value const& v) -> std::string {
|
||||
auto const& o = v.as_object();
|
||||
auto sign = text(o, "sign");
|
||||
auto line = integer(o, "line");
|
||||
auto octave = integer(o, "octave_change");
|
||||
if ((sign != "G" && sign != "F" && sign != "C") || line < 1 || line > 5 || octave < -3 || octave > 3) notation_error("invalid pitched clef");
|
||||
return sign + ":" + std::to_string(line) + ":" + std::to_string(octave);
|
||||
}
|
||||
auto alterations(value const& v) -> std::array<std::int32_t, 7> {
|
||||
auto const& values = v.as_object().at("alterations").as_array();
|
||||
if (values.size() != 7) notation_error("key must have seven diatonic alterations");
|
||||
auto result = std::array<std::int32_t, 7>{};
|
||||
for (auto i = std::size_t{}; i < 7; ++i) {
|
||||
auto a = integer(values[i]);
|
||||
if (a < -3 || a > 3) notation_error("key alteration outside supported engraving range");
|
||||
result[i] = static_cast<std::int32_t>(a);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
auto pitch(value const& v) -> vamp_zero::score_pitch {
|
||||
auto const& o = v.as_object();
|
||||
auto letter = text(o, "letter");
|
||||
auto alter = integer(o.at("alter"));
|
||||
auto octave = integer(o.at("octave"));
|
||||
if (letter.size() != 1 || std::string_view{"cdefgab"}.find(letter[0]) == std::string_view::npos || alter < -3 || alter > 3 || octave < -4 || octave > 16) notation_error("invalid spelled pitch");
|
||||
auto result = vamp_zero::score_pitch{.letter = letter[0], .octave = static_cast<std::int32_t>(octave), .accidental = alter == 0 ? "n" : std::string(static_cast<std::size_t>(std::abs(alter)), alter > 0 ? '#' : 'b'), .id = text(o, "id"), .alteration = static_cast<std::int32_t>(alter), .duration = time(o.at("duration")), .written = written(o.at("written")), .tie_to = text(o, "tie_to"), .tie_from = text(o, "tie_from")};
|
||||
if (auto a = o.if_contains("accidental_display")) {
|
||||
result.explicit_accidental = true;
|
||||
result.accidental_parentheses = boolean(a->as_object(), "parentheses");
|
||||
result.accidental_bracket = boolean(a->as_object(), "bracket");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
auto note(value const& v) -> vamp_zero::score_note {
|
||||
auto const& o = v.as_object();
|
||||
auto result = vamp_zero::score_note{};
|
||||
result.id = text(o, "id");
|
||||
result.kind = text(o, "kind");
|
||||
if (result.kind != "note" && result.kind != "rest" && result.kind != "spacer") notation_error("invalid event kind");
|
||||
result.hidden = boolean(o, "hidden") || result.kind == "spacer";
|
||||
result.onset = time(o.at("onset"));
|
||||
result.duration = time(o.at("duration"));
|
||||
result.written = written(o.at("written"));
|
||||
// Layout coordinates are floating point; the parsed rational remains authoritative.
|
||||
result.duration_ticks = result.duration.quarters() * 4096.0;
|
||||
result.display_ticks = vamp_zero::written_quarters(result.written) * 4096.0;
|
||||
result.lyric = text(o, "lyric");
|
||||
result.notehead = text(o, "notehead");
|
||||
result.stem = text(o, "stem");
|
||||
result.cue = boolean(o, "cue");
|
||||
result.measure_rest = boolean(o, "measure_rest");
|
||||
if (auto position = o.if_contains("rest_position")) {
|
||||
auto const& p = position->as_object();
|
||||
auto letter = text(p,"letter");
|
||||
auto octave = integer(p,"octave");
|
||||
if (letter.size()!=1 || std::string_view{"cdefgab"}.find(letter[0])==std::string_view::npos || octave < -4 || octave > 16) notation_error("invalid rest position");
|
||||
result.rest_position = vamp_zero::score_pitch{.letter=letter[0], .octave=static_cast<std::int32_t>(octave)};
|
||||
}
|
||||
if (auto grace = o.if_contains("grace")) {
|
||||
result.grace = true;
|
||||
result.grace_slash = boolean(grace->as_object(), "slash");
|
||||
}
|
||||
if (result.grace != (result.duration.numerator == 0)) notation_error("only grace notes may have zero metric duration");
|
||||
if (!result.grace && !result.measure_rest && exact(result.duration) != metric_length(result.written)) notation_error("metric and written event durations disagree");
|
||||
for (auto const& p : o.at("pitches").as_array()) {
|
||||
result.pitches.push_back(pitch(p));
|
||||
if (!result.grace && result.pitches.back().duration.numerator == 0) notation_error("metric chord tones require positive duration");
|
||||
if (!result.grace && exact(result.pitches.back().duration) != metric_length(result.pitches.back().written)) notation_error("metric and written chord-tone durations disagree");
|
||||
if (exact(result.pitches.back().duration) > exact(result.duration)) notation_error("chord tone exceeds its anchor duration");
|
||||
}
|
||||
if (result.kind != "note" && !result.pitches.empty()) notation_error("rests/spacers cannot contain pitches");
|
||||
if (result.kind == "note" && result.pitches.empty() && result.notehead != "slash") notation_error("pitched event has no pitches");
|
||||
result.tie_to_next = std::any_of(result.pitches.begin(), result.pitches.end(), [](auto const& p) { return !p.tie_to.empty(); });
|
||||
result.tie_from_previous = std::all_of(result.pitches.begin(), result.pitches.end(), [](auto const& p) { return !p.tie_from.empty(); });
|
||||
if (auto tuplets = o.if_contains("tuplet_ids")) for (auto const& id : tuplets->as_array()) result.tuplet_ids.emplace_back(id.as_string());
|
||||
if (!result.tuplet_ids.empty()) result.tuplet_id = result.tuplet_ids.back();
|
||||
if (auto beams = o.if_contains("beams")) for (auto const& b : beams->as_array()) {
|
||||
auto const& beam = b.as_object();
|
||||
auto level = integer(beam, "level");
|
||||
auto type = text(beam, "type");
|
||||
if (level < 1 || level > 8 || (type != "begin" && type != "continue" && type != "end" && type != "forward hook" && type != "backward hook")) notation_error("invalid beam");
|
||||
result.beams.push_back({static_cast<std::int32_t>(level), type});
|
||||
}
|
||||
if (auto tremolo = o.if_contains("tremolo")) {
|
||||
auto marks = integer(tremolo->as_object(), "marks");
|
||||
if (marks < 0 || marks > 8) notation_error("invalid measured tremolo");
|
||||
result.tremolo_marks = static_cast<std::int32_t>(marks);
|
||||
result.tremolo_type = text(tremolo->as_object(), "type");
|
||||
if (result.tremolo_type != "single" && result.tremolo_type != "start" && result.tremolo_type != "stop") notation_error("invalid tremolo type");
|
||||
result.tremolo_partner = text(tremolo->as_object(), "partner");
|
||||
}
|
||||
if (auto lyric = o.if_contains("lyric_notation")) result.lyric_extend = text(lyric->as_object(), "extend");
|
||||
return result;
|
||||
}
|
||||
auto beats(value const& v) -> std::vector<vamp_zero::score_time> {
|
||||
auto result = std::vector<vamp_zero::score_time>{};
|
||||
if (v.as_object().at("groups").as_array().empty() && !boolean(v.as_object(), "unmetered")) notation_error("meter requires beat groups or senza misura");
|
||||
for (auto const& group : v.as_object().at("groups").as_array()) {
|
||||
auto unit = integer(group.as_object().at("unit"));
|
||||
auto const& counts = group.as_object().at("beats").as_array();
|
||||
if (unit <= 0 || unit > 4096 || counts.empty()) notation_error("invalid meter group");
|
||||
for (auto const& count : counts) {
|
||||
auto n = integer(count);
|
||||
if (n <= 0 || n > 1024) notation_error("invalid meter count");
|
||||
auto compound = counts.size() == 1 && unit >= 8 && n > 3 && n % 3 == 0;
|
||||
auto repetitions = counts.size() > 1 ? 1 : compound ? n / 3 : n;
|
||||
auto numerator = counts.size() > 1 ? n * 4 : compound ? 12 : 4;
|
||||
auto divisor = std::gcd(numerator, unit);
|
||||
for (auto i = std::int64_t{}; i < repetitions; ++i) result.push_back({numerator / divisor, unit / divisor});
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
auto meter_text(value const& v) -> std::string {
|
||||
auto const& meter = v.as_object();
|
||||
auto symbol = text(meter,"symbol","normal");
|
||||
if (symbol == "common" || symbol == "cut") return symbol;
|
||||
if (boolean(meter,"unmetered")) return "senza misura";
|
||||
auto result = std::string{};
|
||||
for (auto const& group : meter.at("groups").as_array()) {
|
||||
if (!result.empty()) result += '+';
|
||||
auto numerator = std::string{};
|
||||
for (auto const& count : group.as_object().at("beats").as_array()) {
|
||||
if (!numerator.empty()) numerator += '+';
|
||||
numerator += std::to_string(integer(count));
|
||||
}
|
||||
result += numerator + '/' + std::to_string(integer(group.as_object().at("unit")));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
auto measure(value const& v) -> vamp_zero::score_measure {
|
||||
auto const& o = v.as_object();
|
||||
auto result = vamp_zero::score_measure{.id = text(o, "id"), .section_label = text(o, "section_label"), .time_signature = text(o, "time_signature"), .key_signature = text(o, "key_signature")};
|
||||
result.duration = time(o.at("duration"));
|
||||
if (result.duration.numerator == 0) notation_error("measure has zero duration");
|
||||
result.clef = clef(o.at("clef"));
|
||||
result.key_alterations = alterations(o.at("key"));
|
||||
result.structured_key = true;
|
||||
result.beats = beats(o.at("meter"));
|
||||
result.time_signature = meter_text(o.at("meter"));
|
||||
result.implicit = boolean(o, "implicit");
|
||||
auto const& voices = o.at("voices").as_array();
|
||||
if (voices.size() != 1) notation_error("exactly one voice is supported; voices cannot be flattened");
|
||||
auto cursor = exact_time{};
|
||||
for (auto const& item : voices.front().as_object().at("notes").as_array()) {
|
||||
auto n = note(item);
|
||||
if (exact(n.onset) + exact(n.duration) > exact(result.duration)) notation_error("event extends beyond measure");
|
||||
if (!n.grace) {
|
||||
if (exact(n.onset) < cursor) notation_error("events overlap within one rhythmic voice");
|
||||
cursor = exact(n.onset) + exact(n.duration);
|
||||
}
|
||||
n.clef = result.clef;
|
||||
result.notes.push_back(std::move(n));
|
||||
}
|
||||
if (auto harmonies = o.if_contains("harmonies")) for (auto const& item : harmonies->as_array()) {
|
||||
auto const& h = item.as_object();
|
||||
auto onset = time(h.at("onset"));
|
||||
if (exact(onset) >= exact(result.duration)) notation_error("harmony outside measure");
|
||||
result.harmonies.push_back({text(h,"id"), onset, text(h,"symbol"), text(h,"kind")});
|
||||
}
|
||||
if (auto attrs = o.if_contains("attributes")) for (auto const& item : attrs->as_array()) {
|
||||
auto const& a = item.as_object();
|
||||
auto change = vamp_zero::score_attribute{.onset = time(a.at("onset"))};
|
||||
if (auto c = a.if_contains("clef")) change.clef = clef(*c);
|
||||
if (auto k = a.if_contains("key")) change.key_alterations = alterations(*k);
|
||||
if (auto m = a.if_contains("meter")) { change.time_signature = meter_text(*m); change.beats = beats(*m); }
|
||||
if (exact(change.onset) >= exact(result.duration)) notation_error("attribute change outside measure");
|
||||
result.attributes.push_back(std::move(change));
|
||||
}
|
||||
if (auto bars = o.if_contains("barlines")) result.right_barline = text(bars->as_object(), "right", "regular");
|
||||
for (auto& n : result.notes) {
|
||||
for (auto const& a : result.attributes)
|
||||
if (exact(a.onset) <= exact(n.onset) && !a.clef.empty()) n.clef = a.clef;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
auto vamp_zero::written_quarters(score_written_duration const& written) -> double {
|
||||
auto found = std::find(types.begin(), types.end(), written.type);
|
||||
if (found == types.end()) return 0.0;
|
||||
return std::ldexp(32.0, -static_cast<int>(std::distance(types.begin(), found))) * (2.0 - std::ldexp(1.0, -written.dots));
|
||||
}
|
||||
|
||||
auto vamp_zero::parse_notation_document(std::string_view json, std::string source_path) -> std::expected<score_document, modulus::error> {
|
||||
try {
|
||||
auto parsed = boost::json::parse(json);
|
||||
auto const& root = parsed.as_object();
|
||||
if (integer(root, "schema_version") != 2) notation_error("unsupported score schema; regenerate assets with the current Product Vamp generator");
|
||||
auto const& meta = root.at("metadata").as_object();
|
||||
auto const& staves = root.at("staves").as_array();
|
||||
if (staves.size() != 1) notation_error("exactly one staff is currently supported");
|
||||
auto const& staff = staves.front().as_object();
|
||||
auto result = score_document{};
|
||||
result.title = text(meta, "title");
|
||||
if (result.title.empty()) notation_error("score title is required");
|
||||
result.composer = text(meta, "composer");
|
||||
result.key_signature = text(meta, "key_signature");
|
||||
result.time_signature = text(meta, "time_signature");
|
||||
result.clef = clef(staff.at("clef"));
|
||||
result.arrangement_part_type = text(staff, "arrangement_part_type", "practice");
|
||||
result.part = text(staff, "name");
|
||||
result.source_path = source_path;
|
||||
result.source_json = std::string{json};
|
||||
result.id = std::filesystem::path(result.source_path).stem().string();
|
||||
if (result.id.empty()) result.id = result.title + "-" + result.part;
|
||||
for (auto const& item : staff.at("measures").as_array()) result.measures.push_back(measure(item));
|
||||
if (result.measures.empty()) notation_error("score has no measures");
|
||||
auto ids = std::set<std::string>{};
|
||||
auto events = std::map<std::string, score_note const*>{};
|
||||
auto event_order = std::map<std::string, std::size_t>{};
|
||||
struct tone_ref { score_pitch const* pitch; exact_time start; };
|
||||
auto tones = std::map<std::string, tone_ref>{};
|
||||
auto absolute = exact_time{};
|
||||
for (auto const& m : result.measures) {
|
||||
for (auto const& n : m.notes) {
|
||||
if (n.id.empty() || !ids.insert(n.id).second) notation_error("duplicate or empty event ID");
|
||||
event_order.emplace(n.id, events.size());
|
||||
events.emplace(n.id, &n);
|
||||
for (auto const& p : n.pitches) {
|
||||
if (p.id.empty() || !tones.emplace(p.id, tone_ref{&p, absolute+exact(n.onset)}).second) notation_error("duplicate or empty pitch ID");
|
||||
}
|
||||
}
|
||||
absolute += exact(m.duration);
|
||||
}
|
||||
constexpr auto natural = std::array<int,7>{0,2,4,5,7,9,11};
|
||||
auto midi = [&](score_pitch const& p) { return 12*(p.octave+1)+natural[std::string_view{"cdefgab"}.find(p.letter)]+p.alteration.value_or(0); };
|
||||
for (auto const& [id, ref] : tones) {
|
||||
auto const& p = *ref.pitch;
|
||||
if (!p.tie_to.empty()) {
|
||||
auto next = tones.find(p.tie_to);
|
||||
if (next == tones.end() || next->second.pitch->tie_from != id || midi(p) != midi(*next->second.pitch) || ref.start+exact(p.duration) != next->second.start) notation_error("invalid or dangling pitch tie");
|
||||
}
|
||||
if (!p.tie_from.empty()) {
|
||||
auto prev = tones.find(p.tie_from);
|
||||
if (prev == tones.end() || prev->second.pitch->tie_to != id) notation_error("dangling incoming pitch tie");
|
||||
}
|
||||
}
|
||||
auto span_ids = std::set<std::string>{};
|
||||
if (auto spans = staff.if_contains("tuplets")) for (auto const& item : spans->as_array()) {
|
||||
auto const& t = item.as_object();
|
||||
if (integer(t,"note_count") <= 0 || integer(t,"note_count") > 1000000 || integer(t,"space_count") <= 0 || integer(t,"space_count") > 1000000 || integer(t,"depth") < 0 || integer(t,"depth") > 16) notation_error("invalid tuplet ratio or nesting depth");
|
||||
auto span = score_tuplet{.id = text(t,"id"), .note_count = static_cast<std::int32_t>(integer(t,"note_count")), .space_count = static_cast<std::int32_t>(integer(t,"space_count")), .start = text(t,"start"), .end = text(t,"end"), .depth = static_cast<std::int32_t>(integer(t,"depth")), .bracket = text(t,"bracket","auto"), .show_number = text(t,"show_number","actual")};
|
||||
if (span.id.empty() || !span_ids.insert(span.id).second || !ids.contains(span.start) || !ids.contains(span.end) || event_order.at(span.start) > event_order.at(span.end)) notation_error("invalid tuplet span");
|
||||
if ((span.bracket != "auto" && span.bracket != "yes" && span.bracket != "no") || (span.show_number != "none" && span.show_number != "actual" && span.show_number != "both")) notation_error("invalid tuplet appearance");
|
||||
for (auto const& [id, n] : events) {
|
||||
auto member = std::find(n->tuplet_ids.begin(), n->tuplet_ids.end(), span.id) != n->tuplet_ids.end();
|
||||
if ((id == span.start || id == span.end) && !member) notation_error("tuplet endpoint is not a member");
|
||||
if (member && (event_order.at(id) < event_order.at(span.start) || event_order.at(id) > event_order.at(span.end))) notation_error("tuplet member outside span");
|
||||
}
|
||||
for (auto& m : result.measures) if (std::any_of(m.notes.begin(), m.notes.end(), [&](auto const& n) { return std::find(n.tuplet_ids.begin(), n.tuplet_ids.end(), span.id) != n.tuplet_ids.end(); })) m.tuplets.push_back(span);
|
||||
}
|
||||
for (auto const& [id, n] : events) {
|
||||
for (auto const& span_id : n->tuplet_ids) if (!span_ids.contains(span_id)) notation_error("event refers to an unknown tuplet");
|
||||
if (n->tremolo_type == "start" || n->tremolo_type == "stop") {
|
||||
auto other = events.find(n->tremolo_partner);
|
||||
auto expected = n->tremolo_type == "start" ? "stop" : "start";
|
||||
if (other == events.end() || other->second->tremolo_partner != id || other->second->tremolo_type != expected || other->second->tremolo_marks != n->tremolo_marks) notation_error("invalid paired tremolo");
|
||||
}
|
||||
}
|
||||
if (auto spans = staff.if_contains("octave_shifts")) for (auto const& item : spans->as_array()) {
|
||||
auto const& span = item.as_object();
|
||||
if (integer(span,"octaves") < -3 || integer(span,"octaves") > 3 || exact(time(span.at("end"))) <= exact(time(span.at("start"))) || exact(time(span.at("end"))) > absolute) notation_error("invalid octave-shift span");
|
||||
result.octave_shifts.push_back({time(span.at("start")), time(span.at("end")), static_cast<std::int32_t>(integer(span,"octaves"))});
|
||||
}
|
||||
auto start = exact_time{};
|
||||
for (auto& m : result.measures) {
|
||||
for (auto& n : m.notes) {
|
||||
for (auto const& span : result.octave_shifts) {
|
||||
if (exact(span.end) <= exact(span.start) || exact(span.end) > absolute || span.octaves < -3 || span.octaves > 3) notation_error("invalid octave-shift span");
|
||||
if (start+exact(n.onset) >= exact(span.start) && start+exact(n.onset) < exact(span.end)) n.display_octaves += span.octaves;
|
||||
}
|
||||
}
|
||||
start += exact(m.duration);
|
||||
}
|
||||
return result;
|
||||
} catch (std::exception const& error) {
|
||||
return std::unexpected(make_error(error_code::INVALID_ARGUMENT, (source_path.empty() ? std::string{} : source_path + ": ") + "score notation: " + error.what()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user