【问题标题】:how to create a blob in node.js to be used in a websocket?如何在 node.js 中创建一个要在 websocket 中使用的 blob?
【发布时间】:2015-11-10 10:38:30
【问题描述】:

我正在尝试使用 IBM 的语音到文本服务的 websocket 实现。目前我无法弄清楚如何通过连接发送 .wav 文件。我知道我需要把它变成一个 blob,但我不知道该怎么做。现在我收到以下错误:

You must pass a Node Buffer object to WebSocketConnec

-或-

Could not read a WAV header from a stream of 0 bytes

...取决于我尝试传递给服务的内容。需要注意的是,我正确地发送了开始消息,并使其进入监听状态。

【问题讨论】:

  • 能否请您在此处添加导致上述错误的一段代码?

标签: node.js websocket blob ibm-watson


【解决方案1】:

从 v1.0 开始(仍处于 beta 阶段)watson-developer-cloud npm 模块支持 websocket。

npm install watson-developer-cloud@1.0.0-beta.2

识别 wav 文件:

var watson = require('watson-developer-cloud');
var fs = require('fs');

var speech_to_text = watson.speech_to_text({
  username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
  password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE',
  version: 'v1',
});


// create the stream
var recognizeStream = speech_to_text.createRecognizeStream({ content_type: 'audio/wav' });

// pipe in some audio
fs.createReadStream('audio-to-recognize.wav').pipe(recognizeStream);

// and pipe out the transcription
recognizeStream.pipe(fs.createWriteStream('transcription.txt'));


// listen for 'data' events for just the final text
// listen for 'results' events to get the raw JSON with interim results, timings, etc.

recognizeStream.setEncoding('utf8'); // to get strings instead of Buffers from `data` events

['data', 'results', 'error', 'connection-close'].forEach(function(eventName) {
  recognizeStream.on(eventName, console.log.bind(console, eventName + ' event: '));
});

查看更多示例here

【讨论】:

    猜你喜欢
    • 2019-05-07
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2013-07-28
    • 1970-01-01
    • 2017-06-21
    相关资源
    最近更新 更多