Skip to content

Commit 16cd359

Browse files
nnegreychingor13
authored andcommitted
samples: Speech samples (#1036)
* speech punctuation, video samples * speech samples WIP * speech samples * wip update * Updated branch to new changes in library. For speech v1p1beta1 * Remove unused commands from README * Update streamingRecognizeFile with try-catch based on PR feedback * Update based on style guidelines
1 parent 8431d8a commit 16cd359

File tree

5 files changed

+405
-297
lines changed

5 files changed

+405
-297
lines changed
1.7 MB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 Google Inc.
2+
* Copyright 2018 Google Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,54 +18,57 @@
1818

1919
// [START speech_quickstart]
2020
// Imports the Google Cloud client library
21-
22-
import com.google.cloud.speech.v1.RecognitionAudio;
23-
import com.google.cloud.speech.v1.RecognitionConfig;
24-
import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding;
25-
import com.google.cloud.speech.v1.RecognizeResponse;
26-
import com.google.cloud.speech.v1.SpeechClient;
27-
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
28-
import com.google.cloud.speech.v1.SpeechRecognitionResult;
21+
import com.google.cloud.speech.v1p1beta1.RecognitionAudio;
22+
import com.google.cloud.speech.v1p1beta1.RecognitionConfig;
23+
import com.google.cloud.speech.v1p1beta1.RecognitionConfig.AudioEncoding;
24+
import com.google.cloud.speech.v1p1beta1.RecognizeResponse;
25+
import com.google.cloud.speech.v1p1beta1.SpeechClient;
26+
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionAlternative;
27+
import com.google.cloud.speech.v1p1beta1.SpeechRecognitionResult;
2928
import com.google.protobuf.ByteString;
3029
import java.nio.file.Files;
3130
import java.nio.file.Path;
3231
import java.nio.file.Paths;
3332
import java.util.List;
3433

3534
public class QuickstartSample {
35+
36+
/**
37+
* Demonstrates using the Speech API to transcribe an audio file.
38+
*/
3639
public static void main(String... args) throws Exception {
3740
// Instantiates a client
38-
SpeechClient speech = SpeechClient.create();
41+
try (SpeechClient speechClient = SpeechClient.create()) {
3942

40-
// The path to the audio file to transcribe
41-
String fileName = "./resources/audio.raw";
43+
// The path to the audio file to transcribe
44+
String fileName = "./resources/audio.raw";
4245

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);
46+
// Reads the audio file into memory
47+
Path path = Paths.get(fileName);
48+
byte[] data = Files.readAllBytes(path);
49+
ByteString audioBytes = ByteString.copyFrom(data);
4750

48-
// Builds the sync recognize request
49-
RecognitionConfig config = RecognitionConfig.newBuilder()
50-
.setEncoding(AudioEncoding.LINEAR16)
51-
.setSampleRateHertz(16000)
52-
.setLanguageCode("en-US")
53-
.build();
54-
RecognitionAudio audio = RecognitionAudio.newBuilder()
55-
.setContent(audioBytes)
56-
.build();
51+
// Builds the sync recognize request
52+
RecognitionConfig config = RecognitionConfig.newBuilder()
53+
.setEncoding(AudioEncoding.LINEAR16)
54+
.setSampleRateHertz(16000)
55+
.setLanguageCode("en-US")
56+
.build();
57+
RecognitionAudio audio = RecognitionAudio.newBuilder()
58+
.setContent(audioBytes)
59+
.build();
5760

58-
// Performs speech recognition on the audio file
59-
RecognizeResponse response = speech.recognize(config, audio);
60-
List<SpeechRecognitionResult> results = response.getResultsList();
61+
// Performs speech recognition on the audio file
62+
RecognizeResponse response = speechClient.recognize(config, audio);
63+
List<SpeechRecognitionResult> results = response.getResultsList();
6164

62-
for (SpeechRecognitionResult result: results) {
63-
// There can be several alternative transcripts for a given chunk of speech. Just use the
64-
// first (most likely) one here.
65-
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
66-
System.out.printf("Transcription: %s%n", alternative.getTranscript());
65+
for (SpeechRecognitionResult result : results) {
66+
// There can be several alternative transcripts for a given chunk of speech. Just use the
67+
// first (most likely) one here.
68+
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
69+
System.out.printf("Transcription: %s%n", alternative.getTranscript());
70+
}
6771
}
68-
speech.close();
6972
}
7073
}
7174
// [END speech_quickstart]

0 commit comments

Comments
 (0)