8000 Adds some basic javadocs and comments · zerolugithub/java-docs-samples@7933aa6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7933aa6

Browse files
committed
Adds some basic javadocs and comments
1 parent 836b4aa commit 7933aa6

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

speech/cloud-client/src/main/java/com/example/speech/Recognize.java

Lines changed: 34 additions & 9 deletions
< 8000 td data-grid-cell-id="diff-fa749b9e0f1d3da17299de1b9ceda907771cfb926332dda571052402f76f8010-50-50-1" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">50
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static void main(String... args) throws Exception {
4949
String command = args[0];
50
String path = args.length > 1 ? args[1] : "";
5151

52+
// Use command and GCS path pattern to invoke transcription.
5253
if (command.equals("syncrecognize")) {
5354
if (path.startsWith("gs://")) {
5455
syncRecognizeGcs(path);
@@ -62,17 +63,21 @@ public static void main(String... args) throws Exception {
6263
asyncRecognizeFile(path);
6364
}
6465
}
65-
66-
6766
}
6867

68+
/**
69+
* Performs speech recognition on raw PCM audio and prints the transcription.
70+
*
71+
* @param fileName the path to a PCM audio file to transcribe.
72+
*/
6973
public static void syncRecognizeFile(String fileName) throws Exception, IOException {
7074
SpeechClient speech = SpeechClient.create();
7175

7276
Path path = Paths.get(fileName);
7377
byte[] data = Files.readAllBytes(path);
7478
ByteString audioBytes = ByteString.copyFrom(data);
7579

80+
// Configure request with local raw PCM audio
7681
RecognitionConfig config = RecognitionConfig.newBuilder()
7782
.setEncoding(AudioEncoding.LINEAR16)
7883
.setSampleRate(16000)
@@ -81,6 +86,7 @@ public static void syncRecognizeFile(String fileName) throws Exception, IOExcept
8186
.setContent(audioBytes)
8287
.build();
8388

89+
// Use blocking call to get audio transcript
8490
SyncRecognizeResponse response = speech.syncRecognize(config, audio);
8591
List<SpeechRecognitionResult> results = response.getResultsList();
8692

@@ -93,11 +99,16 @@ public static void syncRecognizeFile(String fileName) throws Exception, IOExcept
9399
speech.close();
94100
}
95101

102+
/**
103+
* Performs speech recognition on remote FLAC file and prints the transcription.
104+
*
105+
* @param gcsUri the path to the remote FLAC audio file to transcribe.
106+
*/
96107
public static void syncRecognizeGcs(String gcsUri) throws Exception, IOException {
97-
// Instantiates a client
108+
// Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
98109
SpeechClient speech = SpeechClient.create();
99110

100-
// Builds the sync recognize request
111+
// Builds the request for remote FLAC file
101112
RecognitionConfig config = RecognitionConfig.newBuilder()
102113
.setEncoding(AudioEncoding.FLAC)
103114
.setSampleRate(16000)
@@ -106,7 +117,7 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception, IOException
106117
.setUri(gcsUri)
107118
.build();
108119

109-
// Performs speech recognition on the audio file
120+
// Use blocking call for getting audio transcript
110121
SyncRecognizeResponse response = speech.syncRecognize(config, audio);
111122
List<SpeechRecognitionResult> results = response.getResultsList();
112123

@@ -116,18 +127,24 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception, IOException
116127
System.out.printf("Transcription: %s%n", alternative.getTranscript());
117128
}
118129
}
119-
120-
121130
speech.close();
122131
}
123132

133+
/**
134+
* Performs non-blocking speech recognition on raw PCM audio and prints
135+
* the transcription.
136+
*
137+
* @param fileName the path to a PCM audio file to transcribe.
138+
*/
124139
public static void asyncRecognizeFile(String fileName) throws Exception, IOException {
140+
// Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
125141
SpeechClient speech = SpeechClient.create();
126142

127143
Path path = Paths.get(fileName);
128144
byte[] data = Files.readAllBytes(path);
129145
ByteString audioBytes = ByteString.copyFrom(data);
130146

147+
// Configure request with local raw PCM audio
131148
RecognitionConfig config = RecognitionConfig.newBuilder()
132149
.setEncoding(AudioEncoding.LINEAR16)
133150
.setSampleRate(16000)
@@ -136,8 +153,8 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
136153
.setContent(audioBytes)
137154
.build();
138155

156+
// Use non-blocking call for getting file transcription
139157
OperationFuture<AsyncRecognizeResponse> response = speech.asyncRecognizeAsync(config, audio);
140-
141158
while (!response.isDone()) {
142159
System.out.println("Waiting for response...");
143160
Thread.sleep(200);
@@ -154,9 +171,17 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
154171
speech.close();
155172
}
156173

174+
/**
175+
* Performs non-blocking speech recognition on remote FLAC file and prints
176+
* the transcription.
177+
*
178+
* @param gcsUri the path to the remote FLAC audio file to transcribe.
179+
*/
157180
public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOException {
181+
// Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
158182
SpeechClient speech = SpeechClient.create();
159183

184+
// Configure remote file request for FLAC file
160185
RecognitionConfig config = RecognitionConfig.newBuilder()
161186
.setEncoding(AudioEncoding.FLAC)
162187
.setSampleRate(16000)
@@ -165,8 +190,8 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
165190
.setUri(gcsUri)
166191
.build();
167192

193+
// Use non-blocking call for getting file transcription
168194
OperationFuture<AsyncRecognizeResponse> response = speech.asyncRecognizeAsync(config, audio);
169-
170195
while (!response.isDone()) {
171196
System.out.println("Waiting for response...");
172197
Thread.sleep(200);

0 commit comments

Comments
 (0)
0