/
Tie.ts
81 lines (70 loc) · 2.45 KB
/
Tie.ts
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
import {Note} from "./Note";
import { Fraction } from "../../Common/DataObjects/Fraction";
import { Pitch } from "../../Common/DataObjects/Pitch";
import { TieTypes } from "../../Common/Enums/";
import { PlacementEnum } from "../VoiceData/Expressions/AbstractExpression";
import log from "loglevel";
/**
* A [[Tie]] connects two notes of the same pitch and name, indicating that they have to be played as a single note.
*/
export class Tie {
constructor(note: Note, type: TieTypes) {
this.AddNote(note);
this.type = type;
}
private notes: Note[] = [];
private type: TieTypes;
public TieNumber: number = 1;
public TieDirection: PlacementEnum = PlacementEnum.NotYetDefined;
/** Can contain tie directions at certain note indices.
* For example, if it contains {2: PlacementEnum.Below}, then
* the tie should go downwards from Tie.Notes[2] onwards,
* even if tie.TieDirection is PlacementEnum.Above (tie starts going up on Notes[0]).
*/
public NoteIndexToTieDirection: NoteIndexToPlacementEnum = {};
public getTieDirection(startNote?: Note): PlacementEnum {
if (!startNote) {
return this.TieDirection;
}
for (let i: number = 0; i < this.Notes.length; i++) {
const tieNote: Note = this.Notes[i];
if (tieNote === startNote) {
const directionAtIndex: PlacementEnum = this.NoteIndexToTieDirection[i];
if (directionAtIndex) {
return directionAtIndex;
} else {
return this.TieDirection;
}
}
}
log.debug("tie.getTieDuration note not in tie.Notes");
// ^ happens in Christbaum measure 19 - probably note sharing stem
return this.TieDirection;
}
public get Notes(): Note[] {
return this.notes;
}
public get Type(): TieTypes {
return this.type;
}
public get StartNote(): Note {
return this.notes[0];
}
public get Duration(): Fraction {
const duration: Fraction = new Fraction();
for (const note of this.notes) {
duration.Add(note.Length);
}
return duration;
}
public get Pitch(): Pitch {
return this.StartNote.Pitch;
}
public AddNote(note: Note): void {
this.notes.push(note);
note.NoteTie = this;
}
}
export interface NoteIndexToPlacementEnum {
[key: number]: PlacementEnum;
}