Skip to content

Commit aaf5e91

Browse files
committed
added gcs read for audio file
1 parent b3c9d97 commit aaf5e91

File tree

3 files changed

+96
-15
lines changed

3 files changed

+96
-15
lines changed

speech/grpc/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ limitations under the License.
111111

112112
<!-- // [START dependency] -->
113113
<dependencies>
114+
<dependency>
115+
<groupId>com.google.cloud</groupId>
116+
<artifactId>gcloud-java</artifactId>
117+
<version>0.2.2</version>
118+
</dependency>
114119
<dependency>
115120
<groupId>commons-cli</groupId>
116121
<artifactId>commons-cli</artifactId>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
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+
// AudioRequestFactory takes a URI as an input and creates an AudioRequest
18+
// The factory takes as input files of type local and gs
19+
20+
package com.google.cloud.speech.grpc.demos;
21+
22+
import com.google.cloud.speech.v1.AudioRequest;
23+
import java.net.URI;
24+
import java.io.IOException;
25+
26+
import com.google.cloud.storage.Blob;
27+
import com.google.cloud.storage.BlobId;
28+
import com.google.cloud.storage.Storage;
29+
import com.google.cloud.storage.StorageOptions;
30+
import com.google.protobuf.ByteString;
31+
32+
import java.nio.file.Files;
33+
import java.nio.file.Path;
34+
import java.nio.file.Paths;
35+
36+
public class AudioRequestFactory {
37+
38+
private static final String FILE = "file";
39+
private static final String GS = "gs";
40+
41+
/**
42+
* Takes an input URI of form $scheme:// and converts to audio request
43+
*
44+
* @param uri input uri
45+
* @return AudioRequest audio request
46+
*/
47+
public static AudioRequest createRequest(URI uri)
48+
throws IOException {
49+
if(uri.getScheme() == null || uri.getScheme().equals(FILE)) {
50+
Path path = Paths.get(uri);
51+
return audioFromBytes(Files.readAllBytes(path));
52+
}
53+
else if(uri.getScheme().equals(GS)) {
54+
Storage storage = StorageOptions.defaultInstance().service();
55+
String path = uri.getPath();
56+
BlobId blobId = BlobId.of(uri.getHost(), path.substring(1,path.length()));
57+
Blob blob = storage.get(blobId);
58+
return audioFromBytes(blob.content());
59+
}
60+
throw new RuntimeException("scheme not supported "+uri.getScheme());
61+
}
62+
63+
/**
64+
* Convert bytes to AudioRequest
65+
*
66+
* @param bytes input bytes
67+
* @return AudioRequest audio request
68+
*/
69+
private static AudioRequest audioFromBytes(byte[] bytes) {
70+
return AudioRequest.newBuilder()
71+
.setContent(ByteString.copyFrom(bytes))
72+
.build();
73+
}
74+
75+
public static void main(String[] args) {
76+
77+
}
78+
}

speech/grpc/src/main/java/com/google/cloud/speech/grpc/demos/NonStreamingRecognizeClient.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.apache.commons.cli.Options;
4949
import org.apache.commons.cli.ParseException;
5050

51+
import java.net.URI;
5152
import java.io.IOException;
5253
import java.nio.file.Files;
5354
import java.nio.file.Path;
@@ -72,7 +73,7 @@ public class NonStreamingRecognizeClient {
7273

7374
private final String host;
7475
private final int port;
75-
private final String file;
76+
private final URI input;
7677
private final int samplingRate;
7778

7879
private final ManagedChannel channel;
@@ -81,11 +82,11 @@ public class NonStreamingRecognizeClient {
8182
/**
8283
* Construct client connecting to Cloud Speech server at {@code host:port}.
8384
*/
84-
public NonStreamingRecognizeClient(String host, int port, String file, int samplingRate)
85+
public NonStreamingRecognizeClient(String host, int port, URI input, int samplingRate)
8586
throws IOException {
8687
this.host = host;
8788
this.port = port;
88-
this.file = file;
89+
this.input = input;
8990
this.samplingRate = samplingRate;
9091

9192
GoogleCredentials creds = GoogleCredentials.getApplicationDefault();
@@ -99,10 +100,7 @@ public NonStreamingRecognizeClient(String host, int port, String file, int sampl
99100
}
100101

101102
private AudioRequest createAudioRequest() throws IOException {
102-
Path path = Paths.get(file);
103-
return AudioRequest.newBuilder()
104-
.setContent(ByteString.copyFrom(Files.readAllBytes(path)))
105-
.build();
103+
return AudioRequestFactory.createRequest(this.input);
106104
}
107105

108106
public void shutdown() throws InterruptedException {
@@ -115,10 +113,10 @@ public void recognize() {
115113
try {
116114
audio = createAudioRequest();
117115
} catch (IOException e) {
118-
logger.log(Level.WARNING, "Failed to read audio file: " + file);
116+
logger.log(Level.WARNING, "Failed to read audio uri input: " + input);
119117
return;
120118
}
121-
logger.info("Sending " + audio.getContent().size() + " bytes from audio file: " + file);
119+
logger.info("Sending " + audio.getContent().size() + " bytes from audio uri input: " + input);
122120
InitialRecognizeRequest initial = InitialRecognizeRequest.newBuilder()
123121
.setEncoding(AudioEncoding.LINEAR16)
124122
.setSampleRate(samplingRate)
@@ -147,8 +145,8 @@ public static void main(String[] args) throws Exception {
147145
CommandLineParser parser = new DefaultParser();
148146

149147
Options options = new Options();
150-
options.addOption(OptionBuilder.withLongOpt("file")
151-
.withDescription("path to audio file")
148+
options.addOption(OptionBuilder.withLongOpt("uri")
149+
.withDescription("path to audio uri")
152150
.hasArg()
153151
.withArgName("FILE_PATH")
154152
.create());
@@ -170,10 +168,10 @@ public static void main(String[] args) throws Exception {
170168

171169
try {
172170
CommandLine line = parser.parse(options, args);
173-
if (line.hasOption("file")) {
174-
audioFile = line.getOptionValue("file");
171+
if (line.hasOption("uri")) {
172+
audioFile = line.getOptionValue("uri");
175173
} else {
176-
System.err.println("An Audio file path must be specified (e.g. /foo/baz.raw).");
174+
System.err.println("An Audio uri must be specified (e.g. file://foo/baz.raw).");
177175
System.exit(1);
178176
}
179177

@@ -203,7 +201,7 @@ public static void main(String[] args) throws Exception {
203201
}
204202

205203
NonStreamingRecognizeClient client =
206-
new NonStreamingRecognizeClient(host, port, audioFile, sampling);
204+
new NonStreamingRecognizeClient(host, port, URI.create(audioFile), sampling);
207205
try {
208206
client.recognize();
209207
} finally {

0 commit comments

Comments
 (0)