-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
276 lines (247 loc) · 8 KB
/
index.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
const Jimp = require("jimp");
const scribble = require("scribbletune");
const express = require("express");
const multer = require("multer");
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "uploads");
},
filename: function (req, file, cb) {
cb(null, "" + Date.now());
}
});
var upload = multer({ storage: storage });
const fs = require("fs");
let pattern = "",
notes = [],
resizeFactor = 12,
color = [[], [], []],
phraseChord = [],
phraseNature = [],
phrasePace = [],
tempRedSum = 0,
tempGreenSum = 0,
tempBlueSum = 0,
redSum = 0,
greenSum = 0,
blueSum = 0,
colorSum = 0,
imageBrightness = 0,
redPercent = 0,
greenPercent = 0,
bluePercent = 0,
scaleSelector = 0,
chosenScale,
chosenPitch,
loopCount = 0,
songRender = "",
uploadedImage;
var app = express();
app.set("views", "./views");
app.set("view engine", "pug");
app.use(express.static(__dirname + "/public"));
app.get("/", function (req, res) {
console.log(Date.now());
res.render("index");
});
app.post("/", upload.single("image"), function (req, res) {
var cleanUp = function () {
phraseChord = [];
phraseNature = [];
phrasePace = [];
pattern = "";
chords = "";
resizeFactor = 12;
color = [[], [], []];
tempRedSum = 0;
tempGreenSum = 0;
tempBlueSum = 0;
redSum = 0;
greenSum = 0;
blueSum = 0;
colorSum = 0;
imageBrightness = 0;
redPercent = 0;
greenPercent = 0;
bluePercent = 0;
scaleSelector = 0;
loopCount = 0;
songRender = "";
notes = [];
};
var calc7 = function (toCalculate) {
if (toCalculate <= -0.715) {
return Calculated = "A";
} else if (toCalculate <= -0.43) {
return Calculated = "B";
} else if (toCalculate <= -0.145) {
return Calculated = "C";
} else if (toCalculate <= 0.14) {
return Calculated = "D";
} else if (toCalculate <= 0.425) {
return Calculated = "E";
} else if (toCalculate <= 0.71) {
return Calculated = "F";
} else {
return Calculated = "G";
}
};
var convertRed = function (toCalculate) {
if (toCalculate <= 0.142) {
return Calculated = "I";
} else if (toCalculate <= 0.284) {
return Calculated = "II";
} else if (toCalculate <= 0.426) {
return Calculated = "III";
} else if (toCalculate <= 0.568) {
return Calculated = "IV";
} else if (toCalculate <= 0.710) {
return Calculated = "V";
} else if (toCalculate <= 0.852) {
return Calculated = "VI";
} else {
return Calculated = "VII";
}
};
var convertGreen = function (toCalculate) {
if (toCalculate <= 0.5) {
return Calculated = 0;
} else {
return Calculated = 1;
}
};
var convertBlue = function (toCalculate) {
if (toCalculate <= 0.35) {
return Calculated = 0;
} else if (toCalculate <= 0.7) {
return Calculated = 1;
} else {
return Calculated = 2;
}
};
uploadedImage = req.file.path;
Jimp.read(uploadedImage).then(image => {
cleanUp();
console.log("clean");
image
.resize(resizeFactor, resizeFactor)
.quality(80)
.write(uploadedImage + "t", (err, image) => {
Jimp.read(uploadedImage + "t")
.then(image => {
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function (
x,
y,
idx
) {
var red = this.bitmap.data[idx + 0];
redSum += red;
var green = this.bitmap.data[idx + 1];
greenSum += green;
var blue = this.bitmap.data[idx + 2];
blueSum += blue;
color[0].push(red);
color[1].push(green);
color[2].push(blue);
if (x === image.bitmap.width - 1) {
for (i = 0; i < image.bitmap.width; i++) {
tempRedSum = tempRedSum + color[0][image.bitmap.width * loopCount + i];
tempGreenSum = tempGreenSum + color[1][image.bitmap.width * loopCount + i];
tempBlueSum = tempBlueSum + color[2][image.bitmap.width * loopCount + i];
}
tempRedSum = tempRedSum / image.bitmap.width;
tempRedSum = tempRedSum / 256;
tempGreenSum = tempGreenSum / image.bitmap.width;
tempGreenSum = tempGreenSum / 256;
tempBlueSum = tempBlueSum / image.bitmap.width;
tempBlueSum = tempBlueSum / 256;
phraseChord.push(convertRed(tempRedSum));
phraseNature.push(convertGreen(tempGreenSum));
phrasePace.push(convertBlue(tempBlueSum));
tempRedSum = 0;
tempGreenSum = 0;
tempBlueSum = 0;
loopCount++;
}
});
for (var i = 0; i < phraseChord.length; i++) {
if (phraseNature[i] === 1) {
phraseChord[i] = phraseChord[i].toLowerCase();
}
};
colorSum = redSum + blueSum + greenSum;
imageBrightness = colorSum / (resizeFactor * resizeFactor * 256 * 3);
redPercent = redSum / (resizeFactor * resizeFactor * 256);
greenPercent = greenSum / (resizeFactor * resizeFactor * 256);
bluePercent = blueSum / (resizeFactor * resizeFactor * 256);
scaleSelector = (bluePercent - redPercent) / 2;
scaleSelector = scaleSelector / (Math.abs(scaleSelector) + greenPercent);
chosenScale = calc7(scaleSelector);
if (imageBrightness < 0.3) {
chosenPitch = 2;
} else if (imageBrightness < 0.5) {
chosenPitch = 3;
} else if (imageBrightness < 0.7) {
chosenPitch = 4;
} else if (imageBrightness < 0.8) {
chosenPitch = 5;
} else {
chosenPitch = 6;
}
console.log(chosenScale + chosenPitch);
let chords = scribble.progression.getChords(
chosenScale + chosenPitch + " major",
phraseChord.join(" ")
);
chords.split(" ").forEach((c, i) => {
const chord = scribble.chord(c);
if (phrasePace[i] === 0) {
notes.push(chord[0]);
notes.push(chord[1]);
pattern = pattern + "xx";
} else if (phrasePace[i] === 1) {
notes.push(chord[0]);
notes.push(chord[1]);
notes.push(chord[2]);
pattern = pattern + "x[xx]";
} else {
notes.push(chord[0]);
notes.push(chord[1]);
notes.push(chord[2]);
notes.push(chord[0]);
pattern = pattern + "[xx][xx]";
}
});
console.log(pattern + " " + phraseChord.join(" "));
let clip1 = scribble.clip({
notes,
pattern: pattern
});
scribble.midi(clip1, uploadedImage + ".mid", function () {
songRender =
"data:audio/midi;base64," +
Buffer(fs.readFileSync(uploadedImage + ".mid")).toString("base64");
res.render("play", { mario: songRender });
// fs.unlink(uploadedImage, (err) => {
// if (err) throw err;
// console.log('1 was deleted');
// });
fs.unlink(uploadedImage + "t", (err) => {
if (err) throw err;
console.log('2 was deleted');
});
fs.unlink(uploadedImage + ".mid", (err) => {
if (err) throw err;
console.log('clip was deleted');
});
});
})
.catch(err => {
console.error(err);
});
});
});
});
var listener = app.listen(process.env.PORT || 3000, function () {
console.log('Your app is listening on port ' + listener.address().port);
});