-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyncTest.js
55 lines (48 loc) · 1.31 KB
/
asyncTest.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
// Imports the Google Cloud client library
const fs = require('fs');
const path = require('path');
const speech = require('@google-cloud/speech');
// Creates a client
const client = new speech.SpeechClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
const filename = 'flac/normal/goodname.flac';
const encoding = 'FLAC';
const sampleRateHertz = 32000;
const languageCode = 'ja-JP';
const bucket = 'nhk-news'
const config = {
encoding: encoding,
sampleRateHertz: sampleRateHertz,
languageCode: languageCode,
};
const audio = {
uri: 'gs://' + path.posix.join(bucket, filename)
};
const request = {
config: config,
audio: audio,
};
// Detects speech in the audio file
client
.longRunningRecognize(request)
.then(data => {
const operation = data[0];
// Get a Promise representation of the final result of the job
return operation.promise();
})
.then(data => {
const response = data[0];
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
fs.writeFile('log', JSON.stringify(response), function (err) {
if (err) throw err;
console.log('Saved!');
});
console.log(`Transcription: ${transcription}`);
})
.catch(err => {
console.error('ERROR:', err);
});