libopenmpt emscripten/wasm into AudioWorkletProcessor

Started by jllodra, January 09, 2021, 16:55:26

Previous topic - Next topic

jllodra

#15
Built with the patch, and it works without the performance.now polyfill.

The thing is that the performance.now code is still there in the libopenmpt.js, but not called (at least not when loading and playing a song, which is what I'm doing).

Edit: had a look at https://github.com/emscripten-core/emscripten/blob/master/src/library.js#L2934 to see the actual code and why performance.now is there. Anyway, I have proved that with patch, _clock_gettime is called and that uses Date.now internally.

manx


jllodra

#17
Nice, thanks.

Regarding the random numbers, I can't see why emscripten can't rely on Math.random() when crypto is now found, do you happen to have an idea?
I understand that it has not enough entropy level from a cryptography point of view, but I wonder if this would have an impact.

https://github.com/emscripten-core/emscripten/issues/542

EDIT: OH, they USED TO fallback on Math.random, but not anymore: https://github.com/emscripten-core/emscripten/blob/master/src/library.js#L2858
I have just changed crypto to Math.random and it works, I cannot hear any difference in the music.

manx


jllodra

#19
This code at the beginning of the Module


const crypto = {
  getRandomValues: (array) => {
    for (let i = 0; i < array.length; i++) {
      array[i] = (Math.random()*256)|0;
    }
  }
};


and no need for third party polyfills...

manx

r14059 (libopenmpt 0.6 development) or r14060 (libopenmpt 0.5) might work without requiring any polyfill when using make CONFIG=emscripten EMSCRIPTEN_TARGET=audioworkletprocessor. Could you please check that?

You can either get unreleased sources from svn or git, or from tarballs at https://builds.openmpt.org/builds/auto/libopenmpt/src.makefile/ .

jllodra

Hi Manx,

will try to have a look during the weekend.

Another problem I had to overcome too is that with the old libopenmpt emscripten version (which I built 3 years ago maybe), I could do things like:

libopenmpt.Pointer_stringify(libopenmpt._openmpt_module_get_metadata(memPtr, keyNameBuffer));

With the current version, I have to do:

UTF8ToString(libopenmpt._openmpt_module_get_metadata(memPtr, keyNameBuffer));

This UTF8ToString function is not being exported using the new built strategy (ES6 module). So I had to export it myself in the generated .js code (not desirable).
Do you know where this UTF8ToString function comes from? Is it from emscripten or is it from libopenmpt?

https://emscripten.org/docs/api_reference/preamble.js.html

Seems to be defined here but unfortunately it is not exposed in the generated .js module.

jllodra

Answering to my last comment,

probably we'd need to add:

  -s "EXPORTED_RUNTIME_METHODS=['UTF8ToString']"

no idea if this will work, will test.


Saga Musix

There are several other functions that only some users may need that are not exported by default. Everyone may need a different subset of those, so I think leaving it up to the user to specify them is still a good solution. Maybe it should be documented better, but as you noticed yourself the list of potentially useful stuff also keeps changing with every Emscripten release, so it's difficult to provide a list of options.

Here's what I use on a custom libopenmpt.js build that requires all sorts of metadata extraction, before invoking make:
export CXXFLAGS="-s \"EXTRA_EXPORTED_RUNTIME_METHODS=['lengthBytesUTF8','stackAlloc','stackSave','stackRestore','stringToUTF8','Pointer_stringify','writeArrayToMemory']\""
» No support, bug reports, feature requests via private messages - they will not be answered. Use the forums and the issue tracker so that everyone can benefit from your post.

jllodra

Quote from: manx on January 20, 2021, 10:28:58
r14059 (libopenmpt 0.6 development) or r14060 (libopenmpt 0.5) might work without requiring any polyfill when using make CONFIG=emscripten EMSCRIPTEN_TARGET=audioworkletprocessor. Could you please check that?

You can either get unreleased sources from svn or git, or from tarballs at https://builds.openmpt.org/builds/auto/libopenmpt/src.makefile/ .

Excellent, it works without polyfills now.

@Saga Musix: Cool, that helped. Thanks!

I think all set with this issue.