watson使用套接字的文本到语音

时间:2017-10-04 11:31:14

标签: node.js text-to-speech ibm-watson

如何在使用nodejs在本地主机上运行聊天机器人时实现watson文本到语音?

我的聊天机器人已在localhost上运行..我想将沃森文本嵌入语音服务。我已经读过它可以通过websocket接口来完成。我对此没有任何想法

1 个答案:

答案 0 :(得分:1)

假设您有使用Node.js和Conversation Service的IBM Developers构建的Conversation Simple示例,您只需让您的应用通过使用Websocket跟踪此tutorial来提交HTTP REST请求,或者您可以使用一种语言特定的SDK,我将粘贴在下面的链接中。

因此,几个月前@kane构建了一个将对话简单示例与文本转换为语音的示例,您可以在此link中轻松找到它们。

您可以查看此commit以查看更改,并按照逻辑在您的应用程序中实现Text to Speech。您将看到上面的代码使用.env文件中的服务凭据调用Text to Speech服务,就像代码中的注释一样:

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

const textToSpeech = new TextToSpeechV1({
  // If unspecified here, the TEXT_TO_SPEECH_USERNAME and
  // TEXT_TO_SPEECH_PASSWORD env properties will be checked
  // After that, the SDK will fall back to the bluemix-provided VCAP_SERVICES environment property
  // username: '<username>',
  // password: '<password>',
});

app.get('/api/synthesize', (req, res, next) => {
  const transcript = textToSpeech.synthesize(req.query);
  transcript.on('response', (response) => {
    if (req.query.download) {
      if (req.query.accept && req.query.accept === 'audio/wav') {
        response.headers['content-disposition'] = 'attachment; filename=transcript.wav';
      } else {
        response.headers['content-disposition'] = 'attachment; filename=transcript.ogg';
      }
    }
  });
  transcript.on('error', next);
  transcript.pipe(res);
});

// Return the list of voices
app.get('/api/voices', (req, res, next) => {
  textToSpeech.voices(null, (error, voices) => {
    if (error) {
      return next(error);
    }
    res.json(voices);
  });
});

Obs。:我建议您看到Commit并按照相同的逻辑在您的应用中进行更改。

相关问题