Author Topic: libopenmpt emscripten/wasm into AudioWorkletProcessor  (Read 1186 times)

Offline jllodra

  • Active artist
  • *
  • Posts: 14
Re: libopenmpt emscripten/wasm into AudioWorkletProcessor
« Reply #15 on: January 11, 2021, 11:55:41 »
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.
« Last Edit: January 11, 2021, 12:32:38 by jllodra »

Offline manx

  • OpenMPT Developers
  • *****
  • Posts: 288
Re: libopenmpt emscripten/wasm into AudioWorkletProcessor
« Reply #16 on: January 11, 2021, 12:36:23 »
Built with the patch, and it works without the performance.now polyfill.

Thanks for testing.

https://source.openmpt.org/browse/openmpt/?op=revision&rev=14045 will be in the next release of every branch.

Offline jllodra

  • Active artist
  • *
  • Posts: 14
Re: libopenmpt emscripten/wasm into AudioWorkletProcessor
« Reply #17 on: January 11, 2021, 12:47:48 »
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.
« Last Edit: January 11, 2021, 12:52:13 by jllodra »

Offline manx

  • OpenMPT Developers
  • *****
  • Posts: 288
Re: libopenmpt emscripten/wasm into AudioWorkletProcessor
« Reply #18 on: January 11, 2021, 12:50:47 »
I have added a ticket to our issue tracker for the Makefile changes: https://bugs.openmpt.org/view.php?id=1407.


Offline jllodra

  • Active artist
  • *
  • Posts: 14
Re: libopenmpt emscripten/wasm into AudioWorkletProcessor
« Reply #19 on: January 11, 2021, 12:58:07 »
This code at the beginning of the Module

Code: [Select]
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...
« Last Edit: January 11, 2021, 14:35:32 by jllodra »

Offline manx

  • OpenMPT Developers
  • *****
  • Posts: 288
Re: libopenmpt emscripten/wasm into AudioWorkletProcessor
« Reply #20 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/ .

Offline jllodra

  • Active artist
  • *
  • Posts: 14
Re: libopenmpt emscripten/wasm into AudioWorkletProcessor
« Reply #21 on: January 20, 2021, 14:12:29 »
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.

Offline jllodra

  • Active artist
  • *
  • Posts: 14
Re: libopenmpt emscripten/wasm into AudioWorkletProcessor
« Reply #22 on: January 20, 2021, 14:17:52 »
Answering to my last comment,

probably we'd need to add:

  -s "EXPORTED_RUNTIME_METHODS=['UTF8ToString']"

no idea if this will work, will test.


Offline Saga Musix

  • OpenMPT Developers
  • *****
  • Posts: 7,179
  • aka Jojo
    • Download music, samples, VST plugins: Saga Musix Website
  • Operating System: Windows 10 x64
Re: libopenmpt emscripten/wasm into AudioWorkletProcessor
« Reply #23 on: January 20, 2021, 16:44:21 »
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:
Code: [Select]
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.

Offline jllodra

  • Active artist
  • *
  • Posts: 14
Re: libopenmpt emscripten/wasm into AudioWorkletProcessor
« Reply #24 on: January 22, 2021, 15:10:25 »
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.