【发布时间】:2018-12-19 12:15:21
【问题描述】:
由于 IBM 更改了 Watson 的身份验证方法,我们试图在代码中实现该方法,但我们无法使用他们的 SDK 或原始 websocket 从 TTS 服务接收任何数据。
唯一有效的是 HTTP API,它返回类似this 的响应。它不是一个有效的 json,也不是一个缓冲区。
我们已经在 nodejs SDK 中打开了issue,但我们现在想使用 HTTP API。
以下是获得类似响应的方法:
let requestPromise = require('request-promise-native');
let fs = require("fs")
let postData = {
"grant_type":"urn:ibm:params:oauth:grant-type:apikey",
"apikey":"<api_key>"
};
let opts = {
uri : "https://iam.bluemix.net/identity/token",
headers : {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json"
},
method: "POST",
form: postData
}
requestPromise(opts).then((body)=>{
let token = JSON.parse(body).access_token;
let postData = {
"text": 'Hello world',
"accept": 'audio/mp3',
"voice": 'en-US_AllisonVoice'
};
let opts = {
uri : "https://gateway-syd.watsonplatform.net/text-to-speech/api/v1/synthesize",
headers : {
"Content-Type": "application/json",
"Accept": "application/json",
// "Accept": "audio/mp3",
'Content-Length' : Buffer.byteLength(JSON.stringify(postData)),
"Authorization": "Bearer "+token
},
method: "POST",
json: postData
}
requestPromise(opts).then((body)=>{
let chunkStream = fs.createWriteStream('./audio.mp3')
let buf = Buffer.from(body, 'base64')
chunkStream.write(buf)
}).catch((err)=>{
if (err) throw err;
})
}).catch((err)=>{
if (err) throw err;
})
我们不知道如何处理该响应,并将其作为 base64 缓冲区保存到 mp3,产生损坏的音频文件,如果您将响应直接保存到文件中,或者更改了Accept 标头到 audio/mp3。我们甚至尝试通过mp3val 运行音频文件,这修复了很多类似的问题,但也没有用。
【问题讨论】:
标签: javascript node.js text-to-speech ibm-watson watson-text-to-speech