forked from Yiler7274/Konduktiva-github-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.js
103 lines (94 loc) · 3.53 KB
/
midi.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
// --------------------------------------------------------------------------
// -- midi.js
// -- initial author: Renick Bell ([email protected])
// -- initial creation date: Fri Jul 7 10:56 AM CST 2023
// -- contributors: Yiler Huang ([email protected]); Steve Wang ([email protected])
// -- license: GPL 3.0
// --------------------------------------------------------------------------
function sendMidiData(info, player, note) {
e.outputs[player.session - 1].send('noteon', {
note: note,
velocity: info.velocity,
channel: player.channel - 1,
});
//console.log("here")
setTimeout(() => {
e.outputs[player.session - 1].send('noteoff', {
note: note,
velocity: info.velocity,
channel: player.channel - 1,
});
}, beatsToTime(info.IoIs * 1000))
}
function sortMidiInfo(index, player) {
console.log(player.midiData.music)
let info = {
IoIs: player.midiData.IoIs[index],
bool: player.midiData.bools[index],
velocity: player.midiData.velocity[index],
music: player.midiData.music[index],
type: player.midiData.type
}
//console.log(info.IoIs)
if (info.type == "chords") {
//console.log("chords")
info.music.forEach(x => {
console.log(info.music)
console.log(Chord.detect(info.music.map(e => Midi.midiToNoteName(e))))
sendMidiData(info, player, x)
})
} else if (info.type == "melodies") {
//console.log("melodies")
console.log(info.music)
sendMidiData(info, player, info.music)
}
return true
}
function generateChords(root, octave, progression) {
let letterChords = Progression.fromRomanNumerals(root, progression);
let noteChords = letterChords.map(chord => Chord.get(chord).notes);
let midiChords = noteChords.map(c => c.map(n => Note.midi(n + "" + octave)));
//let barArray = progression.map(e => Progression.fromRomanNumerals(root, e).map(k => Chord.get(k).notes.map(f => f + "" + octave)))
//console.log(barArray)
//let midiChords = barArray.map(x => x.map(y => getMidiKeys(y)))
//console.log(midiChords)
return midiChords
}
function generateMelodies(root, octave, majorOrMinor) {
let notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
let scale = Scale.get(notes[root] + octave + " " + majorOrMinor + " blues").notes.concat(Scale.get(notes[root] + (octave + 1) + " " + majorOrMinor + " blues").notes)
console.log(scale)
let midiScale = scale.map(e =>
Note.midi(e)
)
let degree = steveRandomRange(0, midiScale.length - 1);
let currentNote = midiScale[degree];
let direction = (Math.random() < 0.5) ? -1 : 1;
//skip notes
let melody = [];
for (let i = 0; i < 32; i++) {
let interval = (Math.random() < 0.3) ? direction *= 2:direction*= 1;
if (Math.random() < 0.15){
interval = 0
}
currentNote = midiScale[degree];
melody.push(currentNote);
if (i % 4 == 0) {
direction = (Math.random() < 0.5) ? -1 : 1
}
degree += interval
if (degree > midiScale.length - 1) {
degree = midiScale.length - 5
}
if (degree < 0) {
degree = 5
}
}
return melody
}
// THIS FUNCtion is useful when you want to turn a chord into a bunch of midi key values.
//change this
function getMidiKeys(scaleOrChordNotesArray) {
let outputArray = scaleOrChordNotesArray.map(note => Note.midi(note))
return outputArray
}