Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to use google-cloud-cpp to implement texttospeech?

After I add ServiceAccountCredentials according to the demo code, it is still an error. The error message is as follows:

{code_=kUnavailable (14) message_="Error in non-idempotent operation SynthesizeSpeech: empty address list: " ...}

What should I do please?
Is my ServiceAccountCredentials code wrong or do I need to add some parameters?

vs2019

windows 10

// cpp code

auto constexpr kText = R"""(
Four score and seven years ago our fathers brought forth on this
continent, a new nation, conceived in Liberty, and dedicated to
the proposition that all men are created equal.)""";

try {
auto pCr = google:☁️:MakeServiceAccountCredentials("---my ServiceAccountCredentials json data---");
::google:☁️:Options opts;
opts.set<::google:☁️:UnifiedCredentialsOption>(pCr);
namespace texttospeech = ::google:☁️:texttospeech;

auto client = texttospeech::TextToSpeechClient(
texttospeech::MakeTextToSpeechConnection(opts));

google:☁️:texttospeech::v1::SynthesisInput input;
input.set_text(kText);
google:☁️:texttospeech::v1::VoiceSelectionParams voice;
voice.set_language_code("en-US");
google:☁️:texttospeech::v1::AudioConfig audio;
audio.set_audio_encoding(google:☁️:texttospeech::v1::MP3);

auto response = client.SynthesizeSpeech(input, voice, audio);
if (!response) throw std::runtime_error(response.status().message());
// Normally one would play the results (response->audio_content()) over some
// audio device. For this quickstart, we just print some information.
auto constexpr kWavHeaderSize = 48;
auto constexpr kBytesPerSample = 2; // we asked for LINEAR16
auto const sample_count =
(response->audio_content().size() - kWavHeaderSize) / kBytesPerSample;
std::cout << "The audio has " << sample_count << " samples\n";

return 0;
}
catch (std::exception const& ex) {
std::cerr << "Standard exception raised: " << ex.what() << "\n";
return 1;
}

0 0 173
0 REPLIES 0
Top Labels in this Space