-
Notifications
You must be signed in to change notification settings - Fork 667
/
Copy pathspeech_to_text.v1.js
50 lines (41 loc) · 1.41 KB
/
speech_to_text.v1.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
'use strict';
const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
const fs = require('fs');
const speechToText = new SpeechToTextV1({
// See: https://github.com/watson-developer-cloud/node-sdk#authentication
});
/*
This code will print the entire response to the console when it
receives the 'data' event. Some applications will want to write
out only the transcribed text, to the console or to a file.
To do this, remove `objectMode: true` from the `params` object.
Then, uncomment the block of code at Line 30.
*/
const params = {
contentType: 'audio/wav',
objectMode: true,
};
// create the stream
const recognizeStream = speechToText.recognizeUsingWebSocket(params);
// pipe in some audio
fs.createReadStream(__dirname + '/resources/speech.wav').pipe(recognizeStream);
/*
// these two lines of code will only work if `objectMode` is `false`
// pipe out the transcription to a file
recognizeStream.pipe(fs.createWriteStream('transcription.txt'));
// get strings instead of Buffers from `data` events
recognizeStream.setEncoding('utf8');
*/
recognizeStream.on('data', function (event) {
onEvent('Data:', event);
});
recognizeStream.on('error', function (event) {
onEvent('Error:', event);
});
recognizeStream.on('close', function (event) {
onEvent('Close:', event);
});
// Displays events on the console.
function onEvent(name, event) {
console.log(name, JSON.stringify(event, null, 2));
}