| 59 | } |
| 60 | |
| 61 | int main(int argc, char** argv) try { |
| 62 | // Create a Speech client with the default configuration |
| 63 | auto client = speech::SpeechClient(speech::MakeSpeechConnection()); |
| 64 | |
| 65 | // Parse command line arguments. |
| 66 | auto args = ParseArguments(argc, argv); |
| 67 | auto const file_path = args.path; |
| 68 | |
| 69 | speech::v1::StreamingRecognizeRequest request; |
| 70 | auto& streaming_config = *request.mutable_streaming_config(); |
| 71 | *streaming_config.mutable_config() = args.config; |
| 72 | |
| 73 | // Begin a stream. |
| 74 | auto stream = client.AsyncStreamingRecognize(); |
| 75 | // The stream can fail to start, and `.get()` returns an error in this case. |
| 76 | if (!stream->Start().get()) throw stream->Finish().get(); |
| 77 | // Write the first request, containing the config only. |
| 78 | if (!stream->Write(request, grpc::WriteOptions{}).get()) { |
| 79 | // Write().get() returns false if the stream is closed. |
| 80 | throw stream->Finish().get(); |
| 81 | } |
| 82 | |
| 83 | // Simulate a microphone thread using the file as input. |
| 84 | auto microphone = |
| 85 | std::thread(MicrophoneThreadMain, std::ref(*stream), file_path); |
| 86 | // Read responses. |
| 87 | auto read = [&stream] { return stream->Read().get(); }; |
| 88 | for (auto response = read(); response.has_value(); response = read()) { |
| 89 | // Dump the transcript of all the results. |
| 90 | for (auto const& result : response->results()) { |
| 91 | std::cout << "Result stability: " << result.stability() << "\n"; |
| 92 | for (auto const& alternative : result.alternatives()) { |
| 93 | std::cout << alternative.confidence() << "\t" |
| 94 | << alternative.transcript() << "\n"; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | auto status = stream->Finish().get(); |
| 99 | microphone.join(); |
| 100 | if (!status.ok()) throw status; |
| 101 | return 0; |
| 102 | } catch (google::cloud::Status const& s) { |
| 103 | std::cerr << "Recognize stream finished with an error: " << s << "\n"; |
| 104 | return 1; |
| 105 | } catch (std::exception const& ex) { |
| 106 | std::cerr << "Standard C++ exception thrown: " << ex.what() << "\n" |
| 107 | << kUsage << "\n"; |
| 108 | return 1; |
| 109 | } |
| 110 | // [END speech_streaming_recognize] |
nothing calls this directly
no test coverage detected