@@ -19,7 +19,11 @@ using namespace Aws::TranscribeStreamingService::Model;
19
19
20
20
// TODO(User): Update path to location of local .wav test file, if necessary.
21
21
static const Aws::String FILE_NAME{MEDIA_DIR " /transcribe-test-file.wav" };
22
- static const int BUFFER_SIZE = 1024 ;
22
+ static const int SAMPLE_RATE = 8000 ; // for the file above
23
+ // If you're able to specify chunk size with your audio type (such as with PCM), set each chunk to between 50 ms and 200 ms
24
+ static const int CHUNK_LENGTH = 125 ;
25
+ // chunk_size_in_bytes = chunk_duration_in_millisecond / 1000 * audio_sample_rate * 2
26
+ static const int BUFFER_SIZE = CHUNK_LENGTH * SAMPLE_RATE / 1000 * 2 ;
23
27
24
28
// snippet-start:[transcribe.cpp.stream_transcription_async.code]
25
29
int main () {
@@ -30,8 +34,6 @@ int main() {
30
34
// TODO(User): Set to the region of your AWS account.
31
35
const Aws::String region = Aws::Region::US_WEST_2;
32
36
33
- Aws::Utils::Threading::Semaphore canCloseStream (0 /* initialCount*/ , 1 /* maxCount*/ );
34
-
35
37
// Load a profile that has been granted AmazonTranscribeFullAccess AWS managed permission policy.
36
38
Aws::Client::ClientConfiguration config;
37
39
#ifdef _WIN32
@@ -53,33 +55,28 @@ int main() {
53
55
});
54
56
// SetTranscriptEventCallback called for every 'chunk' of file transcripted.
55
57
// Partial results are returned in real time.
56
- handler.SetTranscriptEventCallback ([&canCloseStream](const TranscriptEvent &ev) {
57
- bool isFinal = false ;
58
+ handler.SetTranscriptEventCallback ([](const TranscriptEvent &ev) {
58
59
for (auto &&r: ev.GetTranscript ().GetResults ()) {
59
60
if (r.GetIsPartial ()) {
60
61
std::cout << " [partial] " ;
61
62
}
62
63
else {
63
64
std::cout << " [Final] " ;
64
- isFinal = true ;
65
65
}
66
66
for (auto &&alt: r.GetAlternatives ()) {
67
67
std::cout << alt.GetTranscript () << std::endl;
68
68
}
69
69
}
70
- if (isFinal) {
71
- canCloseStream.Release ();
72
- }
73
70
});
74
71
75
72
StartStreamTranscriptionRequest request;
76
- request.SetMediaSampleRateHertz (8000 );
73
+ request.SetMediaSampleRateHertz (SAMPLE_RATE );
77
74
request.SetLanguageCode (LanguageCode::en_US);
78
75
request.SetMediaEncoding (
79
76
MediaEncoding::pcm); // wav and aiff files are PCM formats.
80
77
request.SetEventStreamHandler (handler);
81
78
82
- auto OnStreamReady = [&canCloseStream ](AudioStream &stream) {
79
+ auto OnStreamReady = [](AudioStream &stream) {
83
80
Aws::FStream file (FILE_NAME, std::ios_base::in | std::ios_base::binary);
84
81
if (!file.is_open ()) {
85
82
std::cerr << " Failed to open " << FILE_NAME << ' \n ' ;
@@ -122,26 +119,22 @@ int main() {
122
119
std::cout << " Successfully sent the empty frame" << std::endl;
123
120
}
124
121
stream.flush ();
125
- // Wait until the final transcript or an error is received.
126
- // Closing the stream prematurely will trigger an error.
127
- canCloseStream.WaitOne ();
128
122
stream.Close ();
129
- };
123
+ };
130
124
131
125
Aws::Utils::Threading::Semaphore signaling (0 /* initialCount*/ , 1 /* maxCount*/ );
132
- auto OnResponseCallback = [&signaling, &canCloseStream ](
126
+ auto OnResponseCallback = [&signaling](
133
127
const TranscribeStreamingServiceClient * /* unused*/ ,
134
128
const Model::StartStreamTranscriptionRequest & /* unused*/ ,
135
- const Model::StartStreamTranscriptionOutcome & outcome,
129
+ const Model::StartStreamTranscriptionOutcome &outcome,
136
130
const std::shared_ptr<const Aws::Client::AsyncCallerContext> & /* unused*/ ) {
137
131
138
- if (!outcome.IsSuccess ())
139
- {
140
- std::cerr << " Transcribe streaming error " << outcome.GetError ().GetMessage () << std::endl;
141
- }
132
+ if (!outcome.IsSuccess ()) {
133
+ std::cerr << " Transcribe streaming error "
134
+ << outcome.GetError ().GetMessage () << std::endl;
135
+ }
142
136
143
- canCloseStream.Release ();
144
- signaling.Release ();
137
+ signaling.Release ();
145
138
};
146
139
147
140
std::cout << " Starting..." << std::endl;
0 commit comments