@@ -49,6 +49,7 @@ public static void main(String... args) throws Exception {
49
49
String command = args [0 ];
50
<
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
String path = args .length > 1 ? args [1 ] : "" ;
51
51
52
+ // Use command and GCS path pattern to invoke transcription.
52
53
if (command .equals ("syncrecognize" )) {
53
54
if (path .startsWith ("gs://" )) {
54
55
syncRecognizeGcs (path );
@@ -62,17 +63,21 @@ public static void main(String... args) throws Exception {
62
63
asyncRecognizeFile (path );
63
64
}
64
65
}
65
-
66
-
67
66
}
68
67
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
+ */
69
73
public static void syncRecognizeFile (String fileName ) throws Exception , IOException {
70
74
SpeechClient speech = SpeechClient .create ();
71
75
72
76
Path path = Paths .get (fileName );
73
77
byte [] data = Files .readAllBytes (path );
74
78
ByteString audioBytes = ByteString .copyFrom (data );
75
79
80
+ // Configure request with local raw PCM audio
76
81
RecognitionConfig config = RecognitionConfig .newBuilder ()
77
82
.setEncoding (AudioEncoding .LINEAR16 )
78
83
.setSampleRate (16000 )
@@ -81,6 +86,7 @@ public static void syncRecognizeFile(String fileName) throws Exception, IOExcept
81
86
.setContent (audioBytes )
82
87
.build ();
83
88
89
+ // Use blocking call to get audio transcript
84
90
SyncRecognizeResponse response = speech .syncRecognize (config , audio );
85
91
List <SpeechRecognitionResult > results = response .getResultsList ();
86
92
@@ -93,11 +99,16 @@ public static void syncRecognizeFile(String fileName) throws Exception, IOExcept
93
99
speech .close ();
94
100
}
95
101
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
+ */
96
107
public static void syncRecognizeGcs (String gcsUri ) throws Exception , IOException {
97
- // Instantiates a client
108
+ // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
98
109
SpeechClient speech = SpeechClient .create ();
99
110
100
- // Builds the sync recognize request
111
+ // Builds the request for remote FLAC file
101
112
RecognitionConfig config = RecognitionConfig .newBuilder ()
102
113
.setEncoding (AudioEncoding .FLAC )
103
114
.setSampleRate (16000 )
@@ -106,7 +117,7 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception, IOException
106
117
.setUri (gcsUri )
107
118
.build ();
108
119
109
- // Performs speech recognition on the audio file
120
+ // Use blocking call for getting audio transcript
110
121
SyncRecognizeResponse response = speech .syncRecognize (config , audio );
111
122
List <SpeechRecognitionResult > results = response .getResultsList ();
112
123
@@ -116,18 +127,24 @@ public static void syncRecognizeGcs(String gcsUri) throws Exception, IOException
116
127
System .out .printf ("Transcription: %s%n" , alternative .getTranscript ());
117
128
}
118
129
}
119
-
120
-
121
130
speech .close ();
122
131
}
123
132
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
+ */
124
139
public static void asyncRecognizeFile (String fileName ) throws Exception , IOException {
140
+ // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
125
141
SpeechClient speech = SpeechClient .create ();
126
142
127
143
Path path = Paths .get (fileName );
128
144
byte [] data = Files .readAllBytes (path );
129
145
ByteString audioBytes = ByteString .copyFrom (data );
130
146
147
+ // Configure request with local raw PCM audio
131
148
RecognitionConfig config = RecognitionConfig .newBuilder ()
132
149
.setEncoding (AudioEncoding .LINEAR16 )
133
150
.setSampleRate (16000 )
@@ -136,8 +153,8 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
136
153
.setContent (audioBytes )
137
154
.build ();
138
155
156
+ // Use non-blocking call for getting file transcription
139
157
OperationFuture <AsyncRecognizeResponse > response = speech .asyncRecognizeAsync (config , audio );
140
-
141
158
while (!response .isDone ()) {
142
159
System .out .println ("Waiting for response..." );
143
160
Thread .sleep (200 );
@@ -154,9 +171,17 @@ public static void asyncRecognizeFile(String fileName) throws Exception, IOExcep
154
171
speech .close ();
155
172
}
156
173
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
+ */
157
180
public static void asyncRecognizeGcs (String gcsUri ) throws Exception , IOException {
181
+ // Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
158
182
SpeechClient speech = SpeechClient .create ();
159
183
184
+ // Configure remote file request for FLAC file
160
185
RecognitionConfig config = RecognitionConfig .newBuilder ()
161
186
.setEncoding (AudioEncoding .FLAC )
162
187
.setSampleRate (16000 )
@@ -165,8 +190,8 @@ public static void asyncRecognizeGcs(String gcsUri) throws Exception, IOExceptio
165
190
.setUri (gcsUri )
166
191
.build ();
167
192
193
+ // Use non-blocking call for getting file transcription
168
194
OperationFuture <AsyncRecognizeResponse > response = speech .asyncRecognizeAsync (config , audio );
169
-
170
195
while (!response .isDone ()) {
171
196
System .out .println ("Waiting for response..." );
172
197
Thread .sleep (200 );
0 commit comments