使用AWS Polly的PCM格式

时间:2017-08-22 20:50:41

标签: aws-lambda text-to-speech amazon-polly

我正在尝试使用来自AWS lambda的JavaScript SDK(用于使用API​​网关通过REST API公开)来使用AWS Polly(用于TTS)。获得PCM输出没有问题。这是一个简短的呼叫流程。

  

.NET应用程序 - > REST API(API网关) - > AWS Lambda(JS SDK) - > AWS Polly

.NET应用程序(我也使用POSTMAN进行测试)以下列格式获取音频流缓冲区。

    [...]

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddErrorDescriber<LocalizedIdentityErrorDescriber>();

        services.AddLocalization(options => options.ResourcesPath = "Resources");

        // Your service configuration code

        services.AddMvc()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization();

        // Your service configuration code cont.
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        // your configuration code

        app.UseRequestLocalization(
            new RequestLocalizationOptions()
                {
                    DefaultRequestCulture = new RequestCulture("fr"),
                    SupportedCultures = SupportedCultures,
                    SupportedUICultures = SupportedCultures
                });

        app.UseStaticFiles();

        app.UseIdentity();

        // your configuration code
    }

    [...]

现在我不知道如何将其转换回原始PCM。我希望它将这些数据作为原始PCM发回,但无法找到方法。我也无法理解为什么AWS会以这种格式发回数据。使用控制台,可以获得原始PCM格式的音频(然后我可以将其提供给Audacity),但使用SDK时则不那么简单。或者我错过了一些非常基本的东西?

有关此问题的任何建议/提示吗?感谢。

1 个答案:

答案 0 :(得分:1)

正如Michael所提到的(在评论中),从Polly发回响应会导致流变为JSON对象。在base64中对来自Polly的接收缓冲区进行编码可以解决这个问题。以下是代码示例现在的样子 -

polly.synthesizeSpeech(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response

    //old code
    //callback(null, data.Audiostream); //this converts buffer to JSON obj
    //use below instead
    if (data && data.AudioStream instanceof Buffer) {

        var buf = data.AudioStream.toString('base64');
        callback(null, buf);
    }
});

PS:我在AWS lambda上使用AWS SDK