function translateFromMicrophone()

in mediatranslation/translate_from_mic.js [71:150]


  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();
      });
  }