串流語音辨識
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
從檔案讀取及寫入音訊。
程式碼範例
C++
如要瞭解如何安裝及使用 Speech-to-Text 的用戶端程式庫,請參閱這篇文章。
如要向語音轉文字服務進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["# Streaming speech recognition\n\nReads and writes audio from a file.\n\nCode sample\n-----------\n\n### C++\n\n\nTo learn how to install and use the client library for Speech-to-Text, see\n[Speech-to-Text client libraries](/speech-to-text/docs/client-libraries).\n\n\nTo authenticate to Speech-to-Text, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n namespace speech = ::google::cloud::speech;\n using RecognizeStream = ::google::cloud::AsyncStreamingReadWriteRpc\u003c\n speech::v1::StreamingRecognizeRequest,\n speech::v1::StreamingRecognizeResponse\u003e;\n\n auto constexpr kUsage = R\"\"\"(Usage:\n streaming_transcribe [--bitrate N] audio.(raw|ulaw|flac|amr|awb)\n )\"\"\";\n\n // Write the audio in 64k chunks at a time, simulating audio content arriving\n // from a microphone.\n void MicrophoneThreadMain(RecognizeStream& stream,\n std::string const& file_path) {\n speech::v1::StreamingRecognizeRequest request;\n std::ifstream file_stream(file_path, std::ios::binary);\n auto constexpr kChunkSize = 64 * 1024;\n std::vector\u003cchar\u003e chunk(kChunkSize);\n while (true) {\n // Read another chunk from the file.\n file_stream.read(chunk.data(), chunk.size());\n auto const bytes_read = file_stream.gcount();\n // And write the chunk to the stream.\n if (bytes_read \u003e 0) {\n request.set_audio_content(chunk.data(), bytes_read);\n std::cout \u003c\u003c \"Sending \" \u003c\u003c bytes_read / 1024 \u003c\u003c \"k bytes.\" \u003c\u003c std::endl;\n if (!stream.Write(request, grpc::WriteOptions()).get()) break;\n }\n if (!file_stream) {\n // Done reading everything from the file, so done writing to the stream.\n stream.WritesDone().get();\n break;\n }\n // Wait a second before writing the next chunk.\n std::this_thread::sleep_for(std::chrono::seconds(1));\n }\n }\n\n int main(int argc, char** argv) try {\n // Create a Speech client with the default configuration\n auto client = speech::SpeechClient(speech::MakeSpeechConnection());\n\n // Parse command line arguments.\n auto args = ParseArguments(argc, argv);\n auto const file_path = args.path;\n\n speech::v1::StreamingRecognizeRequest request;\n auto& streaming_config = *request.mutable_streaming_config();\n *streaming_config.mutable_config() = args.config;\n\n // Begin a stream.\n auto stream = client.AsyncStreamingRecognize();\n // The stream can fail to start, and `.get()` returns an error in this case.\n if (!stream-\u003eStart().get()) throw stream-\u003eFinish().get();\n // Write the first request, containing the config only.\n if (!stream-\u003eWrite(request, grpc::WriteOptions{}).get()) {\n // Write().get() returns false if the stream is closed.\n throw stream-\u003eFinish().get();\n }\n\n // Simulate a microphone thread using the file as input.\n auto microphone =\n std::thread(MicrophoneThreadMain, std::ref(*stream), file_path);\n // Read responses.\n auto read = [&stream] { return stream-\u003eRead().get(); };\n for (auto response = read(); response.has_value(); response = read()) {\n // Dump the transcript of all the results.\n for (auto const& result : response-\u003eresults()) {\n std::cout \u003c\u003c \"Result stability: \" \u003c\u003c result.stability() \u003c\u003c \"\\n\";\n for (auto const& alternative : result.alternatives()) {\n std::cout \u003c\u003c alternative.confidence() \u003c\u003c \"\\t\"\n \u003c\u003c alternative.transcript() \u003c\u003c \"\\n\";\n }\n }\n }\n auto status = stream-\u003eFinish().get();\n microphone.join();\n if (!status.ok()) throw status;\n return 0;\n } catch (google::cloud::Status const& s) {\n std::cerr \u003c\u003c \"Recognize stream finished with an error: \" \u003c\u003c s \u003c\u003c \"\\n\";\n return 1;\n } catch (std::exception const& ex) {\n std::cerr \u003c\u003c \"Standard C++ exception thrown: \" \u003c\u003c ex.what() \u003c\u003c \"\\n\"\n \u003c\u003c kUsage \u003c\u003c \"\\n\";\n return 1;\n }\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=speech)."]]