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

Function MicrophoneThreadMain

speech/api/streaming_transcribe.cc:35–59  ·  view source on GitHub ↗

Write the audio in 64k chunks at a time, simulating audio content arriving from a microphone.

Source from the content-addressed store, hash-verified

33// Write the audio in 64k chunks at a time, simulating audio content arriving
34// from a microphone.
35void MicrophoneThreadMain(RecognizeStream& stream,
36 std::string const& file_path) {
37 speech::v1::StreamingRecognizeRequest request;
38 std::ifstream file_stream(file_path, std::ios::binary);
39 auto constexpr kChunkSize = 64 * 1024;
40 std::vector<char> chunk(kChunkSize);
41 while (true) {
42 // Read another chunk from the file.
43 file_stream.read(chunk.data(), chunk.size());
44 auto const bytes_read = file_stream.gcount();
45 // And write the chunk to the stream.
46 if (bytes_read > 0) {
47 request.set_audio_content(chunk.data(), bytes_read);
48 std::cout << "Sending " << bytes_read / 1024 << "k bytes." << std::endl;
49 if (!stream.Write(request, grpc::WriteOptions()).get()) break;
50 }
51 if (!file_stream) {
52 // Done reading everything from the file, so done writing to the stream.
53 stream.WritesDone().get();
54 break;
55 }
56 // Wait a second before writing the next chunk.
57 std::this_thread::sleep_for(std::chrono::seconds(1));
58 }
59}
60
61int main(int argc, char** argv) try {
62 // Create a Speech client with the default configuration

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected