gogins / csound-extended

Extensions for Csound including algorithmic composition, Android app, and WebAssembly.
GNU Lesser General Public License v2.1
40 stars 1 forks source link

Build CsoundAC for WebAssembly #140

Closed gogins closed 3 years ago

gogins commented 4 years ago

Currently there are two methods of generating bindings automatically, WebIDL and Embind. Calling JavaScript from C++ is supported by Embind, and this seems to provide somewhat more functionality. I also have already used Embind for Csound itself and it works just fine.

For now, I will proceed on the assumption of using Embind. However, there are issues.

The major issue is the Eigen matrix class used by CsoundAC. This class in itself is just plain wonderful, but it has a complex interface and appears everywhere in the CsoundAC interface.

But the payoff here is huge, once done I can do all new CsoundAC code in C++ and use JavaScript only for pieces.

gogins commented 4 years ago

To do:

gogins commented 4 years ago

Actually what I really want is to use CsoundAC instead of the Silencio JavaScript stuff on Android. I don't care that much how it happens but I need to work on the same pieces in both the Android and the desktop environments. Take a look at https://github.com/kivy/python-for-android and see if that helps.

gogins commented 4 years ago

It's not clear how to configure Eigen3 for Emscripten. It was used in ammo.js by @kripken but has been replaced by custom code. It may be necessary to link or copy the Eigen directory to Emscripten's include directory. Then too I will have to rewrite or conditionally omit code that uses FLTK and perhaps some other libraries.

gogins commented 4 years ago

I created this link:

mkg@xenakis:~/emsdk/upstream/emscripten/system/include$ ls -ll
total 132
drwxr-xr-x 2 mkg mkg  4096 Apr 16 17:48 AL
drwxr-xr-x 3 mkg mkg  4096 Apr 16 17:48 compat
drwxr-xr-x 2 mkg mkg  4096 Apr 16 17:48 EGL
lrwxrwxrwx 1 mkg mkg    19 Jun  1 09:32 eigen3 -> /usr/include/eigen3
gogins commented 4 years ago

Creating the link in ~/emsdk/upstream/emscripten/system/include/libcxx did the job, so now libcsoundac-static.a compiles. Getting this to build and link with the other stuff is now the job. Also it is not clear how much work this actually will turn out to be.

For the SWIG-generated Python interface, a good deal of code wrapping pointers and references to Eigen matrices is generated. I do not need the Eigen API but I do need Eigen objects.

But probably I don't actually need the Eigen objects. I don't need them in Python although they have SWIG proxies generated for them more or less by default.

gogins commented 4 years ago

I have wrongly put CsoundAC in the CsoundAudioProcessor, it needs to go in the CsoundAudioNode. But I am not sure how this works out with the MusicModel class embedding Csound. I may have to change that.

I think this might be a big problem. Theoretically I can push it all down into the CsoundAudioProcessor but the amount of messaging clue code becomes nightmarish.

gogins commented 4 years ago

I have refactored csound::System to deal with the message printing code in a more cross-platform way. Things are now building, and I can proceed to actually wrapping stuff.

gogins commented 4 years ago

I'm running into problems with Embind. Specifically, I have properly defined CellAdd as having Cell as a base class but Embind is not seeing that.

Somehow I missed this which I will now evaluate: https://github.com/charto/nbind. Oops, looks like nbind is declining and Embind is keeping on going.

gogins commented 4 years ago

Closed by mistake...

Disabling RTTI is no help, RTTI is needed.

Going to try a test case of an inheritance chain. A chain of two derivations works fine. Try defining them out of order. Still works.

gogins commented 4 years ago

Oops, it's just I got the base class of CellAdd wrong. It's Node, not Cell.

gogins commented 4 years ago

I may now be running into this in Scale::modulations which is one of the coolest things in CsoundAC failing to compile: https://github.com/emscripten-core/emscripten/issues/9005. Or this: https://github.com/emscripten-core/emscripten/issues/9485.

gogins commented 4 years ago

Trying to build with C++ 17 now. No help.

gogins commented 4 years ago

Got rid of the class method overloads and it works. I just spell out different method names. I will try to get rid of all the overloads in the Embind definitions for CsoundAC.

gogins commented 4 years ago

I have substantially completed the Embind wrappers for CsoundAC. I had to change a number of things in the CsoundAC C++ source code to simplify this job. To do:

gogins commented 4 years ago

Can't load CsoundAC.js in NW.js due to __dirname being referenced in generated code. See https://github.com/emscripten-core/emscripten/issues/10381.

But here's a workaround, setting that variable before loading CsoundAC, in the body of the page:

<body>
<script>
try {
    var fs = require("fs");
    var __dirname = fs.realpathSync.native(".");
} catch(e) {
    console.log(e);
}
</script>
<script src="CsoundAC.js"></script>
<script src="CsoundAudioNode.js"></script>
<script>
let CsoundAC;
(async function() { CsoundAC =  await createCsoundAC(); CsoundAC.hello(); }());
</script>

This doesn't affect the loading of CsoundAC.js in the browser.

gogins commented 4 years ago

This workaround seems to have quit working.

gogins commented 3 years ago

No, the workaround is still working.