-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote.js
168 lines (139 loc) · 3.59 KB
/
note.js
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
const noteUtils = require("./notes.js");
const UserException = require("./exception.js");
class Note {
constructor(input) {
if(typeof(input) === "number") {
this.fromNumber(input);
}
else if(!isNaN(input)) {
this.fromNumber(Number(input))
}
else if(typeof(input) === "string") {
this.fromName(input);
}
else if(typeof(input) === "object" && input.constructor.name === "Note") {
return input;
}
else {
console.log(input)
throw new UserException("Invalid input!")
}
if(this.normalName === "B#") {
this.octave += 1;
}
}
fromName(name) {
let lastChar = name.slice(-1)
let octave = 4;
if(!isNaN(lastChar)) {
octave = Number(lastChar);
name = name.slice(0,-1);
}
this.octave = octave;
this.actualName = name.charAt(0).toUpperCase() + name.slice(1)
this.standardName = noteUtils.standardizeNote(name);
this.number = noteUtils.toNumber(this.standardName);
this.normalName = this.actualName;
if(this.actualName.length > 2) {
this.normalName = this.standardName;
}
this.accidental = this.getAccidental(this.normalName);
this.standardAccidental = this.getAccidental(this.standardName);
}
fromNumber(actualNumber) {
this.number = noteUtils.mod12(actualNumber);
this.octave = (actualNumber-this.number)/12 + 4;
let name = noteUtils.toNote(this.number);
this.standardName = name;
this.actualName = name;
this.normalName= name;
this.accidental = this.getAccidental(this.normalName);
this.standardAccidental = this.getAccidental(this.standardName);
}
shift(amt) {
let octaveShift = Math.floor(Math.abs(amt/12))
if(amt < 0) {
octaveShift *= -1;
}
this.octave += octaveShift;
amt -= 12*octaveShift
if(this.number+amt > 11) {
this.octave += 1;
}
else if(this.number+amt < 0) {
this.octave -= 1;
}
this.number = noteUtils.mod12(this.number+amt);
let name = noteUtils.toNote(this.number);
this.actualName = name;
this.normalName = name;
this.standardName = name;
this.accidental = this.getAccidental(this.normalName);
this.standardAccidental = this.getAccidental(this.standardName);
}
getAccidental(name) {
if(name.length === 1) {
return "";
}
else {
return name.charAt(1);
}
}
isAbove(note) {
return this.octave > note.octave ||
(this.octave === note.octave && this.number > note.number)
}
equals(note) {
return this.octave === note.octave && this.number === note.number;
}
static noAccidentalDistance(a, b) {
let lowerNote = new Note(a.actualName);
lowerNote.octave = a.octave;
let upperNote = new Note(b.actualName);
upperNote.octave = b.octave;
let scale = 1;
if(a.isAbove(b)) {
let temp = upperNote;
upperNote = lowerNote;
lowerNote = temp;
scale = -1;
}
let totalShift = 0;
if(lowerNote.accidental === "#") {
lowerNote.shift(-1);
}
if(upperNote.accidental === "#") {
upperNote.shift(-1)
}
while(upperNote.isAbove(lowerNote)) {
let shift = 2;
if(lowerNote.standardName === "B" || lowerNote.standardName === "E") {
shift = 1;
}
lowerNote.shift(shift);
totalShift += 1;
}
return totalShift * scale;
}
static halfstepsBetween(a, b) {
let lowerNote = new Note(a.actualName);
lowerNote.octave = a.octave;
let upperNote = new Note(b.actualName);
upperNote.octave = b.octave;
let scale = 1;
if(a.isAbove(b)) {
let temp = upperNote;
upperNote = lowerNote;
lowerNote = temp;
scale = -1;
}
let halfsteps = 0;
while(upperNote.isAbove(lowerNote)) {
lowerNote.shift(1);
halfsteps += 1;
}
console.log(scale * halfsteps)
return scale*halfsteps;
}
}
module.exports = Note;