• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. 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  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.services.transcribestreaming;
17 
18 import com.github.davidmoten.rx2.Bytes;
19 import io.reactivex.Flowable;
20 import java.io.File;
21 import java.util.concurrent.CompletableFuture;
22 import java.util.function.Consumer;
23 import org.junit.Test;
24 import org.reactivestreams.Publisher;
25 import software.amazon.awssdk.core.SdkBytes;
26 import software.amazon.awssdk.services.transcribestreaming.model.AudioEvent;
27 import software.amazon.awssdk.services.transcribestreaming.model.AudioStream;
28 import software.amazon.awssdk.services.transcribestreaming.model.LanguageCode;
29 import software.amazon.awssdk.services.transcribestreaming.model.MediaEncoding;
30 import software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionRequest;
31 import software.amazon.awssdk.services.transcribestreaming.model.StartStreamTranscriptionResponseHandler;
32 import software.amazon.awssdk.services.transcribestreaming.model.TranscriptEvent;
33 import software.amazon.awssdk.services.transcribestreaming.model.TranscriptResultStream;
34 
35 /**
36  * Option 1: Add a new method to hide: (1) the need for non-replayable publishers, (2) the reconnect boilerplate.
37  */
38 public class Option1 {
39     private File audioFile = new File(getClass().getClassLoader().getResource("silence_16kHz_s16le.wav").getFile());
40 
41     @Test
option1()42     public void option1() {
43         try (TranscribeStreamingAsyncClient client = TranscribeStreamingAsyncClient.create()) {
44             // Create the request for transcribe that includes the audio metadata
45             StartStreamTranscriptionRequest audioMetadata =
46                     StartStreamTranscriptionRequest.builder()
47                                                    .languageCode(LanguageCode.EN_US)
48                                                    .mediaEncoding(MediaEncoding.PCM)
49                                                    .mediaSampleRateHertz(16_000)
50                                                    .build();
51 
52             // Create the audio stream for transcription
53             Publisher<AudioStream> audioStream =
54                     Bytes.from(audioFile)
55                          .map(SdkBytes::fromByteArray)
56                          .map(bytes -> AudioEvent.builder().audioChunk(bytes).build())
57                          .cast(AudioStream.class);
58 
59             // Create the visitor that handles the transcriptions from transcribe
60             Consumer<TranscriptResultStream> reader = event -> {
61                 if (event instanceof TranscriptEvent) {
62                     TranscriptEvent transcriptEvent = (TranscriptEvent) event;
63                     System.out.println(transcriptEvent.transcript().results());
64                 }
65             };
66 
67             StartStreamTranscriptionResponseHandler responseHandler = StartStreamTranscriptionResponseHandler.builder()
68                                                                                                              .subscriber(reader)
69                                                                                                              .build();
70 
71             // Start talking with transcribe using a new auto-reconnect method (method name to be bikeshed)
72             CompletableFuture<Void> result = client.startStreamTranscriptionWithAutoReconnect(audioMetadata,
73                                                                                               audioStream,
74                                                                                               responseHandler);
75             result.join();
76         }
77     }
78 
79 }
80