MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/cpp-samples / WriteAudio

Function WriteAudio

speech/api/streaming_transcribe_coroutines.cc:53–75  ·  view source on GitHub ↗

Simulate a microphone thread sending audio to the Cloud Speech API.

Source from the content-addressed store, hash-verified

51
52// Simulate a microphone thread sending audio to the Cloud Speech API.
53g::future<void> WriteAudio(RecognizeStream& stream,
54 speech::v1::StreamingRecognizeRequest request,
55 std::string const& path, g::CompletionQueue cq) {
56 // Open the input file and read from it until there is no more data.
57 auto file = std::ifstream(path, std::ios::binary);
58 while (file) {
59 // Simulate a delay to acquire the audio.
60 co_await cq.MakeRelativeTimer(std::chrono::seconds(1));
61 auto constexpr kChunkSize = 64 * 1024;
62 std::vector<char> chunk(kChunkSize);
63 file.read(chunk.data(), chunk.size());
64 auto const bytes_read = file.gcount();
65 // If there is any data left on the file, send it to the service
66 if (bytes_read > 0) {
67 request.clear_streaming_config();
68 request.set_audio_content(chunk.data(), bytes_read);
69 std::cout << "Sending " << bytes_read / 1024 << "k bytes." << std::endl;
70 // Terminate the loop if there is an error in the stream.
71 if (!co_await stream.Write(request, grpc::WriteOptions())) co_return;
72 }
73 }
74 co_await stream.WritesDone();
75}
76
77g::future<g::Status> StreamingTranscribe(g::CompletionQueue cq,
78 ParseResult args) {

Callers 1

StreamingTranscribeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected