Advertisement
cyga

aws cpp sdk - transribe streaming problem

Mar 23rd, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.73 KB | None | 0 0
  1. #include <aws/core/Aws.h>
  2. #include <aws/core/internal/AWSHttpResourceClient.h>
  3. #include <aws/core/client/ClientConfiguration.h>
  4. #include <aws/core/utils/threading/Semaphore.h>
  5. #include <aws/core/utils/threading/Executor.h>
  6. #include <aws/core/platform/FileSystem.h>
  7. #include <aws/core/http/HttpTypes.h>
  8. #include <aws/transcribestreaming/TranscribeStreamingServiceClient.h>
  9. #include <aws/transcribestreaming/model/StartStreamTranscriptionRequest.h>
  10. #include <aws/transcribestreaming/model/StartStreamTranscriptionHandler.h>
  11. #include <fstream>
  12.  
  13. using namespace std;
  14. using namespace Aws;
  15. using namespace Aws::Utils;
  16. using namespace Aws::TranscribeStreamingService;
  17. using namespace Aws::TranscribeStreamingService::Model;
  18.  
  19. /**
  20.  * Test for transcribe streaming.
  21.  */
  22.  
  23. //static const char TEST_FILE_NAME[] = "../../data/test1-small.wav";
  24. //static const int FREQ = 16000;
  25. const char TEST_FILE_NAME[] = "../../data/transcribe-test-file.wav";
  26. const int FREQ = 8000;
  27. const char ALLOC_TAG[] = "TestAwsCppSdk";
  28.  
  29. int main(int argc, char** argv) {
  30.     Aws::SDKOptions options;
  31.     Aws::InitAPI(options);
  32.     {
  33.         Aws::UniquePtr<TranscribeStreamingServiceClient> client;
  34.         Aws::Client::ClientConfiguration config;
  35.         client = Aws::MakeUnique<TranscribeStreamingServiceClient>(ALLOC_TAG, config);
  36.  
  37.         Aws::String transcribedResult;
  38.         StartStreamTranscriptionHandler handler;
  39.         handler.SetTranscriptEventCallback([&transcribedResult](const TranscriptEvent& ev) {
  40.             // TODO: only check the result marked as "final"
  41.             const auto& results = ev.GetTranscript().GetResults();
  42.             if (results.empty()) {
  43.                 return;
  44.             }
  45.             const auto& last = results.back();
  46.             const auto& alternatives = last.GetAlternatives();
  47.             if (alternatives.empty()) {
  48.                 return;
  49.             }
  50.             transcribedResult = alternatives.back().GetTranscript();
  51.         });
  52.  
  53.         handler.SetOnErrorCallback([&transcribedResult](const Aws::Client::AWSError<TranscribeStreamingServiceErrors>& err) {
  54.             // we will receive an error because the request was abruptly shutdown (via stream.Close()).
  55.             // However, we cannot delay the call to closing the stream, because HTTP clients such as libcurl buffer the
  56.             // requests before sending them over the wire, so it will keep BUFFER_SIZE bytes in its memory if we don't
  57.             // signal the completion of the stream (by closing it).
  58.             // To discern between that case and a true error, we check if we have received any text back from the service.
  59.             if(transcribedResult.empty()) {
  60.                 cerr << "Error and empty transcribed result:\n" << err.GetMessage() << "\n";
  61.             }
  62.         });
  63.  
  64.         StartStreamTranscriptionRequest request;
  65.         request.SetMediaSampleRateHertz(FREQ);
  66.         request.SetLanguageCode(LanguageCode::en_US);
  67.         request.SetMediaEncoding(MediaEncoding::pcm);
  68.         request.SetEventStreamHandler(handler);
  69.  
  70.         auto OnStreamReady = [](AudioStream& stream) {
  71.             Aws::FStream file(TEST_FILE_NAME, std::ios_base::in | std::ios_base::binary);
  72.             char buf[1024];
  73.             // try to fix:
  74.             // Error and empty transcribed result:
  75.             // A complete signal was sent without the preceding empty frame.
  76.             //stream.WriteAudioEvent({}); // per the spec, we have to send an empty event (i.e. without a payload) at the end.
  77.             while(file) {
  78.                 file.read(buf, sizeof(buf));
  79.                 Aws::Vector<unsigned char> bits{buf, buf + file.gcount()};
  80.                 AudioEvent event(std::move(bits));
  81.                 if (!stream) {
  82.                     break;
  83.                 }
  84.                 if (!stream.WriteAudioEvent(event)) {
  85.                     break;
  86.                 }
  87.             }
  88.             // per the spec, we have to send an empty event (i.e. without a payload) at the end.
  89.             stream.WriteAudioEvent({});
  90.             stream.flush();
  91.             stream.Close();
  92.         };
  93.  
  94.         Aws::Utils::Threading::Semaphore semaphore(0, 1);
  95.         auto OnResponseCallback = [&semaphore](
  96.             const TranscribeStreamingServiceClient*,
  97.             const StartStreamTranscriptionRequest&,
  98.             const StartStreamTranscriptionOutcome&,
  99.             const std::shared_ptr<const Aws::Client::AsyncCallerContext>&)
  100.         {
  101.             semaphore.ReleaseAll();
  102.         };
  103.  
  104.         client->StartStreamTranscriptionAsync(request, OnStreamReady, OnResponseCallback, nullptr /*context*/);
  105.         semaphore.WaitOne();
  106.         cout << "transcribed result:\n" << transcribedResult << "\n";
  107.     }
  108.     Aws::ShutdownAPI(options);
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement