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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 | /** * 音符位置计算器 * * @description 计算音符的Y坐标,支持多声部垂直布局 * * 音符区域高度分配(单声部): * ┌─────────────────────────────┐ * │ 升降号区域 (12px) │ ← accidental * ├─────────────────────────────┤ * │ 高音点区域 (8px) │ ← octave dots (high) * ├─────────────────────────────┤ * │ 音符数字 (24px) │ ← note number (基准线) * ├─────────────────────────────┤ * │ 低音点区域 (8px) │ ← octave dots (low) * ├─────────────────────────────┤ * │ 减时线区域 (12px) │ ← underlines * ├─────────────────────────────┤ * │ 歌词区域 (20px) │ ← lyrics (可选) * └─────────────────────────────┘ * * 多声部布局: * - 多声部垂直居中分布 * - 声部间距默认60px */ import { JianpuNote } from '../../models/JianpuNote'; import { JianpuMeasure } from '../../models/JianpuMeasure'; import { JianpuSystem } from '../../models/JianpuSystem'; import { RenderConfig, DEFAULT_RENDER_CONFIG } from '../config/RenderConfig'; // ==================== 类型定义 ==================== /** 音符位置计算配置 */ export interface NotePositionConfig { /** 声部间距(像素) */ voiceSpacing: number; /** 音符字体大小(像素),用于计算基准高度 */ noteFontSize: number; /** 歌词字体大小(像素) */ lyricFontSize: number; /** 是否显示歌词 */ showLyrics: boolean; /** 升降号区域高度(像素) */ accidentalAreaHeight: number; /** 高音点区域高度(像素) */ highOctaveAreaHeight: number; /** 低音点区域高度(像素) */ lowOctaveAreaHeight: number; /** 减时线区域高度(像素) */ underlineAreaHeight: number; /** 歌词区域高度(像素) */ lyricAreaHeight: number; } /** 音符垂直区域 */ export interface NoteVerticalRegions { /** 升降号Y坐标 */ accidentalY: number; /** 高音点起始Y坐标 */ highOctaveY: number; /** 音符数字Y坐标(基准线) */ noteY: number; /** 低音点起始Y坐标 */ lowOctaveY: number; /** 减时线起始Y坐标 */ underlineY: number; /** 歌词Y坐标 */ lyricY: number; /** 总高度 */ totalHeight: number; } /** 声部布局信息 */ export interface VoiceLayout { /** 声部索引 */ voiceIndex: number; /** 声部基准Y坐标 */ baseY: number; /** 声部区域 */ regions: NoteVerticalRegions; } /** 布局结果 */ export interface NoteLayoutResult { /** 更新后的音符数组 */ notes: JianpuNote[]; /** 声部布局信息 */ voiceLayouts: VoiceLayout[]; /** 布局统计 */ stats: { noteCount: number; voiceCount: number; totalHeight: number; layoutTime: number; }; } // ==================== 默认配置 ==================== const DEFAULT_NOTE_POSITION_CONFIG: NotePositionConfig = { voiceSpacing: 60, noteFontSize: DEFAULT_RENDER_CONFIG.noteFontSize, lyricFontSize: DEFAULT_RENDER_CONFIG.lyricFontSize, showLyrics: DEFAULT_RENDER_CONFIG.showLyrics, accidentalAreaHeight: 12, highOctaveAreaHeight: 8, lowOctaveAreaHeight: 8, underlineAreaHeight: 12, lyricAreaHeight: 20, }; // ==================== 主类 ==================== /** * 音符位置计算器 */ export class NotePositionCalculator { /** 配置 */ private config: NotePositionConfig; /** * 构造函数 * @param config 配置选项 */ constructor(config: Partial<NotePositionConfig> = {}) { this.config = { ...DEFAULT_NOTE_POSITION_CONFIG, ...config }; } /** * 计算单个音符的Y坐标 * * @param note 音符对象 * @param voiceCount 总声部数量 * @param baseY 基准Y坐标(行的Y坐标) * @returns 音符Y坐标 */ calculateNoteY(note: JianpuNote, voiceCount: number, baseY: number = 0): number { const voiceY = this.calculateVoiceY(baseY, note.voiceIndex, voiceCount); return voiceY; } /** * 计算声部的基准Y坐标 * * 多声部垂直居中分布: * - 单声部:直接使用baseY * - 多声部:以baseY为中心,声部均匀分布 * * @param baseY 行的基准Y坐标 * @param voiceIndex 声部索引(0开始) * @param voiceCount 总声部数量 * @returns 声部Y坐标 */ calculateVoiceY(baseY: number, voiceIndex: number, voiceCount: number): number { if (voiceCount <= 1) { return baseY; } const { voiceSpacing } = this.config; // 多声部垂直居中分布 // 总高度 = (声部数-1) * 声部间距 const totalHeight = (voiceCount - 1) * voiceSpacing; // 起始Y = 基准Y - 总高度/2 const startY = baseY - totalHeight / 2; // 声部Y = 起始Y + 声部索引 * 声部间距 return startY + voiceIndex * voiceSpacing; } /** * 计算音符的垂直区域 * * @param noteY 音符基准Y坐标 * @returns 垂直区域信息 */ calculateVerticalRegions(noteY: number): NoteVerticalRegions { const { noteFontSize, accidentalAreaHeight, highOctaveAreaHeight, lowOctaveAreaHeight, underlineAreaHeight, lyricAreaHeight, showLyrics, } = this.config; // 音符数字高度(约等于字体大小) const noteHeight = noteFontSize; // 计算各区域Y坐标(从上到下) // 音符Y是数字的中心点 const halfNoteHeight = noteHeight / 2; // 升降号区域:在音符上方 const accidentalY = noteY - halfNoteHeight - highOctaveAreaHeight - accidentalAreaHeight / 2; // 高音点区域:在音符上方,升降号下方 const highOctaveY = noteY - halfNoteHeight - highOctaveAreaHeight / 2; // 低音点区域:在音符下方 const lowOctaveY = noteY + halfNoteHeight + lowOctaveAreaHeight / 2; // 减时线区域:在低音点下方 const underlineY = noteY + halfNoteHeight + lowOctaveAreaHeight + underlineAreaHeight / 2; // 歌词区域:在减时线下方 const lyricY = noteY + halfNoteHeight + lowOctaveAreaHeight + underlineAreaHeight + lyricAreaHeight / 2; // 计算总高度 const topHeight = accidentalAreaHeight + highOctaveAreaHeight + halfNoteHeight; const bottomHeight = halfNoteHeight + lowOctaveAreaHeight + underlineAreaHeight + (showLyrics ? lyricAreaHeight : 0); const totalHeight = topHeight + bottomHeight; return { accidentalY, highOctaveY, noteY, lowOctaveY, underlineY, lyricY, totalHeight, }; } /** * 计算单声部的完整高度 * * @param showLyrics 是否显示歌词 * @returns 单声部高度 */ calculateSingleVoiceHeight(showLyrics: boolean = this.config.showLyrics): number { const { noteFontSize, accidentalAreaHeight, highOctaveAreaHeight, lowOctaveAreaHeight, underlineAreaHeight, lyricAreaHeight, } = this.config; let height = accidentalAreaHeight + highOctaveAreaHeight + noteFontSize + lowOctaveAreaHeight + underlineAreaHeight; if (showLyrics) { height += lyricAreaHeight; } return height; } /** * 计算多声部的总高度 * * @param voiceCount 声部数量 * @param showLyrics 是否显示歌词 * @returns 总高度 */ calculateMultiVoiceHeight(voiceCount: number, showLyrics: boolean = this.config.showLyrics): number { if (voiceCount <= 1) { return this.calculateSingleVoiceHeight(showLyrics); } const { voiceSpacing } = this.config; const singleVoiceHeight = this.calculateSingleVoiceHeight(showLyrics); // 多声部高度 = 单声部高度 + (声部数-1) * 声部间距 return singleVoiceHeight + (voiceCount - 1) * voiceSpacing; } /** * 为小节内的所有音符计算Y坐标 * * @param measure 小节对象 * @param baseY 基准Y坐标 * @returns 布局结果 */ layoutMeasureNotes(measure: JianpuMeasure, baseY: number): NoteLayoutResult { const startTime = performance.now(); const voiceCount = measure.voices.length; const voiceLayouts: VoiceLayout[] = []; let noteCount = 0; // 为每个声部计算布局 for (let voiceIndex = 0; voiceIndex < voiceCount; voiceIndex++) { const voice = measure.voices[voiceIndex]; const voiceBaseY = this.calculateVoiceY(baseY, voiceIndex, voiceCount); const regions = this.calculateVerticalRegions(voiceBaseY); voiceLayouts.push({ voiceIndex, baseY: voiceBaseY, regions, }); // 更新该声部所有音符的Y坐标 for (const note of voice) { note.y = voiceBaseY; note.height = regions.totalHeight; noteCount++; } } // 收集所有音符 const notes = measure.voices.flat(); return { notes, voiceLayouts, stats: { noteCount, voiceCount, totalHeight: this.calculateMultiVoiceHeight(voiceCount), layoutTime: performance.now() - startTime, }, }; } /** * 为行内的所有小节计算音符Y坐标 * * @param system 行对象 * @returns 布局结果 */ layoutSystemNotes(system: JianpuSystem): NoteLayoutResult { const startTime = performance.now(); const allNotes: JianpuNote[] = []; const allVoiceLayouts: VoiceLayout[] = []; let totalNoteCount = 0; let maxVoiceCount = 0; // 计算行内的基准Y坐标(行高度的中心) const baseY = system.y + system.height / 2; for (const measure of system.measures) { const result = this.layoutMeasureNotes(measure, baseY); allNotes.push(...result.notes); // 只记录第一个小节的声部布局(假设行内声部数量一致) if (allVoiceLayouts.length === 0) { allVoiceLayouts.push(...result.voiceLayouts); } totalNoteCount += result.stats.noteCount; maxVoiceCount = Math.max(maxVoiceCount, result.stats.voiceCount); } return { notes: allNotes, voiceLayouts: allVoiceLayouts, stats: { noteCount: totalNoteCount, voiceCount: maxVoiceCount, totalHeight: this.calculateMultiVoiceHeight(maxVoiceCount), layoutTime: performance.now() - startTime, }, }; } /** * 批量为所有行的音符计算Y坐标 * * @param systems 行数组 * @returns 所有布局结果 */ layoutAllSystems(systems: JianpuSystem[]): NoteLayoutResult[] { return systems.map(system => this.layoutSystemNotes(system)); } // ==================== 辅助方法 ==================== /** * 获取音符各部分的Y坐标偏移 * * @param note 音符对象 * @returns 各部分的Y坐标 */ getNotePartPositions(note: JianpuNote): { accidentalY: number; highDotsY: number[]; noteY: number; lowDotsY: number[]; underlineY: number; lyricY: number; } { const regions = this.calculateVerticalRegions(note.y); const { highOctaveAreaHeight, lowOctaveAreaHeight } = this.config; // 计算高音点位置(从下往上) const highDotsY: number[] = []; const highDotSpacing = highOctaveAreaHeight / 3; // 最多2个高音点 for (let i = 0; i < Math.abs(note.octave); i++) { if (note.octave > 0) { highDotsY.push(regions.highOctaveY + (i - 0.5) * highDotSpacing); } } // 计算低音点位置(从上往下) const lowDotsY: number[] = []; const lowDotSpacing = lowOctaveAreaHeight / 3; for (let i = 0; i < Math.abs(note.octave); i++) { if (note.octave < 0) { lowDotsY.push(regions.lowOctaveY + (i + 0.5) * lowDotSpacing); } } return { accidentalY: regions.accidentalY, highDotsY, noteY: regions.noteY, lowDotsY, underlineY: regions.underlineY, lyricY: regions.lyricY, }; } /** * 获取当前配置 */ getConfig(): NotePositionConfig { return { ...this.config }; } /** * 更新配置 */ updateConfig(config: Partial<NotePositionConfig>): void { Object.assign(this.config, config); } } // ==================== 工厂函数 ==================== /** * 创建音符位置计算器 */ export function createNotePositionCalculator(config?: Partial<NotePositionConfig>): NotePositionCalculator { return new NotePositionCalculator(config); } // ==================== 工具函数 ==================== /** * 计算声部Y坐标(独立函数) * * @param baseY 基准Y坐标 * @param voiceIndex 声部索引 * @param voiceCount 总声部数 * @param voiceSpacing 声部间距 * @returns 声部Y坐标 */ export function calculateVoiceY( baseY: number, voiceIndex: number, voiceCount: number, voiceSpacing: number = DEFAULT_NOTE_POSITION_CONFIG.voiceSpacing ): number { if (voiceCount <= 1) { return baseY; } const totalHeight = (voiceCount - 1) * voiceSpacing; const startY = baseY - totalHeight / 2; return startY + voiceIndex * voiceSpacing; } /** * 验证音符Y坐标布局 * * @param measures 小节数组 * @returns 验证结果 */ export function validateNoteYPositions(measures: JianpuMeasure[]): { valid: boolean; errors: string[]; } { const errors: string[] = []; for (const measure of measures) { // 检查声部Y坐标是否正确分布 const voiceYs = measure.voices.map(voice => { if (voice.length === 0) return null; return voice[0].y; }).filter(y => y !== null) as number[]; // 检查Y坐标是否递增(多声部时) for (let i = 1; i < voiceYs.length; i++) { if (voiceYs[i] <= voiceYs[i - 1]) { errors.push(`小节 ${measure.index} 声部Y坐标未递增: 声部${i-1}=${voiceYs[i-1]}, 声部${i}=${voiceYs[i]}`); } } // 检查同一声部的音符Y坐标是否一致 for (let v = 0; v < measure.voices.length; v++) { const voice = measure.voices[v]; if (voice.length < 2) continue; const firstY = voice[0].y; for (let n = 1; n < voice.length; n++) { if (voice[n].y !== firstY) { errors.push(`小节 ${measure.index} 声部 ${v} 音符Y坐标不一致: 音符0=${firstY}, 音符${n}=${voice[n].y}`); } } } } return { valid: errors.length === 0, errors, }; } /** * 获取默认配置 */ export function getDefaultNotePositionConfig(): NotePositionConfig { return { ...DEFAULT_NOTE_POSITION_CONFIG }; } |