-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
79 lines (70 loc) · 2.26 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
const Discord = require("discord.js");
const client = new Discord.Client();
//const config = require("./config.json");
const commands = new (require("./Commands/handler"))(`${__dirname}/Commands/Commands`)
const fs = require("fs");
let songs = require("./song_list.json")
fs.readFile("song_list.json", (err, data) => {
if(err) throw err;
//var obj;
//obj = JSON.parse(data);
});
function getSongs(lf, hf){
return songs.filter(song => song["lowest frequency"] >= lf && song["highest frequency"] <= hf)
}
const WavDecoder = require("wav-decoder");
const Pitchfinder = require("pitchfinder");
const upload = require("express-fileupload");
const bodyParser = require('body-parser');
const http = require('http');
var express = require("express");
var app = express();
app.use(express.static('public'));
app.use(upload());
app.use(bodyParser.urlencoded({extended: false}));
console.log("Creating a server");
var server = app.listen(process.env.PORT, function(){
var port = server.address().port;
console.log("Server started at http://localhost:%s", port);
});
app.post("/mmm", function(req, res){
if(req.files){
console.log("We got the files!!");
let freqArray = Object.values(req.files);
console.log(freqArray)
let pitch = getKTP(freqArray);
console.log("LowKey:" + pitch[0]);
console.log("highKey:" + pitch[1]);
console.log(JSON.stringify(getSongs(pitch[0], pitch[1])));
let resData = `{"LowKey":${pitch[0]},"HighKey":${pitch[1]},"songs":${JSON.stringify(getSongs(pitch[0], pitch[1]))}}`
res.send(resData);
}
})
function getKTP(freqArray){
let lowPitch = 20000;
let highPitch = null;
let pitchVal = null;
let total = 0;
let countCorrect = 0;
for(i = 0; i < freqArray.length; i++){
pitchVal = getPitch(freqArray[i].data);
if(pitchVal != null){
console.log(" Pitch: " + pitchVal);
if(pitchVal > highPitch){
highPitch = pitchVal;
}
if(pitchVal < lowPitch){
lowPitch = pitchVal;
}
}
}
return [lowPitch, highPitch];
}
function getPitch(bufferData){
const detectPitch = new Pitchfinder.DynamicWavelet();
const buffer = bufferData; //Conversion from pcm to wav required
const decoded = WavDecoder.decode.sync(buffer);
const float32Array = decoded.channelData[0];
console.log("Detection Process");
return detectPitch(float32Array);
}