Skip to content

Commit

Permalink
feat(samples): demonstrate real-time conversation translation (#48)
Browse files Browse the repository at this point in the history
This PR improves the Media Translation microphone sample so that it is more aware of "state." When the API returns the SpeechEventType.END_OF_SINGLE_UTTERANCE event, the stream is closed and the user can initiate another call to the API.

The following region tags are affected:
media_translation_translate_from_mic
  • Loading branch information
telpirion authored Jun 16, 2020
1 parent 5163fde commit 41ee3ad
Showing 1 changed file with 44 additions and 23 deletions.
67 changes: 44 additions & 23 deletions media-translation/translate_from_mic.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ function main(encoding, sampleRateHertz, sourceLanguage, targetLanguage) {

// [START media_translation_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');

Expand All @@ -58,13 +76,15 @@ function main(encoding, sampleRateHertz, sourceLanguage, targetLanguage) {
//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.
Expand All @@ -73,6 +93,8 @@ function main(encoding, sampleRateHertz, sourceLanguage, targetLanguage) {
audioContent: null,
};

let currentTranslation = '';
let currentRecognition = '';
// Create a recognize stream
const stream = client
.streamingTranslateSpeech()
Expand All @@ -84,31 +106,30 @@ function main(encoding, sampleRateHertz, sourceLanguage, targetLanguage) {
}
})
.on('data', response => {
const {result} = response;
if (result.textTranslationResult.isFinal) {
console.log(
`\nFinal translation: ${result.textTranslationResult.translation}`
);
console.log(`Final recognition result: ${result.recognitionResult}`);
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 {
console.log(
`\nPartial translation: ${result.textTranslationResult.translation}`
);
console.log(
`Partial recognition result: ${result.recognitionResult}`
);
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
recorder
.record({
sampleRateHertz: sampleRateHertz,
threshold: 0, //silence threshold
recordProgram: 'rec',
silence: '5.0', //seconds of silence before ending
})
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) {
Expand All @@ -120,13 +141,13 @@ function main(encoding, sampleRateHertz, sourceLanguage, targetLanguage) {
audioContent: chunk.toString('base64'),
};
stream.write(request);
})
.on('close', () => {
doTranslationLoop();
});

console.log('Listening, press Ctrl+C to stop.');
}

translateFromMicrophone();

doTranslationLoop();
// [END media_translation_translate_from_mic]
}
main(...process.argv.slice(2));

0 comments on commit 41ee3ad

Please sign in to comment.