简体   繁体   English

如何在 node.js 中使用 Watson text-to-speech api?

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

Im a beginner at using express and node.js.我是使用 express 和 node.js 的初学者。 I am confused on how to use Watson api and I can barely understand the documentations/apis.我对如何使用 Watson api 感到困惑,我几乎无法理解文档/api。 I just want to be able to try using the watson api to my application.我只是希望能够尝试在我的应用程序中使用 watson api。 So I will just enumerate what I did and where I got stuck.所以我只会列举我做了什么以及我在哪里卡住了。

So first I executed this at the command line, to get the framework.所以首先我在命令行执行这个,以获得框架。

express test

Then I did installed the dependencies using this command.然后我确实使用此命令安装了依赖项。

cd test && npm install

Then I installed watson via the command然后我通过命令安装了 watson

npm install watson-developer-cloud

Then I placed this code at my app.js file.然后我将此代码放在我的 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'));

So I know that means I am creating a Watson object.所以我知道这意味着我正在创建一个 Watson 对象。 But I do not know where to go from here.但我不知道从哪里开始。 I just want to be able to create a simple text to speech, wherein there is a textbox and a speak button.我只想能够创建一个简单的文本到语音,其中有一个文本框和一个说话按钮。

a textbox and a speak button.一个文本框和一个说话按钮。

Do you mean in a web browser?你的意思是在网络浏览器中? In that case, you probably want to checkout the watson-speech SDK instead of the Node.js one.在这种情况下,您可能想要查看watson-speech SDK 而不是 Node.js。 There's a example at https://github.com/watson-developer-cloud/speech-javascript-sdk/blob/v0.20.0/examples/static/text-to-speech.html that does more or less exactly what you're describing. https://github.com/watson-developer-cloud/speech-javascript-sdk/blob/v0.20.0/examples/static/text-to-speech.html上有一个示例,它或多或少地完全符合您的要求重新描述。

(Note that the watson-speech browser SDK still requires some server-side code in Node.js or whatever to generate auth tokens. See https://github.com/watson-developer-cloud/speech-javascript-sdk/tree/v0.20.0/examples ) (请注意,watson-speech 浏览器 SDK 仍然需要 Node.js 中的一些服务器端代码或其他代码来生成身份验证令牌。请参阅https://github.com/watson-developer-cloud/speech-javascript-sdk/tree/ v0.20.0/示例

If you would prefer to do it in Node.js for whatever reason, check out the demo https://text-to-speech-demo.mybluemix.net/ &https://github.com/watson-developer-cloud/text-to-speech-nodejs - the current version uses the Node.js SDK and then sends the audio through Node.js to the browser for playback.如果您出于某种原因更喜欢在 Node.js 中执行此操作,请查看演示https://text-to-speech-demo.mybluemix.net/https://github.com/watson-developer-cloud/ text-to-speech-nodejs - 当前版本使用 Node.js SDK,然后通过 Node.js 将音频发送到浏览器进行播放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM