MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

speech/infiniteStreaming.js:53–244  ·  view source on GitHub ↗

* Note: Correct microphone settings 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 #

(
  encoding = 'LINEAR16',
  sampleRateHertz = 16000,
  languageCode = 'en-US',
  streamingLimit = 290000
)

Source from the content-addressed store, hash-verified

51 */
52
53function main(
54 encoding = 'LINEAR16',
55 sampleRateHertz = 16000,
56 languageCode = 'en-US',
57 streamingLimit = 290000
58) {
59 // [START speech_transcribe_infinite_streaming]
60
61 // const encoding = 'LINEAR16';
62 // const sampleRateHertz = 16000;
63 // const languageCode = 'en-US';
64 // const streamingLimit = 10000; // ms - set to low number for demo purposes
65
66 const chalk = require('chalk');
67 const {Writable} = require('stream');
68 const recorder = require('node-record-lpcm16');
69
70 // Imports the Google Cloud client library
71 // Currently, only v1p1beta1 contains result-end-time
72 const speech = require('@google-cloud/speech').v1p1beta1;
73
74 const client = new speech.SpeechClient();
75
76 const config = {
77 encoding: encoding,
78 sampleRateHertz: sampleRateHertz,
79 languageCode: languageCode,
80 };
81
82 const request = {
83 config,
84 interimResults: true,
85 };
86
87 let recognizeStream = null;
88 let restartCounter = 0;
89 let audioInput = [];
90 let lastAudioInput = [];
91 let resultEndTime = 0;
92 let isFinalEndTime = 0;
93 let finalRequestEndTime = 0;
94 let newStream = true;
95 let bridgingOffset = 0;
96 let lastTranscriptWasFinal = false;
97
98 function startStream() {
99 // Clear current audioInput
100 audioInput = [];
101 // Initiate (Reinitiate) a recognize stream
102 recognizeStream = client
103 .streamingRecognize(request)
104 .on('error', err => {
105 if (err.code === 11) {
106 // restartStream();
107 } else {
108 console.error('API request error ' + err);
109 }
110 })

Callers 1

Calls 1

startStreamFunction · 0.85

Tested by

no test coverage detected