Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <aws/core/Aws.h>
- #include <aws/core/internal/AWSHttpResourceClient.h>
- #include <aws/core/client/ClientConfiguration.h>
- #include <aws/core/utils/threading/Semaphore.h>
- #include <aws/core/utils/threading/Executor.h>
- #include <aws/core/platform/FileSystem.h>
- #include <aws/core/http/HttpTypes.h>
- #include <aws/transcribestreaming/TranscribeStreamingServiceClient.h>
- #include <aws/transcribestreaming/model/StartStreamTranscriptionRequest.h>
- #include <aws/transcribestreaming/model/StartStreamTranscriptionHandler.h>
- #include <fstream>
- using namespace std;
- using namespace Aws;
- using namespace Aws::Utils;
- using namespace Aws::TranscribeStreamingService;
- using namespace Aws::TranscribeStreamingService::Model;
- /**
- * Test for transcribe streaming.
- */
- //static const char TEST_FILE_NAME[] = "../../data/test1-small.wav";
- //static const int FREQ = 16000;
- const char TEST_FILE_NAME[] = "../../data/transcribe-test-file.wav";
- const int FREQ = 8000;
- const char ALLOC_TAG[] = "TestAwsCppSdk";
- int main(int argc, char** argv) {
- Aws::SDKOptions options;
- Aws::InitAPI(options);
- {
- Aws::UniquePtr<TranscribeStreamingServiceClient> client;
- Aws::Client::ClientConfiguration config;
- client = Aws::MakeUnique<TranscribeStreamingServiceClient>(ALLOC_TAG, config);
- Aws::String transcribedResult;
- StartStreamTranscriptionHandler handler;
- handler.SetTranscriptEventCallback([&transcribedResult](const TranscriptEvent& ev) {
- // TODO: only check the result marked as "final"
- const auto& results = ev.GetTranscript().GetResults();
- if (results.empty()) {
- return;
- }
- const auto& last = results.back();
- const auto& alternatives = last.GetAlternatives();
- if (alternatives.empty()) {
- return;
- }
- transcribedResult = alternatives.back().GetTranscript();
- });
- handler.SetOnErrorCallback([&transcribedResult](const Aws::Client::AWSError<TranscribeStreamingServiceErrors>& err) {
- // we will receive an error because the request was abruptly shutdown (via stream.Close()).
- // However, we cannot delay the call to closing the stream, because HTTP clients such as libcurl buffer the
- // requests before sending them over the wire, so it will keep BUFFER_SIZE bytes in its memory if we don't
- // signal the completion of the stream (by closing it).
- // To discern between that case and a true error, we check if we have received any text back from the service.
- if(transcribedResult.empty()) {
- cerr << "Error and empty transcribed result:\n" << err.GetMessage() << "\n";
- }
- });
- StartStreamTranscriptionRequest request;
- request.SetMediaSampleRateHertz(FREQ);
- request.SetLanguageCode(LanguageCode::en_US);
- request.SetMediaEncoding(MediaEncoding::pcm);
- request.SetEventStreamHandler(handler);
- auto OnStreamReady = [](AudioStream& stream) {
- Aws::FStream file(TEST_FILE_NAME, std::ios_base::in | std::ios_base::binary);
- char buf[1024];
- // try to fix:
- // Error and empty transcribed result:
- // A complete signal was sent without the preceding empty frame.
- //stream.WriteAudioEvent({}); // per the spec, we have to send an empty event (i.e. without a payload) at the end.
- while(file) {
- file.read(buf, sizeof(buf));
- Aws::Vector<unsigned char> bits{buf, buf + file.gcount()};
- AudioEvent event(std::move(bits));
- if (!stream) {
- break;
- }
- if (!stream.WriteAudioEvent(event)) {
- break;
- }
- }
- // per the spec, we have to send an empty event (i.e. without a payload) at the end.
- stream.WriteAudioEvent({});
- stream.flush();
- stream.Close();
- };
- Aws::Utils::Threading::Semaphore semaphore(0, 1);
- auto OnResponseCallback = [&semaphore](
- const TranscribeStreamingServiceClient*,
- const StartStreamTranscriptionRequest&,
- const StartStreamTranscriptionOutcome&,
- const std::shared_ptr<const Aws::Client::AsyncCallerContext>&)
- {
- semaphore.ReleaseAll();
- };
- client->StartStreamTranscriptionAsync(request, OnStreamReady, OnResponseCallback, nullptr /*context*/);
- semaphore.WaitOne();
- cout << "transcribed result:\n" << transcribedResult << "\n";
- }
- Aws::ShutdownAPI(options);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement