score-reader 0.1.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- score_reader-0.1.5.dist-info/METADATA +154 -0
- score_reader-0.1.5.dist-info/RECORD +64 -0
- score_reader-0.1.5.dist-info/WHEEL +5 -0
- score_reader-0.1.5.dist-info/entry_points.txt +2 -0
- score_reader-0.1.5.dist-info/licenses/LICENSE +202 -0
- score_reader-0.1.5.dist-info/licenses/NOTICE +13 -0
- score_reader-0.1.5.dist-info/top_level.txt +1 -0
- sheet_interpreter/__init__.py +9 -0
- sheet_interpreter/arpeggio_notation.py +111 -0
- sheet_interpreter/articulation_export.py +49 -0
- sheet_interpreter/audio.py +260 -0
- sheet_interpreter/beam_source.py +163 -0
- sheet_interpreter/boundary_ties.py +82 -0
- sheet_interpreter/bow_export.py +35 -0
- sheet_interpreter/clef_export.py +76 -0
- sheet_interpreter/cli.py +142 -0
- sheet_interpreter/clock_context.py +261 -0
- sheet_interpreter/continuous_slide_curve.py +89 -0
- sheet_interpreter/cue_caption.py +54 -0
- sheet_interpreter/data/LICENSE +202 -0
- sheet_interpreter/data/NOTICE +13 -0
- sheet_interpreter/data/music_sheets_v4_float16.tflite +0 -0
- sheet_interpreter/dynamic_export.py +76 -0
- sheet_interpreter/fall_off_notation.py +128 -0
- sheet_interpreter/grace_export.py +83 -0
- sheet_interpreter/guide_writer.py +287 -0
- sheet_interpreter/guitar_effect.py +32 -0
- sheet_interpreter/guitar_export.py +36 -0
- sheet_interpreter/hairpin_export.py +102 -0
- sheet_interpreter/interpreter.jar +0 -0
- sheet_interpreter/midi.py +340 -0
- sheet_interpreter/multi_rest_wire.py +62 -0
- sheet_interpreter/musicxml.py +937 -0
- sheet_interpreter/natural_harmonic_notation.py +125 -0
- sheet_interpreter/navigation.py +186 -0
- sheet_interpreter/neutral_rest_caption.py +58 -0
- sheet_interpreter/ocr.py +103 -0
- sheet_interpreter/octave_shift_export.py +94 -0
- sheet_interpreter/ornament_export.py +53 -0
- sheet_interpreter/page_processing_budget.py +17 -0
- sheet_interpreter/parenthesized_note_group.py +198 -0
- sheet_interpreter/pdf_annotations.py +52 -0
- sheet_interpreter/performance.py +478 -0
- sheet_interpreter/printed_tempo_spans.py +124 -0
- sheet_interpreter/reader.py +204 -0
- sheet_interpreter/rest_kinds.py +33 -0
- sheet_interpreter/runtime.py +17 -0
- sheet_interpreter/semantic_wire.py +321 -0
- sheet_interpreter/slide_numbering.py +43 -0
- sheet_interpreter/slide_performance.py +204 -0
- sheet_interpreter/slur_export.py +124 -0
- sheet_interpreter/slur_wire.py +159 -0
- sheet_interpreter/source_tempo_clock.py +93 -0
- sheet_interpreter/spelling_export.py +48 -0
- sheet_interpreter/straight_slide.py +76 -0
- sheet_interpreter/sudden_dynamic.py +47 -0
- sheet_interpreter/technique_export.py +62 -0
- sheet_interpreter/tie_owners.py +43 -0
- sheet_interpreter/tremolo_export.py +93 -0
- sheet_interpreter/tremolo_performance.py +45 -0
- sheet_interpreter/tremolo_wire.py +138 -0
- sheet_interpreter/typed_events.py +57 -0
- sheet_interpreter/typed_performance.py +44 -0
- sheet_interpreter/visual_size.py +18 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
# Copyright 2026 Luckolite
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
"""Write a desktop score result in the current Android guide-cache wire format.
|
|
4
|
+
|
|
5
|
+
This is derived cache data. The source PDF and previously saved guides are never
|
|
6
|
+
modified by this module. Keep GUIDE_VERSION aligned with PdfPageRenderCache.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import io
|
|
10
|
+
import math
|
|
11
|
+
import os
|
|
12
|
+
import struct
|
|
13
|
+
import tempfile
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from . import semantic_wire
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# Recognition revisions 264/265/266/267/268/269/270/271/272/273/274/275 retain the complete framed 263 record layout.
|
|
19
|
+
RECOGNITION_REVISION = 136
|
|
20
|
+
GUIDE_VERSION = 299
|
|
21
|
+
SUPPORTED_GUIDE_VERSIONS = (260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299)
|
|
22
|
+
MAX_GUIDE_BYTES = 128 * 1024 * 1024
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _field(row, name):
|
|
26
|
+
if name not in row:
|
|
27
|
+
raise ValueError(f"Missing guide field: {name}")
|
|
28
|
+
return row[name]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _pack(stream, format_code, *values):
|
|
32
|
+
if any(isinstance(value, float) and not math.isfinite(value) for value in values):
|
|
33
|
+
raise ValueError("Non-finite guide value")
|
|
34
|
+
stream.write(struct.pack(">" + format_code, *values))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _records(stream, rows, fields, maximum):
|
|
38
|
+
if not isinstance(rows, list) or len(rows) > maximum:
|
|
39
|
+
raise ValueError("Invalid guide record count")
|
|
40
|
+
_pack(stream, "i", len(rows))
|
|
41
|
+
for row in rows:
|
|
42
|
+
if not isinstance(row, dict):
|
|
43
|
+
raise ValueError("Invalid guide record")
|
|
44
|
+
_pack(stream, "".join(code for _, code in fields),
|
|
45
|
+
*(_field(row, name) for name, _ in fields))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _valid_boundary_ties(value):
|
|
49
|
+
if type(value) is not int or value < 0 or value & ~((1 << 22) - 1):
|
|
50
|
+
return False
|
|
51
|
+
if not value & 16:
|
|
52
|
+
return value <= 15
|
|
53
|
+
string = value >> 5 & 7
|
|
54
|
+
count = 6 + (value >> 8 & 1)
|
|
55
|
+
fret = value >> 9 & 63
|
|
56
|
+
effective_open = value >> 15 & 127
|
|
57
|
+
return bool(value & 15) and string < count and fret <= 36 and effective_open + fret <= 127
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _signature_records(stream, rows, fields, measure_count, guide_version):
|
|
61
|
+
# A signature at measure_count belongs to the following page, never to a played bar.
|
|
62
|
+
limit = measure_count + (1 if guide_version >= 287 and measure_count else 0)
|
|
63
|
+
if not isinstance(rows, list) or any(not isinstance(row, dict)
|
|
64
|
+
or type(row.get("measureIndex")) is not int
|
|
65
|
+
or not 0 <= row["measureIndex"] < limit for row in rows):
|
|
66
|
+
raise ValueError("Invalid signature boundary for guide format")
|
|
67
|
+
_records(stream, rows, fields, limit)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def encode(score, guide_version=GUIDE_VERSION):
|
|
71
|
+
"""Serialize the complete score object returned by the standalone reader."""
|
|
72
|
+
if guide_version not in SUPPORTED_GUIDE_VERSIONS or not isinstance(score, dict):
|
|
73
|
+
raise ValueError("Unsupported guide format")
|
|
74
|
+
output = io.BytesIO()
|
|
75
|
+
_pack(output, "i", guide_version)
|
|
76
|
+
measures = score["measures"]
|
|
77
|
+
_records(output, measures, (("left", "f"), ("right", "f"),
|
|
78
|
+
("top", "f"), ("bottom", "f")), 10_000)
|
|
79
|
+
_pack(output, "i", _field(score, "firstMeasureNumber"))
|
|
80
|
+
note_fields = (
|
|
81
|
+
("measureIndex", "i"), ("positionInMeasure", "f"), ("staffStep", "i"),
|
|
82
|
+
("staffIndex", "i"), ("staffCount", "i"), ("pageY", "f"),
|
|
83
|
+
("tiedFromPrevious", "?"), ("augmentationDots", "i"), ("beamCount", "i"),
|
|
84
|
+
("writtenAccidental", "i"), ("unbeamedDurationBeats", "f"),
|
|
85
|
+
("tupletDivisor", "i"), ("followingRestBeats", "f"), ("articulations", "i"),
|
|
86
|
+
("clefBottomDiatonic", "i"), ("crossStaffBeam", "?"),
|
|
87
|
+
("leadingRestBeats", "f"), ("compactOpening", "?"), ("octaveShift", "i"))
|
|
88
|
+
if guide_version >= 262:
|
|
89
|
+
note_fields += (("boundaryTies", "i"),)
|
|
90
|
+
if guide_version >= 276:
|
|
91
|
+
note_fields += (("tupletNormalNotes", "i"),)
|
|
92
|
+
if guide_version >= 281:
|
|
93
|
+
note_fields += (("stemDirection", "i"),)
|
|
94
|
+
if guide_version >= 282:
|
|
95
|
+
note_fields += (("kind", "B"),)
|
|
96
|
+
if guide_version >= 293:
|
|
97
|
+
note_fields += (("visualSize", "B"),)
|
|
98
|
+
notes = score["notes"]
|
|
99
|
+
if not isinstance(notes, list) or any(not isinstance(row, dict)
|
|
100
|
+
or not _valid_boundary_ties(row.get("boundaryTies", 0)) for row in notes):
|
|
101
|
+
raise ValueError("Invalid boundary tie evidence")
|
|
102
|
+
normalized_notes = []
|
|
103
|
+
for row in notes:
|
|
104
|
+
if guide_version < 284 and row.get("boundaryTies", 0) & 16:
|
|
105
|
+
raise ValueError("Typed tab tie identity requires guide284")
|
|
106
|
+
actual = _field(row, "tupletDivisor")
|
|
107
|
+
if type(actual) is int and actual in (2, 4):
|
|
108
|
+
if guide_version < 288:
|
|
109
|
+
raise ValueError("Compound tuplets require guide288")
|
|
110
|
+
if "tupletNormalNotes" not in row:
|
|
111
|
+
raise ValueError("Compound tuplets require an explicit normal count")
|
|
112
|
+
default_normal = {3: 2, 5: 4, 6: 4, 7: 4}.get(actual, 1)
|
|
113
|
+
normal = row.get("tupletNormalNotes", default_normal)
|
|
114
|
+
if (type(actual) is not int or actual not in (1, 2, 3, 4, 5, 6, 7)
|
|
115
|
+
or type(normal) is not int or not 1 <= normal <= 16
|
|
116
|
+
or actual == 1 and normal != 1):
|
|
117
|
+
raise ValueError("Invalid tuplet ratio")
|
|
118
|
+
if guide_version < 276 and normal != default_normal:
|
|
119
|
+
raise ValueError("Explicit tuplet ratio requires guide276")
|
|
120
|
+
clef = row.get("clefBottomDiatonic")
|
|
121
|
+
if type(clef) is not int or clef not in (-1, 18, 22, 24, 25, 30, 32, 37):
|
|
122
|
+
raise ValueError("Invalid intrinsic clef")
|
|
123
|
+
if guide_version < 290 and clef == 32:
|
|
124
|
+
raise ValueError("Intrinsic two-octave bass clef requires guide290")
|
|
125
|
+
if guide_version < 289 and row.get("clefBottomDiatonic") == 25:
|
|
126
|
+
raise ValueError("Octave-bass clef requires guide289")
|
|
127
|
+
stem = row.get("stemDirection", 0)
|
|
128
|
+
if type(stem) is not int or stem not in (-1, 0, 1):
|
|
129
|
+
raise ValueError("Invalid printed stem direction")
|
|
130
|
+
if guide_version < 281 and (stem != 0 or row.get("clefBottomDiatonic") in (22, 24)):
|
|
131
|
+
raise ValueError("Printed stem direction and C clefs require guide281")
|
|
132
|
+
kind = row.get("kind", "PITCHED")
|
|
133
|
+
if type(kind) is not str or kind not in ("PITCHED", "UNPITCHED"):
|
|
134
|
+
raise ValueError("Invalid attack kind")
|
|
135
|
+
if kind != "PITCHED" and row.get("boundaryTies", 0) & 16:
|
|
136
|
+
raise ValueError("Unpitched tab tie identity")
|
|
137
|
+
if guide_version < 282 and kind != "PITCHED":
|
|
138
|
+
raise ValueError("Unpitched attacks require guide282")
|
|
139
|
+
size = row.get("visualSize", "FULL")
|
|
140
|
+
if type(size) is not str or size not in ("FULL", "CUE"):
|
|
141
|
+
raise ValueError("Invalid note visual size")
|
|
142
|
+
if guide_version < 293 and size != "FULL":
|
|
143
|
+
raise ValueError("Cue visual size requires guide293")
|
|
144
|
+
normalized_notes.append(dict(row, boundaryTies=row.get("boundaryTies", 0),
|
|
145
|
+
tupletNormalNotes=normal, stemDirection=stem,
|
|
146
|
+
kind=0 if kind == "PITCHED" else 1,
|
|
147
|
+
visualSize=0 if size == "FULL" else 1))
|
|
148
|
+
_records(output, normalized_notes, note_fields, 250_000)
|
|
149
|
+
_signature_records(output, score["keyChanges"], (("measureIndex", "i"), ("fifths", "i")), len(measures), guide_version)
|
|
150
|
+
_records(output, score["tempoChanges"], (("measureIndex", "i"),
|
|
151
|
+
("positionInMeasure", "f"), ("bpm", "d"), ("beatUnit", "d")), len(measures) * 2)
|
|
152
|
+
_signature_records(output, score["meterChanges"], (("measureIndex", "i"),
|
|
153
|
+
("numerator", "i"), ("denominator", "i")), len(measures), guide_version)
|
|
154
|
+
rest_fields = (("measureIndex", "i"),
|
|
155
|
+
("positionInMeasure", "f"), ("pageY", "f"), ("pageHeight", "f"),
|
|
156
|
+
("staffIndex", "i"), ("staffCount", "i"), ("durationBeats", "d"))
|
|
157
|
+
if guide_version >= 283:
|
|
158
|
+
rest_fields += (("kind", "B"),)
|
|
159
|
+
rests = score["rests"]
|
|
160
|
+
if not isinstance(rests, list) or any(not isinstance(row, dict) for row in rests):
|
|
161
|
+
raise ValueError("Invalid rest records")
|
|
162
|
+
normalized_rests = []
|
|
163
|
+
for row in rests:
|
|
164
|
+
kind = row.get("kind", "LITERAL")
|
|
165
|
+
if type(kind) is not str or kind not in ("LITERAL", "FULL_MEASURE"):
|
|
166
|
+
raise ValueError("Invalid rest kind")
|
|
167
|
+
duration = _field(row, "durationBeats")
|
|
168
|
+
if (type(duration) not in (int, float) or not math.isfinite(duration)
|
|
169
|
+
or not 0 < duration <= 16):
|
|
170
|
+
raise ValueError("Invalid rest duration")
|
|
171
|
+
if kind == "FULL_MEASURE" and duration != 4:
|
|
172
|
+
raise ValueError("Invalid full-measure rest glyph base")
|
|
173
|
+
if guide_version < 283 and kind == "FULL_MEASURE":
|
|
174
|
+
raise ValueError("Full-measure rest requires guide283")
|
|
175
|
+
normalized_rests.append(dict(row, kind=0 if kind == "LITERAL" else 1))
|
|
176
|
+
_records(output, normalized_rests, rest_fields, 250_000)
|
|
177
|
+
techniques=score["techniqueChanges"]
|
|
178
|
+
if (not isinstance(techniques,list) or any(not isinstance(row,dict)
|
|
179
|
+
or type(row.get("technique")) is not int or not 0<=row["technique"]<=7 for row in techniques)):
|
|
180
|
+
raise ValueError("Invalid playing technique")
|
|
181
|
+
if guide_version < 291 and any(row["technique"] in (6,7) for row in techniques):
|
|
182
|
+
raise ValueError("Attack-local bow directions require guide291")
|
|
183
|
+
# Bow entries are exact attacks, never persistent instructions. Validate before writing.
|
|
184
|
+
fields=("measureIndex","positionInMeasure","staffIndex","staffCount")
|
|
185
|
+
attacks={tuple(note.get(k) for k in fields) for note in notes}
|
|
186
|
+
for row in techniques:
|
|
187
|
+
if row["technique"] in (6,7) and tuple(row.get(k) for k in fields) not in attacks:
|
|
188
|
+
raise ValueError("Bow direction has no exact source attack")
|
|
189
|
+
_records(output, techniques, (("measureIndex", "i"),
|
|
190
|
+
("positionInMeasure", "f"), ("staffIndex", "i"), ("staffCount", "i"),
|
|
191
|
+
("technique", "i")), len(measures) * 16 + (len(notes) if guide_version >= 291 else 0))
|
|
192
|
+
dynamics = score["dynamicChanges"]
|
|
193
|
+
if not isinstance(dynamics, list) or any(not isinstance(row, dict)
|
|
194
|
+
or type(row.get("fixedTarget", False)) is not bool
|
|
195
|
+
or type(row.get("sharedTiming", row.get("sharedStaffs", False))) is not bool for row in dynamics):
|
|
196
|
+
raise ValueError("Invalid dynamic target")
|
|
197
|
+
dynamic_fields = (("measureIndex", "i"), ("positionInMeasure", "f"),
|
|
198
|
+
("staffIndex", "i"), ("staffCount", "i"), ("endMeasureIndex", "i"),
|
|
199
|
+
("endPosition", "f"), ("decibels", "f"), ("direction", "i"), ("sharedStaffs", "?"))
|
|
200
|
+
if guide_version >= 261:
|
|
201
|
+
dynamic_fields += (("fixedTarget", "?"), ("sharedTiming", "?"))
|
|
202
|
+
_records(output, [dict(row, fixedTarget=row.get("fixedTarget", False),
|
|
203
|
+
sharedTiming=row.get("sharedTiming", False) or row.get("sharedStaffs", False))
|
|
204
|
+
for row in dynamics], dynamic_fields, len(measures) * 32)
|
|
205
|
+
directions = score.get("playbackDirections", [])
|
|
206
|
+
expressions = score.get("expressiveEvents", [])
|
|
207
|
+
if not isinstance(directions,list) or not isinstance(expressions,list):
|
|
208
|
+
raise ValueError("Invalid semantic record lists")
|
|
209
|
+
if guide_version >= 263:
|
|
210
|
+
output.write(semantic_wire.encode(directions, expressions, len(measures), guide_version))
|
|
211
|
+
else:
|
|
212
|
+
if guide_version==260 and directions:
|
|
213
|
+
raise ValueError("Guide260 cannot preserve playback directions")
|
|
214
|
+
if expressions or any(semantic_wire.direction(row,len(measures))['details'] != semantic_wire.DEFAULT_DETAILS
|
|
215
|
+
or row['kind'] > 3 for row in directions):
|
|
216
|
+
raise ValueError("Legacy guide format would lose semantic records")
|
|
217
|
+
if guide_version < 263 and (not isinstance(directions, list) or any(
|
|
218
|
+
not isinstance(row, dict) or type(row.get("measureBoundary")) is not int
|
|
219
|
+
or not 0 <= row["measureBoundary"] <= len(measures)
|
|
220
|
+
or type(row.get("kind")) is not int or not 0 <= row["kind"] <= 3
|
|
221
|
+
for row in directions)):
|
|
222
|
+
raise ValueError("Invalid playback direction")
|
|
223
|
+
if 261 <= guide_version < 263:
|
|
224
|
+
_records(output, directions, (("measureBoundary", "i"), ("kind", "i")), (len(measures)+1)*4)
|
|
225
|
+
if guide_version >= 285:
|
|
226
|
+
from . import slur_wire
|
|
227
|
+
output.write(slur_wire.encode_source(score.get("slurSource"), len(score["notes"])))
|
|
228
|
+
elif score.get("slurSource") is not None:
|
|
229
|
+
from . import slur_wire
|
|
230
|
+
if not slur_wire.is_empty_source(score["slurSource"], len(score["notes"])):
|
|
231
|
+
raise ValueError("Legacy guide format cannot preserve retained slur source")
|
|
232
|
+
pickup = score.get("openingPickup", False)
|
|
233
|
+
if type(pickup) is not bool:
|
|
234
|
+
raise ValueError("Invalid opening pickup flag")
|
|
235
|
+
if pickup and (not measures or score["firstMeasureNumber"] > 1):
|
|
236
|
+
raise ValueError("Opening pickup conflicts with source geometry or numbering")
|
|
237
|
+
if guide_version >= 286:
|
|
238
|
+
_pack(output, "B", int(pickup))
|
|
239
|
+
elif pickup:
|
|
240
|
+
raise ValueError("Legacy guide format cannot preserve opening pickup proof")
|
|
241
|
+
from . import tremolo_wire
|
|
242
|
+
tremolo = tremolo_wire.encode_source(score.get("tremoloSource"), len(score["notes"]), allow_clock_policies=guide_version >= 298)
|
|
243
|
+
if guide_version >= 289:
|
|
244
|
+
output.write(tremolo)
|
|
245
|
+
elif tremolo != tremolo_wire.encode_source(None, len(score["notes"])):
|
|
246
|
+
raise ValueError("Legacy guide format cannot preserve tremolo source")
|
|
247
|
+
from . import multi_rest_wire
|
|
248
|
+
groups = multi_rest_wire.checked_score(score)
|
|
249
|
+
if guide_version >= 292:
|
|
250
|
+
output.write(multi_rest_wire.encode_groups(groups, len(measures)))
|
|
251
|
+
elif groups:
|
|
252
|
+
raise ValueError("Legacy guide format cannot preserve printed rest groups")
|
|
253
|
+
from . import beam_source
|
|
254
|
+
if guide_version >= 297:
|
|
255
|
+
output.write(beam_source.encode_source(score.get("beamSource"), len(notes)))
|
|
256
|
+
else:
|
|
257
|
+
beam_source.require_legacy_compatible(score.get("beamSource"), len(notes))
|
|
258
|
+
from . import clock_context
|
|
259
|
+
context = score.get("clockContextSource")
|
|
260
|
+
if context is None:
|
|
261
|
+
context = clock_context.unavailable(len(notes), len(measures), len(score.get("rests", [])))
|
|
262
|
+
if guide_version >= 299:
|
|
263
|
+
output.write(clock_context.encode(context, len(notes), len(measures), len(score.get("rests", []))))
|
|
264
|
+
else:
|
|
265
|
+
clock_context.require_legacy_compatible(context, len(notes), len(measures), len(score.get("rests", [])))
|
|
266
|
+
if output.tell() > MAX_GUIDE_BYTES:
|
|
267
|
+
raise ValueError("Guide exceeds cache limit")
|
|
268
|
+
return output.getvalue()
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def write_new(path, score, guide_version=GUIDE_VERSION):
|
|
272
|
+
"""Create one candidate guide without replacing any existing result."""
|
|
273
|
+
path = Path(path)
|
|
274
|
+
if path.exists():
|
|
275
|
+
raise FileExistsError(path)
|
|
276
|
+
data = encode(score, guide_version)
|
|
277
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
278
|
+
descriptor, temporary = tempfile.mkstemp(prefix=".guide-", dir=path.parent)
|
|
279
|
+
try:
|
|
280
|
+
with os.fdopen(descriptor, "wb") as stream:
|
|
281
|
+
stream.write(data)
|
|
282
|
+
stream.flush()
|
|
283
|
+
os.fsync(stream.fileno())
|
|
284
|
+
os.link(temporary, path)
|
|
285
|
+
finally:
|
|
286
|
+
if os.path.exists(temporary):
|
|
287
|
+
os.unlink(temporary)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Copyright 2026 Luckolite
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
"""Tab performance semantics, independent of written pitch and duration."""
|
|
4
|
+
import math
|
|
5
|
+
|
|
6
|
+
KINDS = {'none', 'slide', 'hammer_on', 'pull_off', 'bend', 'bend_release',
|
|
7
|
+
'dead', 'harmonic', 'tap', 'whammy', 'whammy_release', 'pre_bend',
|
|
8
|
+
'strum_down', 'strum_up'}
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def validate(effect):
|
|
12
|
+
kind = effect.get('type', 'none')
|
|
13
|
+
delta = effect.get('semitones', 0)
|
|
14
|
+
rank = effect.get('strumIndex', 0)
|
|
15
|
+
if (kind not in KINDS or type(delta) not in (int, float)
|
|
16
|
+
or not math.isfinite(delta) or abs(delta) > 24
|
|
17
|
+
or type(rank) is not int or not 0 <= rank <= 6
|
|
18
|
+
or (kind.startswith('strum_') and delta != 0)
|
|
19
|
+
or (not kind.startswith('strum_') and rank != 0)):
|
|
20
|
+
raise ValueError('Unsupported guitar performance effect')
|
|
21
|
+
return kind, delta, rank
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def pitch_offset(kind, delta, t):
|
|
25
|
+
smooth = lambda x: x*x*(3-2*x)
|
|
26
|
+
if kind == 'slide':
|
|
27
|
+
return delta*(1-smooth(min(1, t/.3)))
|
|
28
|
+
if kind in ('bend', 'whammy'):
|
|
29
|
+
return delta*smooth(min(1, t/.4))
|
|
30
|
+
if kind in ('bend_release', 'whammy_release'):
|
|
31
|
+
return delta*(smooth(t/.5) if t < .5 else 1-smooth((t-.5)/.5))
|
|
32
|
+
return delta if kind == 'pre_bend' else 0
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Copyright 2026 Luckolite
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
"""MusicXML guitar gestures; bend changes are semitones, fractions included."""
|
|
4
|
+
import xml.etree.ElementTree as ET
|
|
5
|
+
from .guitar_effect import validate
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def write(notation, effect):
|
|
9
|
+
kind, delta, _ = validate(effect)
|
|
10
|
+
technical = None
|
|
11
|
+
if kind in ('bend', 'bend_release', 'whammy', 'whammy_release', 'pre_bend'):
|
|
12
|
+
technical = notation.find('technical')
|
|
13
|
+
if technical is None:
|
|
14
|
+
technical = ET.SubElement(notation, 'technical')
|
|
15
|
+
bend = ET.SubElement(technical, 'bend', {'shape': 'curved'})
|
|
16
|
+
ET.SubElement(bend, 'bend-alter').text = f'{delta:g}'
|
|
17
|
+
if kind == 'pre_bend':
|
|
18
|
+
ET.SubElement(bend, 'pre-bend')
|
|
19
|
+
if kind.startswith('whammy'):
|
|
20
|
+
ET.SubElement(bend, 'with-bar')
|
|
21
|
+
if kind.endswith('_release'):
|
|
22
|
+
release = ET.SubElement(technical, 'bend', {'shape': 'curved'})
|
|
23
|
+
ET.SubElement(release, 'bend-alter').text = f'{-delta:g}'
|
|
24
|
+
ET.SubElement(release, 'release')
|
|
25
|
+
if kind.startswith('whammy'):
|
|
26
|
+
ET.SubElement(release, 'with-bar')
|
|
27
|
+
elif kind.startswith('strum_'):
|
|
28
|
+
# A downstroke crosses bass to treble; MusicXML's direction is sounding pitch order.
|
|
29
|
+
ET.SubElement(notation, 'arpeggiate', {'direction': 'up' if kind == 'strum_down' else 'down'})
|
|
30
|
+
else:
|
|
31
|
+
description = kind
|
|
32
|
+
if delta:
|
|
33
|
+
description += f' ({delta:+g} semitones)'
|
|
34
|
+
ET.SubElement(notation, 'other-notation', {'type': 'single'}).text = description
|
|
35
|
+
if effect.get('vibrato'):
|
|
36
|
+
ET.SubElement(notation, 'other-notation', {'type': 'single'}).text = 'vibrato'
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# Copyright 2026 Luckolite
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
"""Export admitted wedges using owned written clocks and explicit bar boundaries."""
|
|
4
|
+
from .technique_export import position
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def plan(page, rendered, bars, page_identity):
|
|
8
|
+
notes = page.get('score', {}).get('notes', [])
|
|
9
|
+
events = page.get('events', [])
|
|
10
|
+
if len(notes) != len(events) or len(rendered) != len(notes):
|
|
11
|
+
return []
|
|
12
|
+
|
|
13
|
+
def scoped(note, measure, staff, count, shared):
|
|
14
|
+
return (note.get('measureIndex') == measure
|
|
15
|
+
and (note.get('staffCount') == count if shared else note.get('staffIndex') == staff))
|
|
16
|
+
|
|
17
|
+
def clock(i, measure, count):
|
|
18
|
+
note, event, row = notes[i], events[i], rendered[i]
|
|
19
|
+
if (any(type(note.get(k)) is not int for k in ('measureIndex', 'staffIndex', 'staffCount'))
|
|
20
|
+
or note['staffCount'] != count or not 0 <= note['staffIndex'] < count
|
|
21
|
+
or type(event.get('sourceNoteIndex')) is not int or event['sourceNoteIndex'] != i
|
|
22
|
+
or event.get('durationFallback') is not False
|
|
23
|
+
or any(type(event.get(k)) is not int or event[k] != note[k]
|
|
24
|
+
for k in ('measureIndex', 'staffIndex', 'staffCount'))
|
|
25
|
+
or row.get('grace', False)
|
|
26
|
+
or type(row.get('start')) is not int or type(row.get('end')) is not int
|
|
27
|
+
or not bars[measure]['start'] <= row['start'] < bars[measure]['start'] + bars[measure]['length']
|
|
28
|
+
or row['end'] <= row['start']):
|
|
29
|
+
return None
|
|
30
|
+
return row['start'], row['end']
|
|
31
|
+
|
|
32
|
+
def column(measure, p, staff, count, shared):
|
|
33
|
+
owners = [i for i, n in enumerate(notes) if scoped(n, measure, staff, count, shared)
|
|
34
|
+
and position(n.get('positionInMeasure')) == p]
|
|
35
|
+
if not owners:
|
|
36
|
+
return 'unowned', None
|
|
37
|
+
clocks = [clock(i, measure, count) for i in owners]
|
|
38
|
+
if any(c is None for c in clocks) or len({c[0] for c in clocks}) != 1:
|
|
39
|
+
return 'invalid', None
|
|
40
|
+
return 'exact', clocks[0][0]
|
|
41
|
+
|
|
42
|
+
def whole_hold(measure, p, staff, count, shared):
|
|
43
|
+
owners = [i for i, n in enumerate(notes) if scoped(n, measure, staff, count, shared)
|
|
44
|
+
and n.get('staffCount') == count]
|
|
45
|
+
if not owners:
|
|
46
|
+
return None
|
|
47
|
+
for i in owners:
|
|
48
|
+
n = notes[i]
|
|
49
|
+
x = position(n.get('positionInMeasure'))
|
|
50
|
+
c = clock(i, measure, count)
|
|
51
|
+
if (x is None or x <= p or n.get('leadingRestBeats', 0) != 0
|
|
52
|
+
or n.get('followingRestBeats', 0) != 0 or c != (
|
|
53
|
+
bars[measure]['start'], bars[measure]['start'] + bars[measure]['length'])):
|
|
54
|
+
return None
|
|
55
|
+
return bars[measure]['start']
|
|
56
|
+
|
|
57
|
+
result, seen = [], set()
|
|
58
|
+
for index, change in enumerate(page.get('score', {}).get('dynamicChanges', [])):
|
|
59
|
+
m, end, staff, count = (change.get(k) for k in
|
|
60
|
+
('measureIndex', 'endMeasureIndex', 'staffIndex', 'staffCount'))
|
|
61
|
+
start_pos, end_pos = position(change.get('positionInMeasure')), position(change.get('endPosition'))
|
|
62
|
+
direction = change.get('direction')
|
|
63
|
+
shared = change.get('sharedStaffs', False)
|
|
64
|
+
timing = change.get('sharedTiming', shared)
|
|
65
|
+
if (type(direction) is not int or direction not in (-1, 1)
|
|
66
|
+
or type(m) is not int or type(end) is not int or not 0 <= m <= end < len(bars)
|
|
67
|
+
or type(staff) is not int or type(count) is not int or not 0 <= staff < count <= 8
|
|
68
|
+
or start_pos is None or end_pos is None
|
|
69
|
+
or type(shared) is not bool or type(timing) is not bool):
|
|
70
|
+
continue
|
|
71
|
+
timing |= shared
|
|
72
|
+
status, start = column(m, start_pos, staff, count, timing)
|
|
73
|
+
if status == 'invalid':
|
|
74
|
+
continue
|
|
75
|
+
if status == 'unowned':
|
|
76
|
+
start = whole_hold(m, start_pos, staff, count, timing)
|
|
77
|
+
if start is None:
|
|
78
|
+
start = bars[m]['start'] + int(bars[m]['length'] * start_pos + .5)
|
|
79
|
+
if end_pos in (0, 1):
|
|
80
|
+
stop = bars[end]['start'] + int(bars[end]['length'] * end_pos)
|
|
81
|
+
else:
|
|
82
|
+
status, stop = column(end, end_pos, staff, count, timing)
|
|
83
|
+
if status == 'invalid':
|
|
84
|
+
continue
|
|
85
|
+
if status == 'unowned':
|
|
86
|
+
stop = bars[end]['start'] + int(bars[end]['length'] * end_pos + .5)
|
|
87
|
+
if stop <= start:
|
|
88
|
+
continue
|
|
89
|
+
targets = sorted({n['staffIndex'] for n in notes
|
|
90
|
+
if type(n.get('staffIndex')) is int and 0 <= n['staffIndex'] < count
|
|
91
|
+
and type(n.get('staffCount')) is int and n['staffCount'] == count}) if shared else [staff]
|
|
92
|
+
for target in targets:
|
|
93
|
+
key = (m, end, target, count, direction, start, stop)
|
|
94
|
+
if key in seen:
|
|
95
|
+
continue
|
|
96
|
+
seen.add(key)
|
|
97
|
+
result.append(dict(identity=f'{page_identity}/hairpin:{index}:{target}',
|
|
98
|
+
kind='CRESCENDO' if direction == 1 else 'DIMINUENDO', scope='PART',
|
|
99
|
+
staffIndex=target, staffCount=count, startTick=start, endTick=stop,
|
|
100
|
+
ownerIds=[], evidence=[], _hairpin='crescendo' if direction == 1 else 'diminuendo',
|
|
101
|
+
_hairpinStartBarStart=bars[m]['start'], _hairpinEndBarStart=bars[end]['start']))
|
|
102
|
+
return result
|
|
Binary file
|