使用AWS Polly的PCM格式 [英] Using PCM format of AWS Polly
问题描述
我正在尝试使用来自AWS lambda(通过使用API网关通过REST API公开)的JavaScript SDK的AWS Polly(用于TTS).获取PCM输出没有问题.这是简短的呼叫流程.
I am trying to use AWS Polly (for TTS) using JavaScript SDK from AWS lambda (which is exposed through a REST API using API gateway). There is no trouble in getting the PCM output. Here is a call flow in brief.
.NET应用程序-> REST API(API网关)-> AWS Lambda(JS SDK)-> AWS Polly
.NET application --> REST API (API gateway) --> AWS Lambda (JS SDK) --> AWS Polly
.NET应用程序(也在使用POSTMAN进行测试)获得了以下格式的音频流缓冲区.
The .NET application (am using POSTMAN too for testing) gets an audio stream buffer in following format.
{"type":"Buffer","data":[255,255,0,0,0,0,255,255,255,255,0,0,0,0,0,0,255,255,255,255,0,0,0,0,255,255,255,255,255,255,255,255,0,0,255,255,255,255,0,0,0,0,255,255,255,255,0,0,255,255,255, more such data]
现在我不知道如何将其转换回原始PCM.我想将这些数据作为原始PCM发送回去,但找不到解决方法.我也无法理解为什么AWS会以这种格式发送数据.使用那里的控制台,可以获取原始PCM格式的音频(然后可以将其馈送到Audacity),但使用SDK并非如此简单.还是我错过了一些基本的东西?
Now I don't know how to convert it back to raw PCM. I would like it send this data back as raw PCM but unable to find a way to do it. I also cannot understand why AWS would send data back in such a format. Using there console, one can get audio in raw PCM format (which I can then feed to Audacity), but not so simple with SDK. Or am I missing something really basic?
对此有任何建议/提示吗?谢谢.
Any suggestions/tips on this? Thanks.
推荐答案
正如Michael在评论中提到的那样,从Polly发回响应会导致流变成JSON对象.在base64中对从Polly接收到的缓冲区进行编码可以解决此问题.这是现在的代码示例-
As Michael mentioned (in the comment), sending the response from Polly back causes the stream to turn into a JSON object. Encoding the received buffer from Polly in base64 fixes this. Here's what code sample now looks like -
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开发工具包
PS: I am using AWS SDK on AWS lambda
这篇关于使用AWS Polly的PCM格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!