forked from soundhound/hound-sdk-web-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-client-voice.js
110 lines (88 loc) · 2.68 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
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
//"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));
// Convert WAV to Speex and stream it to Houndify backend
function compressAndStreamWAV() {
var voiceRequest;
var reader = new wav.Reader();
reader.on("format", function (format) {
voiceRequest = new Houndify.VoiceRequest({
clientId: config.clientId,
clientKey: config.clientKey,
sampleRate: format.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);
},
});
});
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();
});
file.pipe(reader);
}
// Stream full WAV/Opus/Speex audio file to Houndify backend
function streamAudioFile(file) {
var voiceRequest = new Houndify.VoiceRequest({
clientId: config.clientId,
clientKey: config.clientKey,
convertAudioToSpeex: false,
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);
},
});
file.on("data", function (chunk) {
voiceRequest.write(chunk);
});
file.on("end", function () {
voiceRequest.end();
});
}
var audioFile =
argv.audio || path.join("test_audio", "whatistheweatherlikeintoronto.wav");
var audioFilePath = path.join(__dirname, audioFile);
var file = fs.createReadStream(audioFilePath);
if (audioFile.endsWith(".wav") || audioFile.endsWith(".wave")) {
compressAndStreamWAV(file);
} else {
streamAudioFile(file);
}