Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 27x 33x 6x 6x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 1x 32x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x | /**
* @file 音符解析器
* @description 解析 MusicXML 中的 note 元素
*/
import { Note, TimeModification, TieInfo, BeamInfo, Lyric, GraceNote, NoteNotations } from '../models/Note';
import { Pitch, Rest, Unpitched } from '../models/Pitch';
import { Notations, TiedNotation, SlurNotation, TupletNotation, Articulation, Ornament } from '../models/Notations';
import { StepName, NoteType, StemDirection, BeamType, AccidentalType, TieSlurType, SyllabicType, Placement, ArticulationType, OrnamentType } from '../models/types';
import { getChild, getChildren, getTextContent, getIntContent, getNumberContent, getAttribute, getBooleanAttribute, hasChild } from '../utils/XMLUtils';
/**
* 解析 pitch 元素
*/
export function parsePitch(element: Element): Pitch {
const stepText = getTextContent(element, 'step');
const octaveNum = getIntContent(element, 'octave');
const alterNum = getNumberContent(element, 'alter');
if (!stepText || octaveNum === undefined) {
throw new Error('Invalid pitch element: missing step or octave');
}
return new Pitch({
step: stepText as StepName,
octave: octaveNum,
alter: (alterNum ?? 0) as any
});
}
/**
* 解析 rest 元素
*/
export function parseRest(element: Element): Rest {
const measure = hasChild(element, 'measure') || getAttribute(element, 'measure') === 'yes';
const displayStep = getTextContent(element, 'display-step') as StepName | undefined;
const displayOctave = getIntContent(element, 'display-octave');
return new Rest({
measure,
displayStep,
displayOctave
});
}
/**
* 解析 unpitched 元素
*/
export function parseUnpitched(element: Element): Unpitched {
const displayStep = getTextContent(element, 'display-step');
const displayOctave = getIntContent(element, 'display-octave');
if (!displayStep || displayOctave === undefined) {
throw new Error('Invalid unpitched element: missing display-step or display-octave');
}
return new Unpitched({
displayStep: displayStep as StepName,
displayOctave
});
}
/**
* 解析 time-modification 元素(连音符)
*/
export function parseTimeModification(element: Element): TimeModification {
const actualNotes = getIntContent(element, 'actual-notes');
const normalNotes = getIntContent(element, 'normal-notes');
const normalType = getTextContent(element, 'normal-type') as NoteType | undefined;
const normalDots = getChildren(element, 'normal-dot').length;
if (actualNotes === undefined || normalNotes === undefined) {
throw new Error('Invalid time-modification: missing actual-notes or normal-notes');
}
return {
actualNotes,
normalNotes,
normalType,
normalDots: normalDots > 0 ? normalDots : undefined
};
}
/**
* 解析 tie 元素
*/
export function parseTie(element: Element): TieInfo {
const type = getAttribute(element, 'type') as TieSlurType;
const number = getIntContent(element, 'number');
return { type, number };
}
/**
* 解析 beam 元素
*/
export function parseBeam(element: Element): BeamInfo {
const number = parseInt(getAttribute(element, 'number') ?? '1', 10);
const type = element.textContent?.trim() as BeamType;
return { number, type };
}
/**
* 解析 lyric 元素
*/
export function parseLyric(element: Element): Lyric {
const number = getAttribute(element, 'number');
const syllabic = getTextContent(element, 'syllabic') as SyllabicType | undefined;
const text = getTextContent(element, 'text') ?? '';
const extend = hasChild(element, 'extend');
const elision = hasChild(element, 'elision');
return {
number,
syllabic,
text,
extend,
elision
};
}
/**
* 解析 grace 元素
*/
export function parseGrace(element: Element): GraceNote {
const slash = getBooleanAttribute(element, 'slash');
const stealTimePrevious = getNumberContent(element, 'steal-time-previous');
const stealTimeFollowing = getNumberContent(element, 'steal-time-following');
const makeTime = getNumberContent(element, 'make-time');
return {
slash,
stealTimePrevious,
stealTimeFollowing,
makeTime
};
}
/**
* 解析 notations 元素中的 tied
*/
function parseTiedNotation(element: Element): TiedNotation {
return {
type: getAttribute(element, 'type') as TieSlurType,
number: getIntContent(element, 'number'),
placement: getAttribute(element, 'placement') as Placement | undefined,
orientation: getAttribute(element, 'orientation') as 'over' | 'under' | undefined
};
}
/**
* 解析 notations 元素中的 slur
*/
function parseSlurNotation(element: Element): SlurNotation {
return {
type: getAttribute(element, 'type') as TieSlurType,
number: getIntContent(element, 'number') ?? 1,
placement: getAttribute(element, 'placement') as Placement | undefined
};
}
/**
* 解析 notations 元素中的 tuplet
*/
function parseTupletNotation(element: Element): TupletNotation {
return {
type: getAttribute(element, 'type') as 'start' | 'stop',
number: getIntContent(element, 'number'),
bracket: getBooleanAttribute(element, 'bracket'),
showNumber: getAttribute(element, 'show-number') as 'actual' | 'both' | 'none' | undefined,
placement: getAttribute(element, 'placement') as Placement | undefined
};
}
/**
* 解析 articulations 元素
*/
function parseArticulations(element: Element): Articulation[] {
const articulations: Articulation[] = [];
const articulationTypes: ArticulationType[] = [
'accent', 'strong-accent', 'staccato', 'tenuto', 'detached-legato',
'staccatissimo', 'spiccato', 'scoop', 'plop', 'doit', 'falloff',
'breath-mark', 'caesura', 'stress', 'unstress', 'soft-accent'
];
for (const child of element.children) {
const type = child.tagName as ArticulationType;
if (articulationTypes.includes(type)) {
articulations.push({
type,
placement: getAttribute(child, 'placement') as Placement | undefined
});
}
}
return articulations;
}
/**
* 解析 ornaments 元素
*/
function parseOrnaments(element: Element): Ornament[] {
const ornaments: Ornament[] = [];
const ornamentTypes: OrnamentType[] = [
'trill-mark', 'turn', 'delayed-turn', 'inverted-turn', 'delayed-inverted-turn',
'vertical-turn', 'inverted-vertical-turn', 'shake', 'wavy-line', 'mordent',
'inverted-mordent', 'schleifer', 'tremolo', 'haydn', 'other-ornament'
];
for (const child of element.children) {
const type = child.tagName as OrnamentType;
if (ornamentTypes.includes(type)) {
ornaments.push({
type,
placement: getAttribute(child, 'placement') as Placement | undefined
});
}
}
return ornaments;
}
/**
* 解析 notations 元素
*/
export function parseNotations(element: Element): Notations {
const tied: TiedNotation[] = [];
const slur: SlurNotation[] = [];
const tuplet: TupletNotation[] = [];
let articulations: Articulation[] = [];
let ornaments: Ornament[] = [];
const dynamics: string[] = [];
for (const child of element.children) {
switch (child.tagName) {
case 'tied':
tied.push(parseTiedNotation(child));
break;
case 'slur':
slur.push(parseSlurNotation(child));
break;
case 'tuplet':
tuplet.push(parseTupletNotation(child));
break;
case 'articulations':
articulations = parseArticulations(child);
break;
case 'ornaments':
ornaments = parseOrnaments(child);
break;
case 'dynamics':
// 力度标记,获取第一个子元素的标签名
for (const dynChild of child.children) {
dynamics.push(dynChild.tagName);
}
break;
case 'fermata':
// 延音记号,简单处理
break;
}
}
return new Notations({
tied: tied.length > 0 ? tied : undefined,
slur: slur.length > 0 ? slur : undefined,
tuplet: tuplet.length > 0 ? tuplet : undefined,
articulations: articulations.length > 0 ? articulations : undefined,
ornaments: ornaments.length > 0 ? ornaments : undefined,
dynamics: dynamics.length > 0 ? dynamics : undefined
});
}
/**
* 解析 note 元素
*/
export function parseNote(element: Element): Note {
// 音高/休止符/无固定音高
let pitch: Pitch | undefined;
let rest: Rest | undefined;
let unpitched: Unpitched | undefined;
const pitchEl = getChild(element, 'pitch');
const restEl = getChild(element, 'rest');
const unpitchedEl = getChild(element, 'unpitched');
if (pitchEl) {
pitch = parsePitch(pitchEl);
} else if (restEl) {
rest = parseRest(restEl);
} else if (unpitchedEl) {
unpitched = parseUnpitched(unpitchedEl);
}
// 时值
const duration = getIntContent(element, 'duration') ?? 0;
const type = getTextContent(element, 'type') as NoteType | undefined;
const dots = getChildren(element, 'dot').length;
// 连音符
const timeModEl = getChild(element, 'time-modification');
const timeModification = timeModEl ? parseTimeModification(timeModEl) : undefined;
// 和弦
const chord = hasChild(element, 'chord');
// 装饰音
const graceEl = getChild(element, 'grace');
const grace = graceEl ? parseGrace(graceEl) : undefined;
// 提示音
const cue = hasChild(element, 'cue');
// 连音线
const tieElements = getChildren(element, 'tie');
const ties = tieElements.length > 0
? tieElements.map(parseTie)
: undefined;
// 符杠
const beamElements = getChildren(element, 'beam');
const beams = beamElements.length > 0
? beamElements.map(parseBeam)
: undefined;
// 符干
const stemText = getTextContent(element, 'stem');
const stem = stemText as StemDirection | undefined;
// 临时记号
const accidentalEl = getChild(element, 'accidental');
const accidental = accidentalEl?.textContent?.trim() as AccidentalType | undefined;
const accidentalBracket = accidentalEl ? getBooleanAttribute(accidentalEl, 'bracket') : undefined;
const accidentalCourtesy = accidentalEl ? getBooleanAttribute(accidentalEl, 'courtesy') : undefined;
// 声部和谱表
const voice = getIntContent(element, 'voice') ?? 1;
const staff = getIntContent(element, 'staff') ?? 1;
// 歌词
const lyricElements = getChildren(element, 'lyric');
const lyrics = lyricElements.length > 0
? lyricElements.map(parseLyric)
: undefined;
// 乐器
const instrumentEl = getChild(element, 'instrument');
const instrumentId = instrumentEl ? getAttribute(instrumentEl, 'id') : undefined;
// 记号
const notationsEl = getChild(element, 'notations');
const notationsData = notationsEl ? parseNotations(notationsEl) : undefined;
// ID
const id = getAttribute(element, 'id');
return new Note({
id,
pitch,
rest,
unpitched,
duration,
type,
dots,
timeModification,
chord,
grace,
cue,
ties,
beams,
stem,
accidental,
accidentalBracket: accidentalBracket ?? false,
accidentalCourtesy: accidentalCourtesy ?? false,
voice,
staff,
lyrics,
instrumentId,
notations: notationsData ? {
tied: notationsData.tied,
slurs: notationsData.slur,
tuplet: notationsData.tuplet?.[0],
articulations: notationsData.getArticulationNames(),
ornaments: notationsData.getOrnamentNames(),
dynamics: notationsData.dynamics
} : undefined
});
}
|