Skip to content

Commit 4be00ee

Browse files
committed
First public release
0 parents  commit 4be00ee

20 files changed

+2734
-0
lines changed

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
migrate_working_dir/
12+
13+
# IntelliJ related
14+
*.iml
15+
*.ipr
16+
*.iws
17+
.idea/
18+
19+
# The .vscode folder contains launch configuration and tasks you configure in
20+
# VS Code which you may wish to be included in version control, so this line
21+
# is commented out by default.
22+
.vscode/
23+
24+
# Flutter/Dart/Pub related
25+
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
26+
/pubspec.lock
27+
**/doc/api/
28+
.dart_tool/
29+
build/

.metadata

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: "9e1c857886f07d342cf106f2cd588bcd5e031bb2"
8+
channel: "stable"
9+
10+
project_type: package

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.1.0
2+
3+
* First public release.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Vadym Borodavko
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
AWS Transcribe Streaming client for producing real-time transcriptions for your media content using HTTP/2.
2+
3+
## Getting started
4+
5+
Add necessary dependencies to `pubspec.yaml`:
6+
```yaml
7+
dependencies:
8+
aws_common: ^0.6.0
9+
aws_transcribe_streaming: ^0.1.0
10+
```
11+
12+
Obtain a pair of Access/Secret keys for the AWS IAM user with `transcribe:StartStreamTranscription` permission.
13+
See details in AWS documentation: [Transcribing streaming audio](https://docs.aws.amazon.com/transcribe/latest/dg/streaming.html)
14+
15+
It is recommended to use [Temporary security credentials](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp.html) with session token obtained from the backend just before starting the transcribing process.
16+
17+
See also [best practices](https://docs.aws.amazon.com/transcribe/latest/dg/streaming.html#best-practices) to improve streaming transcription efficiency.
18+
19+
## Usage
20+
21+
1. Create a new transcribe streaming client:
22+
```dart
23+
import 'package:aws_common/aws_common.dart';
24+
import 'package:aws_transcribe_streaming/aws_transcribe_streaming.dart';
25+
26+
final transcribeStreamingClient = TranscribeStreamingClient(
27+
region: 'eu-central-1',
28+
credentialsProvider: StaticCredentialsProvider(AWSCredentials(
29+
'ASIAIOEXAMPLEEXAMPLE', // accessKeyId
30+
'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY', // secretAccessKey
31+
'AQoDYXdzEJr...', // sessionToken
32+
DateTime.now().add(const Duration(hours: 1)), // expiration
33+
)),
34+
);
35+
```
36+
37+
2. Start a stream transcription:
38+
```dart
39+
final (response, audioStreamSink, transcriptEventStream) =
40+
await transcribeStreamingClient.startStreamTranscription(
41+
const StartStreamTranscriptionRequest(
42+
languageCode: LanguageCode.enUs,
43+
mediaSampleRateHertz: 48000,
44+
mediaEncoding: MediaEncoding.pcm,
45+
),
46+
);
47+
```
48+
49+
3. Subscribe to a raw TranscriptEvent stream:
50+
```dart
51+
final transcriptSubscription = transcriptEventStream
52+
.listen((TranscriptEvent event) => print(event));
53+
```
54+
or use a custom strategy to decode TranscriptEvents and build the realtime transcription:
55+
```dart
56+
final transcriptSubscription = transcriptEventStream
57+
.transform(const TranscriptEventStreamDecoder(PlainTextTranscriptionStrategy()))
58+
.listen((String message) => print(message));
59+
```
60+
61+
4. Start sending audio data to the audio stream sink:
62+
```dart
63+
// Raw audio data from the microphone in PCM signed 16-bit little-endian audio format
64+
Stream<Uint8List> audioStream;
65+
66+
final audioStreamSubscription = audioStream.listen(
67+
audioStreamSink.add,
68+
onDone: audioStreamSink.close,
69+
);
70+
```

analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: package:lints/recommended.yaml

example/main.dart

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import 'dart:async';
2+
import 'dart:typed_data';
3+
4+
import 'package:aws_common/aws_common.dart';
5+
import 'package:aws_transcribe_streaming/aws_transcribe_streaming.dart';
6+
7+
void main() async {
8+
// Create a client.
9+
final transcribeStreamingClient = TranscribeStreamingClient(
10+
region: 'eu-central-1',
11+
// Provide credentials with `transcribe:StartStreamTranscription` permission
12+
credentialsProvider: StaticCredentialsProvider(AWSCredentials(
13+
'ASIAIOEXAMPLEEXAMPLE', // accessKeyId
14+
'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY', // secretAccessKey
15+
'AQoDYXdzEJr...', // sessionToken
16+
DateTime.now().add(const Duration(hours: 1)), // expiration
17+
)),
18+
);
19+
20+
late final StartStreamTranscriptionResponse response;
21+
late final Stream<TranscriptEvent> transcriptEventStream;
22+
late final StreamSink<Uint8List> audioStreamSink;
23+
StreamSubscription<Uint8List>? audioStreamSubscription;
24+
25+
try {
26+
// Start a stream transcription.
27+
(response, audioStreamSink, transcriptEventStream) =
28+
await transcribeStreamingClient.startStreamTranscription(
29+
const StartStreamTranscriptionRequest(
30+
languageCode: LanguageCode.enUs,
31+
mediaSampleRateHertz: 48000,
32+
mediaEncoding: MediaEncoding.pcm,
33+
),
34+
);
35+
} on TranscribeStreamingException catch (e) {
36+
print('Error starting transcription: $e');
37+
return;
38+
}
39+
40+
print('Session ID: ${response.sessionId}');
41+
42+
final transcriptionCompleter = Completer<void>();
43+
44+
// Listen to transcript events.
45+
// final transcriptSubscription =
46+
// transcriptEventStream.listen((TranscriptEvent event) => print(event));
47+
// or use a custom strategy to decode transcript events.
48+
transcriptEventStream
49+
.transform(
50+
const TranscriptEventStreamDecoder(PlainTextTranscriptionStrategy()))
51+
.listen(
52+
(String message) {
53+
print('Transcription: $message');
54+
},
55+
onError: (Object error, StackTrace stackTrace) async {
56+
print('Transcription error: $error');
57+
await audioStreamSubscription?.cancel();
58+
await audioStreamSink.close();
59+
},
60+
onDone: () async {
61+
print('Transcription done');
62+
await audioStreamSubscription?.cancel();
63+
transcriptionCompleter.complete();
64+
},
65+
);
66+
67+
// Instead use a real stream of audio data from the microphone
68+
// in PCM signed 16-bit little-endian audio format with 48kHz sample rate.
69+
final audioStream = Stream<Uint8List>.periodic(
70+
const Duration(milliseconds: 200), (count) => Uint8List(19200)).take(25);
71+
72+
// Send audio data to the audio stream sink.
73+
audioStreamSubscription = audioStream.listen(
74+
audioStreamSink.add,
75+
onDone: audioStreamSink.close,
76+
);
77+
78+
await transcriptionCompleter.future;
79+
80+
print('Finished');
81+
}

lib/aws_transcribe_streaming.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2023 Vadym Borodavko
2+
// Licensed under the MIT License.
3+
4+
/// This library provides AWS Transcribe Streaming client for producing
5+
/// real-time transcriptions for your media content using HTTP/2.
6+
library aws_transcribe_streaming;
7+
8+
export 'src/client.dart';
9+
export 'src/event_stream/exceptions.dart';
10+
export 'src/event_stream/header.dart';
11+
export 'src/event_stream/message.dart';
12+
export 'src/exceptions.dart';
13+
export 'src/models.dart';
14+
export 'src/transcription.dart';

0 commit comments

Comments
 (0)