forked from tonaljs/tonal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
145 lines (134 loc) · 4.47 KB
/
test.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
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
import Note from "./index";
const $ = (str: string) => str.split(" ");
describe("note", () => {
test("get", () => {
expect(Note.get("C4")).toEqual({
acc: "",
alt: 0,
chroma: 0,
coord: [0, 4],
empty: false,
freq: 261.6255653005986,
height: 60,
letter: "C",
midi: 60,
name: "C4",
oct: 4,
pc: "C",
step: 0,
});
expect(Note.get("C4")).toEqual(Note.get(Note.get("C4")));
});
test("property shorthands", () => {
expect(Note.name("db")).toEqual("Db");
expect(Note.pitchClass("Ax4")).toEqual("A##");
expect(Note.chroma("db4")).toEqual(1);
expect(Note.midi("db4")).toEqual(61);
expect(Note.freq("A4")).toEqual(440);
});
test("simplify", () => {
expect(Note.simplify("C#")).toEqual("C#");
expect(Note.simplify("C##")).toEqual("D");
expect(Note.simplify("C###")).toEqual("D#");
expect(Note.simplify("B#4")).toEqual("C5");
const notes = $("C## C### F##4 Gbbb5 B#4 Cbb4");
expect(notes.map(Note.simplify)).toEqual($("D D# G4 E5 C5 Bb3"));
expect(Note.simplify("x")).toEqual("");
});
test("from midi", () => {
expect(Note.fromMidi(70)).toEqual("Bb4");
expect([60, 61, 62].map(Note.fromMidi)).toEqual(["C4", "Db4", "D4"]);
expect([60, 61, 62].map(Note.fromMidiSharps)).toEqual(["C4", "C#4", "D4"]);
});
test("names", () => {
expect(Note.names()).toEqual(["C", "D", "E", "F", "G", "A", "B"]);
expect(Note.names(["fx", "bb", 12, "nothing", {}, null])).toEqual([
"F##",
"Bb",
]);
});
test("sortedNames", () => {
expect(Note.sortedNames($("c f g a b h j"))).toEqual($("C F G A B"));
expect(Note.sortedNames($("c f g a b h j j h b a g f c"))).toEqual(
$("C C F F G G A A B B"),
);
expect(Note.sortedNames($("c2 c5 c1 c0 c6 c"))).toEqual(
$("C C0 C1 C2 C5 C6"),
);
expect(Note.sortedNames($("c2 c5 c1 c0 c6 c"), Note.descending)).toEqual(
$("C6 C5 C2 C1 C0 C"),
);
});
test("sortedUniq", () => {
expect(Note.sortedUniqNames($("a b c2 1p p2 c2 b c c3"))).toEqual(
$("C A B C2 C3"),
);
});
test("transpose", () => {
expect(Note.transpose("A4", "3M")).toEqual("C#5");
expect(Note.tr("A4", "3M")).toEqual("C#5");
});
test("transposeFrom", () => {
expect(Note.transposeFrom("C4")("5P")).toEqual("G4");
expect(["1P", "3M", "5P"].map(Note.transposeFrom("C"))).toEqual([
"C",
"E",
"G",
]);
});
test("transposeBy", () => {
expect(Note.transposeBy("5P")("C4")).toEqual("G4");
expect(["C", "D", "E"].map(Note.transposeBy("5P"))).toEqual([
"G",
"A",
"B",
]);
});
test("enharmonic", () => {
expect(Note.enharmonic("C#")).toEqual("Db");
expect(Note.enharmonic("C##")).toEqual("D");
expect(Note.enharmonic("C###")).toEqual("Eb");
expect(Note.enharmonic("B#4")).toEqual("C5");
const notes = $("C## C### F##4 Gbbb5 B#4 Cbb4");
expect(notes.map((n) => Note.enharmonic(n))).toEqual(
$("D Eb G4 E5 C5 A#3"),
);
expect(Note.enharmonic("x")).toEqual("");
expect(Note.enharmonic("F2", "E#")).toBe("E#2");
expect(Note.enharmonic("B2", "Cb")).toBe("Cb3");
expect(Note.enharmonic("C2", "B#")).toBe("B#1");
expect(Note.enharmonic("F2", "Eb")).toBe("");
});
test("transposeFifths", () => {
expect(Note.transposeFifths("G4", 3)).toEqual("E6");
expect(Note.transposeFifths("G", 3)).toEqual("E");
const ns = [0, 1, 2, 3, 4, 5].map((n) => Note.transposeFifths("C2", n));
expect(ns).toEqual(["C2", "G2", "D3", "A3", "E4", "B4"]);
const sharps = [0, 1, 2, 3, 4, 5, 6].map((n) =>
Note.transposeFifths("F#", n),
);
expect(sharps).toEqual(["F#", "C#", "G#", "D#", "A#", "E#", "B#"]);
const flats = [0, -1, -2, -3, -4, -5, -6].map((n) =>
Note.transposeFifths("Bb", n),
);
expect(flats).toEqual(["Bb", "Eb", "Ab", "Db", "Gb", "Cb", "Fb"]);
});
test("transposeOctaves", () => {
expect(
[0, 1, 2, 3, 4].map((oct) => Note.transposeOctaves("C4", oct)).join(" "),
).toEqual("C4 C5 C6 C7 C8");
expect(
[-1, -2, -3, -4, -5]
.map((oct) => Note.transposeOctaves("C4", oct))
.join(" "),
).toEqual("C3 C2 C1 C0 C-1");
});
test("fromFreq", () => {
expect(Note.fromFreq(440)).toEqual("A4");
expect(Note.fromFreq(444)).toEqual("A4");
expect(Note.fromFreq(470)).toEqual("Bb4");
expect(Note.fromFreqSharps(470)).toEqual("A#4");
expect(Note.fromFreq(0)).toEqual("");
expect(Note.fromFreq(NaN)).toEqual("");
});
});