-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusic.js
More file actions
111 lines (111 loc) · 2.94 KB
/
Music.js
File metadata and controls
111 lines (111 loc) · 2.94 KB
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
class Solfeggio {
constructor(height=5) {
this.setHeight(height);
this.MAJOR = [2,2,1,2,2,2,1];
this.SOLMISATION = this.defaultScale();
this.MINOR = this.modeShift(this.MAJOR,5);
this.MODES_NAMES = [
"IONIAN",
"DORIAN",
"PHRIGIAN",
"LYDIAN",
"MIXOLYDIAN",
"AEOLIAN",
"LOCRIAN"
];
this.MODES = {};
this.MODES_NAMES.map((e,i) => {
this.MODES[e] = this.makeMode(i);
})
}
setHeight(octaves) {
this.HEIGHT = 12*octaves;
}
makeNote(note,absolute=true) {
note = (isNaN(parseInt(note)))
? this.noteNameToMidi(note,absolute)
: parseInt(note);
return note;
}
makeMode(note,reference=this.MAJOR) {
note = (isNaN(parseInt(note)))
? this.SOLMISATION.indexOf(note)
: parseInt(note);
note = note % 12;
return this.modeShift(reference,note);
}
defaultScale() {
let scale = [];
scale.length = 7;
scale.fill(0);
scale = scale.map((e,i) => {
return String.fromCharCode(65+i); //65 is keycode for A
});
return this.modeShift(scale,2); //scale is shifted to C
}
generateScale(tonic,mode) {
let scale = [tonic];
for (let i in mode) {
scale.push(scale[i]+mode[i]);
}
return scale;
}
modeShift(reference,shifting) {
let out_mode = [];
reference.map((e,i,a) => {
out_mode[i] = a[(i+shifting)%a.length];
});
return out_mode;
}
cumulativeSumToIndex(array,max_index) {
return array.reduce((accumulator,element,index) => {
if (index > max_index) {
return accumulator;
} else {
return accumulator+element;
}
});
}
noteNameToMidi(human_note,absolute=true) {
let note = this.cumulativeSumToIndex(
[0,...this.MAJOR],
this.SOLMISATION.indexOf(human_note[0])
);
let sharps = {"#":1,"b":-1};
if (typeof sharps[human_note[1]] !== "undefined") {
note = note+sharps[human_note[1]];
}
note = (absolute) ? (this.HEIGHT+note):note;
return note;
}
makeChord(chord_object) {
chord_object.position = (typeof chord_object.position === "undefined")
? 0
: chord_object.position;
chord_object.tonic = this.makeNote(chord_object.tonic);
chord_object.mode = (Array.isArray(chord_object.mode))
? chord_object.mode
: this.makeMode(chord_object.mode);
chord_object.tonic = chord_object.tonic+this.cumulativeSumToIndex(
[0,...chord_object.mode],
chord_object.degree-1
);
let chord = [chord_object.tonic];
let degree_mode = this.modeShift(chord_object.mode,chord_object.degree-1);
[3,5,7].map(e =>
chord.push(chord_object.tonic+this.cumulativeSumToIndex(
[0,...degree_mode],
e-1
))
);
chord = this.modeShift(chord,chord_object.position);
for (let i in chord) {
while ((i > 0) && (chord[i] < chord[i-1])) {
chord[i] = chord[i]+12
}
};
return chord;
}
}
let Music = new Solfeggio();
export { Music };