Last active
May 23, 2022 19:41
-
-
Save coryan/166b3c889299a7e3de7fcbcc5bd4e57e to your computer and use it in GitHub Desktop.
Code snippet showing C++ coroutines and Google Speech-to-Text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Full code at:
https://github.com/GoogleCloudPlatform/cpp-samples/blob/main/speech/api/streaming_transcribe_coroutines.cc