forked from QXIP/RTPEngine-Speech2Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeech2hep.js
More file actions
91 lines (85 loc) · 2.81 KB
/
speech2hep.js
File metadata and controls
91 lines (85 loc) · 2.81 KB
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
const chokidar = require('chokidar');
const fs = require('fs');
const speechService = require('ms-bing-speech-service');
var config = require('./config.js');
if (config.hep_config) {
console.log('Enabling HEP...');
var hep_client = require('./hep.js');
hep_client.init(config.hep_config);
}
const options = config.bing_options;
const socket = new speechService(options);
const watcher = chokidar.watch('/recording', {ignored: /^\./, persistent: true });
watcher
.on('error', function(error) {console.error('Error happened', error);})
.on('add', function(path) {console.log('File', path, 'has been added'); })
// .on('change', function(path) {console.log('File', path, 'has been changed'); })
.on('unlink', function(path) {console.log('File', path, 'has been removed');
if(path.endsWith('.meta')){
var newpath = path.replace(/\.meta/i, '-mix.wav');
try { var xcid = path.match(/\/([^\/]+)\/?\.meta$/)[1].split('-')[0]; } catch(e) { console.log(e); }
// Get file timestamp, detection is delayed
var stats = fs.statSync(newpath);
var datenow = stats.mtime ? new Date(stats.mtime).getTime() : new Date().getTime();
var t_sec = Math.floor( datenow / 1000);
var u_sec = ( datenow - (t_sec*1000))*1000;
console.log('Meta Hit! Seeking Audio at: ',newpath);
socket.start((error, service) => {
console.log('service started');
service.on('recognition', (e) => {
if (e.RecognitionStatus === 'Success') {
console.log('Response',e);
if (hep_client){
console.log('Sending HEP...');
try {
var payload = { Speech: e.DisplayText };
payload.timestamp = new Date();
payload.CallID = xcid;
if ((e.DisplayText.length - e.DisplayText.replace(RegExp('*'), '').length) > 1){
payload.profanity = true;
}
var message = {
rcinfo: {
type: 'HEP',
version: 3,
payload_type: 100,
time_sec: t_sec,
time_usec: u_sec,
ip_family: 2,
protocol: 17,
proto_type: 100,
srcIp: '127.0.0.1',
dstIp: '127.0.0.1',
srcPort: 0,
dstPort: 0,
captureId: 2999,
capturePass: 'SPEECH-TO-HEP',
correlation_id: xcid
},
payload: JSON.stringify(payload)
};
hep_client.preHep(message);
} catch(e) { console.log(e); }
}
}
});
service.sendFile(newpath, function(e){
setTimeout(function(){ fs.unlink(newpath); }, 1000);
});
});
}
});
var exit = false;
process.on('SIGINT', function() {
console.log();
if (exit) {
console.log("Exiting...");
process.exit();
} else {
console.log("Press CTRL-C within 2 seconds to Exit...");
exit = true;
setTimeout(function () {
exit = false;
}, 2000);
}
});