-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
196 lines (172 loc) · 5.12 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const KEY_TONE_MAPPING = { "2": 1, "3": 3, "5": 6, "6": 8, "7": 10, // black keys
"q": 0, "w": 2, "e": 4, "r": 5, "t": 7, "y": 9, "u": 11} // white keys
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
let model = null;
let loadModel = async function(){
model = await tf.loadLayersModel('https://raw.githubusercontent.com/kubzoey95/chorder/main/model.json');
model.resetStates();
console.log("Model loaded!");
console.log(model);
}
await loadModel()
let synth = 0;
let loadSynth = async function(){
synth = new Tone.Sampler({
urls: {
A2: "A2.mp3",
A4: "A4.mp3",
A6: "A6.mp3",
B1: "B1.mp3",
B3: "B3.mp3",
B5: "B5.mp3",
B6: "B6.mp3",
C3: "C3.mp3",
C5: "C5.mp3",
D2: "D2.mp3",
D4: "D4.mp3",
D6: "D6.mp3",
D7: "D7.mp3",
E1: "E1.mp3",
E3: "E3.mp3",
E5: "E5.mp3",
F2: "F2.mp3",
F4: "F4.mp3",
F6: "F6.mp3",
F7: "F7.mp3",
G1: "G1.mp3",
G3: "G3.mp3",
G5: "G5.mp3"
},
attack: 1,
baseUrl: "https://raw.githubusercontent.com/nbrosowsky/tonejs-instruments/master/samples/harp/"
}).toDestination()
synth.volume.value = -20;
console.log("Synth loaded!");
console.log(synth);
}
loadSynth()
let noteStack = [];
let lastTime = performance.now();
let refreshTime = 0;
const canvas = document.querySelector('canvas');
const engine = new BABYLON.Engine(canvas, true);
let timeDelta = 0;
let time = performance.now();
let catmullRom = null;
let catmullRomSpline = null;
let path = []
for (let i=0;i<20;i++){
path.push(BABYLON.Vector3.Zero());
}
let camera = null;
const createScene = function () {
const scene = new BABYLON.Scene(engine);
scene.clearColor = new BABYLON.Color3(0, 0, 0);
camera = new BABYLON.ArcRotateCamera('camera', Math.PI / 2, 0, 100, new BABYLON.Vector3(0, 0, 0), scene);
camera.mode = BABYLON.Camera.ORTHOGRAPHIC_CAMERA;
catmullRomSpline = BABYLON.Mesh.CreateLines(null, path, null, null, catmullRomSpline);
return scene;
};
const scene = createScene();
var updatePath = function(path) {
for (var i = 0; noteStack.length > 1 && i < noteStack.length && i < path.length; i++) {
var x = noteStack[noteStack.length - 1 - i].x;
var z = noteStack[noteStack.length - 1 - i].z;
var y = noteStack[noteStack.length - 1 - i].y;
path[i].x = x;
path[i].y = y;
path[i].z = z;
}
};
let render = function(){
catmullRomSpline = BABYLON.Mesh.CreateLines("catmullRomSpline", path, scene, true);
scene.registerBeforeRender(function() {
updatePath(path);
catmullRomSpline = BABYLON.Mesh.CreateLines(null, path, null, null, catmullRomSpline);
});
engine.runRenderLoop(function () {
let perf = performance.now();
timeDelta = perf - time;
time = perf;
for (let note of noteStack){
note.x += timeDelta / 10;
}
scene.render();
});
window.addEventListener("resize", function () {
engine.resize();
});
}
let toneStarted = false;
const now = Tone.now();
let currentTone = null;
let currentChord = null;
let lastNotes = [0,0,0];
let playAndPush = function(toneToPlay){
synth && synth.triggerAttackRelease(Math.pow(2, (toneToPlay + 3) / 12) * 440.0, 5, Tone.now());
noteStack.push(new BABYLON.Vector3(-canvas.getBoundingClientRect().width / 2, 0, -(toneToPlay - 5) / 25 * canvas.getBoundingClientRect().height / 2));
if (noteStack.length > path.length){
noteStack = noteStack.slice(noteStack.length - path.length)
}
}
let chooseRandomNumber = function(weights, lnght = 4){
let sum = 0;
let weightsEntries = Array.from(weights.entries());
weightsEntries.sort((e1, e2) => e1[1] - e2[1]);
weightsEntries = weightsEntries.slice(weightsEntries.length - lnght);
weightsEntries = weightsEntries.map((e) => [e[0], (e[1] - weightsEntries[0][1]) / (weightsEntries[weightsEntries.length - 1][1] - weightsEntries[0][1])]);
let randomNumber = Math.random();
for(let [index, weight] of weightsEntries){
let newSum = sum + weight;
if (randomNumber < newSum){
return index;
}
sum = newSum;
}
}
let firstRun = true;
let goThroughModel = function(){
let prediction = null;
while(lastNotes.length > 2){
let lastNotesTensor = tf.oneHot(tf.tensor2d([lastNotes.slice(0,3)], [1, 3], 'int32'), 26);
prediction = model.predict([lastNotesTensor]);
lastNotes = lastNotes.slice(1);
}
prediction && lastNotes.push(chooseRandomNumber(Array.from(prediction.reshape([26]).dataSync()), firstRun ? 25 : 4));
firstRun = false;
}
let predictMelody = function(){
if (lastNotes.length > 2){
goThroughModel();
}
currentTone += lastNotes[lastNotes.length - 1] - 1 - 12;
if (currentTone > 30){
currentTone -= 12;
}
if (currentTone < -20){
currentTone += 12;
}
playAndPush(currentTone);
}
let play = false;
render();
let playLoop = async function(){
while(!play || !synth.loaded){
await sleep(500);
}
while(play){
await sleep(250);
predictMelody();
}
}
playLoop();
$('#button').on('mousedown', async function(e){
if(!toneStarted){
await Tone.start();
toneStarted = true;
}
play = true;
$('#button').hide();
});