Skip to content

Commit 5a06849

Browse files
jmdobrychingor13
authored andcommitted
samples: Add Speech API quickstart sample. (#497)
1 parent 6380fa3 commit 5a06849

3 files changed

Lines changed: 129 additions & 0 deletions

File tree

56.6 KB
Binary file not shown.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
29+
30+
/**
31+
* Tests for quickstart sample.
32+
*/
33+
@RunWith(JUnit4.class)
34+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
35+
public class QuickstartSampleIT {
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
@Before
40+
public void setUp() {
41+
bout = new ByteArrayOutputStream();
42+
out = new PrintStream(bout);
43+
System.setOut(out);
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
System.setOut(null);
49+
}
50+
51+
@Test
52+
public void testQuickstart() throws Exception {
53+
// Act
54+
QuickstartSample.main();
55+
56+
// Assert
57+
String got = bout.toString();
58+
assertThat(got).contains("how old is the Brooklyn Bridge");
59+
}
60+
}

0 commit comments

Comments
 (0)