Add LilyPond slice replacement with a structured engrave window
Re-engraving is a rescue path for the handful of systems a scan cannot deliver, so the window is an editing surface rather than an automation project. Three full-width rows - the scanned system, the render, the form - because a system is wide and short and the job is comparing one against the other bar by bar. The render is shown scaled to the scan's staff height, which is what export does anyway, so it previews the real thing. A form rather than a text box. Key and time are slice-level, clef, notes and lyrics per voice: every staff in a system carries the same key signature, and Kaipaava proves it across five-staff and two-staff systems alike. Notes and lyrics stay raw LilyPond, so slurs, dynamics, tuplets and the laissezVibrer/repeatTie idiom for ties crossing into the next slice all work untouched. Notes are entered in \relative mode, referenced to the middle of each clef's staff, so a part needs no octave marks at all in the common case. The time signature is used for spacing and bar checks but not printed: the printed score repeats the key at every system and the time only at the first, so a re-engraved middle slice showing one would stand out. Seeded from what can be known reliably. Voice count comes from counting staves in the slice; key, time and clefs are inherited from the song, because the slices being re-engraved are the illegible ones and reading a key signature off them is exactly the measurement that fails. After the first replacement in a song only the notes need typing. Staff counting needed two corrections against the corpus: compare gaps against line spacing rather than staff height, since adjacent staves can sit closer together than one staff is tall; and require five lines in a group, since Engel's 'uh______' lyric extenders are long horizontal runs too and each counted as a staff. Kaipaava now reads 2,2,2,2,5 on page 1, Ketun 6, Engel 4. Also in this change: - Title is required for export, every other metadata field optional, enforced in bundle.write so the CLI and the editor both get it. Tempo added; noteman already has a free-form column for it. - The panel is a splitter rather than a fixed width, sections collapse under bold grey disclosure headers, and it scrolls. - A re-engraved slice is washed amber with an ENGRAVED badge, and markers get badges too. Thin coloured text was invisible against a scan. Closes #31 Closes #32 Closes #33 Closes #34
This commit is contained in:
@@ -32,6 +32,8 @@ _STAFF_KERNEL = 0.05 # horizontal open kernel, as a fraction of page width
|
||||
_STAFF_MIN_WIDTH = 0.2 # a staff line spans at least this share of the page
|
||||
_CONTENT_MARGIN = 0.01 # slack past the staff ends, for ledger lines and lyrics
|
||||
_EDGE_PERCENTILE = 15 # tolerate this share of staff lines merged into scan artefacts
|
||||
_STAFF_BREAK = 2.5 # a gap this many line-spacings wide separates two staves
|
||||
_STAFF_LINES = 4 # lines a group needs to be a staff rather than an extender (5, minus one for a broken line)
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -190,6 +192,46 @@ def content_columns(
|
||||
return max(0.0, left - margin) / width, min(float(width), right + margin) / width
|
||||
|
||||
|
||||
def staff_count(gray: np.ndarray) -> int:
|
||||
"""How many staves are in this slice — i.e. how many voices it holds.
|
||||
|
||||
Kaipaava's first four systems have two staves and its fifth has five, so
|
||||
this cannot be a song-level constant. Counts long horizontal runs and
|
||||
divides by the five lines a staff has; the same signal that finds the music
|
||||
area, so it degrades the same way and no worse.
|
||||
"""
|
||||
height, width = gray.shape
|
||||
binary = (gray < _INK).astype(np.uint8)
|
||||
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (max(3, int(width * _STAFF_KERNEL)), 1))
|
||||
lines = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
|
||||
|
||||
count, _, stats, _ = cv2.connectedComponentsWithStats(lines, 8)
|
||||
rows = sorted(
|
||||
stats[i, cv2.CC_STAT_TOP]
|
||||
for i in range(1, count)
|
||||
if stats[i, cv2.CC_STAT_WIDTH] > width * _STAFF_MIN_WIDTH
|
||||
)
|
||||
if not rows:
|
||||
return 1
|
||||
|
||||
# Compare against the *line* spacing, not the staff height: adjacent staves
|
||||
# can sit closer together than one staff is tall, so a staff-height
|
||||
# threshold merges them into one.
|
||||
line_spacing = (staff_height(gray, 0, height) or height * 0.05) / 4
|
||||
|
||||
groups: list[list[int]] = [[rows[0]]]
|
||||
for row in rows[1:]:
|
||||
if row - groups[-1][-1] > line_spacing * _STAFF_BREAK:
|
||||
groups.append([])
|
||||
groups[-1].append(row)
|
||||
|
||||
# A staff is five evenly spaced lines. Lone long runs are lyric extenders —
|
||||
# Engel's "uh______" — and hairpins, which are just as horizontal as a
|
||||
# staff line and would otherwise each count as a staff.
|
||||
staves = sum(1 for group in groups if len(group) >= _STAFF_LINES)
|
||||
return max(1, staves)
|
||||
|
||||
|
||||
def ink_runs(gray: np.ndarray) -> list[tuple[int, int]]:
|
||||
"""Rows containing ink, despeckled — specks are the known failure mode."""
|
||||
profile = row_darkness(cv2.medianBlur(gray, 3))
|
||||
|
||||
Reference in New Issue
Block a user