-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Working with Audio Worklet #6230
Comments
Hi, I'm following (kind of) the same path and I'm running into these issues as well. At a certain point I just tried to inline the Emscripten code in the worklet, that didn't work obviously. The console didn't give me a lot (yet), but I discovered it's not allowed to set variables outside of a function in the worklet. I stopped there because I figured it would be difficult to get my 1MB Emscripten 'blob' in 1 or more functions. Since a lot of audio applications use transpiled C code I guess it would be a nice addition. Regards, Niek |
I'm not sure this helps, but I link my C/C++ object files for use in the AudioWorkletProcessor with
and use |
@FalkorX Sure sounds like a solution! So you generate the whole worklet? Is there an example online? The code that comes out overhere starts with
Not a great start for a worklet I'd say. What would I add to the pre.js to get this to work? I tried to add the |
@niekvlessert Sorry, I don't have it online, and yes, I generate the whole worklet, no extra steps needed. My pre.js is really only the line // post.js
registerProcessor('ProcessorName', class extends AudioWorkletProcessor {
...
process(input, output) {
...
Module.process();
...
}
} If you're worried about polluting your AudioWorkletGlobalScope's namespace, you can also just use the |
@FalkorX Amazing, I never thought I would be possible to create some javascript code, put a class behind it and then just use the class. I have one issue left; I want to access the Module in the process function in the class as you said, but the Module variable does not exist in the class, only outside of it? |
That is not a problem, the Module will keep existing in the class's (and thus the process function's) closure. var Module = ...; // *
...
class Proc {
process() {
Module.func(); // refers to *
}
} |
@FalkorX My mistake, it works fine. I'm pretty sure I got undefined for Module, but it probably had something to do with the fact that I was using an old version of Emscripten and had some browser issues. This is a nice method, for me at least, thanks! |
Hey there, this approach seems to give the following error :
Where my post.js contains the following :
Any suggestions ? |
Well, not right now, more information is required. Is the whole source online somewhere? Maybe, If it's easy to fetch and is ready to build on Linux, I can try to build it for you. |
Yes, here it is : It is the AudioWorklet branch of that code base. |
I had the same (or a similar) problem: the |
I made this change - now post is correctly added. When run in the browser this is executed in test-element.html :
There is now a bug where the code doesn't seem to be executed by the addModule command :
I have updated the repo branch AudioWorklet : |
OK - got it. |
I wanted to do the same thing and I managed to get this done by using es6 modules. As suggested in #6284 I added |
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant. |
Hi, I'm running into the same problem now again. Since your conversation, defining However, an AudioWorklet is not a worker. If we still use the above used approach, then we'll run into the problem that in a Worklet If I simply compile without an environment parameter, add my
Essentially a Edit: The solution by @ArnaudBienner seems to work, requires though a browser that can handle es modules. So as long as every browser supports audioworklets and es modules, it's possible. IMO however there should be a way to do it without modules. |
Hi, What is the latest state of this issue? // in generated glue code js
if (ENVIRONMENT_IS_WORKER) {
_scriptDir = self.location.href;
} with the error Also further down it is looking for if (!(typeof window === "object" || typeof importScripts === "function")) throw new Error ("..."); Now if I don't compile it as worker environment, I get another exception because my WebAssembly uses Pthreads and Fetch API (don't even know whether it is possible to use threads from within audio worklet). |
The simple AudioWorklet C++ project has moved to here : https://github.com/flatmax/WASMAudio/tree/AudioWorklet-litelement It was working before, but unfortunately something has changed. It now returns a new error : You can check some of the old tricks to getting it working on that branch ... if you get past the problem I mentioned link back. |
@flatmax thanks, I guess it's because how |
|
Thanks for the tips!
When I use the --extern-post-js the browser can't find the libwasmaudio
module - probably because it hasn't been compiled in the browser yet.
The binded js file starts with this :
var libwasmaudio = (function() {
and this function ends with this :
})();
I would expect the browser to have executed that function which compiles
the WASM in the browser after executing the following in the webapp :
this.context = new AudioContext();
this.context.audioWorklet.addModule('libwasmaudio.js').then(() => {
But for some reason it doesn't seem to run the libwasmaudio function
from the binded libwasmaudio.js file.
…On 15/7/20 3:56 am, Alon Zakai wrote:
|--extern-post-js| is an easy way to append code outside the
modularize scope.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#6230 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFLUBYFKGPXPACTNTEEW6TR3SL47ANCNFSM4EQI6PGQ>.
|
Got it working again. I added an external post.js file which runs the module like so : libwasmaudio(); If anyone needs an AudioWorklet and Emscripten reference in the future, checkout WASMAudio (the AudioWorklet litelement branch) : |
Thanks, it works, unfortunately I still cannot use Pthreads since |
I also get problems when I add "-s ENVIRONMENT=worker" to [the bind command].(https://github.com/flatmax/WASMAudio/blob/AudioWorklet-litelement/src/Makefile.am#L39) :
The browser reports : |
Right, I had to add something like this to var self = {
location: {
href: "https://localhost:4443" // URL where the module was loaded from
}
} |
I followed this tutorial, and it worked for me to load an emscripten-compiled C++ class into an AudioWorklet: |
@boourns the SINGLE_FILE option suggested by that tutorial creates a JS file with the WASM embedded in it (no separate WASM binary). It is equivalent to use the -s WASM=0 option. The problem with this approach is that the resulting module is bigger in size (i.e. the JS + WASM files separately are smaller in size that a single JS file with WASM embedded). In addition, it runs slower than with the WASM binary as a standalone file. In my personal experience with WASM in audio worklets, the SINGLE_FILE option increases the size in around 30 %. Quoting the Emscripten FAQs: |
IIUC |
Thanks @sbc100 for the correction. Does it mean that |
The speed of It is probably more bytes over the wire because the way the binary gets encodes in the JS text. Its also fewer requests over the wire.. so maybe slower download due to less download parallelism? |
@sbc100 is there a way to reduce the JS size when using the For example, I have a Wasm binary of size 5.84 MB + ~200 KB of the JS glue code (already using It looks like a huge loss. |
Today the wasm file gets encoded as base64 here: Lines 3250 to 3253 in 9d63033
Its possible that you could use a more compact encoding perhaps. Is this related to the original issue? Are you trying to use a single file to solve some worklet related issue? If not, perhaps we should move this discussion to a separate issue? |
It seems like I'm still missing something, I followed the advice above (setting
The post-js is occupied by the glue file generated by WebIDL, which I used to create the bindings. Now, when I try to load the Module in the Worklet, I get the following error message:
What am I missing here ? |
You are using the flag: ENVIRONMENT="worker", but it seems that you are not running the wasm code inside a web worker. |
Hmm I'm running the code in an AudioWorklet, yes ... I just tried EDIT: using |
Hey guys,
I have a hard time integrating WASM + Emscripten with Audio Worklet.
He's how we tried to do it:
but this is problematic. To do that we need to import Emscripten glue-js output file in AudioWorklet context. So I've tried to do something like that: audioCtx.audioWorklet.addModule('wasm/sndt.js') but it didn't work since Emscripten is not prepared to work in the AudioWorklet context.
Best,
Jacek.
The text was updated successfully, but these errors were encountered: