Re: [Csnd-dev] Unity
Giovanni Bedetti <[email protected]>
| Newsgroups | gmane.comp.audio.csound.devel |
|---|---|
| Message-ID | <CAO+OKxkFEDgrUi0jzY=NxhQ=oSc0_knoRugHzEi3QuKP3a=gvg@mail.gmail.com> |
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
<https://docs.unity3d.com/Manual/webgl-native-plugins-with-emscripten.html>:
- *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
<https://github.com/csound/csound/tree/fix-csound-wasm> branch (off CS7)
and I can confirm that it works on macOS (commit bac483d
<https://github.com/csound/csound/commit/bac483ddd75d8345044548d7a91424b77039c4a8>
).
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
<https://docs.unity3d.com/Manual/web-interacting-browsers-c-to-unity.html>
*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
<https://docs.unity3d.com/Manual/web-interacting-browser-js.html>,* but how
to create a javascript version of Csound?
Last option, *Unity native audio plugins*
<https://docs.unity3d.com/Manual/AudioNativeDSPPlugin.html#1-create-an-audio-plug-in-file>,
but they can't be used for WebGL, see this comment on the official Native
plugins repo
<https://github.com/Unity-Technologies/NativeAudioPlugins/issues/10#issuecomment-2106188686>
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?
>>
>