Skip to content

Instantly share code, notes, and snippets.

@coryan
Last active May 23, 2022 19:41
Show Gist options
  • Save coryan/166b3c889299a7e3de7fcbcc5bd4e57e to your computer and use it in GitHub Desktop.
Save coryan/166b3c889299a7e3de7fcbcc5bd4e57e to your computer and use it in GitHub Desktop.
Code snippet showing C++ coroutines and Google Speech-to-Text
using RecognizeStream = google::cloud::AsyncStreamingReadWriteRpc<
google::cloud::speech::v1::StreamingRecognizeRequest,
google::cloud::speech::v1::StreamingRecognizeResponse>;
// Print the responses as they are received.
google::cloud::future<void> ReadTranscript(RecognizeStream& stream) {
while (true) {
auto response = co_await stream.Read();
// Terminate the loop if the stream is closed or has an error.
if (!response) co_return;
for (auto const& result : response->results()) {
std::cout << "Result stability: " << result.stability() << "\n";
for (auto const& alternative : result.alternatives()) {
std::cout << alternative.confidence() << "\t"
<< alternative.transcript() << "\n";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment