grpc::ClientContext context;
google::cloud::speech::v2::StreamingRecognizeRequest config_request;
config_request.set_recognizer(recognizer_name);
auto *streaming_config = config_request.mutable_streaming_config();
auto *recognition_config = streaming_config->mutable_config();
recognition_config->mutable_explicit_decoding_config()->set_encoding(google::cloud::speech::v2::ExplicitDecodingConfig::LINEAR16);
recognition_config->mutable_explicit_decoding_config()->set_sample_rate_hertz(SAMPLE_RATE);
recognition_config->mutable_explicit_decoding_config()->set_audio_channel_count(NUM_CHANNELS);
auto *streamingFeatures = streaming_config->mutable_streaming_features();
streamingFeatures->set_interim_results(true);
auto grpc_stream = stub->StreamingRecognize(&context);
if (!grpc_stream->Write(config_request)) {
throw std::runtime_error("Failed to send config request");
}
std::atomic<bool> stop_recognition(false);
std::thread response_thread([&]() {
google::cloud::speech::v2::StreamingRecognizeResponse response;
while (grpc_stream->Read(&response)) {
for (const auto &result : response.results()) {
if (result.is_final()) {
std::string transcript;
for (const auto &alternative : result.alternatives()) {
transcript += alternative.transcript();
}
std::cout << "Recognized (final): " << transcript << std::endl;
stop_recognition.store(true);
return;
} else {
std::string interim;
for (const auto &alternative : result.alternatives()) {
interim += alternative.transcript();
}
std::cout << "Recognized (interim): " << interim << std::endl;
}
}
}
});
int16_t buffer[FRAMES_PER_BUFFER];
while (!stop_recognition.load()) {
err = Pa_ReadStream(audio_stream, buffer, FRAMES_PER_BUFFER);
if (err && err != paInputOverflowed) {
throw std::runtime_error("PortAudio error: " + std::string(Pa_GetErrorText(err)));
}
google::cloud::speech::v2::StreamingRecognizeRequest audio_request;
audio_request.set_audio(std::string(reinterpret_cast<const char *>(buffer), sizeof(buffer)));
std::cout<<"send audio: "<<sizeof(buffer)<<std::endl;
if (!grpc_stream->Write(audio_request)) {
break;
}
}
grpc_stream->WritesDone();
response_thread.join();
grpc::Status status = grpc_stream->Finish();
if (!status.ok()) {
std::cerr << "Error code: " << status.error_code() << "\n"
<< "Error details: " << status.error_details() << "\n"
<< "Error message: " << status.error_message() << std::endl;
throw std::runtime_error("gRPC call failed ffff: " + status.error_message());
}
but for all languages except english i receive something like this:
Error code: 3
Error details:
Error message: StreamingRecognize does not support the "wo-SN" language code for the "chirp_2" model at location "us-central1".
I see created recognizers in console.
I also see google.cloud.speech.v2.Speech.StreamingRecognize requests in "APi and services" detailes,but error rate is 90%.
All works only with english language.