• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *       http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * EDITING INSTRUCTIONS
19  * This file is referenced in READMEs. Any change to this file should be reflected in
20  * the project's READMEs.
21  */
22 
23 package com.google.cloud.examples.speech.snippets;
24 
25 import com.google.cloud.speech.v1.RecognitionAudio;
26 import com.google.cloud.speech.v1.RecognitionConfig;
27 import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding;
28 import com.google.cloud.speech.v1.RecognizeResponse;
29 import com.google.cloud.speech.v1.SpeechClient;
30 import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
31 import com.google.cloud.speech.v1.SpeechRecognitionResult;
32 import com.google.protobuf.ByteString;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.util.List;
37 
38 /**
39  * A snippet for Google Natural Language API showing how to convert houman speech from an audio file
40  * into a text form.
41  */
42 public class RecognizeSpeech {
main(String... args)43   public static void main(String... args) throws Exception {
44     // Instantiates a client
45     SpeechClient speech = SpeechClient.create();
46 
47     // The path to the audio file to transcribe
48     String fileName = "your/speech/audio/file.raw"; // for example "./resources/audio.raw";
49 
50     // Reads the audio file into memory
51     Path path = Paths.get(fileName);
52     byte[] data = Files.readAllBytes(path);
53     ByteString audioBytes = ByteString.copyFrom(data);
54 
55     // Builds the sync recognize request
56     RecognitionConfig config =
57         RecognitionConfig.newBuilder()
58             .setEncoding(AudioEncoding.LINEAR16)
59             .setSampleRateHertz(16000)
60             .setLanguageCode("en-US")
61             .build();
62     RecognitionAudio audio = RecognitionAudio.newBuilder().setContent(audioBytes).build();
63 
64     // Performs speech recognition on the audio file
65     RecognizeResponse response = speech.recognize(config, audio);
66     List<SpeechRecognitionResult> results = response.getResultsList();
67 
68     for (SpeechRecognitionResult result : results) {
69       List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList();
70       for (SpeechRecognitionAlternative alternative : alternatives) {
71         System.out.printf("Transcription: %s%n", alternative.getTranscript());
72       }
73     }
74     speech.close();
75   }
76 }
77