如何将 Google Cloud Text 中的 grpc.max_receive_message_length 配置更改为 NodeJS 上的语音?

How can I change the grpc.max_receive_message_length configuration in Google Cloud Text to speech on NodeJS?

我正在使用包 @google-cloud/text-to-speech 将文本转换为语音,大致使用以下代码:

import GoogleTextToSpeech, { SynthesizeSpeechRequest } from '@google-cloud/text-to-speech';
const fs = require('fs');
const path = require('path');

// ...

const request: SynthesizeSpeechRequest = {
    input: { ssml }, // ssml is a valid SSML string from 0 to about 2000 chars.
    voice: {
        languageCode: 'en-US',
        name: 'en-US-Wavenet-A',
    },
    audioConfig: {
        audioEncoding: 'LINEAR16',
        pitch: 0,
        speakingRate: 1.0,
    },
};
const clientConfig = JSON.parse(
    fs.readFileSync(
        path.join(
            require.resolve('@google-cloud/text-to-speech'),
            '..',
            'v1',
            'text_to_speech_client_config.json',
        ),
    ),
);
clientConfig.interfaces[
    'google.cloud.texttospeech.v1.TextToSpeech'
].methods.SynthesizeSpeech.timeout_millis = 3000000;

const client = new GoogleTextToSpeech.TextToSpeechClient(clientConfig);

// Performs the Text-to-Speech request
const [response] = await client.synthesizeSpeech(request);
// response gets processed further

这对我们有用,而且已经为我们工作了很长时间。然而最近在测试中,我们注意到当文本接近 2000 个字符长度时,我们有时会出现错误,错误似乎是因为来自 google 的音频响应太大。错误如下所示:

Error: 8 RESOURCE_EXHAUSTED: Received message larger than max (5162312 vs. 4194304)

据我们所知,错误似乎是在幕后,grpc 配置为不接收超过 4MB 限制的响应。根据 this github issue, , this github issue, , and this github thread,似乎应该可以在我们的代码中将 clientConfig 中的此项配置为 -1(无限制)。问题是,所有这些帖子要么是针对其他语言的,要么不是特定于云文本到语音的。

我今天的问题是,如何在@google-cloud/text-to-speech中专门配置这个值?这似乎是可能的,但似乎我将不得不跳过几个环节(比如可能构建一个 Channel,将其传递给 GRPC,然后将该 GRPC 实例传递给 TextToSpeechClient),并且尽我所能告诉,这些 GRPC 配置属性中的大多数都没有在 the docs 中列出。例如,interfaces['google.cloud.texttospeech.v1.TextToSpeech'].methods.SynthesizeSpeech.timeout_milllis 属性 是我们过去成功更新的一个值,但它似乎没有包含在那些文档中,因为它可以传递给 TextToSpeechClient 构造函数。

感谢任何帮助,以及指向记录此类配置的有用资源的链接(如果存在)!

谢谢!

Max 建议联系 Google 支持或在 Google 云论坛中搜索

要使用 NodeJS 更改 grpc.max_receive_message_length 的值,您只需在 TextToSpeechClient() 中传递 {'grpc.max_receive_message_length': 1} 和您想要的值。对于这个例子,我定义了 1 只是为了更容易显示它生效了。它看起来像这样:

const client = new GoogleTextToSpeech.TextToSpeechClient({'grpc.max_receive_message_length': 1});

测试完成: