-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathharmony.js
71 lines (65 loc) · 2.93 KB
/
harmony.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
// --------------------------------------------------------------------------
// -- harmony.js
// -- initial author: Renick Bell ([email protected])
// -- initial creation date: Wed Jun 28 10:08:48 AM CST 2023
// -- contributors: Yiler Huang ([email protected]); Steve Wang ([email protected])
// -- license: GPL 3.0
// --------------------------------------------------------------------------
//Array to record notes played on current beat, beat before and beat after:
let dissonanceRecorder = []
//function that checks for dissonance and changes them to 0:
function handleDissonance (beat, info){
add2Log('flagged dissonanceRecorder')
if (dissonanceRecorder.length > 5){
dissonanceRecorder.shift()
}
let indexOfBeat = dissonanceRecorder.indexOf(dissonanceRecorder.find(x => x.beat === beat))
if (indexOfBeat === -1){
dissonanceRecorder.push({beat: beat, notesPlayed: []})
indexOfBeat = dissonanceRecorder.length - 1
}
return info.noteValues.map(x => {
// if (dissonanceRecorder[indexOfBeat].notesPlayed.indexOf(x - 1) === - 1 && dissonanceRecorder[indexOfBeat].notesPlayed.indexOf(x + 1) === - 1 && checkBeatBeforeForDissonance(indexOfBeat, x) === false && checkBeatAfterForDissonance(indexOfBeat, x) === false){
if (dissonanceRecorder[indexOfBeat].notesPlayed.indexOf(x - 1) === - 1 && dissonanceRecorder[indexOfBeat].notesPlayed.indexOf(x + 1) === - 1){
checkIfAddToDissonanceRecorder(beat, x, indexOfBeat)
return x
}
else{
console.log('flagged disonacne', x - 1, dissonanceRecorder[indexOfBeat].notesPlayed.indexOf(x - 1) === - 1, 'next', x + 1, dissonanceRecorder[indexOfBeat].notesPlayed.indexOf(x + 1) === - 1)
checkIfAddToDissonanceRecorder(beat, x, indexOfBeat)
return 0
}
})
}
//Check beat before current beat for disonacne
function checkBeatBeforeForDissonance (indexOfBeat, x){
if (indexOfBeat === 0){
return false
}
else if (dissonanceRecorder[indexOfBeat - 1].notesPlayed.indexOf(x - 1) !== -1) {
return true
}
else if (dissonanceRecorder[indexOfBeat - 1].notesPlayed.indexOf(x + 1) !== -1) {
return true
}
return false
}
//Check beat after current beat for disonacne
function checkBeatAfterForDissonance (indexOfBeat, x){
if (indexOfBeat === dissonanceRecorder.length - 1){
return false
}
else if (dissonanceRecorder[indexOfBeat + 1].notesPlayed.indexOf(x - 1) !== -1) {
return true
}
else if (dissonanceRecorder[indexOfBeat + 1].notesPlayed.indexOf(x + 1) !== -1) {
return true
}
return false
}
//Checks if this not has been played in the current beat. If no, record it in dissonanceRecorder:
function checkIfAddToDissonanceRecorder (beat, note, indexOfBeat){
if (dissonanceRecorder[indexOfBeat].notesPlayed.indexOf(note) === -1){
dissonanceRecorder[indexOfBeat].notesPlayed.push(note)
}
}