如何在node.js中创建一个blob以用于websocket? [英] how to create a blob in node.js to be used in a websocket?
问题描述
我正在尝试使用IBM的语音转文本服务的websocket实现.目前,我无法弄清楚如何通过连接发送.wav文件.我知道我需要将其转换为blob,但不确定如何执行.现在我出现以下错误:
I'm trying to use IBM's websocket implementation of their speech-to-text service. Currently I'm unable to figure out how to send a .wav file over the connection. I know I need to transform it into a blob, but I'm not sure how to do it. Right now I'm getting errors of:
You must pass a Node Buffer object to WebSocketConnec
-或-
Could not read a WAV header from a stream of 0 bytes
...取决于我尝试传递给服务的内容.应该注意的是,我正确地发送了开始消息,并将其设置为侦听状态.
...depending on what I try to pass to the service. It should be noted that I am correctly sending the start message and am making it to the state of listening.
推荐答案
Starting from the v1.0 (still in beta) the watson-developer-cloud npm module has support for websockets.
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: '));
});
在此处中查看更多示例.
这篇关于如何在node.js中创建一个blob以用于websocket?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!