|
| 1 | +/* |
| 2 | + * Copyright 2025 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.speech; |
| 18 | + |
| 19 | +// [START TranscribeStreamingV2] |
| 20 | + |
| 21 | +import com.google.api.gax.rpc.BidiStream; |
| 22 | +import com.google.cloud.speech.v2.*; |
| 23 | +import com.google.protobuf.ByteString; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.nio.file.Paths; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Iterator; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +public class TranscribeStreamingV2 { |
| 34 | + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 35 | + |
| 36 | + // Transcribes audio from an audio file stream using Google Cloud Speech-to-Text API. |
| 37 | +// Args: |
| 38 | +// streamFile (String): Path to the local audio file to be transcribed. |
| 39 | +// Example: "resources/audio.wav" |
| 40 | +// Returns: |
| 41 | +// List<StreamingRecognizeResponse>: A list of objects. |
| 42 | +// Each response includes the transcription results for the corresponding audio segment. |
| 43 | +// |
| 44 | + public static List<StreamingRecognizeResponse> transcribeStreamingV2(String streamFile) throws IOException { |
| 45 | + List<StreamingRecognizeResponse> responses = new ArrayList<>(); |
| 46 | +// Instantiates a client |
| 47 | + try (SpeechClient client = SpeechClient.create()) { |
| 48 | + |
| 49 | + Path path = Paths.get(streamFile); |
| 50 | + byte[] audioContent = Files.readAllBytes(path); |
| 51 | + |
| 52 | +// In practice, stream should be a generator yielding chunks of audio data |
| 53 | + int chunkLength = audioContent.length / 5; |
| 54 | + List<byte[]> stream = new ArrayList<>(); |
| 55 | + for (int i = 0; i < audioContent.length; i += chunkLength) { |
| 56 | + int end = Math.min(i + chunkLength, audioContent.length); |
| 57 | + byte[] chunk = new byte[end - i]; |
| 58 | + System.arraycopy(audioContent, i, chunk, 0, end - i); |
| 59 | + stream.add(chunk); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + List<StreamingRecognizeRequest> audioRequests = new ArrayList<>(); |
| 64 | + for (byte[] audio : stream) { |
| 65 | + audioRequests.add(StreamingRecognizeRequest.newBuilder().setAudio(ByteString.copyFrom(audio)).build()); |
| 66 | + } |
| 67 | + |
| 68 | + RecognitionConfig recognitionConfig = RecognitionConfig.newBuilder() |
| 69 | + .setAutoDecodingConfig(AutoDetectDecodingConfig.getDefaultInstance()) |
| 70 | + .addLanguageCodes("en-US") |
| 71 | + .setModel("long") |
| 72 | + .build(); |
| 73 | + |
| 74 | + StreamingRecognitionConfig streamingConfig = StreamingRecognitionConfig.newBuilder() |
| 75 | + .setConfig(recognitionConfig) |
| 76 | + .build(); |
| 77 | + |
| 78 | + StreamingRecognizeRequest configRequest = StreamingRecognizeRequest.newBuilder() |
| 79 | + .setRecognizer(String.format("projects/%s/locations/global/recognizers/_", PROJECT_ID)) |
| 80 | + .setStreamingConfig(streamingConfig) |
| 81 | + .build(); |
| 82 | + |
| 83 | + |
| 84 | + List<StreamingRecognizeRequest> requests = new ArrayList<>(); |
| 85 | + requests.add(configRequest); |
| 86 | + requests.addAll(audioRequests); |
| 87 | + |
| 88 | + BidiStream<StreamingRecognizeRequest, StreamingRecognizeResponse> stream1 = client.streamingRecognizeCallable().call(); |
| 89 | + for (StreamingRecognizeRequest request : requests) { |
| 90 | + stream1.send(request); |
| 91 | + } |
| 92 | + for (int i = 0; i < requests.size(); i++) { |
| 93 | + StreamingRecognizeRequest request = requests.get(i); |
| 94 | + System.out.println("Request " + (i + 1) + ": " + request); |
| 95 | + // You might want to print specific parts of the request, like the audio or config, rather than the entire object |
| 96 | + if (request.hasAudio()) { |
| 97 | + System.out.println("Audio: " + request.getAudio().size() + " bytes"); |
| 98 | + } |
| 99 | + if (request.hasStreamingConfig()) { |
| 100 | + System.out.println("Streaming Config: " + request.getStreamingConfig()); |
| 101 | + } |
| 102 | + } |
| 103 | + stream1.closeSend(); |
| 104 | + |
| 105 | + Iterator<StreamingRecognizeResponse> responseIterator = stream1.iterator(); |
| 106 | + while (responseIterator.hasNext()) { |
| 107 | + StreamingRecognizeResponse response = responseIterator.next(); |
| 108 | + System.out.println(response); |
| 109 | + // Process the response and extract the transcript |
| 110 | + System.out.println("Transcript: " + response.getResultsList().get(0).getAlternativesList().get(0).getTranscript()); |
| 111 | + responses.add(response); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + } |
| 116 | + return responses; |
| 117 | + } |
| 118 | + |
| 119 | + public static void main(String[] args) throws IOException { |
| 120 | + List<StreamingRecognizeResponse> responses = transcribeStreamingV2("./resources/brooklyn_bridge.wav"); |
| 121 | + } |
| 122 | +} |
| 123 | +// [END TranscribeStreamingV2] |
0 commit comments