0

I'm having problem with the encode of speech input for Amazon Lex.

If i assign InputStream as null, it works, i receive the default voice answer from Lex : "How can i help you"

            var amazonLexClient = new AmazonLexClient("APPID", "APPSECRET", Amazon.RegionEndpoint.USEast1);
            var amazonPostRequest = new Amazon.Lex.Model.PostContentRequest();
            var amazonPostResponse = new Amazon.Lex.Model.PostContentResponse();
            amazonPostRequest.BotAlias = "BookTrip";
            amazonPostRequest.BotName = "BookTrip";
            amazonPostRequest.ContentType = "audio/l16; rate=16000; channels=1";
            amazonPostRequest.UserId = "user";
            amazonPostRequest.InputStream = null;

            amazonPostResponse = await amazonLexClient.PostContentAsync(amazonPostRequest);

If i try to send a recorded voice "How are you" using the encode (required by Lex : 16KHz, 8bits, 1 channel) below

            var amazonLexClient = new AmazonLexClient("APPID", "APPSECRET", Amazon.RegionEndpoint.USEast1);
            var amazonPostRequest = new Amazon.Lex.Model.PostContentRequest();
            var amazonPostResponse = new Amazon.Lex.Model.PostContentResponse();
            amazonPostRequest.BotAlias = "BookTrip";
            amazonPostRequest.BotName = "BookTrip";
            amazonPostRequest.ContentType = "audio/l16; rate=16000; channels=1";
            amazonPostRequest.UserId = "user";
            amazonPostRequest.InputStream = new MemoryStream();

            WaveFormat target = new WaveFormat(16000, 8, 1);
            WaveStream streamIn = new WaveFileReader("F:\\Whatever.wav");
            WaveFormatConversionStream str = new WaveFormatConversionStream(target, streamIn);
            WaveFileWriter.WriteWavFileToStream(amazonPostRequest.InputStream, str);

            amazonPostResponse = await amazonLexClient.PostContentAsync(amazonPostRequest);

Then it doesn't work, after about 20~25s Lex server will return null.

Amazon.Runtime.AmazonUnmarshallingException: 'Error unmarshalling response back from AWS.'
NullReferenceException: Object reference not set to an instance of an object.

enter image description here

Can anyone tell me how to encode a wav file to make it work with Amazon Lex? Btw im using Visual Studio 2017, C# with NAudio library.

GarretLR
  • 55
  • 7

2 Answers2

0

Amazon Lex expects the audio to be in PCM or Opus format (read this documentation for more details). You can refer this AI blog post from Amazon to get more information on how to PCM encode your wav audio.

Saurabh
  • 7,894
  • 2
  • 23
  • 31
0

There seems to be some kind of problem inside the AWSSDk for c# - what's happened is that the Lex service has returned a plain-text error message, and the SDK is trying to parse it as JSON. Sometimes you can dig into the exception details and find that raw response, or just use Fiddler.

DavidCC
  • 315
  • 2
  • 10