我需要 import/require 一个节点模块——但它包含一个大于 4kb 的 wasm 文件。我的应用程序是一个电子应用程序。我怎样才能使这项工作?
I need to import/require a node module - but it contains a wasm file greater than 4kb. My application is an electron app. How can I make this work?
我正在尝试导入 essentia.js。当 运行 时,任何导入模块的尝试都会导致
"WebAssembly.Compile is disallowed on the main thread, if the buffer size is larger than 4KB. Use WebAssembly.compile, or compile on a worker thread."
我尝试创建一个 worker,但发现您无法导入节点模块。
如果 wasm 文件大于 4kb,是否可以处理 npm 导入?我该怎么做?
作为参考,我正在尝试在 Electron 应用程序上使用 essentia.js 中的函数。我通过 cmd 成功 运行 一个节点 test.js 的测试函数;没有意识到它会在 Electron 上失败。
Essentia.js 有一个教程,他们说:
Note: You shouldn't import the essentia-wasm.module.js on the main thread. The ideal way is to use it along with the AudioWorklet design pattern or with WebWorkers. Also, it is recommended to use local files instead of CDN in production.
https://mtg.github.io/essentia.js/docs/api/tutorial-1.%20Getting%20Started.html
但是,我不知道该怎么做。我一直在努力理解我必须做什么,但要接受的东西太多了。
我的目标是调用 Essentia 函数进行 bpm 和键分析 - 但我对多线程处理还很陌生。
例如,我需要 essentia.js-core - 但这个文件需要 wasm 文件。如何准备 wasm 文件以便我可以使用其他文件?这可能吗?
回想起来,电子可能不是最好的设计选择——但我们选择了熟悉。我知道 nodeJS 不是问题 - 这个应用程序本质上是在 chrome 浏览器上运行的事实。
electron 可以在其 web worker 中支持节点模块。
在 webPreferences 中,确保设置了 nodeIntegrationInWorker: true。
例如
Main.mainWindow = new Main.BrowserWindow({ width: 800, height: 600, webPreferences: {nodeIntegration: true, enableRemoteModule: true, nodeIntegrationInWorker: true} });
执行此操作后,您可能需要 worker.js 文件中的节点模块
您还必须确保您的工人的类型是这样的:
const worker = new Worker('./someWorker.js', { type: 'module' });
然后我可以执行以下操作
await worker.postMessage(audioData);
worker.onmessage = ({ data }) => {
console.log(`page got message: ${data}`);
};
它在我的工作文件中执行以下操作
essentia = require('essentia.js');
addEventListener('message', e => {
console.log(e.data);
let audio = e.data.channelData[0];
let inputSignalVector = essentia.arrayToVector(audio);
let key = essentia.KeyExtractor(inputSignalVector);
console.log("working");
postMessage(key);
});
在我的 worker 中,我使用节点模块来估计我传递的音频缓冲区的密钥。这是电子工作者需要节点模块的证明。
我正在尝试导入 essentia.js。当 运行 时,任何导入模块的尝试都会导致
"WebAssembly.Compile is disallowed on the main thread, if the buffer size is larger than 4KB. Use WebAssembly.compile, or compile on a worker thread."
我尝试创建一个 worker,但发现您无法导入节点模块。
如果 wasm 文件大于 4kb,是否可以处理 npm 导入?我该怎么做?
作为参考,我正在尝试在 Electron 应用程序上使用 essentia.js 中的函数。我通过 cmd 成功 运行 一个节点 test.js 的测试函数;没有意识到它会在 Electron 上失败。
Essentia.js 有一个教程,他们说:
Note: You shouldn't import the essentia-wasm.module.js on the main thread. The ideal way is to use it along with the AudioWorklet design pattern or with WebWorkers. Also, it is recommended to use local files instead of CDN in production. https://mtg.github.io/essentia.js/docs/api/tutorial-1.%20Getting%20Started.html
但是,我不知道该怎么做。我一直在努力理解我必须做什么,但要接受的东西太多了。
我的目标是调用 Essentia 函数进行 bpm 和键分析 - 但我对多线程处理还很陌生。
例如,我需要 essentia.js-core - 但这个文件需要 wasm 文件。如何准备 wasm 文件以便我可以使用其他文件?这可能吗?
回想起来,电子可能不是最好的设计选择——但我们选择了熟悉。我知道 nodeJS 不是问题 - 这个应用程序本质上是在 chrome 浏览器上运行的事实。
electron 可以在其 web worker 中支持节点模块。
在 webPreferences 中,确保设置了 nodeIntegrationInWorker: true。
例如
Main.mainWindow = new Main.BrowserWindow({ width: 800, height: 600, webPreferences: {nodeIntegration: true, enableRemoteModule: true, nodeIntegrationInWorker: true} });
执行此操作后,您可能需要 worker.js 文件中的节点模块
您还必须确保您的工人的类型是这样的:
const worker = new Worker('./someWorker.js', { type: 'module' });
然后我可以执行以下操作
await worker.postMessage(audioData);
worker.onmessage = ({ data }) => {
console.log(`page got message: ${data}`);
};
它在我的工作文件中执行以下操作
essentia = require('essentia.js');
addEventListener('message', e => {
console.log(e.data);
let audio = e.data.channelData[0];
let inputSignalVector = essentia.arrayToVector(audio);
let key = essentia.KeyExtractor(inputSignalVector);
console.log("working");
postMessage(key);
});
在我的 worker 中,我使用节点模块来估计我传递的音频缓冲区的密钥。这是电子工作者需要节点模块的证明。