// Creates a client. client, err := speech.NewClient(ctx) if err != nil { log.Fatalf("Failed to create client: %v", err) }
// Reads the audio file into memory. data, err := ioutil.ReadFile(filename) if err != nil { log.Fatalf("Failed to read file: %v", err) }
// Detects speech in the audio file. resp, err := client.Recognize(ctx, &speechpb.RecognizeRequest{ Config: &speechpb.RecognitionConfig{ Encoding: speechpb.RecognitionConfig_LINEAR16, SampleRateHertz: 16000, LanguageCode: "en-US", }, Audio: &speechpb.RecognitionAudio{ AudioSource: &speechpb.RecognitionAudio_Content{Content: data}, }, }) if err != nil { log.Fatalf("failed to recognize: %v", err) }
// Prints the results. for _, result := range resp.Results { for _, alt := range result.Alternatives { fmt.Printf("\"%v\" (confidence=%3f)\n", alt.Transcript, alt.Confidence) } } }