Skip to content

Commit f4f1fc4

Browse files
munkhuushmglaverikitsch
authored andcommitted
media-translation-sample: translate from file (#15)
* media-translation: translate from the file * formatted code * added translate from the mic * added test for translate from file * made requested changes * added README, added Translate with main * deleted Translate and updated README * added single utterance * duplicate the resource folder * added main and removed catch * trigger tests * fixed the lint * media-translation: translate from the file * formatted code * trigger kokoro tests * removed weird file
1 parent caca61a commit f4f1fc4

File tree

6 files changed

+340
-0
lines changed

6 files changed

+340
-0
lines changed

mediatranslation/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[//]: # "This README.md file is auto-generated, all changes to this file will be lost."
2+
[//]: # "To regenerate it, use `python -m synthtool`."
3+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
4+
5+
# [Cloud Media Translation: Java Samples](https://github.com/googleapis/java-mediatranslation)
6+
7+
[![Open in Cloud Shell][shell_img]][shell_link]
8+
9+
10+
11+
## Table of Contents
12+
13+
* [Build the sample](#build-the-sample)
14+
* [Samples](#samples)
15+
16+
17+
## Build the sample
18+
19+
Install [Maven](http://maven.apache.org/).
20+
21+
Build your project with:
22+
23+
```
24+
mvn clean package -DskipTests
25+
```
26+
27+
## Samples
28+
29+
Please follow the [Set Up Your Project](https://cloud.google.com/media-translation/docs/getting-started#set_up_your_project)
30+
steps in the Quickstart doc to create a project and enable the Google Cloud
31+
Media Translation API. Following those steps, make sure that you
32+
[Set Up a Service Account](https://cloud.google.com/media-translation/docs/common/auth#set_up_a_service_account),
33+
and export the following environment variable:
34+
35+
```
36+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
37+
```
38+
39+
After you have authorized, you can translate media.
40+
41+
42+
## Run
43+
Run all tests:
44+
```
45+
mvn clean verify
46+
```
47+
48+
[shell_img]: https://gstatic.com/cloudssh/images/open-btn.png
49+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-mediatranslation&page=editor&open_in_editor=samples/snippets/README.md
50+
[product-docs]: https://cloud.google.com/mediatranslation/docs/

mediatranslation/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@
4343
<scope>test</scope>
4444
</dependency>
4545
</dependencies>
46+
4647
</project>
56.6 KB
Binary file not shown.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2020 Google LLC
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.mediatranslation;
18+
19+
// [START media_translation_translate_from_file]
20+
21+
import com.google.api.gax.rpc.BidiStream;
22+
import com.google.cloud.mediatranslation.v1beta1.SpeechTranslationServiceClient;
23+
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechConfig;
24+
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechRequest;
25+
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechResponse;
26+
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechResult;
27+
import com.google.cloud.mediatranslation.v1beta1.TranslateSpeechConfig;
28+
import com.google.protobuf.ByteString;
29+
import java.io.IOException;
30+
import java.nio.file.Files;
31+
import java.nio.file.Path;
32+
import java.nio.file.Paths;
33+
34+
public class TranslateFromFile {
35+
36+
public static void translateFromFile() throws IOException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String filePath = "path/to/audio.raw";
39+
translateFromFile(filePath);
40+
}
41+
42+
public static void translateFromFile(String filePath) throws IOException {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests. After completing all of your requests, call
45+
// the "close" method on the client to safely clean up any remaining background resources.
46+
try (SpeechTranslationServiceClient client = SpeechTranslationServiceClient.create()) {
47+
Path path = Paths.get(filePath);
48+
byte[] content = Files.readAllBytes(path);
49+
ByteString audioContent = ByteString.copyFrom(content);
50+
51+
TranslateSpeechConfig audioConfig =
52+
TranslateSpeechConfig.newBuilder()
53+
.setAudioEncoding("linear16")
54+
.setSampleRateHertz(16000)
55+
.setSourceLanguageCode("en-US")
56+
.setTargetLanguageCode("fr-FR")
57+
.build();
58+
59+
StreamingTranslateSpeechConfig config =
60+
StreamingTranslateSpeechConfig.newBuilder()
61+
.setAudioConfig(audioConfig)
62+
.setSingleUtterance(true)
63+
.build();
64+
65+
BidiStream<StreamingTranslateSpeechRequest, StreamingTranslateSpeechResponse> bidiStream =
66+
client.streamingTranslateSpeechCallable().call();
67+
68+
// The first request contains the configuration.
69+
StreamingTranslateSpeechRequest requestConfig =
70+
StreamingTranslateSpeechRequest.newBuilder().setStreamingConfig(config).build();
71+
72+
// The second request contains the audio
73+
StreamingTranslateSpeechRequest request =
74+
StreamingTranslateSpeechRequest.newBuilder().setAudioContent(audioContent).build();
75+
76+
bidiStream.send(requestConfig);
77+
bidiStream.send(request);
78+
79+
for (StreamingTranslateSpeechResponse response : bidiStream) {
80+
// Once the transcription settles, the response contains the
81+
// is_final result. The other results will be for subsequent portions of
82+
// the audio.
83+
StreamingTranslateSpeechResult res = response.getResult();
84+
String translation = res.getTextTranslationResult().getTranslation();
85+
String source = res.getRecognitionResult();
86+
87+
if (res.getTextTranslationResult().getIsFinal()) {
88+
System.out.println(String.format("\nFinal translation: %s", translation));
89+
System.out.println(String.format("Final recognition result: %s", source));
90+
break;
91+
}
92+
System.out.println(String.format("\nPartial translation: %s", translation));
93+
System.out.println(String.format("Partial recognition result: %s", source));
94+
}
95+
}
96+
}
97+
}
98+
// [END media_translation_translate_from_file]
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright 2020 Google LLC
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.mediatranslation;
18+
19+
// [START media_translation_translate_from_mic]
20+
21+
import com.google.api.gax.rpc.ClientStream;
22+
import com.google.api.gax.rpc.ResponseObserver;
23+
import com.google.api.gax.rpc.StreamController;
24+
import com.google.cloud.mediatranslation.v1beta1.SpeechTranslationServiceClient;
25+
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechConfig;
26+
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechRequest;
27+
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechResponse;
28+
import com.google.cloud.mediatranslation.v1beta1.StreamingTranslateSpeechResult;
29+
import com.google.cloud.mediatranslation.v1beta1.TranslateSpeechConfig;
30+
import com.google.protobuf.ByteString;
31+
import java.io.IOException;
32+
import javax.sound.sampled.AudioFormat;
33+
import javax.sound.sampled.AudioInputStream;
34+
import javax.sound.sampled.AudioSystem;
35+
import javax.sound.sampled.DataLine;
36+
import javax.sound.sampled.LineUnavailableException;
37+
import javax.sound.sampled.TargetDataLine;
38+
39+
public class TranslateFromMic {
40+
41+
public static void main(String[] args) throws IOException, LineUnavailableException {
42+
translateFromMic();
43+
}
44+
45+
public static void translateFromMic() throws IOException, LineUnavailableException {
46+
47+
ResponseObserver<StreamingTranslateSpeechResponse> responseObserver = null;
48+
49+
// Initialize client that will be used to send requests. This client only needs to be created
50+
// once, and can be reused for multiple requests. After completing all of your requests, call
51+
// the "close" method on the client to safely clean up any remaining background resources.
52+
try (SpeechTranslationServiceClient client = SpeechTranslationServiceClient.create()) {
53+
responseObserver =
54+
new ResponseObserver<StreamingTranslateSpeechResponse>() {
55+
56+
@Override
57+
public void onStart(StreamController controller) {}
58+
59+
@Override
60+
public void onResponse(StreamingTranslateSpeechResponse response) {
61+
StreamingTranslateSpeechResult res = response.getResult();
62+
String translation = res.getTextTranslationResult().getTranslation();
63+
String source = res.getRecognitionResult();
64+
65+
if (res.getTextTranslationResult().getIsFinal()) {
66+
System.out.println(String.format("\nFinal translation: %s", translation));
67+
System.out.println(String.format("Final recognition result: %s", source));
68+
} else {
69+
System.out.println(String.format("\nPartial translation: %s", translation));
70+
System.out.println(String.format("Partial recognition result: %s", source));
71+
}
72+
}
73+
74+
@Override
75+
public void onComplete() {}
76+
77+
public void onError(Throwable t) {
78+
System.out.println(t);
79+
}
80+
};
81+
82+
ClientStream<StreamingTranslateSpeechRequest> clientStream =
83+
client.streamingTranslateSpeechCallable().splitCall(responseObserver);
84+
85+
TranslateSpeechConfig audioConfig =
86+
TranslateSpeechConfig.newBuilder()
87+
.setAudioEncoding("linear16")
88+
.setSourceLanguageCode("en-US")
89+
.setTargetLanguageCode("es-ES")
90+
.setSampleRateHertz(16000)
91+
.build();
92+
93+
StreamingTranslateSpeechConfig streamingRecognitionConfig =
94+
StreamingTranslateSpeechConfig.newBuilder().setAudioConfig(audioConfig).build();
95+
96+
StreamingTranslateSpeechRequest request =
97+
StreamingTranslateSpeechRequest.newBuilder()
98+
.setStreamingConfig(streamingRecognitionConfig)
99+
.build(); // The first request in a streaming call has to be a config
100+
101+
clientStream.send(request);
102+
// SampleRate:16000Hz, SampleSizeInBits: 16, Number of channels: 1, Signed: true,
103+
// bigEndian: false
104+
AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false);
105+
DataLine.Info targetInfo =
106+
new DataLine.Info(
107+
TargetDataLine.class,
108+
audioFormat); // Set the system information to read from the microphone audio stream
109+
110+
if (!AudioSystem.isLineSupported(targetInfo)) {
111+
System.out.println("Microphone not supported");
112+
System.exit(0);
113+
}
114+
// Target data line captures the audio stream the microphone produces.
115+
TargetDataLine targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
116+
targetDataLine.open(audioFormat);
117+
targetDataLine.start();
118+
System.out.println("Start speaking... Press Ctrl-C to stop");
119+
long startTime = System.currentTimeMillis();
120+
// Audio Input Stream
121+
AudioInputStream audio = new AudioInputStream(targetDataLine);
122+
123+
while (true) {
124+
byte[] data = new byte[6400];
125+
audio.read(data);
126+
request =
127+
StreamingTranslateSpeechRequest.newBuilder()
128+
.setAudioContent(ByteString.copyFrom(data))
129+
.build();
130+
clientStream.send(request);
131+
}
132+
}
133+
}
134+
}
135+
// [END media_translation_translate_from_mic]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2020 Google LLC
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.mediatranslation;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
@RunWith(JUnit4.class)
31+
public class TranslateFromFileTest {
32+
33+
private ByteArrayOutputStream bout;
34+
35+
@Before
36+
public void setUp() {
37+
bout = new ByteArrayOutputStream();
38+
System.setOut(new PrintStream(bout));
39+
}
40+
41+
@After
42+
public void tearDown() {
43+
System.setOut(null);
44+
bout.reset();
45+
}
46+
47+
@Test
48+
public void testTranslateFromFile() throws IOException {
49+
// Call translateFromFile to print out the translated output.
50+
TranslateFromFile.translateFromFile("resources/audio.raw");
51+
String output = bout.toString();
52+
53+
// Check that the output contain some translation.
54+
assertThat(output).contains("Partial translation");
55+
}
56+
}

0 commit comments

Comments
 (0)