forked from ninachaubal/ConversationSentiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
137 lines (123 loc) · 3.19 KB
/
server.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
//create a server
var express = require('express');
var app = express();
// other required modules
var fs = require('fs');
var exec = require('child_process').exec;
var request = require('request');
// SWN
var swn = require('./swn/swn.js');
swn = new swn.swn('./swn/swn.txt');
/*
use multipart request bodies
*/
app.use(express.multipart());
/*
parse request bodies.
*/
app.use(function(req, res, next) {
var data = '';
req.setEncoding('binary');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
req.body.audio = data;
next();
});
});
/*
mount static content at /
*/
app.use(express.static(__dirname + '/static'));
/*
Set headers for cross site requests
*/
app.get('/*',function(req,res,next){
res.header("Access-Control-Allow-Origin", "*");
next();
});
app.post('/*',function(req,res,next){
res.header("Access-Control-Allow-Origin", "*");
next();
});
//============================================================================//
var requests = {}
/*
GET /token
returns a unique token
*/
app.get('/token', function(req, res){
var token = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i=0; i < 6; i++) {
token += possible.charAt(Math.floor(Math.random() * possible.length));
}
requests[token] = 'reserved'
res.json({token: token});
});
/*
POST /audio
param:
token: a unique identifier for this recording
*/
app.post('/audio', function(req, res){
var token = req.param('token');
if (requests[token] == 'reserved') {
// write .wav file to tmp
var filepath = __dirname + '/tmp/' + token + '.wav';
var fd = fs.openSync(filepath, 'a');
var buff = new Buffer(req.body.audio, 'binary');
fs.writeSync(fd, buff, 0, buff.length, 0);
fs.closeSync(fd);
// convert to flac
var child = exec('flac -0 -f ' + __dirname + '/tmp/' + token + '.wav',
function(err){
if (err != null) {
console.log(err);
} else {
var flac = fs.readFileSync(__dirname + '/tmp/' + token + '.flac');
request({
url: 'https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US',
method: 'POST',
headers: {
'Content-Type': 'audio/x-flac; rate=22050'
},
body: flac
}, function (err, res, body) {
body = JSON.parse(body);
if (body.hypotheses.length > 0) {
console.log('recognized: ' + body.hypothesis[0].utterance);
requests[token] = swn.getSentiment(body.hypothesis[0].utterance);
} else {
requests[token] = 'error';
}
});
}
}
);
}
});
/*
GET /text
param:
token: unique identifier passed to /audio
*/
app.get('/text', function(req, res){
var token = req.param('token');
if (requests[token] !== undefined) {
if (requests[token] == 'error') {
res.json({'error': 'no speech detected'});
} else {
// TODO
delete requests[token];
}
} else {
res.json({'error': 'invalid token'})
}
});
if (process.env.PORT !== undefined) {
app.listen(process.env.PORT);
} else {
app.listen(8080);
}