|
| 1 | +/* |
| 2 | + Copyright 2017, Google Inc. |
| 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 | +package com.example.speech; |
| 18 | + |
| 19 | +// [START speech_quickstart] |
| 20 | +// Imports the Google Cloud client library |
| 21 | +import com.google.cloud.speech.spi.v1beta1.SpeechClient; |
| 22 | +import com.google.cloud.speech.v1beta1.RecognitionAudio; |
| 23 | +import com.google.cloud.speech.v1beta1.RecognitionConfig; |
| 24 | +import com.google.cloud.speech.v1beta1.RecognitionConfig.AudioEncoding; |
| 25 | +import com.google.cloud.speech.v1beta1.SpeechRecognitionAlternative; |
| 26 | +import com.google.cloud.speech.v1beta1.SpeechRecognitionResult; |
| 27 | +import com.google.cloud.speech.v1beta1.SyncRecognizeResponse; |
| 28 | +import com.google.protobuf.ByteString; |
| 29 | + |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
| 32 | +import java.nio.file.Paths; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +public class QuickstartSample { |
| 36 | + public static void main(String... args) throws Exception { |
| 37 | + // Instantiates a client |
| 38 | + SpeechClient speech = SpeechClient.create(); |
| 39 | + |
| 40 | + // The path to the audio file to transcribe |
| 41 | + String fileName = "./resources/audio.raw"; |
| 42 | + |
| 43 | + // Reads the audio file into memory |
| 44 | + Path path = Paths.get(fileName); |
| 45 | + byte[] data = Files.readAllBytes(path); |
| 46 | + ByteString audioBytes = ByteString.copyFrom(data); |
| 47 | + |
| 48 | + // Builds the sync recognize request |
| 49 | + RecognitionConfig config = RecognitionConfig.newBuilder() |
| 50 | + .setEncoding(AudioEncoding.LINEAR16) |
| 51 | + .setSampleRate(16000) |
| 52 | + .build(); |
| 53 | + RecognitionAudio audio = RecognitionAudio.newBuilder() |
| 54 | + .setContent(audioBytes) |
| 55 | + .build(); |
| 56 | + |
| 57 | + // Performs speech recognition on the audio file |
| 58 | + SyncRecognizeResponse response = speech.syncRecognize(config, audio); |
| 59 | + List<SpeechRecognitionResult> results = response.getResultsList(); |
| 60 | + |
| 61 | + for (SpeechRecognitionResult result: results) { |
| 62 | + List<SpeechRecognitionAlternative> alternatives = result.getAlternativesList(); |
| 63 | + for (SpeechRecognitionAlternative alternative: alternatives) { |
| 64 | + System.out.printf("Transcription: %s%n", alternative.getTranscript()); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +// [END speech_quickstart] |
0 commit comments