forked from dgan181/kylo-ren-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (93 loc) · 3.71 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
const express = require('express');
const app = express();
const fs = require('fs');
const {spawn} = require('child_process');
const port = 3000;
const { Midi } = require('@tonejs/midi')
const __model_dirname = "./Backend/forDeploy"
// Function to convert an Uint8Array to a string
var uint8arrayToString = function(data){
return String.fromCharCode.apply(null, data);
};
app.listen(port, (err) =>{
if(!err)
console.log('server started running on:' + port);
else
console.log('unable to start server');
})
app.use(express.static('public'));
app.use(express.json());
//---------------------------------------------------------------------------------------------------------------
// Post Request:
//
// Receives file parameters and runs python script to excute model
//---------------------------------------------------------------------------------------------------------------
app.post('/api', (request,response) => {
var filestuff;
console.log("I got the generate request!")
// Deal with the request
const options = {
cwd: __model_dirname
}
const param = request.body
const python = spawn('python',['./gen_full.py', param.temp, param.timsig_n, param.timsig_d , param.numOfBars , param.valence, param.density, param.model ], options);
// Testing
python.stdout.on('data', (data) => {
console.log('from file...');
filestuff = data.toString();
console.log(filestuff);
console.log(data.toString());
});
// Handle error output
python.stderr.on('data', (data) => {
// As said before, convert the Uint8Array to a readable string.
console.log(uint8arrayToString(data));
});
// Handle error output
python.stderr.on('data', (data) => {
// As said before, convert the Uint8Array to a readable string.
console.log(uint8arrayToString(data));
});
python.on('close', (code) => {
console.log(`child process close all stdio with code ${code}`);
// send data to browser
if(code == 0){
response.send("File generated!");}
else{response.send("Error in generating file");}
});
});
//---------------------------------------------------------------------------------------------------------------
// Get Request:
//
// Converts midi data file to Tonejs readable-JSON and sends it to the client
//---------------------------------------------------------------------------------------------------------------
app.get('/api', (request,response) => {
console.log("I got the play request!")
// const midiData = fs.readFileSync("./Backend/seq2seq_test/generations/13_[4, 4]_fybso.mid")
const midiData = fs.readFileSync(__model_dirname+"/midi/music.mid")
const midi = new Midi(midiData)
response.json(midi)
});
//---------------------------------------------------------------------------------------------------------------
// Get Request:
//
// Sends musicXML to client for sheet music
//---------------------------------------------------------------------------------------------------------------
app.get('/sheet', (request,response) => {
console.log("Sending sheet music")
response.set('Content-Type', 'text/xml');
const xmlData = fs.readFileSync(__model_dirname+"/musicxml/sheet.xml")
// const xmlData = fs.readFileSync("")
response.send(xmlData)
});
//---------------------------------------------------------------------------------------------------------------
// Get Request:
//
// Sends musicXML to client for sheet music
//---------------------------------------------------------------------------------------------------------------
app.get('/sheet', (request,response) => {
console.log("Sending sheet music")
response.set('Content-Type', 'text/xml');
const xmlData = fs.readFileSync(__model_dirname+"/musicxml/sheet.xml")
response.send(xmlData)
});