Recognize a synchronization request

Loads the audio file from the disk into the request.

Code sample

C++

To learn how to install and use the client library for Speech-to-Text, see Speech-to-Text client libraries .

To authenticate to Speech-to-Text, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  // Create a Speech client with the default configuration 
 auto 
  
 client 
  
 = 
  
 speech 
 :: 
 SpeechClient 
 ( 
 speech 
 :: 
 MakeSpeechConnection 
 ()); 
 // Parse command line arguments. 
 auto 
  
 const 
  
 args 
  
 = 
  
 ParseArguments 
 ( 
 argc 
 , 
  
 argv 
 ); 
 speech 
 :: 
 v1 
 :: 
 RecognizeRequest 
  
 request 
 ; 
 * 
 request 
 . 
 mutable_config 
 () 
  
 = 
  
 args 
 . 
 config 
 ; 
  
 // Load the audio file from disk into the request. 
  
 auto 
  
 content 
  
 = 
  
 std 
 :: 
 string 
 { 
 std 
 :: 
 istreambuf_iterator<char> 
 ( 
  
 std 
 :: 
 ifstream 
 ( 
 args 
 . 
 path 
 , 
  
 std 
 :: 
 ios 
 :: 
 binary 
 ). 
 rdbuf 
 ()), 
  
 {}}; 
  
 request 
 . 
 mutable_audio 
 () 
 - 
> mutable_content 
 () 
 - 
> assign 
 ( 
 std 
 :: 
 move 
 ( 
 content 
 )); 
 // Send audio content using Recognize(). 
 auto 
  
 response 
  
 = 
  
 client 
 . 
 Recognize 
 ( 
 request 
 ); 
 if 
  
 ( 
 ! 
 response 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 response 
 ). 
 status 
 (); 
 // Dump the transcript of all the results. 
 for 
  
 ( 
 auto 
  
 const 
&  
 result 
  
 : 
  
 response 
 - 
> results 
 ()) 
  
 { 
  
 for 
  
 ( 
 auto 
  
 const 
&  
 alternative 
  
 : 
  
 result 
 . 
 alternatives 
 ()) 
  
 { 
  
 std 
 :: 
 cout 
 << 
 alternative 
 . 
 confidence 
 () 
 << 
 " 
 \t 
 " 
 << 
 alternative 
 . 
 transcript 
 () 
 << 
 " 
 \n 
 " 
 ; 
  
 } 
 } 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Design a Mobile Site
View Site in Mobile | Classic
Share by: