Javascript 解析nodejs中的Watson TTS HTTP响应
由于IBM更改了Watson的身份验证方法,我们试图在代码中实现它,但我们无法使用其SDK或原始WebSocket从TTS服务接收任何数据 唯一有效的是HTTP API,它返回的响应类似于。它不是有效的json,也不是缓冲区 我们已经在nodejs SDK中打开了一个应用程序,但是现在我们想使用HTTP API 下面是如何获得类似的响应:Javascript 解析nodejs中的Watson TTS HTTP响应,javascript,node.js,text-to-speech,ibm-watson,watson-text-to-speech,Javascript,Node.js,Text To Speech,Ibm Watson,Watson Text To Speech,由于IBM更改了Watson的身份验证方法,我们试图在代码中实现它,但我们无法使用其SDK或原始WebSocket从TTS服务接收任何数据 唯一有效的是HTTP API,它返回的响应类似于。它不是有效的json,也不是缓冲区 我们已经在nodejs SDK中打开了一个应用程序,但是现在我们想使用HTTP API 下面是如何获得类似的响应: let requestPromise = require('request-promise-native'); let fs = require("fs")
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,则会出现这种情况。我们甚至尝试运行音频文件,通过这些文件修复了许多类似的问题,但也没有成功。您可以使用官方的: 然后呢
var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
var fs = require('fs');
var textToSpeech = new TextToSpeechV1({
iam_apikey: 'API_KEY',
url: 'https://gateway-syd.watsonplatform.net/text-to-speech/api/'
});
var synthesizeParams = {
text: 'How are you doing?',
accept: 'audio/wav',
voice: 'en-US_AllisonVoice'
};
textToSpeech.synthesize(synthesizeParams, function (err, audio) {
if (err) {
// do something
console.log('failure');
return;
}
fs.writeFileSync('result-audio.wav', audio);
console.log('scuccess');
});
请注意,由于调用了/v1/synthesis,将其包装到TextToSpeechV1会更改链接。您可以使用官方的: 然后呢
var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
var fs = require('fs');
var textToSpeech = new TextToSpeechV1({
iam_apikey: 'API_KEY',
url: 'https://gateway-syd.watsonplatform.net/text-to-speech/api/'
});
var synthesizeParams = {
text: 'How are you doing?',
accept: 'audio/wav',
voice: 'en-US_AllisonVoice'
};
textToSpeech.synthesize(synthesizeParams, function (err, audio) {
if (err) {
// do something
console.log('failure');
return;
}
fs.writeFileSync('result-audio.wav', audio);
console.log('scuccess');
});
请注意,将其包装到TextToSpeechV1中会更改链接,因为调用了/v1/synthesis。Nice,这很有效。但是为什么这里的例子给了我无法读取未定义属性“on”的原因呢?这与您的tsconfig.json有关。返回类型为命名空间NodeJS中的“ReadableStream”。看这里:我根本不知道那个文件,但我们没有,我们应该添加它吗?或者我们修改了包中的一个?不,没有尝试Websocket API。这取决于您的项目设置,我们使用Typescript并使用tsconfig.json.Cool配置传输。非常感谢你的帮助。很好,成功了。但是为什么这里的例子给了我无法读取未定义属性“on”的原因呢?这与您的tsconfig.json有关。返回类型为命名空间NodeJS中的“ReadableStream”。看这里:我根本不知道那个文件,但我们没有,我们应该添加它吗?或者我们修改了包中的一个?不,没有尝试Websocket API。这取决于您的项目设置,我们使用Typescript并使用tsconfig.json.Cool配置传输。非常感谢你的帮助。