我試圖做一些我認爲很簡單的事情。我正在使用nwjs(以前稱爲Node-Webkit),如果您不知道,這基本上意味着我正在使用Chromium開發桌面應用程序&節點,其中DOM與節點的範圍相同。我想將工作卸載到webworker,以便在我將一些文本發送到Ivona Cloud(使用ivona-node)文本到語音API時,GUI不會掛起。音頻在生成時會以塊形式返回,並寫入MP3。 ivona-node使用fs將mp3寫入驅動器。我在dom工作,但需要web工作人員不要掛上用戶界面。所以我需要在webworker,ivona-node和fs中使用兩個節點模塊。無法在WebWorker中需要節點模塊(NWJS)
問題是,在webworker中你不能使用require。所以我嘗試用browserify打包ivona-node和fs(我使用了一個名爲browserify-fs的包),並用importScripts()替換require。現在,我在節點模塊中收到了var錯誤。
注意:我不認爲native_fs_的方法可以用來將mp3寫入磁盤(流),因爲它應該是這樣,並且Ivona軟件包中也出現錯誤(實際上是首先和最重要的),我不知道如何解決。我包括所有信息來重現此。
下面是我收到的控制檯錯誤:未捕獲的SyntaxError:意外的標記VAR VM39 ivonabundle.js:23132個
- 重現步驟在NWJS:
NPM安裝IVONA節點
NPM安裝browserify-FS
NPM安裝-g browserify
- 現在我browserified爲browserify-FS爲IVONA節點main.js和index.js:
browserify main.js> ivonabundle.js
browserify index.js> fsbundle.js
的package.json ...
{
"name": "appname",
"description": "appdescr",
"title": "apptitle",
"main": "index.html",
"window":
{
"toolbar": true,
"resizable": false,
"width": 800,
"height": 500
},
"webkit":
{
"plugin": true
}
}
個index.html的...
<html>
<head>
<title>apptitle</title>
</head>
<body>
<p><output id="result"></output></p>
<button onclick="startWorker()">Start Worker</button>
<button onclick="stopWorker()">Stop Worker</button>
<br><br>
<script>
var w;
function startWorker() {
if(typeof(Worker) !== "undefined") {
if(typeof(w) == "undefined") {
w = new Worker("TTMP3.worker.js");
w.postMessage(['This is some text to speak.']);
}
w.onmessage = function(event) {
document.getElementById("result").innerHTML = event.data;
};
} else {
document.getElementById("result").innerHTML = "Sorry! No Web Worker support.";
}
}
function stopWorker() {
w.terminate();
w = undefined;
}
</script>
</body>
</html>
TTMP3.worker.js ...
importScripts('node_modules/browserify-fs/fsbundle.js','node_modules/ivona-node/src/ivonabundle.js');
onmessage = function T2MP3(Text2Speak)
{
postMessage(Text2Speak.data[0]);
//var fs = require('fs'),
// Ivona = require('ivona-node');
var ivona = new Ivona({
accessKey: 'xxxxxxxxxxx',
secretKey: 'xxxxxxxxxxx'
});
//ivona.listVoices()
//.on('end', function(voices) {
//console.log(voices);
//});
// ivona.createVoice(text, config)
// [string] text - the text to be spoken
// [object] config (optional) - override Ivona request via 'body' value
ivona.createVoice(Text2Speak.data[0], {
body: {
voice: {
name: 'Salli',
language: 'en-US',
gender: 'Female'
}
}
}).pipe(fs.createWriteStream('text.mp3'));
postMessage("Done");
}
你必須將它保存到磁盤?您可以將工作人員的箱數據發送到頂部以保存您是否真的需要,但我不明白爲什麼需要保存。 – dandavis
也許,但是我從ivona-node獲得的錯誤呢?我只是在網絡工作者那裏沒有得到他們。 – xendi
未捕獲的SyntaxError:意外的令牌var VM39 ivonabundle.js:23132 – xendi