Re: [Csnd-dev] Unity

Steven Yi <[email protected]>
Newsgroups gmane.comp.audio.csound.devel
Message-ID <CANtcCs7c75v-KvzXFR4fc9MiSxMKtwt8J2uTgRpDLJv1ef7GJQ@mail.gmail.com>
Webassembly Csound already compiles to a single JS file that embeds
the WASM binary into it. I assume this would work with Unity but am
unsure. I think you would only need the dist/csound.js file. You could
also try using the one from 6.18.7:

https://unpkg.com/browse/@csound/[email protected]/

It might be possible to rename it .jspre and create a test .jslib that
uses the Csound API.

On Sat, Jun 15, 2024 at 2:02 PM Giovanni Bedetti
<[email protected]> wrote:
>
> Quick update on this: no luck yet!
>
> Sorry for the long email that follows. Any help would be much appreciated!
>
>
> Unity uses Emscripten for the WebGL build.
>
> There are 2 options using Emscripten:
>
> Compile Csound as a static library to be used as Unity plug-in
> First thing I tried the old emscripten build for Web Csound from the master branch (6.18.1), but I couldn't build it on macOs with this error message:
>
> Error: Could not resolve './libcsound' from src/CsoundScriptProcessorNode.js
>
>
> I also tried older versions of emscripten, but I cannot install the ones older than version 2.0, so no luck there.
> This should be the preferred way of creating the required files for Unity.
>
> The supported native plug-in file formats are
>
> LLVM Bitcode file format (.bc)
> GNU archive file format (.a)
>
> I noticed that the Csound wasm build was exporting a file with that extension so I went this route.
> I was able to build the fix-csound-wasm branch (off CS7) and I can confirm that it works on macOS (commit bac483d).
> I ran yarn build in the wasm folder.
> In the wasm/lib folder I could find a "libcsound-wasm.a" file, that will be accepted by Unity as a native WebGL plug-in.
>
> I was hoping that the .a file would be enough, but it appears that I cannot reach its methods from C#, I guess I need some js glue code with the explicit exports.
> In fact, If I try to build the Unity project with only the above .a file it builds ok, but as soon as I import the CsoundUnity package (with a little change to try and load the library for WebGL) I get loads of build errors (one for each native method we have, like "error: undefined symbol: csoundAddSpinSample (referenced by top-level compiled C/C++ code").
>
> So I started to wonder if I needed the wasm/browser part to be able to use the above library.
> I tried to build the browser package. First of all I ran yarn link "@csound/wasm-bin" to link it with the previously built lib, then yarn build.
> When built, it creates a "dist" folder, with several .js and .map files with the name starting with "__compiled", like "__compiled.vanilla.worker.js", "__compiled.vanilla.worker.inline.js", "__compiled.vanilla.worker.js.map", together with "__csound_wasm.inline.js", "csound.js" and "csound.js.map".
> I'm not sure how to import those files in Unity, I have very little experience on the WebGL platform.
> Unity supports two JavaScript plug-in file types that let you add JavaScript code to your Unity project
>
> .jslib A JavaScript library to use in addition to those in Emscripten’s core libraries: emscripten.org/docs/tools_reference/emcc.html#emcc-js-library
> .jspre A a file whose contents are added before the emitted code: emscripten.org/docs/tools_reference/emcc.html#emcc-pre-js
>
> Furthermore, Unity expects this syntax to retrieve the native methods that can be used in C#:
>
> const hello = {
>     Hello: function() {
>         window.alert("Hello, world!");
>     }
> };
> mergeInto(LibraryManager.library, hello);
>
> to be called in C# like this:
>
> [DllImport("__Internal")]
>     private static extern void Hello();
>
> Bundle the C/C++ code in the Unity project
> This option is very undocumented and there's no mention of any build script or makefile to edit, so at the moment I have no idea how Csound will be built by Unity.
> Out of curiosity, I tried to import some folders from the Csound repo:
> - Engine
> - H
> - include
> - InOut
> - interfaces
> - OOps
> - Opcodes
> - po
> - util
> - util1
> - util2
>
> When I build I get the following error, "Plugins colliding with each other.":
>
> Plugin 'mp3dec_internal.h' is used from several locations:
>  Assets/Plugins/InOut/libmpadec/mp3dec_internal.h would be copied to <PluginPath>/mp3dec_internal.h
>  Assets/Plugins/H/mp3dec_internal.h would be copied to <PluginPath>/mp3dec_internal.h
> Plugin 'mpadec_internal.h' is used from several locations:
>  Assets/Plugins/H/mpadec_internal.h would be copied to <PluginPath>/mpadec_internal.h
>  Assets/Plugins/InOut/libmpadec/mpadec_internal.h would be copied to <PluginPath>/mpadec_internal.h
> Plugin 'stdopcod.h' is used from several locations:
>  Assets/Plugins/Opcodes/stdopcod.h would be copied to <PluginPath>/stdopcod.h
>  Assets/Plugins/Opcodes/gab/stdopcod.h would be copied to <PluginPath>/stdopcod.h
>
> I had a look at the two mp3dec_internal.h and they are identical except for one line, the Assets/Plugins/InOut/libmpadec/mp3dec_internal.h has an additional #include "mpadec_config.h".
> I'm not sure what to try next here, I guess removing one of the two folders is too simple to be an option :D but maybe I'll try it later!
>
> There is also the option to use a pure Javascript plugin, but how to create a javascript version of Csound?
>
> Last option, Unity native audio plugins, but they can't be used for WebGL, see this comment on the official Native plugins repo
>
> Hope someone has some tips here, I'm lost in the Web(GL)!
>
>
> Il giorno ven 31 mag 2024 alle ore 10:07 Giovanni Bedetti <[email protected]> ha scritto:
>>
>> Not yet, there are limitations in the Unity FMOD implementation for the WebGL platform. Specifically, it doesn't support the OnAudioFilterRead callback, which is the function we use to fill the Unity AudioSources content with the Csound output.
>> Technically though it shouldn't be hard to import a Csound library for the Web, and use the API. The difficulty is sending the Csound content to the AudioSources, and so being able to move those sources in the 3D space and having them spatialized (Unity spatializer is first order ambisonic).
>>
>> In the next days I will try something and report here my findings.
>>
>>
>> Il Ven 31 Mag 2024, 09:29 Michael Gogins <[email protected]> ha scritto:
>>>
>>> Is there WebAssembly support for Csound in Unity yet?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.