如何在 node.js 中使用 Watson 文本转语音 api?

How to use Watson text-to-speech api in node.js?

我是使用 express 和 node.js 的初学者。我对如何使用 Watson 感到困惑 api,我几乎无法理解 documentations/apis。我只是想尝试在我的应用程序中使用 watson api。所以我就列举一下我做了什么,我卡在哪里。

所以我首先在命令行执行了这个,以获取框架。

express test

然后我使用这个命令安装了依赖项。

cd test && npm install

然后我通过命令安装了watson

npm install watson-developer-cloud

然后我将这段代码放在我的 app.js 文件中。

var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
var fs = require('fs');

var text_to_speech = new TextToSpeechV1({
    username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE', (placed my username and password)
    password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE'
});

var params = {
    text: 'Hello from IBM Watson',
    voice: 'en-US_AllisonVoice', // Optional voice
    accept: 'audio/wav'
};

// Pipe the synthesized text to a file
text_to_speech.synthesize(params).pipe(fs.createWriteStream('output.wav'));

所以我知道这意味着我正在创建一个 Watson 对象。但我不知道从这里去哪里。我只想创建一个简单的文本转语音,其中有一个文本框和一个说话按钮。

a textbox and a speak button.

您是指在网络浏览器中吗?在这种情况下,您可能想要检查 watson-speech SDK instead of the Node.js one. There's a example at https://github.com/watson-developer-cloud/speech-javascript-sdk/blob/v0.20.0/examples/static/text-to-speech.html 或多或少完全符合您的描述。

(请注意,watson-speech 浏览器 SDK 仍然需要 Node.js 中的一些服务器端代码或其他任何内容来生成身份验证令牌。请参阅 https://github.com/watson-developer-cloud/speech-javascript-sdk/tree/v0.20.0/examples

如果您出于任何原因更愿意在 Node.js 中执行此操作,请查看演示 https://text-to-speech-demo.mybluemix.net/ & https://github.com/watson-developer-cloud/text-to-speech-nodejs - 当前版本使用 Node.js SDK,然后通过 Node.js到浏览器播放。