1 | ;
|
2 | /*
|
3 | * Copyright 2017 Google Inc. All Rights Reserved.
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.ImprovedStreamingClient = void 0;
|
19 | const common = require("@google-cloud/common");
|
20 | const pumpify = require("pumpify");
|
21 | const streamEvents = require("stream-events");
|
22 | const stream_1 = require("stream");
|
23 | class ImprovedStreamingClient {
|
24 | /**
|
25 | * Performs bidirectional streaming speech recognition: receive results while
|
26 | * sending audio. This method is only available via the gRPC API (not REST).
|
27 | *
|
28 | * @param {object} config The configuration for the stream. This is
|
29 | * appropriately wrapped and sent as the first argument. It should be an
|
30 | * object conforming to the [StreamingRecognitionConfig]{@link StreamingRecognitionConfig}
|
31 | * structure.
|
32 | * @param {object} [options] Optional parameters. You can override the default
|
33 | * settings for this call, e.g, timeout, retries, paginations, etc. See
|
34 | * [gax.CallOptions]{@link https://googleapis.github.io/gax-nodejs/global.html#CallOptions}
|
35 | * for the details.
|
36 | * @returns {stream} An object stream which is both readable and writable. It
|
37 | * accepts raw audio for the `write()` method, and will emit objects
|
38 | * representing [StreamingRecognizeResponse]{@link StreamingRecognizeResponse}
|
39 | * on the 'data' event asynchronously.
|
40 | *
|
41 | * @example
|
42 | * const speech = require('@google-cloud/speech');
|
43 | * const client = new speech.SpeechClient();
|
44 | *
|
45 | * const stream = client.streamingRecognize({
|
46 | * config: {
|
47 | * encoding: 'LINEAR16',
|
48 | * languageCode: 'en-us',
|
49 | * sampleRateHertz: 44100,
|
50 | * },
|
51 | * }).on('data', function(response) {
|
52 | * // doThingsWith(response);
|
53 | * });
|
54 | * const request = {};
|
55 | * // Write request objects.
|
56 | * stream.write(request);
|
57 | */
|
58 | streamingRecognize(streamingConfig, options) {
|
59 | options = options || {};
|
60 | streamingConfig = streamingConfig || {};
|
61 | // Format the audio content as input request for pipeline
|
62 | const recognizeStream = streamEvents(new pumpify.obj());
|
63 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
64 | const requestStream = this
|
65 | ._streamingRecognize(options)
|
66 | .on('error', (err) => {
|
67 | recognizeStream.destroy(err);
|
68 | })
|
69 | .on('response', (response) => {
|
70 | recognizeStream.emit('response', response);
|
71 | });
|
72 | // Attach the events to the request stream, but only do so
|
73 | // when the first write (of data) comes in.
|
74 | //
|
75 | // This also means that the sending of the initial request (with the
|
76 | // config) is delayed until we get the first burst of data.
|
77 | recognizeStream.once('writing', () => {
|
78 | // The first message should contain the streaming config.
|
79 | requestStream.write({ streamingConfig });
|
80 | // Set up appropriate piping between the stream returned by
|
81 | // the underlying API method and the one that we return.
|
82 | recognizeStream.setPipeline([
|
83 | // Format the user's input.
|
84 | // This entails that the user sends raw audio; it is wrapped in
|
85 | // the appropriate request structure.
|
86 | new stream_1.PassThrough({
|
87 | objectMode: true,
|
88 | transform: (audioContent, _, next) => {
|
89 | if (audioContent !== undefined) {
|
90 | next(undefined, { audioContent });
|
91 | return;
|
92 | }
|
93 | next();
|
94 | },
|
95 | }),
|
96 | requestStream,
|
97 | new stream_1.PassThrough({
|
98 | objectMode: true,
|
99 | transform: (response, enc, next) => {
|
100 | if (response.error) {
|
101 | next(new common.util.ApiError(response.error));
|
102 | return;
|
103 | }
|
104 | next(undefined, response);
|
105 | },
|
106 | }),
|
107 | ]);
|
108 | });
|
109 | return recognizeStream;
|
110 | }
|
111 | }
|
112 | exports.ImprovedStreamingClient = ImprovedStreamingClient;
|
113 | //# sourceMappingURL=helpers.js.map |
\ | No newline at end of file |