-
Notifications
You must be signed in to change notification settings - Fork 2
/
node-client-voice.js
69 lines (52 loc) · 1.67 KB
/
node-client-voice.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
//"houndify" module contains both client-side ("HoundifyClient") and server-side ("HoundifyExpress") parts of SDK
var Houndify = require('houndify');
var wav = require('wav');
var fs = require('fs');
var path = require('path');
//parse arguments
var argv = require('minimist')(process.argv.slice(2));
//config file
var configFile = argv.config || 'config.json';
var config = require(path.join(__dirname, configFile));
function startVoiceRequest(sampleRate) {
return new Houndify.VoiceRequest({
clientId: config.clientId,
clientKey: config.clientKey,
sampleRate: sampleRate,
enableVAD: true,
//REQUEST INFO JSON
//see https://houndify.com/reference/RequestInfo
requestInfo: {
UserID: "test_user",
Latitude: 37.388309,
Longitude: -121.973968
},
onResponse: function(response, info) {
console.log(response);
},
onTranscriptionUpdate: function(trObj) {
console.log("Partial Transcript:", trObj.PartialTranscript);
},
onError: function(err, info) {
console.log(err);
}
});
}
//Read wave file and stream it to Houndify backend
var voiceRequest;
var reader = new wav.Reader();
reader.on('format', function (format) {
voiceRequest = startVoiceRequest(format.sampleRate);
});
reader.on('data', function (chunk) {
var arrayBuffer = new Uint8Array(chunk).buffer;
var view = new Int16Array(arrayBuffer);
voiceRequest.write(view);
});
reader.on('end', function() {
voiceRequest.end();
});
var audioFile = argv.audio || path.join('test_audio', 'whatistheweatherthere.wav');
var audioFilePath = path.join(__dirname, audioFile);
var file = fs.createReadStream(audioFilePath);
file.pipe(reader);