-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-opposite-chord.js
28 lines (23 loc) · 958 Bytes
/
find-opposite-chord.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
const getNotesFromKey = (key) => {
const notesFromKeyC = ['C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B'];
const keyIndex = noteToIndex(key,notesFromKeyC);
const notesPre = notesFromKeyC.slice(0,keyIndex);
const notesPost = notesFromKeyC.slice(keyIndex);
return notesPost.concat(notesPre);
};
const noteToIndex = (note,notes) => notes.findIndex((x) => (x == note));
const indexToNote = (index,notes) => (index < 12) ? notes[index] : '?';
const getOppositeIndex = (index) => {
if (index < 8){
//fifth interval half center
return (7 - index);
} else {
//fourth interval half center
const newIndex = index - 8; // newIndex always in [0,3]
const newOpposite = 3 - newIndex;
return newOpposite+8;
}
};
const getOppositeNote = (x,notes) => indexToNote(getOppositeIndex(noteToIndex(x,notes)),notes);
const findOppositeChord = (key, chord) => chord.map(x => getOppositeNote(x,getNotesFromKey(key)));
module.exports = findOppositeChord;