From e1fbfade7b7cfa94d80dc5d9f05a01ea10273607 Mon Sep 17 00:00:00 2001 From: Nerual Dreming Date: Fri, 9 Oct 2026 06:21:44 +0300 Subject: [PATCH 1/5] A planned score nothing can be sung from (no M:, L: or K:, no voice with bars, colons in the note runs) stops the request before the semantic stage, in seconds, with the reason --- docs/ARCHITECTURE.md | 7 +++ src/pipeline.h | 13 ++++ src/score-check.h | 121 +++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 4 ++ tests/test-score-check.cpp | 68 +++++++++++++++++++++ tools/yue-plan.cpp | 7 ++- 6 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 src/score-check.h create mode 100644 tests/test-score-check.cpp diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 2a35aac..9eda237 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -692,6 +692,13 @@ repetition penalty positive, penalty window in [1, 100], `min_tokens` between 0 and `max_tokens`, `max_tokens` at least 1. A preset outside the bounds is a 400 from the server and a FATAL from the CLI. +A planned score nothing can be sung from stops the request before the +semantic stage (`src/score-check.h`): a header without a well-formed `M:`, +`L:` or `K:`, no voice with bars, or colons inside the note runs outside +chord symbols, inline fields and repeat bars. The log shows the start of the +score in printable ASCII and a FATAL naming what is wrong; `yue-plan` only +warns. + ## yue-plan reference Runs the first autoregressive stage alone and writes the composition the diff --git a/src/pipeline.h b/src/pipeline.h index f5a2159..130c62a 100644 --- a/src/pipeline.h +++ b/src/pipeline.h @@ -22,6 +22,7 @@ #include "model-store.h" #include "nar.h" #include "request.h" +#include "score-check.h" #include "timer.h" #include "torch-cpu-rng.h" #include "vae-enc.h" @@ -498,6 +499,18 @@ static bool pipeline_generate(Yue2Pipeline * p, abc_ids[i] = plans[i].tokens; scores[i] = bpe_decode(tok, abc_ids[i]); truncated[i] = plans[i].truncated; + // a plan nothing can be sung from stops the song before the long stage + std::string problem = yue2_score_problem(scores[i]); + if (!problem.empty()) { + fprintf(stderr, "[Pipeline] score of song %d as written:\n%s\n", i + 1, + yue2_score_excerpt(scores[i], 800).c_str()); + fprintf(stderr, "[Pipeline] FATAL: the score the model wrote for song %d cannot be sung: %s. %s\n", + i + 1, problem.c_str(), + r.adapters.empty() ? "Draw another seed." + : "Adapters past their limits on the score half do this: lower their " + "strengths or draw another seed."); + return false; + } } } diff --git a/src/score-check.h b/src/score-check.h new file mode 100644 index 0000000..b44eddd --- /dev/null +++ b/src/score-check.h @@ -0,0 +1,121 @@ +// score-check.h: a planned score nothing can be sung from +// +// Adapters pushed past their limits, or a seed that goes astray, make the +// planner write garbage: no key, no meter, colons inside the note runs. The +// semantic stage would then spend minutes on it. The plan is read once it is +// written and the song stops there, in seconds, with what is wrong. +#pragma once + +#include +#include + +// "M:4/4", "L:1/16": a positive fraction and nothing after it +static bool yue2_score_fraction(const std::string & v) { + int num = 0, den = 0; + char end = 0; + return sscanf(v.c_str(), "%d/%d%c", &num, &den, &end) == 2 && num > 0 && den > 0; +} + +// A field line: a letter, then ':' +static bool yue2_score_field(const std::string & line) { + return line.size() >= 2 && line[1] == ':' && + ((line[0] >= 'A' && line[0] <= 'Z') || (line[0] >= 'a' && line[0] <= 'z')); +} + +// The colons of a note line outside chord symbols, inline fields [K:...] +// and repeat bars (|: :| ::) +static int yue2_score_stray_colons(const std::string & line) { + int stray = 0; + bool quoted = false; + int depth = 0; + for (size_t i = 0; i < line.size(); i++) { + char c = line[i]; + if (c == '"') { + quoted = !quoted; + } else if (quoted) { + continue; + } else if (c == '[') { + depth++; + } else if (c == ']') { + depth = depth > 0 ? depth - 1 : 0; + } else if (c == ':' && depth == 0) { + char before = i > 0 ? line[i - 1] : 0; + char after = i + 1 < line.size() ? line[i + 1] : 0; + if (before != '|' && after != '|' && before != ':' && after != ':') { + stray++; + } + } + } + return stray; +} + +// The start of a score for a log line: printable ASCII and newlines only, the +// rest as '?'. A garbage plan holds partial UTF-8 sequences, and a reader of +// the log as UTF-8 text would stop at the first one. +static std::string yue2_score_excerpt(const std::string & abc, size_t limit) { + std::string out; + for (size_t i = 0; i < abc.size() && out.size() < limit; i++) { + unsigned char c = (unsigned char) abc[i]; + out += (c == '\n' || (c >= 0x20 && c < 0x7F)) ? (char) c : '?'; + } + return out; +} + +// What makes a score unusable, "" when nothing does +static std::string yue2_score_problem(const std::string & abc) { + bool meter = false, length = false, key = false, voice = false, bars = false; + int stray = 0; + size_t from = 0; + std::string first_stray; + while (from < abc.size()) { + size_t nl = abc.find('\n', from); + std::string line = abc.substr(from, nl == std::string::npos ? std::string::npos : nl - from); + from = nl == std::string::npos ? abc.size() : nl + 1; + if (!line.empty() && line.back() == '\r') { + line.pop_back(); + } + if (line.empty() || line[0] == '%') { + continue; + } + if (yue2_score_field(line)) { + std::string v = line.substr(2); + size_t a = v.find_first_not_of(" \t"); + v = a == std::string::npos ? std::string() : v.substr(a); + while (!v.empty() && (v.back() == ' ' || v.back() == '\t')) { + v.pop_back(); + } + if (!key && line[0] == 'M') { + meter = meter || yue2_score_fraction(v) || v == "C" || v == "C|" || v == "none"; + } else if (!key && line[0] == 'L') { + length = length || yue2_score_fraction(v); + } else if (line[0] == 'K') { + key = key || !v.empty(); + } else if (line[0] == 'V') { + voice = true; + } + continue; + } + bars = bars || line.find('|') != std::string::npos; + int here = yue2_score_stray_colons(line); + if (here > 0 && first_stray.empty()) { + first_stray = line.size() > 40 ? line.substr(0, 40) + "..." : line; + } + stray += here; + } + std::string missing; + for (auto [have, name] : { std::pair{ meter, "M:" }, { length, "L:" }, { key, "K:" } }) { + if (!have) { + missing += (missing.empty() ? "" : ", ") + std::string(name); + } + } + if (!missing.empty()) { + return "its header has no well-formed " + missing; + } + if (!voice || !bars) { + return "it has no voice with bars"; + } + if (stray > 0) { + return "colons stand inside its note runs (" + std::to_string(stray) + ", first in \"" + first_stray + "\")"; + } + return ""; +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3139311..b921eab 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -35,3 +35,7 @@ link_ggml_backends(test-sheetsage) add_executable(test-tokenizer test-tokenizer.cpp) target_link_libraries(test-tokenizer PRIVATE yyjson) link_ggml_backends(test-tokenizer) + +# test-score-check: a planned score nothing can be sung from, pure CPU, no ggml. +add_executable(test-score-check test-score-check.cpp) +target_include_directories(test-score-check PRIVATE ${CMAKE_SOURCE_DIR}/src) diff --git a/tests/test-score-check.cpp b/tests/test-score-check.cpp new file mode 100644 index 0000000..ab32f99 --- /dev/null +++ b/tests/test-score-check.cpp @@ -0,0 +1,68 @@ +// test-score-check: a planned score nothing can be sung from, pure CPU. + +#include "score-check.h" + +#include +#include + +static int failures = 0; + +static void check(bool ok, const char * what) { + if (!ok) { + fprintf(stderr, "[ERROR] %s\n", what); + failures++; + } +} + +static const std::string GOOD = + "X:1\nT:\nM:2/4\nL:1/32\nQ:1/4=70\nV: Vocal clef=treble name=\"Vocal Melody\" snm=\"Vocal\"\n" + "V: Ins clef=treble name=\"Ins Melody\" snm=\"Inst.\"\nK:Em\n% intro\nV: Vocal\n" + "z12\"Em\"z4|\"Em\"z16|\"C\"z16|\nV: Ins\nZ|E4B4f2g4d2-|d2B4B2A2G2F2G2|\n% verse\nV: Vocal\n" + "|:\"Em\"e8b8|\"C\"e8a4f4:|[K:G]\"G\"g16|\n"; + +int main(int argc, char ** argv) { + // with files: the verdict on each score, for real plans + if (argc > 1) { + int bad = 0; + for (int i = 1; i < argc; i++) { + FILE * f = fopen(argv[i], "rb"); + if (!f) { + fprintf(stderr, "[ERROR] cannot read %s\n", argv[i]); + return 1; + } + std::string text; + char buf[4096]; + size_t n; + while ((n = fread(buf, 1, sizeof buf, f)) > 0) { + text.append(buf, n); + } + fclose(f); + std::string problem = yue2_score_problem(text); + if (!problem.empty()) { + bad++; + printf("%s: %s\n", argv[i], problem.c_str()); + } + } + printf("%d of %d scores refused\n", bad, argc - 1); + return 0; + } + check(yue2_score_problem(GOOD).empty(), "a well-formed score passes"); + check(yue2_score_problem("X:1\nM:C\nL:1/8\nK:C\nV:1\nCDEF|GABc|\n").empty(), "common time passes"); + check(yue2_score_problem("X:1\nL:1/8\nK:C\nV:1\nCDEF|\n") == "its header has no well-formed M:", "no meter"); + check(yue2_score_problem("X:1\nM:4/4\nL:1/8\nV:1\nCDEF|\n") == "its header has no well-formed K:", "no key"); + check(yue2_score_problem("X:1\nM:4/x\nL:\nK:C\nV:1\nCDEF|\n") == "its header has no well-formed M:, L:", + "malformed meter and length"); + check(yue2_score_problem("X:1\nM:4/4\nL:1/8\nK:C\n") == "it has no voice with bars", "no body"); + check(yue2_score_problem("X:1\nM:4/4\nL:1/8\nK:C\nV:1\nCD:E:F|G2A2:B|\n").rfind("colons stand inside", 0) == 0, + "colons in a note run"); + check(yue2_score_stray_colons("|:CDEF:|::GABc|") == 0, "repeat bars are no stray colons"); + check(yue2_score_stray_colons("\"A:7\"C[K:G]D") == 0, "colons in chords and inline fields are not stray"); + check(yue2_score_excerpt("K:C\n\xd0\xb0\xe4\xb8", 64) == "K:C\n????", "a log excerpt is printable ASCII"); + check(yue2_score_excerpt("abcdef", 3) == "abc", "an excerpt stops at its limit"); + if (failures) { + fprintf(stderr, "[ERROR] %d checks failed\n", failures); + return 1; + } + fprintf(stderr, "[OK] test-score-check\n"); + return 0; +} diff --git a/tools/yue-plan.cpp b/tools/yue-plan.cpp index 42f1d1d..94f7e69 100644 --- a/tools/yue-plan.cpp +++ b/tools/yue-plan.cpp @@ -9,6 +9,7 @@ #include "generate.h" #include "prompt.h" #include "request.h" +#include "score-check.h" #include "version.h" #include @@ -167,7 +168,11 @@ int main(int argc, char ** argv) { } const Yue2Generation & plan = plans[0]; - std::string score = bpe_decode(&tok, plan.tokens); + std::string score = bpe_decode(&tok, plan.tokens); + std::string problem = yue2_score_problem(score); + if (!problem.empty()) { + fprintf(stderr, "[Plan] WARNING: this score cannot be sung: %s\n", problem.c_str()); + } if (!write_file(out_path, score)) { qw3lm_kv_free(&kv); qw3lm_free(&lm); From 40dff1a78f23bde683c02df0edf1b8cbb5fb7249 Mon Sep 17 00:00:00 2001 From: Nerual Dreming Date: Thu, 22 Oct 2026 10:19:58 +0300 Subject: [PATCH 2/5] A planned score that cannot be sung is planned again: plan_retries (default 2, up to 8) draws song i's attempt k with lm_seed + i + k * lm_batch_size before the request stops --- docs/ARCHITECTURE.md | 15 ++++++++++++--- src/pipeline.h | 24 +++++++++++++++++++++++- src/request.cpp | 7 +++++++ src/request.h | 3 +++ tools/yue-server.cpp | 5 +++++ 5 files changed, 50 insertions(+), 4 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 9eda237..5d25eac 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -591,6 +591,7 @@ the server rejects a request with neither. "source_tokens": "", "source_start": 0.0, "source_end": -1.0, + "plan_retries": 2, "abc_sampling": { "temperature": 0.7, "top_p": 0.9, "top_k": 30, "repetition_penalty": 1.005, "penalty_window": 100, "min_tokens": 32, "max_tokens": 4096 }, "semantic_sampling": { "temperature": 1.0, "top_p": 0.95, "top_k": 100, "repetition_penalty": 1.2, "penalty_window": 50, "min_tokens": 200, "max_tokens": 9000 } } @@ -699,6 +700,14 @@ chord symbols, inline fields and repeat bars. The log shows the start of the score in printable ASCII and a FATAL naming what is wrong; `yue-plan` only warns. +**`plan_retries`** (int, default `2`, bound `8`) +How many times a planned score that cannot be sung is planned again before +the request stops. Song `i`'s attempt `k` draws with `lm_seed + i + k * +lm_batch_size`, a seed no other song of the batch draws, so the same request +replays to the same score; the replay request of the track carries the score +that was sung. Outside the bound is a 400 from the server and a FATAL from +the CLI. + ## yue-plan reference Runs the first autoregressive stage alone and writes the composition the @@ -813,9 +822,9 @@ POST /synth Submit a generation job, returns job ID response: {"id":"1a2b..."} 400 on malformed JSON, unknown cot mode, unknown output_format, steps < 1, lm_batch_size outside [1, --max-batch], synth_batch_size - outside [1, 9], a sampling preset outside the protocol bounds, an - adapter missing from the --adapters directory, or both semantic_tokens - and source_tokens + outside [1, 9], plan_retries outside [0, 8], a sampling preset outside + the protocol bounds, an adapter missing from the --adapters directory, or + both semantic_tokens and source_tokens POST /transcribe Submit a transcription job, returns job ID body: multipart/form-data, an "audio" part (WAV or MP3) and an optional diff --git a/src/pipeline.h b/src/pipeline.h index 130c62a..6fad643 100644 --- a/src/pipeline.h +++ b/src/pipeline.h @@ -435,6 +435,10 @@ static bool pipeline_generate(Yue2Pipeline * p, if (!yue2_sampling_valid(r.abc_sampling, "abc") || !yue2_sampling_valid(r.semantic_sampling, "semantic")) { return false; } + if (r.plan_retries < 0 || r.plan_retries > 8) { + fprintf(stderr, "[Pipeline] FATAL: plan_retries must be between 0 and 8\n"); + return false; + } std::vector ar_adapters, nar_adapters; if (!pipeline_adapters(p, r, &ar_adapters, &nar_adapters)) { return false; @@ -499,8 +503,26 @@ static bool pipeline_generate(Yue2Pipeline * p, abc_ids[i] = plans[i].tokens; scores[i] = bpe_decode(tok, abc_ids[i]); truncated[i] = plans[i].truncated; - // a plan nothing can be sung from stops the song before the long stage + // a plan nothing can be sung from is planned again on seeds no + // other song of the batch draws, and stops the song before the + // long stage once the retries are spent std::string problem = yue2_score_problem(scores[i]); + for (int attempt = 1; !problem.empty() && attempt <= r.plan_retries; attempt++) { + const int64_t seed = r.lm_seed + i + (int64_t) attempt * B; + fprintf(stderr, + "[Pipeline] score of song %d cannot be sung: %s; planning it again with lm_seed %lld " + "(%d of %d)\n", + i + 1, problem.c_str(), (long long) seed, attempt, r.plan_retries); + std::vector again; + if (!yue2_generate(lm, &p->kv, { open }, {}, 1.0f, r.abc_sampling, seed, YUE2_PHASE_ABC, &again, + cancelled, cancel_data)) { + return false; + } + abc_ids[i] = again[0].tokens; + scores[i] = bpe_decode(tok, abc_ids[i]); + truncated[i] = again[0].truncated; + problem = yue2_score_problem(scores[i]); + } if (!problem.empty()) { fprintf(stderr, "[Pipeline] score of song %d as written:\n%s\n", i + 1, yue2_score_excerpt(scores[i], 800).c_str()); diff --git a/src/request.cpp b/src/request.cpp index ad6c564..6c48edf 100644 --- a/src/request.cpp +++ b/src/request.cpp @@ -32,6 +32,7 @@ void request_init(Yue2Request * r) { r->semantic_tokens = ""; r->abc_sampling = YUE2_ABC_SAMPLING; + r->plan_retries = 2; r->semantic_sampling = YUE2_SEMANTIC_SAMPLING; r->output_format = OUTPUT_FORMAT_MP3; @@ -153,6 +154,9 @@ static void request_parse_obj(yyjson_val * obj, Yue2Request * r) { r->synth_batch_size = yyjson_get_int(v); } parse_sampling(obj, "abc_sampling", &r->abc_sampling); + if ((v = yyjson_obj_get(obj, "plan_retries")) && yyjson_is_int(v)) { + r->plan_retries = yyjson_get_int(v); + } parse_sampling(obj, "semantic_sampling", &r->semantic_sampling); if ((v = yyjson_obj_get(obj, "semantic_tokens")) && yyjson_is_str(v)) { r->semantic_tokens = yy_str(v); @@ -265,6 +269,9 @@ std::string request_to_json(const Yue2Request * r, bool sparse) { yyjson_mut_obj_add_int(doc, root, "synth_batch_size", r->synth_batch_size); } add_sampling(doc, root, "abc_sampling", r->abc_sampling, d.abc_sampling, sparse); + if (!sparse || r->plan_retries != d.plan_retries) { + yyjson_mut_obj_add_int(doc, root, "plan_retries", r->plan_retries); + } add_sampling(doc, root, "semantic_sampling", r->semantic_sampling, d.semantic_sampling, sparse); if (!sparse || r->semantic_tokens != d.semantic_tokens) { yyjson_mut_obj_add_strncpy(doc, root, "semantic_tokens", r->semantic_tokens.c_str(), r->semantic_tokens.size()); diff --git a/src/request.h b/src/request.h index c508f5b..e7712a4 100644 --- a/src/request.h +++ b/src/request.h @@ -53,6 +53,9 @@ struct Yue2Request { // sampling of each autoregressive stage, the checkpoint presets by default Yue2Sampling abc_sampling; + // a planned score nothing can be sung from (score-check.h) is planned + // again, song i's attempt k drawing with lm_seed + i + k * lm_batch_size + int plan_retries; // 2, at most 8 Yue2Sampling semantic_sampling; // semantic stream (CSV of codec values, 25 per second). Non empty replaces diff --git a/tools/yue-server.cpp b/tools/yue-server.cpp index 56ed439..a85f58f 100644 --- a/tools/yue-server.cpp +++ b/tools/yue-server.cpp @@ -529,6 +529,11 @@ static bool validate(const std::string & json, httplib::Response & res, Yue2Requ res.set_content(json_string("error", "synth_batch_size must be between 1 and 9"), "application/json"); return false; } + if (r->plan_retries < 0 || r->plan_retries > 8) { + res.status = 400; + res.set_content(json_string("error", "plan_retries must be between 0 and 8"), "application/json"); + return false; + } if (!yue2_sampling_valid(r->abc_sampling, "abc") || !yue2_sampling_valid(r->semantic_sampling, "semantic")) { res.status = 400; res.set_content(json_string("error", "sampling preset outside the protocol bounds"), "application/json"); From 74435a5c4e974e275b527fde4ec1102121b6de5b Mon Sep 17 00:00:00 2001 From: Nerual Dreming Date: Thu, 5 Nov 2026 15:49:13 +0300 Subject: [PATCH 3/5] yue2-48: fill a dropped M:/L:/K: header so a plan is sung instead of thrown away A plan whose only fault is a missing or malformed M:/L:/K: header was retried on new seeds and then thrown away once the retries were spent - the top telemetry failure for generated songs. When the body is otherwise singable (a voice with bars, no stray colons), the header is now filled in with the conventions the model is trained on (M:4/4, L:1/16, K:C), the meter and length ahead of the key, and the plan is kept. yue2_score_problem re-checks the result, so a plan wrong in other ways still fails as before. --- src/pipeline.h | 14 ++++++++ src/score-check.h | 66 ++++++++++++++++++++++++++++++++++++++ tests/test-score-check.cpp | 12 +++++++ 3 files changed, 92 insertions(+) diff --git a/src/pipeline.h b/src/pipeline.h index 6fad643..d9f60ca 100644 --- a/src/pipeline.h +++ b/src/pipeline.h @@ -523,6 +523,20 @@ static bool pipeline_generate(Yue2Pipeline * p, truncated[i] = again[0].truncated; problem = yue2_score_problem(scores[i]); } + if (!problem.empty()) { + // A plan the model simply would not give a header is salvaged by + // writing the trained defaults in, rather than thrown away once + // the retries are spent; a plan wrong in other ways still fails. + std::string repaired = yue2_score_fill_header(scores[i]); + if (repaired != scores[i] && yue2_score_problem(repaired).empty()) { + fprintf(stderr, + "[Pipeline] score of song %d had no usable header; filled in M:/L:/K: defaults and keeping it\n", + i + 1); + scores[i] = repaired; + abc_ids[i] = bpe_encode(tok, repaired); + problem.clear(); + } + } if (!problem.empty()) { fprintf(stderr, "[Pipeline] score of song %d as written:\n%s\n", i + 1, yue2_score_excerpt(scores[i], 800).c_str()); diff --git a/src/score-check.h b/src/score-check.h index b44eddd..84ea625 100644 --- a/src/score-check.h +++ b/src/score-check.h @@ -119,3 +119,69 @@ static std::string yue2_score_problem(const std::string & abc) { } return ""; } + +// Fills a plan's missing M:/L:/K: header with the conventions the model is +// trained on, so a score whose only fault is a dropped header can still be +// sung instead of thrown away. Well-formed fields the model did write are kept +// and only the missing ones are added, ahead of the body (after an opening +// X: line when there is one). The caller re-checks the result with +// yue2_score_problem, so a score this cannot rescue - stray colons, no bars - +// still fails as before. +static std::string yue2_score_fill_header(const std::string & abc) { + bool meter = false, length = false, key = false; + size_t from = 0; + while (from < abc.size()) { + size_t nl = abc.find('\n', from); + std::string line = abc.substr(from, nl == std::string::npos ? std::string::npos : nl - from); + from = nl == std::string::npos ? abc.size() : nl + 1; + if (!line.empty() && line.back() == '\r') { + line.pop_back(); + } + if (line.empty() || line[0] == '%' || !yue2_score_field(line)) { + continue; + } + std::string v = line.substr(2); + size_t a = v.find_first_not_of(" \t"); + v = a == std::string::npos ? std::string() : v.substr(a); + while (!v.empty() && (v.back() == ' ' || v.back() == '\t')) { + v.pop_back(); + } + if (!key && line[0] == 'M') { + meter = meter || yue2_score_fraction(v) || v == "C" || v == "C|" || v == "none"; + } else if (!key && line[0] == 'L') { + length = length || yue2_score_fraction(v); + } else if (line[0] == 'K') { + key = key || !v.empty(); + } + } + if (meter && length && key) { + return abc; + } + // The meter and unit length go into the header; a key must follow them, + // since yue2_score_problem counts M:/L: only before the first K:. + std::string head; + if (!meter) { + head += "M:4/4\n"; + } + if (!length) { + head += "L:1/16\n"; + } + std::string key_line = key ? std::string() : "K:C\n"; + // M:/L: after an opening X: line, or at the very top when there is none. + size_t at = 0; + if (abc.compare(0, 2, "X:") == 0) { + size_t nl = abc.find('\n'); + at = nl == std::string::npos ? abc.size() : nl + 1; + } + std::string out = abc.substr(0, at) + head; + std::string rest = abc.substr(at); + if (key_line.empty()) { + return out + rest; + } + // K: ends the header, so it is written in just before the body, which the + // first bar line opens; with no bar line the score cannot be sung anyway. + size_t bar = rest.find('|'); + size_t insert = bar == std::string::npos ? rest.size() : rest.rfind('\n', bar); + insert = insert == std::string::npos ? 0 : (bar == std::string::npos ? rest.size() : insert + 1); + return out + rest.substr(0, insert) + key_line + rest.substr(insert); +} diff --git a/tests/test-score-check.cpp b/tests/test-score-check.cpp index ab32f99..68fc7a6 100644 --- a/tests/test-score-check.cpp +++ b/tests/test-score-check.cpp @@ -59,6 +59,18 @@ int main(int argc, char ** argv) { check(yue2_score_stray_colons("\"A:7\"C[K:G]D") == 0, "colons in chords and inline fields are not stray"); check(yue2_score_excerpt("K:C\n\xd0\xb0\xe4\xb8", 64) == "K:C\n????", "a log excerpt is printable ASCII"); check(yue2_score_excerpt("abcdef", 3) == "abc", "an excerpt stops at its limit"); + // yue2_score_fill_header: a plan whose only fault is a dropped header is made + // singable with the trained defaults; one wrong in the body still is not. + auto rescued = [](const std::string & abc) { + return !yue2_score_problem(abc).empty() && yue2_score_problem(yue2_score_fill_header(abc)).empty(); + }; + check(rescued("X:1\nV: Vocal\nc4d4e4f4|g4a4b4c'4|\nV: Ins\nZ4|\n"), "a headerless plan is filled and sings"); + check(rescued("X:1\nM:4/4\nL:1/16\nV: Vocal\nc4d4e4f4|\nV: Ins\nZ4|\n"), "a dropped key is filled in after meter and length"); + check(rescued("X:1\nK:C\nV: Vocal\nc4d4e4f4|\nV: Ins\nZ4|\n"), "a dropped meter and length are filled in"); + check(rescued("V: Vocal\nc4d4e4f4|\nV: Ins\nZ4|\n"), "a plan with no X: line is filled at its top"); + check(yue2_score_fill_header(GOOD) == GOOD, "a well-formed header is left untouched"); + check(!yue2_score_problem(yue2_score_fill_header("V: Vocal\nc4 d4\nV: Ins\nZ\n")).empty(), "a body without bars is not rescued"); + check(!yue2_score_problem(yue2_score_fill_header("X:1\nV: Vocal\nc4:d4|\nV: Ins\nZ4|\n")).empty(), "stray colons are not rescued"); if (failures) { fprintf(stderr, "[ERROR] %d checks failed\n", failures); return 1; From 2d7c8740cd45bd68009b8e2ae194cc3a772045df Mon Sep 17 00:00:00 2001 From: Nerual Dreming Date: Thu, 12 Nov 2026 15:34:42 +0300 Subject: [PATCH 4/5] A score still unsingable once its header is filled in fails with what is left wrong, not with the header --- src/pipeline.h | 18 +++++++++++------- tests/test-score-check.cpp | 8 ++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/pipeline.h b/src/pipeline.h index d9f60ca..aa61999 100644 --- a/src/pipeline.h +++ b/src/pipeline.h @@ -528,13 +528,17 @@ static bool pipeline_generate(Yue2Pipeline * p, // writing the trained defaults in, rather than thrown away once // the retries are spent; a plan wrong in other ways still fails. std::string repaired = yue2_score_fill_header(scores[i]); - if (repaired != scores[i] && yue2_score_problem(repaired).empty()) { - fprintf(stderr, - "[Pipeline] score of song %d had no usable header; filled in M:/L:/K: defaults and keeping it\n", - i + 1); - scores[i] = repaired; - abc_ids[i] = bpe_encode(tok, repaired); - problem.clear(); + if (repaired != scores[i]) { + // with the header filled in, what is still wrong is the reason, not the header + std::string left = yue2_score_problem(repaired); + if (left.empty()) { + fprintf(stderr, + "[Pipeline] score of song %d had no usable header; filled in M:/L:/K: defaults and keeping it\n", + i + 1); + scores[i] = repaired; + abc_ids[i] = bpe_encode(tok, repaired); + } + problem = left; } } if (!problem.empty()) { diff --git a/tests/test-score-check.cpp b/tests/test-score-check.cpp index 68fc7a6..2a6967e 100644 --- a/tests/test-score-check.cpp +++ b/tests/test-score-check.cpp @@ -71,6 +71,14 @@ int main(int argc, char ** argv) { check(yue2_score_fill_header(GOOD) == GOOD, "a well-formed header is left untouched"); check(!yue2_score_problem(yue2_score_fill_header("V: Vocal\nc4 d4\nV: Ins\nZ\n")).empty(), "a body without bars is not rescued"); check(!yue2_score_problem(yue2_score_fill_header("X:1\nV: Vocal\nc4:d4|\nV: Ins\nZ4|\n")).empty(), "stray colons are not rescued"); + { + // a plan with no header and no bars is reported for what is left once the header is filled in + const char * barless = "V: Vocal\nc4 d4\nV: Ins\nZ\n"; + const std::string before = yue2_score_problem(barless); + const std::string after = yue2_score_problem(yue2_score_fill_header(barless)); + check(!after.empty() && after != before && after.find("M:") == std::string::npos, + "the reason left after the header is filled in names no header"); + } if (failures) { fprintf(stderr, "[ERROR] %d checks failed\n", failures); return 1; From 7cf315034d7893b1e04139f5145cadb9c131e9e1 Mon Sep 17 00:00:00 2001 From: Nerual Dreming Date: Thu, 12 Nov 2026 17:06:25 +0300 Subject: [PATCH 5/5] Fill-in of a dropped header also drops the stray text the planner wrote before the first voice A plan that lost its K: often carries junk header lines too ("Qtre Ins2=", ": VocalIns ...|"), and the junk then counted as a note run with stray colons, so the filled-in plan still failed. Before the first V: line only fields, comments and blank lines are kept when the header is filled in, and K: goes in right before the first voice. A well-formed header is still left untouched. --- src/score-check.h | 54 ++++++++++++++++++++++++++++++-------- tests/test-score-check.cpp | 12 +++++++++ 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/score-check.h b/src/score-check.h index 84ea625..81aa767 100644 --- a/src/score-check.h +++ b/src/score-check.h @@ -124,7 +124,8 @@ static std::string yue2_score_problem(const std::string & abc) { // trained on, so a score whose only fault is a dropped header can still be // sung instead of thrown away. Well-formed fields the model did write are kept // and only the missing ones are added, ahead of the body (after an opening -// X: line when there is one). The caller re-checks the result with +// X: line when there is one); text before the first voice that is neither a +// field nor a comment goes with the fill. The caller re-checks the result with // yue2_score_problem, so a score this cannot rescue - stray colons, no bars - // still fails as before. static std::string yue2_score_fill_header(const std::string & abc) { @@ -157,6 +158,31 @@ static std::string yue2_score_fill_header(const std::string & abc) { if (meter && length && key) { return abc; } + // A header the model dropped fields from often carries its stray text too: + // before the first voice, only fields, comments and blank lines are kept. + std::string kept; + size_t voice_at = std::string::npos; + from = 0; + while (from < abc.size()) { + size_t nl = abc.find('\n', from); + std::string raw = abc.substr(from, nl == std::string::npos ? std::string::npos : nl - from + 1); + std::string line = raw; + from = nl == std::string::npos ? abc.size() : nl + 1; + while (!line.empty() && (line.back() == '\n' || line.back() == '\r')) { + line.pop_back(); + } + bool voice = yue2_score_field(line) && line[0] == 'V'; + if (voice_at == std::string::npos && voice) { + voice_at = kept.size(); + } + if (voice_at == std::string::npos && !line.empty() && line[0] != '%' && !yue2_score_field(line)) { + continue; + } + kept += raw; + } + if (voice_at == std::string::npos) { + kept = abc; + } // The meter and unit length go into the header; a key must follow them, // since yue2_score_problem counts M:/L: only before the first K:. std::string head; @@ -169,19 +195,25 @@ static std::string yue2_score_fill_header(const std::string & abc) { std::string key_line = key ? std::string() : "K:C\n"; // M:/L: after an opening X: line, or at the very top when there is none. size_t at = 0; - if (abc.compare(0, 2, "X:") == 0) { - size_t nl = abc.find('\n'); - at = nl == std::string::npos ? abc.size() : nl + 1; + if (kept.compare(0, 2, "X:") == 0) { + size_t nl = kept.find('\n'); + at = nl == std::string::npos ? kept.size() : nl + 1; } - std::string out = abc.substr(0, at) + head; - std::string rest = abc.substr(at); + std::string out = kept.substr(0, at) + head; + std::string rest = kept.substr(at); if (key_line.empty()) { return out + rest; } - // K: ends the header, so it is written in just before the body, which the - // first bar line opens; with no bar line the score cannot be sung anyway. - size_t bar = rest.find('|'); - size_t insert = bar == std::string::npos ? rest.size() : rest.rfind('\n', bar); - insert = insert == std::string::npos ? 0 : (bar == std::string::npos ? rest.size() : insert + 1); + // K: ends the header, so it is written in just before the first voice, or + // before the first bar line with no voice; with neither the score cannot + // be sung anyway. + size_t insert; + if (voice_at != std::string::npos) { + insert = voice_at - at; + } else { + size_t bar = rest.find('|'); + insert = bar == std::string::npos ? rest.size() : rest.rfind('\n', bar); + insert = insert == std::string::npos ? 0 : (bar == std::string::npos ? rest.size() : insert + 1); + } return out + rest.substr(0, insert) + key_line + rest.substr(insert); } diff --git a/tests/test-score-check.cpp b/tests/test-score-check.cpp index 2a6967e..0def969 100644 --- a/tests/test-score-check.cpp +++ b/tests/test-score-check.cpp @@ -69,6 +69,18 @@ int main(int argc, char ** argv) { check(rescued("X:1\nK:C\nV: Vocal\nc4d4e4f4|\nV: Ins\nZ4|\n"), "a dropped meter and length are filled in"); check(rescued("V: Vocal\nc4d4e4f4|\nV: Ins\nZ4|\n"), "a plan with no X: line is filled at its top"); check(yue2_score_fill_header(GOOD) == GOOD, "a well-formed header is left untouched"); + { + // a plan that dropped its key and wrote stray text in its header instead + const char * stray_head = + "X:1\nT:\nM:2/4\nL:1/16\nQtre Ins2=\"\n: VocalIns Ins Insadc2|B8|\n% verse\nV: Vocal\nM:1/4\nA4|\n" + "V: Ins\nM:1/4\nZ|\nV: Vocal\nM:2/4\nB4c3c|d2d2d2c2|B4z4|\nV: Ins\nZ4|\n"; + const std::string filled = yue2_score_fill_header(stray_head); + check(rescued(stray_head), "stray text before the first voice goes with the filled header"); + check(filled.find("Qtre") == std::string::npos && filled.find("VocalIns") == std::string::npos, + "the stray header lines are dropped"); + check(filled.find("K:C\nV: Vocal") != std::string::npos, "the key goes in before the first voice"); + check(filled.find("% verse") != std::string::npos, "comments before the first voice stay"); + } check(!yue2_score_problem(yue2_score_fill_header("V: Vocal\nc4 d4\nV: Ins\nZ\n")).empty(), "a body without bars is not rescued"); check(!yue2_score_problem(yue2_score_fill_header("X:1\nV: Vocal\nc4:d4|\nV: Ins\nZ4|\n")).empty(), "stray colons are not rescued"); {