Recognize a synchronization request

Loads the audio file from the disk into the request.

Code sample

C++

// Create a Speech Stub connected to the speech service.
auto creds = grpc::GoogleDefaultCredentials();
auto channel = grpc::CreateChannel("speech.googleapis.com", creds);
std::unique_ptr<Speech::Stub> speech(Speech::NewStub(channel));
// Parse command line arguments.
RecognizeRequest request;
char* file_path = ParseArguments(argc, argv, request.mutable_config());
if (nullptr == file_path) {
  std::cerr << kUsage;
  return -1;
}
  // Load the audio file from disk into the request.
  request.mutable_audio()->mutable_content()->assign(
      std::istreambuf_iterator<char>(std::ifstream(file_path).rdbuf()),
      std::istreambuf_iterator<char>());
// Send audio content using Recognize().
grpc::ClientContext context;
RecognizeResponse response;
grpc::Status rpc_status = speech->Recognize(&context, request, &response);
if (!rpc_status.ok()) {
  // Report the RPC failure.
  std::cerr << rpc_status.error_message() << std::endl;
  return -1;
}
// Dump the transcript of all the results.
for (int r = 0; r < response.results_size(); ++r) {
  const auto& result = response.results(r);
  for (int a = 0; a < result.alternatives_size(); ++a) {
    const auto& alternative = result.alternatives(a);
    std::cout << alternative.confidence() << "\t" << alternative.transcript()
              << std::endl;
  }
}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.