-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
283 lines (251 loc) · 8.66 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
var canvas, ctx; //canvas variables
var color = "#007bff", //current line color
line={start:{x:-1, y:-1}, end:{x:-1, y:-1}}, //current line start and end points
notes=[], //array of saved lines
mode=true, //draw=true, erase=false
maxNoteSize, //the biggest possible length of a line
instrumentRange=[{start:21, end:108}, {start:21, end:108}, {start:21, end:108}, {start:21, end:96}]; //min and max MIDI note values per instrument (piano, ac. guitar, flute, el. guitar)
var previousMouse={x:-1, y:-1}; //where the mouse was in the previous frame (used for erasing lines)
var mediaRecorder, //used for recording
chunks = []; //used by MediaRecorder to save intermediate recordings
var time={previous:Date.now(), current:Date.now()}; //time between two frames, used to calculate velocity
window.onload=function(){
canvas = document.getElementById('canvas');
maxNoteSize=Math.round(Math.sqrt(Math.pow(canvas.width, 2)+Math.pow(canvas.height, 2)));
ctx = canvas.getContext("2d");
clearCanvas();
ctx.lineWidth = 3;
$("#canvas").mousedown(function(e) {
findxy('down', e);
});
$("#canvas").mouseup(function(e) {
findxy('up', e);
});
$("#canvas").mousemove(function(e) {
findxy('move', e);
});
MIDI.loadPlugin({
soundfontUrl: "./soundfont/",
instruments: ["acoustic_grand_piano", "acoustic_guitar_nylon", "flute", "electric_guitar_jazz"],
onprogress: function(state, progress) {
console.log(state, progress);
},
onsuccess: function() {
console.log("MIDI ready");
MIDI.programChange(0, 0); // acoustic_grand_piano
MIDI.programChange(1, 24); // acoustic_guitar_nylon
MIDI.programChange(2, 73); // flute
MIDI.programChange(3, 26); // electric_guitar_jazz
mediaRecorder=new MediaRecorder(MIDI.getStream().stream); //hopefully to record media
// push each chunk (blobs) in an array
mediaRecorder.ondataavailable = function(evt) {
chunks.push(evt.data);
};
// Make blob out of our blobs, and open it.
mediaRecorder.onstop = function(evt) {
var element = document.createElement('a');
var blob = new Blob(chunks, {'type':'audio/ogg; codecs=opus'});
element.setAttribute('href', URL.createObjectURL(blob));
element.setAttribute('download', "sound.ogg");
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
};
}
});
$(".custom-file-input").change(function(){
$(this).siblings(".custom-file-label").addClass("selected").html("Loaded "+$(this).val().split("\\").pop());
});
};
//function for exporting the line data
function exportNotes(){
var element = document.createElement('a');
var exporting=JSON.stringify(notes);
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(exporting));
element.setAttribute('download', "notes.txt");
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
//handle file uploads - load the drawn notes
function uploadNotes(e){
var fr=new FileReader();
fr.onload=function(){
try{
var content=JSON.parse(fr.result);
if(content.length>0){
notes=content;
}
drawAll();
}
catch(e){
console.log("Couldn't parse file.");
console.log(e);
}
}
fr.readAsText(e.files[0]);
}
function recorder(mode){
if(mode){
chunks = [];
mediaRecorder.start();
$("#beginRec").hide();
$("#endRec").show();
}
else{
mediaRecorder.stop();
$("#endRec").hide();
$("#beginRec").show();
}
}
//set color of line
function colorSet(newcolor){
$(color).removeClass("spinner-grow spinner-grow-sm");
color=newcolor;
mode=true;
$(color).addClass("spinner-grow spinner-grow-sm");
$("#eraser").removeClass("spinner-grow spinner-grow-sm");
}
//draw a single line on the canvas
function draw(color, line){
ctx.beginPath();
ctx.moveTo(line.start.x, line.start.y);
ctx.lineTo(line.end.x, line.end.y);
ctx.strokeStyle=color;
ctx.stroke();
ctx.closePath();
}
//clear canvas, draw all lines on the canvas stored in the array and the current line if we're in drawing mode
function drawAll(){
clearCanvas();
for (var i = 0; i < notes.length; i++) {
draw(notes[i].color, notes[i].line);
}
if(mode){
draw(color, line);
}
}
//clear the entire canvas
function clearCanvas(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
//change mode to removing lines
function eraser(){
mode=false;
$(color).removeClass("spinner-grow spinner-grow-sm");
$("#eraser").addClass("spinner-grow spinner-grow-sm");
}
//remove all lines
function clearAll(){
mode=true;
notes=[];
clearCanvas();
$("#eraser").removeClass("spinner-grow spinner-grow-sm");
$(color).addClass("spinner-grow spinner-grow-sm");
}
//calculate distance between points on given line (start, end)
function distance(line){
return Math.sqrt(Math.pow(line.start.x-line.end.x, 2) + Math.pow(line.start.y-line.end.y, 2));
}
//return speed of mouse used for note velocity
function mouseVelocity(){
time.current = Date.now();
x_dist = line.start.x - line.end.x;
y_dist = line.start.y - line.end.y;
interval = time.current - time.previous;
time.previous = time.current;
return Math.round(10 * Math.sqrt(Math.pow(x_dist, 2) + Math.pow(y_dist, 2)) / interval);
}
//map values between two ranges [A-B]->[C-D]
function map(x, in_min, in_max, out_min, out_max){
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
//function for deciding the action we're doing and the coordinates of the action
function findxy(action, e){
var rect = canvas.getBoundingClientRect();
//set starting point of line
if(action=='down'){
line.start.x=e.clientX-rect.left;
line.start.y=e.clientY-rect.top;
}
//mouse button was released
if(action=='up'){
//save line to array to redraw always
if(mode)
notes.push({color:color, line:line});
//console.log(JSON.stringify(notes));
//clear this line
line={start:{x:-1, y:-1}, end:{x:-1, y:-1}}
//draw (from array only)
if(mode)
drawAll();
}
//the mouse was moved to a different spot
if(action=='move'){
line.end.x=e.clientX-rect.left;
line.end.y=e.clientY-rect.top;
//draw line from start point to current point if mouse was pressed down before
if(line.start.x!=-1 && line.start.y!=-1 && mode)
drawAll();
//erase lines we cross if mouse was pressed down before
else if(line.start.x!=-1 && line.start.y!=-1 && !mode){
for(var i=0;i<notes.length;i++){
var crosses=intersects(notes[i].line, {start:previousMouse, end:line.end});
if(crosses){
notes.splice(i, 1);
drawAll();
i--;
}
}
}
//play note if we're not holding the mouse button
else{
for(var i=0;i<notes.length;i++){
//calculating if line between previous mouseX/Y and current mouseX/Y intersects the line
var crosses=intersects(notes[i].line, {start:previousMouse, end:line.end});
if(crosses){
//calculate mouse velocity
var velocity = Math.min(127, mouseVelocity());
//calculate the note based on the length of the line
var noteSize=Math.round(distance(notes[i].line));
var instrument;
switch (notes[i].color) {
case "#007bff":
instrument=0;
break;
case "#28a745":
instrument=1;
break;
case "#ffc107":
instrument=2;
break;
default:
instrument=3;
}
var note=Math.round(map(noteSize, 1, maxNoteSize, instrumentRange[instrument].end, instrumentRange[instrument].start));
MIDI.noteOn(instrument, note, velocity, 0);
MIDI.noteOff(instrument, note, 1);
console.log(note+" "+velocity);
}
}
}
//update where mouse was in this frame, to use in the next frame
previousMouse.x=line.end.x;
previousMouse.y=line.end.y;
}
}
//returns if the two lines intersect
function intersects(line1, line2){
var det, gamma, lambda;
det = (line1.end.x - line1.start.x) * (line2.end.y - line2.start.y) - (line2.end.x - line2.start.x) * (line1.end.y - line1.start.y);
if (det === 0) {
return false;
}
else {
lambda = ((line2.end.y - line2.start.y) * (line2.end.x - line1.start.x) + (line2.start.x - line2.end.x) * (line2.end.y - line1.start.y)) / det;
gamma = ((line1.start.y - line1.end.y) * (line2.end.x - line1.start.x) + (line1.end.x - line1.start.x) * (line2.end.y - line1.start.y)) / det;
return (0 < lambda && lambda < 1) && (0 < gamma && gamma < 1);
}
}