import { TheoryINote } from "../interface/TheoryINote"; import { TheoryBeats } from "../../beats/TheoryBeats"; import { TheoryMath } from "../../../util/TheoryMath"; import { TheoryConstant } from "../../../util/TheoryConstant"; export class TheoryDuration { public static addNoteDuration(noteList_: TheoryINote[], Beats_: TheoryBeats) { // Array 记录同音连符的其实音符的 id const tieRecorderBank: number[] = []; /** *开始算staff1上的duration, 注意要排除 grace note */ for (let i = 0; i < noteList_.length; i++) { // 如果没有同音连线 if (!noteList_[i].isTieGroup) { // 如果没有同音连线,并且没有staccato if (!noteList_[i].isStaccato) { noteList_[i].duration = TheoryMath.keepDigitals( (noteList_[i].beats + noteList_[i].beatsDot) * Beats_.unit64Beats, 3 ); } // 如果没有同音连线, 但是有staccato else { // 该音符是大于16音符(全,2,4,8, 16), 都按16分音符换算, 因此 64/16=4 if (noteList_[i].type <= 16) { noteList_[i].duration = TheoryMath.keepDigitals(4 * Beats_.unit64Beats, 3); } // 如果没有同音连线, 但是有staccato, 并且该音符是小于16音符(32,64),按自身去算 else { noteList_[i].duration = TheoryMath.keepDigitals( (noteList_[i].beats + noteList_[i].beatsDot) * Beats_.unit64Beats, 3 ); } } noteList_[i].originalDuration = noteList_[i].duration; } // 如果该音符为同音连线相关音符 两个或多个同音连线链接 else { // 首先默认赋值 duration noteList_[i].duration = TheoryMath.keepDigitals( (noteList_[i].beats + noteList_[i].beatsDot) * Beats_.unit64Beats, 3 ); noteList_[i].originalDuration = noteList_[i].duration; // 并且不是最后一个(同音连符都出现在小节中间) if ( // 同音连服第一个音 noteList_[i].tie1 === "start" && noteList_[i].tie2 === null ) { tieRecorderBank.push(noteList_[i].svgID); } else { let firstTieNotedIndex = tieRecorderBank[0]; if (noteList_[firstTieNotedIndex] !== undefined) { noteList_[firstTieNotedIndex].duration = TheoryMath.keepDigitals( noteList_[firstTieNotedIndex].duration + noteList_[i].duration, 3 ); } // 最后在把这个note的 原始 duration 变成0; noteList_[i].duration = 0; if (noteList_[i].tie1 === "stop" && noteList_[i].tie2 === null) { tieRecorderBank.shift(); } } } } } public static addGraceNoteDuration(graceNoteList_: TheoryINote[]) { graceNoteList_.forEach((graceNote) => { graceNote.duration = TheoryConstant.graceNoteDuration; }); } }