|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +function main() { |
| 18 | + // [START speech_v1_generated_Speech_StreamingRecognize_async] |
| 19 | + /** |
| 20 | + * TODO(developer): Uncomment these variables before running the sample. |
| 21 | + */ |
| 22 | + /** |
| 23 | + * Provides information to the recognizer that specifies how to process the |
| 24 | + * request. The first `StreamingRecognizeRequest` message must contain a |
| 25 | + * `streaming_config` message. |
| 26 | + */ |
| 27 | + // const streamingConfig = {} |
| 28 | + /** |
| 29 | + * The audio data to be recognized. Sequential chunks of audio data are sent |
| 30 | + * in sequential `StreamingRecognizeRequest` messages. The first |
| 31 | + * `StreamingRecognizeRequest` message must not contain `audio_content` data |
| 32 | + * and all subsequent `StreamingRecognizeRequest` messages must contain |
| 33 | + * `audio_content` data. The audio bytes must be encoded as specified in |
| 34 | + * `RecognitionConfig`. Note: as with all bytes fields, proto buffers use a |
| 35 | + * pure binary representation (not base64). See |
| 36 | + * content limits (https://cloud.google.com/speech-to-text/quotas#content). |
| 37 | + */ |
| 38 | + // const audioContent = 'Buffer.from('string')' |
| 39 | + |
| 40 | + // Imports the Speech library |
| 41 | + const {SpeechClient} = require('@google-cloud/speech').v1; |
| 42 | + |
| 43 | + // Instantiates a client |
| 44 | + const speechClient = new SpeechClient(); |
| 45 | + |
| 46 | + async function callStreamingRecognize() { |
| 47 | + // Construct request |
| 48 | + const request = {}; |
| 49 | + |
| 50 | + // Run request |
| 51 | + const stream = await speechClient.streamingRecognize(); |
| 52 | + stream.on('data', response => { |
| 53 | + console.log(response); |
| 54 | + }); |
| 55 | + stream.on('error', err => { |
| 56 | + throw err; |
| 57 | + }); |
| 58 | + stream.on('end', () => { |
| 59 | + /* API call completed */ |
| 60 | + }); |
| 61 | + stream.write(request); |
| 62 | + stream.end(); |
| 63 | + } |
| 64 | + |
| 65 | + callStreamingRecognize(); |
| 66 | + // [END speech_v1_generated_Speech_StreamingRecognize_async] |
| 67 | +} |
| 68 | + |
| 69 | +process.on('unhandledRejection', err => { |
| 70 | + console.error(err.message); |
| 71 | + process.exitCode = 1; |
| 72 | +}); |
| 73 | +main(...process.argv.slice(2)); |
0 commit comments