Skip to content

Commit 0a839a8

Browse files
SergeyRyabininmeyertst-aws
andauthoredFeb 5, 2024
CPP: No need to wait for a final transcript to be received in transcribe streaming (#6025)
* No need to wait for a final transcript to be received starting the CPP SDK version 1.11.252 --------- Co-authored-by: Steven Meyer <108885656+meyertst-aws@users.noreply.github.com>
1 parent a54abe5 commit 0a839a8

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed
 

‎cpp/example_code/transcribe/get_transcript.cpp

+16-23
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ using namespace Aws::TranscribeStreamingService::Model;
1919

2020
//TODO(User): Update path to location of local .wav test file, if necessary.
2121
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;
2327

2428
// snippet-start:[transcribe.cpp.stream_transcription_async.code]
2529
int main() {
@@ -30,8 +34,6 @@ int main() {
3034
//TODO(User): Set to the region of your AWS account.
3135
const Aws::String region = Aws::Region::US_WEST_2;
3236

33-
Aws::Utils::Threading::Semaphore canCloseStream(0 /*initialCount*/, 1 /*maxCount*/);
34-
3537
//Load a profile that has been granted AmazonTranscribeFullAccess AWS managed permission policy.
3638
Aws::Client::ClientConfiguration config;
3739
#ifdef _WIN32
@@ -53,33 +55,28 @@ int main() {
5355
});
5456
//SetTranscriptEventCallback called for every 'chunk' of file transcripted.
5557
// Partial results are returned in real time.
56-
handler.SetTranscriptEventCallback([&canCloseStream](const TranscriptEvent &ev) {
57-
bool isFinal = false;
58+
handler.SetTranscriptEventCallback([](const TranscriptEvent &ev) {
5859
for (auto &&r: ev.GetTranscript().GetResults()) {
5960
if (r.GetIsPartial()) {
6061
std::cout << "[partial] ";
6162
}
6263
else {
6364
std::cout << "[Final] ";
64-
isFinal = true;
6565
}
6666
for (auto &&alt: r.GetAlternatives()) {
6767
std::cout << alt.GetTranscript() << std::endl;
6868
}
6969
}
70-
if (isFinal) {
71-
canCloseStream.Release();
72-
}
7370
});
7471

7572
StartStreamTranscriptionRequest request;
76-
request.SetMediaSampleRateHertz(8000);
73+
request.SetMediaSampleRateHertz(SAMPLE_RATE);
7774
request.SetLanguageCode(LanguageCode::en_US);
7875
request.SetMediaEncoding(
7976
MediaEncoding::pcm); // wav and aiff files are PCM formats.
8077
request.SetEventStreamHandler(handler);
8178

82-
auto OnStreamReady = [&canCloseStream](AudioStream &stream) {
79+
auto OnStreamReady = [](AudioStream &stream) {
8380
Aws::FStream file(FILE_NAME, std::ios_base::in | std::ios_base::binary);
8481
if (!file.is_open()) {
8582
std::cerr << "Failed to open " << FILE_NAME << '\n';
@@ -122,26 +119,22 @@ int main() {
122119
std::cout << "Successfully sent the empty frame" << std::endl;
123120
}
124121
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();
128122
stream.Close();
129-
};
123+
};
130124

131125
Aws::Utils::Threading::Semaphore signaling(0 /*initialCount*/, 1 /*maxCount*/);
132-
auto OnResponseCallback = [&signaling, &canCloseStream](
126+
auto OnResponseCallback = [&signaling](
133127
const TranscribeStreamingServiceClient * /*unused*/,
134128
const Model::StartStreamTranscriptionRequest & /*unused*/,
135-
const Model::StartStreamTranscriptionOutcome & outcome,
129+
const Model::StartStreamTranscriptionOutcome &outcome,
136130
const std::shared_ptr<const Aws::Client::AsyncCallerContext> & /*unused*/) {
137131

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+
}
142136

143-
canCloseStream.Release();
144-
signaling.Release();
137+
signaling.Release();
145138
};
146139

147140
std::cout << "Starting..." << std::endl;

0 commit comments

Comments
 (0)
Please sign in to comment.