Search code examples
node.jstypescriptgrpcgoogle-text-to-speechgrpc-node

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


I am using the package @google-cloud/text-to-speech in order to convert text to speech, using roughly this code:

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

This works for us, and has worked for us for a long time. Recently in testing however, we noticed that we sometimes get an error when the text is close to the 2000 character length, the error seems to be because the audio response from google is too large. The error looks like this:

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

As far as we can tell, the error seems to be that under the hood, grpc is configured to not receive a response above that 4MB limit. According to this github issue, this SO post, this github issue, this SO post, and this github thread, it seems like it should be possible to configure this in clientConfig in our code to be -1 (unlimited) instead. The problem is, all of those posts are for either some other language, or aren't specific to cloud text-to-speech.

My question today is, how can I configure this value specifically in @google-cloud/text-to-speech? It seems like it's possible, but also seems like I'm going to have to jump through several hoops (such as perhaps constructing a Channel, passing that to GRPC, then passing that GRPC instance to the TextToSpeechClient), and as far as I can tell, most of these GRPC configuration properties are not listed in the docs. For example, the interfaces['google.cloud.texttospeech.v1.TextToSpeech'].methods.SynthesizeSpeech.timeout_milllis property is a value that we have successfully updated in the past, but it does not appear to be included in those docs for what is passable to the TextToSpeechClient constructor.

Any help is appreciated, as well as links to helpful resources on where this kind of configuration is documented, if they exist!

Thanks!


Solution

  • To change the value of grpc.max_receive_message_length using NodeJS, you just pass {'grpc.max_receive_message_length': 1} in TextToSpeechClient() with the values that you desire. For this example I defined 1 just so it will be easier to show that it took effect. It will look like this:

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

    Testing done:

    enter image description here