【问题标题】:IBM Watson TextToSpeech - cannot read property .pipe of undefinedIBM Watson TextToSpeech - 无法读取未定义的属性 .pipe
【发布时间】:2018-12-03 21:33:14
【问题描述】:

我有以下代码,直接来自文档:

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

var textToSpeech = new TextToSpeechV1({
iam_apikey: '---myapikey---',
url: 'https://stream.watsonplatform.net/text-to-speech/api/'
});

var synthesizeParams = {
text: 'Hello world, you dummy ass',
accept: 'audio/wav',
voice: 'en-US_AllisonVoice'
};

// Pipe the synthesized text to a file. 
textToSpeech.synthesize(synthesizeParams).on('error', function(error) {
console.log(error);
}).pipe(fs.createWriteStream('hello_world.wav'));

当我运行它时,它会给出以下错误:

pi@raspberrypi:~/Desktop/tjbotcz_lite $ sudo node ttstest.js
/home/pi/Desktop/tjbotcz_lite/ttstest.js:16
textToSpeech.synthesize(synthesizeParams).on('error', function(error) {
                                         ^

TypeError: Cannot read property 'on' of undefined
    at Object.<anonymous> (/home/pi/Desktop/tjbotcz_lite/ttstest.js:16:42)
    at Module._compile (internal/modules/cjs/loader.js:654:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
    at Module.load (internal/modules/cjs/loader.js:566:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
    at Function.Module._load (internal/modules/cjs/loader.js:498:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
    at startup (internal/bootstrap/node.js:201:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:516:3)

任何线索为什么?我对 TJBot 也有同样的问题,所以我从文档中尝试了一个简单的例子,瞧——同样的错误。 当我使用旧服务(使用用户名和密码,而不是 api 密钥)时,它工作正常。我有新版本的 watson-cloud 库 (3.13.1)。

感谢您的任何提示。 问候, 一月。

【问题讨论】:

    标签: text-to-speech ibm-watson


    【解决方案1】:

    我在这里寻求帮助,但那里的文档肯定是错误的。我可以通过使用audio.result.pipe(fs.createWriteStream('audio.wav')

    来解决这个问题

    【讨论】:

    • 成功了!一般来说,我发现在使用流时,记录响应,你认为是流的结果是 JSON 对象,其中一个属性是流,response.dataresponse.results 等。跨度>
    【解决方案2】:

    我使用 Promise 实现了这一点,因此我可以等待音频文件完全保存,然后进行进一步处理。此外,正如其他人所说,文档是错误的,正确的方法是使用 audio.result.pipe() 而不是 audio.pipe()。

    function synthesize_audio(text, mp3Path) {
        return new Promise((resolve, reject) => {
        console.log(`> Synthesizing audio from text: "${text}"`)
    
        const textToSpeech = new TextToSpeechV1({
          authenticator: new IamAuthenticator({
            apikey: apikey,
          }),
          url: apiUrl,
        });
    
        const synthesizeParams = {
          text: text,
          accept: 'audio/mp3',
          voice: 'pt-BR_IsabelaV3Voice',
        };
    
        textToSpeech.synthesize(synthesizeParams)
        .then(audio => {    
          audio.result
                .pipe(fs.createWriteStream(mp3Path))
                .on('finish', resolve)
                .on('error', reject);
        })
        .catch(err => {
          console.log('error:', err);
        });
      });
    }
    

    然后你可以等待并做你想做的事:

     await synthesize_audio(text, outputPath)
      .then(() => {
        //then you can safely do what you want
      })
    }
    

    【讨论】:

      【解决方案3】:

      以下代码适用于我并使用 Text-to-Speech API 密钥生成 audio.wav 文件

      var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
      var fs = require('fs');
      
      var textToSpeech = new TextToSpeechV1({
        iam_apikey: '<API_KEY>',
        url: 'https://stream.watsonplatform.net/text-to-speech/api'
      });
      
      var synthesizeParams = {
        text: 'Hello world',
        accept: 'audio/wav',
        voice: 'en-US_AllisonVoice'
      };
      
      textToSpeech
        .synthesize(synthesizeParams, function(err, audio) {
          if (err) {
            console.log(err);
            return;
          }
          textToSpeech.repairWavHeader(audio);
          fs.writeFileSync('audio.wav', audio);
          console.log('audio.wav written with a corrected wav header');
      });
      

      更新了代码 sn-p 并且可以正常工作

      【讨论】:

      猜你喜欢
      • 2017-03-09
      • 1970-01-01
      • 2020-04-06
      • 2020-10-30
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多