|
| 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] |
0 commit comments