-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathtranslate_from_mic.js
155 lines (139 loc) · 5 KB
/
translate_from_mic.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2020, Google LLC.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
/**
* This application demonstrates how to perform basic translation operations
* with the Google Cloud Media Translation API.
*
* Note: Correct microphone settings is required: check enclosed link, and make
* sure the following conditions are met:
* 1. SoX must be installed and available in your $PATH- it can be found here:
* http://sox.sourceforge.net/
* 2. Microphone must be working
* 3. Encoding, sampleRateHertz, and # of channels must match header of audio file you're
* recording to.
* 4. Get Node-Record-lpcm16 https://www.npmjs.com/package/node-record-lpcm16
* More Info: https://cloud.google.com/speech-to-text/docs/streaming-recognize
*/
/**
* Translates audio streaming from a microphone
* @param {string} encoding the audio encoding codec
* @param {string} sampleRateHertz the sampling rate of the audio stream
* @param {string} sourceLanguageCode the language to translate from
* @param {string} targetLanguageCode the language to translate to
*/
function main(encoding, sampleRateHertz, sourceLanguage, targetLanguage) {
sampleRateHertz = Number(sampleRateHertz);
// [START mediatranslation_translate_from_mic]
// Allow user input from terminal
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
function doTranslationLoop() {
rl.question("Press any key to translate or 'q' to quit: ", answer => {
if (answer.toLowerCase() === 'q') {
rl.close();
} else {
translateFromMicrophone();
}
});
}
// Node-Record-lpcm16
const recorder = require('node-record-lpcm16');
// Imports the Cloud Media Translation client library
const {
SpeechTranslationServiceClient,
} = require('@google-cloud/media-translation');
// Creates a client
const client = new SpeechTranslationServiceClient();
function translateFromMicrophone() {
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
//const encoding = 'linear16';
//const sampleRateHertz = 16000;
//const sourceLanguage = 'Language to translate from, as BCP-47 locale';
//const targetLanguage = 'Language to translate to, as BCP-47 locale';
console.log('Begin speaking ...');
const config = {
audioConfig: {
audioEncoding: encoding,
sourceLanguageCode: sourceLanguage,
targetLanguageCode: targetLanguage,
},
singleUtterance: true,
};
// First request needs to have only a streaming config, no data.
const initialRequest = {
streamingConfig: config,
audioContent: null,
};
let currentTranslation = '';
let currentRecognition = '';
// Create a recognize stream
const stream = client
.streamingTranslateSpeech()
.on('error', e => {
if (e.code && e.code === 4) {
console.log('Streaming translation reached its deadline.');
} else {
console.log(e);
}
})
.on('data', response => {
const {result, speechEventType} = response;
if (speechEventType === 'END_OF_SINGLE_UTTERANCE') {
console.log(`\nFinal translation: ${currentTranslation}`);
console.log(`Final recognition result: ${currentRecognition}`);
stream.destroy();
recording.stop();
} else {
currentTranslation = result.textTranslationResult.translation;
currentRecognition = result.recognitionResult;
console.log(`\nPartial translation: ${currentTranslation}`);
console.log(`Partial recognition result: ${currentRecognition}`);
}
});
let isFirst = true;
// Start recording and send microphone input to the Media Translation API
const recording = recorder.record({
sampleRateHertz: sampleRateHertz,
threshold: 0, //silence threshold
recordProgram: 'rec',
silence: '5.0', //seconds of silence before ending
});
recording
.stream()
.on('data', chunk => {
if (isFirst) {
stream.write(initialRequest);
isFirst = false;
}
const request = {
streamingConfig: config,
audioContent: chunk.toString('base64'),
};
if (!stream.destroyed) {
stream.write(request);
}
})
.on('close', () => {
doTranslationLoop();
});
}
doTranslationLoop();
// [END mediatranslation_translate_from_mic]
}
main(...process.argv.slice(2));