Skip to content

Commit 8b6e0ec

Browse files
authored
C++, Java: Transcribe-streaming, add scenarios (#7169)
add scenarios for file and audio transcriptions
1 parent db53bdc commit 8b6e0ec

File tree

5 files changed

+117
-31
lines changed

5 files changed

+117
-31
lines changed

.doc_gen/metadata/transcribe-streaming_metadata.yaml

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ transcribe-streaming_StartMedicalStreamTranscription:
4242
- transcribe.java-medical-streaming-demo
4343
services:
4444
transcribe-streaming: {StartMedicalStreamTranscription}
45-
transcribe-streaming_Scenario_StreamEvents:
46-
title: Generate a transcription of an audio stream using an &AWS; SDK
45+
transcribe-streaming_Scenario_StreamEvents_File:
46+
title: Generate a transcription of a file using an &AWS; SDK
4747
title_abbrev: Transcribe an audio file
4848
synopsis: generate a transcription of a source audio file using &TSC; streaming.
4949
category: Scenarios
@@ -58,5 +58,39 @@ transcribe-streaming_Scenario_StreamEvents:
5858
snippet_tags:
5959
- swift.transcribe-streaming.all
6060
- swift.transcribe-streaming.transcribeerror
61+
Java:
62+
versions:
63+
- sdk_version: 2
64+
github: javav2/example_code/transcribe-streaming
65+
sdkguide:
66+
excerpts:
67+
- description:
68+
snippet_tags:
69+
- transcribe.java-streaming-demo-file.main
70+
C++:
71+
versions:
72+
- sdk_version: 1
73+
github: cpp/example_code/transcribe-streaming
74+
excerpts:
75+
- description:
76+
snippet_tags:
77+
- transcribe.cpp.stream_transcription_async.code
78+
services:
79+
transcribe-streaming: {StartStreamTranscription}
80+
transcribe-streaming_Scenario_StreamEvents_Microphone:
81+
title: Generate a transcription from a microphone using an &AWS; SDK
82+
title_abbrev: Transcribe audio from a microphone
83+
synopsis: generate a transcription from a microphone using &TSC; streaming.
84+
category: Scenarios
85+
languages:
86+
Java:
87+
versions:
88+
- sdk_version: 2
89+
github: javav2/example_code/transcribe-streaming
90+
sdkguide:
91+
excerpts:
92+
- description:
93+
snippet_tags:
94+
- transcribe.java-streaming-demo
6195
services:
6296
transcribe-streaming: {StartStreamTranscription}

cpp/example_code/transcribe-streaming/README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ Code excerpts that show you how to call individual service functions.
4343

4444
- [StartStreamTranscription](get_transcript.cpp#L28)
4545

46+
### Scenarios
47+
48+
Code examples that show you how to accomplish a specific task by calling multiple
49+
functions within the same service.
50+
51+
- [Transcribe an audio file](get_transcript.cpp)
52+
4653

4754
<!--custom.examples.start-->
4855
<!--custom.examples.end-->
@@ -69,6 +76,18 @@ folder.
6976

7077

7178

79+
#### Transcribe an audio file
80+
81+
This example shows you how to generate a transcription of a source audio file using Amazon Transcribe streaming.
82+
83+
84+
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_File.start-->
85+
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_File.end-->
86+
87+
88+
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_File.start-->
89+
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_File.end-->
90+
7291
### Tests
7392

7493
⚠ Running tests might result in charges to your AWS account.
@@ -99,4 +118,4 @@ folder.
99118

100119
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
101120

102-
SPDX-License-Identifier: Apache-2.0
121+
SPDX-License-Identifier: Apache-2.0

javav2/example_code/transcribe-streaming/README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ For prerequisites, see the [README](../../README.md#Prerequisites) in the `javav
3434
Code excerpts that show you how to call individual service functions.
3535

3636
- [StartMedicalStreamTranscription](src/main/java/com/amazonaws/transcribestreaming/TranscribeMedicalStreamingDemoApp.java#L25)
37-
- [StartStreamTranscription](src/main/java/com/amazonaws/transcribestreaming/TranscribeStreamingDemoApp.java#L26)
37+
- [StartStreamTranscription](src/main/java/com/amazonaws/transcribestreaming/TranscribeStreamingDemoApp.java#L36)
38+
39+
### Scenarios
40+
41+
Code examples that show you how to accomplish a specific task by calling multiple
42+
functions within the same service.
43+
44+
- [Transcribe an audio file](src/main/java/com/amazonaws/transcribestreaming/TranscribeStreamingDemoFile.java)
45+
- [Transcribe audio from a microphone](src/main/java/com/amazonaws/transcribestreaming/TranscribeStreamingDemoApp.java)
3846

3947

4048
<!--custom.examples.start-->
@@ -50,6 +58,30 @@ Code excerpts that show you how to call individual service functions.
5058

5159

5260

61+
#### Transcribe an audio file
62+
63+
This example shows you how to generate a transcription of a source audio file using Amazon Transcribe streaming.
64+
65+
66+
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_File.start-->
67+
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_File.end-->
68+
69+
70+
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_File.start-->
71+
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_File.end-->
72+
73+
#### Transcribe audio from a microphone
74+
75+
This example shows you how to generate a transcription from a microphone using Amazon Transcribe streaming.
76+
77+
78+
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_Microphone.start-->
79+
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_Microphone.end-->
80+
81+
82+
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_Microphone.start-->
83+
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_Microphone.end-->
84+
5385
### Tests
5486

5587
⚠ Running tests might result in charges to your AWS account.
@@ -76,4 +108,4 @@ in the `javav2` folder.
76108

77109
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
78110

79-
SPDX-License-Identifier: Apache-2.0
111+
SPDX-License-Identifier: Apache-2.0

javav2/example_code/transcribe-streaming/src/main/java/com/amazonaws/transcribestreaming/TranscribeStreamingDemoApp.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@
1212
import software.amazon.awssdk.regions.Region;
1313
import software.amazon.awssdk.services.transcribestreaming.TranscribeStreamingAsyncClient;
1414
import software.amazon.awssdk.services.transcribestreaming.model.*;
15-
import javax.sound.sampled.*;
16-
import java.io.*;
15+
16+
import javax.sound.sampled.AudioFormat;
17+
import javax.sound.sampled.AudioInputStream;
18+
import javax.sound.sampled.AudioSystem;
19+
import javax.sound.sampled.DataLine;
20+
import javax.sound.sampled.LineUnavailableException;
21+
import javax.sound.sampled.TargetDataLine;
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.PrintWriter;
25+
import java.io.StringWriter;
26+
import java.io.UncheckedIOException;
1727
import java.net.URISyntaxException;
1828
import java.nio.ByteBuffer;
1929
import java.util.List;
@@ -28,7 +38,7 @@ public class TranscribeStreamingDemoApp {
2838
private static final Region REGION = Region.US_EAST_1;
2939
private static TranscribeStreamingAsyncClient client;
3040

31-
public static void main(String args[])
41+
public static void main(String[] args)
3242
throws URISyntaxException, ExecutionException, InterruptedException, LineUnavailableException {
3343

3444
client = TranscribeStreamingAsyncClient.builder()
@@ -85,7 +95,7 @@ private static StartStreamTranscriptionResponseHandler getResponseHandler() {
8595
System.out.println(e.getMessage());
8696
StringWriter sw = new StringWriter();
8797
e.printStackTrace(new PrintWriter(sw));
88-
System.out.println("Error Occurred: " + sw.toString());
98+
System.out.println("Error Occurred: " + sw);
8999
})
90100
.onComplete(() -> {
91101
System.out.println("=== All records stream successfully ===");
@@ -101,19 +111,10 @@ private static StartStreamTranscriptionResponseHandler getResponseHandler() {
101111
.build();
102112
}
103113

104-
private InputStream getStreamFromFile(String audioFileName) {
105-
try {
106-
File inputFile = new File(getClass().getClassLoader().getResource(audioFileName).getFile());
107-
InputStream audioStream = new FileInputStream(inputFile);
108-
return audioStream;
109-
} catch (FileNotFoundException e) {
110-
throw new RuntimeException(e);
111-
}
112-
}
113-
114+
114115
private static class AudioStreamPublisher implements Publisher<AudioStream> {
115-
private final InputStream inputStream;
116116
private static Subscription currentSubscription;
117+
private final InputStream inputStream;
117118

118119
private AudioStreamPublisher(InputStream inputStream) {
119120
this.inputStream = inputStream;
@@ -122,22 +123,22 @@ private AudioStreamPublisher(InputStream inputStream) {
122123
@Override
123124
public void subscribe(Subscriber<? super AudioStream> s) {
124125

125-
if (this.currentSubscription == null) {
126-
this.currentSubscription = new SubscriptionImpl(s, inputStream);
126+
if (currentSubscription == null) {
127+
currentSubscription = new SubscriptionImpl(s, inputStream);
127128
} else {
128-
this.currentSubscription.cancel();
129-
this.currentSubscription = new SubscriptionImpl(s, inputStream);
129+
currentSubscription.cancel();
130+
currentSubscription = new SubscriptionImpl(s, inputStream);
130131
}
131132
s.onSubscribe(currentSubscription);
132133
}
133134
}
134135

135136
public static class SubscriptionImpl implements Subscription {
136-
private static final int CHUNK_SIZE_IN_BYTES = 1024 * 1;
137+
private static final int CHUNK_SIZE_IN_BYTES = 1024;
137138
private final Subscriber<? super AudioStream> subscriber;
138139
private final InputStream inputStream;
139-
private ExecutorService executor = Executors.newFixedThreadPool(1);
140-
private AtomicLong demand = new AtomicLong(0);
140+
private final ExecutorService executor = Executors.newFixedThreadPool(1);
141+
private final AtomicLong demand = new AtomicLong(0);
141142

142143
SubscriptionImpl(Subscriber<? super AudioStream> s, InputStream inputStream) {
143144
this.subscriber = s;

swift/example_code/transcribe-streaming/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ and run commands.
7272
This example shows you how to generate a transcription of a source audio file using Amazon Transcribe streaming.
7373

7474

75-
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents.start-->
76-
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents.end-->
75+
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_File.start-->
76+
<!--custom.scenario_prereqs.transcribe-streaming_Scenario_StreamEvents_File.end-->
7777

7878

79-
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents.start-->
80-
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents.end-->
79+
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_File.start-->
80+
<!--custom.scenarios.transcribe-streaming_Scenario_StreamEvents_File.end-->
8181

8282
### Tests
8383

0 commit comments

Comments
 (0)