【发布时间】:2018-11-07 22:57:06
【问题描述】:
如何将 IBM Watson Text-to-speech 中的音频文件(大约 10K)保存到 Firebase Cloud Storage?这是我的代码,复制自 IBM Watson documentation:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
var fs = require('fs');
exports.TextToSpeech = functions.firestore.document('Test_Value').onUpdate((change, context) => {
var textToSpeech = new TextToSpeechV1({
username: 'groucho',
password: 'swordfish',
url: 'https://stream.watsonplatform.net/text-to-speech/api'
});
var synthesizeParams = {
text: 'Hello world',
accept: 'audio/wav',
voice: 'en-US_AllisonVoice'
};
textToSpeech.synthesize(synthesizeParams).on('error', function(error) {
console.log(error);
}).pipe(fs.createWriteStream('hello_world.wav')); // what goes here?
const file = ?????
file.download()
.then(function(data) {
console.log("File downloaded."
})
.catch(error => {
console.error(error);
});
});
缺少的代码在之间
}).pipe(fs.createWriteStream('hello_world.wav'));
和
file.download()
不知何故,我必须将 IBM Watson 提供的文件转换为 Firebase Cloud Storage 可识别的文件。 fs 不允许在 Google Cloud Functions 中使用吗?
另外,第 6 行不应该是
var fs = require('fs-js');
不是
var fs = require('fs');
根据NPM,fs 包已弃用。
Google Cloud Functions 中是否允许使用 pipe?如果是这样,我将文件传送到什么?我需要看起来像这样的东西:
}).pipe(file);
file.download()
【问题讨论】:
-
我可以帮忙,但我首先有一个问题 - 你需要
file是什么类型的变量?Buffer?我主要想知道,download()函数来自哪里 - 还是那个伪代码? -
您需要从您的函数中返回一个承诺,当所有工作完成时,该承诺会解决。
pipe可能是一个异步函数,这意味着您需要开始更仔细地考虑要如何在函数中进行异步编程。 -
Doug,IBM Watson textToSpeech.synthesize 返回一个回调,而不是一个承诺。我会研究谷歌文本到语音,如果它返回一个承诺,它会更容易编码吗?
-
根据stackoverflow.com/questions/20085513/using-pipe-in-node-js-net,“[Node 中] 的 pipe() 函数在可读流可用时读取数据并将其写入目标可写流。”我想写入文件。我会看看是否有管道到文件节点命令。
-
stackoverflow.com/questions/20085513/using-pipe-in-node-js-net 中的另一个答案说“pipe() 从可读流中读取并写入可写流,就像 Unix 管道一样。它在此过程中会做所有“合理”的事情,但会出错,文件结束,如果一侧落后等。”这听起来像是异步的。
标签: node.js google-cloud-functions firebase-storage text-to-speech ibm-watson