Reconnaissance vocale en continu
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Lecture et écriture du contenu audio d'un fichier.
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","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)."]]