-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathraspberry-dialogflow.js
91 lines (68 loc) · 2.55 KB
/
raspberry-dialogflow.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
'use strict';
const projectId = 'multilocalesample-a62ad';
const sessionId = 'sessionId';
const dialogflow = require('dialogflow');
const sessionClient = new dialogflow.SessionsClient();
const session = sessionClient.sessionPath(projectId, sessionId);
async function detectText(text, languageCode = 'es-ES') {
const request = {
session,
queryInput: {
text: {text, languageCode},
},
};
return await sessionClient.detectIntent(request);
}
async function detectFile(filename) {
const util = require('util');
const fs = require('fs');
const readFile = util.promisify(fs.readFile);
let inputAudio = await readFile(filename);
const request = {
session, inputAudio,
queryInput: {
audioConfig:
{audioEncoding: 'AUDIO_ENCODING_LINEAR_16', sampleRateHertz: 16000, languageCode: 'es-ES'}
}
};
return await sessionClient.detectIntent(request);
}
function detectAudio(exitOnSilence = 30) {
return new Promise(async (resolve, reject) => {
const mic = require('mic');
const fs = require('fs');
const filename = 'test.wav';
const config = {
rate: 16000,
channels: 1,
debug: true,
fileType: 'wav',
exitOnSilence
};
const micInstance = mic(config);
const stream = micInstance.getAudioStream();
const outputFileStream = fs.WriteStream(filename);
stream.pipe(outputFileStream);
micInstance.start();
stream.on('silence', async () => {
console.log('Received silence... let\'s process it');
micInstance.stop();
const responses = await detectFile(filename);
resolve(responses);
});
stream.on('data', data => console.log('Recieved Input Stream: ' + data.length));
stream.on('error', err => console.log('Error in Input Stream: ' + err));
stream.on('startComplete', () => console.log('Received startComplete'));
stream.on('stopComplete', () => console.log('Received stopComplete'));
stream.on('pauseComplete', () => console.log('Received pauseComplete'));
stream.on('resumeComplete', () => console.log('Received resumeComplete'));
stream.on('processExitComplete', () => {
console.log('Got SIGNAL processExitComplete');
micInstance.stop();
reject();
});
});
}
module.exports.detectText = detectText;
module.exports.detectFile = detectFile;
module.exports.detectAudio = detectAudio;