Cabbage Logo
Back to Cabbage Site

CSound Unity IOS build failure

Hi @David_Harris,
It looks like the error is related with some Environment Variable.
What settings do you have there? (Under CsoundUnity Settings)

Hi @giovannibedetti,
Not sure where to look for CsoundUnity Settings. In Xcode the Environemnt Variables are empty, in the Unity Player settings, its using IL2CPP for compiling to XCode, .Net Standard 2.1, Minimal Stripping Level.

I did try building the Csound library from source (https://github.com/csound/csound) running the build.sh script in the IOS folder, it built libcsound.a and I tried using that in the XCode project (incase some stripping via Unity had happened) which did remove a couple of the errors but XCode still complains of:

Undefined symbols for architecture arm64:
“_vDSP_fft_zrip”, referenced from:
_csoundRealFFT2 in libcsound.a(fftlib.o)
_vDSP_DCT_execute in libcsound.a(fftlib.o)
“_vDSP_create_fftsetup”, referenced from:
_csoundRealFFT2Setup in libcsound.a(fftlib.o)
“_vDSP_destroy_fftsetup”, referenced from:
_setupDispose in libcsound.a(fftlib.o)
ld: symbol(s) not found for architecture arm64

I’m way out of my depth (so this is probably nonsense) but in the csound code I can see a lot of defines wrapping the various funtion calls


I’m wondering if there could be a missing definition wrapping the referenced functions

Ah okay, I think the issue is I’m missing the accelerate framework in XCode. “_vDSP_fft_zrip()” is part of that library. I will beaver away and see how I sort that out and report back.

1 Like

I built CsoundUnity for iOS long time ago, unfortunately I have no access anymore to a developer account so I’m a bit blindfolded atm.
But I’m happy that someone is using CsoundUnity on iOS :wink:
I’m sure we will understand what’s wrong.
Can you try building the official Csound iOS project and see if it works? https://github.com/csound/csound/releases/download/6.18.0/Csound-iOS-6.18.0.zip
We can start from there.

Btw the environment settings are there (and indeed they are part of the recent updates):
image
Have a look here: http://rorywalsh.github.io/CsoundUnity/#/environment_variables

I’m wondering why those two methods are not available for arm64:

csoundLoadPlugins
csoundSetOpcodedir

FYI we are not using them on iOS when Csound is created.

The project compiled once I added in the Accelerate framework


It has to be in the General > Framework and Libraries (not the Build Phase)

Those functions _vDSP_fft_zrip, _vDSP_create_fftsetup, _vDSP_destroy_fftsetup can now be found and it compiles

1 Like

Unfortunately I’m now getting some nasty errors in the console and the app crashes, still some progress :slight_smile:

Unloading 5 Unused Serialized files (Serialized files now loaded: 0)
UnloadTime: 7.019167 ms
CSoundPerf(1495,0x1015af880) malloc: can't allocate region
:*** mach_vm_map(size=3614457856, flags: 100) failed (error code=3)
CSoundPerf(1495,0x1015af880) malloc: *** set a breakpoint in malloc_error_break to debug
--Csound version 6.18 (float samples) Oct 20 2022
[commit: f0bea4d87b49da2be7e9f1fc77826a7327cbc391]
<Logging>d__160:MoveNext()
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
CsoundUnity:Awake()

Csound zerdbfs: 1
CsoundUnity:Awake()

CsoundUnity done init, compiledOk? True
CsoundUnity:Awake()

(lldb)  

XCode shows me these compiled lines as the source of the crash

// Native function invocation
	float returnValue = reinterpret_cast<PInvokeFunc>(csoundGetSpoutSample)(___csound0, ___frame1, ___channel2);

and if I print out the ___csound0 and ___channel2 variables it tells me “the register x0 is not available

I’ve not much experience of debugging in xcode but if there’s anything that I can look into let me know, I’m in way over my head now for sure :grimacing:

Good find, this must be in the docs then!
Ideally we should add a PostProcessBuild action to add the framework to the exported XCode project.

About the error, we experienced something similar on Android. I’ll have to find a developer account to be able to test this on my side, but it could be simply related with a missing setting on the csd.
Can you check that those lines exist in the section?

sr = 44100 (or whatever sampling rate you have)
ksmps = 32 (or whatever sr/kr ratio you have - as integer, try different values here because not all ratios could be supported, 1 is a good bet)
nchnls = 2

Edit: Developer account found, will test this later this evening and get back to you :crossed_fingers:

Great. I’m using Unity 2021.3.13f.
I managed to run without crashing. I think the issue was the sampling rate being set to “0” in the project settings, I haven’t modified that so I assume it must be like that for new projects (I created a fresh project then imported CsoundUnity into it for testing).

Also I updated the sampling rate in the csd file via the Unity inspector, I think that also caused some issue as when I opened the file up in my IDE it had some non-printable character at the end, which I thne deleted.

I think you’re onto the root of the issue, modifying the sr and ksmps does seem to stop the crashing from happening, however I haven’t found a value that gets me audio generating (I haven’t tried “1” yet)

I did try creating a build post process, but it adds the framework into the “Unity-iPhone” target rather than the “UnityFramework” target, I’m attaching it incase its useful (it needs to go into an “Editor” folder). Gotta sign off today but will have another crack at it at somepoint this week.

ios_build_accelerate_framework.cs (2.1 KB)

1 Like

Hi @David_Harris, so it’s compiling, the scene runs, but you are getting no audio?

Once the Accelerate framework is added in XCode it compiles and is uploaded to my iPad. From there it seems hit and miss to either run without any audio or just crash. It does seem to depend on the settings in the csd, although the build that didn’t crash yestarday crashed today. I haven’t managed to pin down what the cause is, the crashes are always at the same point, the same as I noted above.

The only other useful bit of info is the call stack of the crash, this is the IL2CPP c++ compiled code but I think the method names are preserved from the C#

#0 0x0000000103511bd4 in csoundGetSpoutSample ()
#1 0x0000000103ba5f2c in NativeMethods_csoundGetSpoutSample_mAF972518DA7168A8264F15DE902EFD24B952AFBF [inlined] at /Volumes/samone/Projects/Unity/CSound/Builds/IOSE/Classes/Native/com.csound.unity.cpp:17174
#2 0x0000000103ba5f28 in CsoundUnityBridge_GetSpoutSample_m413A42DC0A5C6B9A7BAEBBFCCE8A7F1CDB05DF06 [inlined] at /Volumes/samone/Projects/Unity/CSound/Builds/IOSE/Classes/Native/com.csound.unity.cpp:14064
#3 0x0000000103ba5f24 in CsoundUnity_GetOutputSample_m2B3796E4F6C067CD65FC11C4F9578A0B20A3EB25 [inlined] at /Volumes/samone/Projects/Unity/CSound/Builds/IOSE/Classes/Native/com.csound.unity.cpp:6701
#4 0x0000000103ba5f24 in ::CsoundUnity_ProcessBlock_m3166840E2ADDD2197D09E05894E74F4542E3962C(CsoundUnity_t640C9ECC15E6B894414145782A4E47A1B300119F *, SingleU5BU5D_t89DEFE97BCEDB5857010E79ECE0F52CF6E93B87C *, int32_t, const RuntimeMethod *) at /Volumes/samone/Projects/Unity/CSound/Builds/IOSE/Classes/Native/com.csound.unity.cpp:10498
#5 0x00000001032beeec in il2cpp::vm::Runtime::InvokeWithThrow(MethodInfo const*, void*, void**) at /Users/bokken/build/output/unity/unity/External/il2cpp/builds/libil2cpp/vm/Runtime.cpp:567
#6 0x00000001032bed4c in il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**) at /Users/bokken/build/output/unity/unity/External/il2cpp/builds/libil2cpp/vm/Runtime.cpp:553
#7 0x0000000102a848f8 in ::scripting_method_invoke() at /Users/bokken/build/output/unity/unity/Runtime/ScriptingBackend/Il2Cpp/ScriptingApi_Il2Cpp.cpp:292
#8 0x0000000102a90438 in ::Invoke() at /Users/bokken/build/output/unity/unity/Runtime/Scripting/ScriptingInvocation.cpp:298
#9 0x0000000102bdf554 in Invoke [inlined] at /Users/bokken/build/output/unity/unity/Runtime/Scripting/ScriptingInvocation.h:72
#10 0x0000000102bdf544 in ::readCallback() at /Users/bokken/build/output/unity/unity/Modules/Audio/Public/AudioCustomFilter.cpp:367

Currently I’m testing using the “Basic Test” scene from the samples, I was using the Thermin sample but I thought there maybe some issue in the Awake setup, but both crash at exactly the same point.

I had the same results yesterday unfortunately.
I tried adding sr and different ksmps but no luck yet.
Will try again in some minutes.

So I tried building Csound for iOS and using the built libcsound.a file.
I tried calling PerformKsmps after Csound compile, but it crashes there too. So it’s not strictly related with csoundGetSpoutSample.
It is very similar to what we are experiencing on Android on 32bits, where even if we load 32bit libraries correctly it crashes on the same methods as above.
Not sure yet if it makes sense to open a Unity issue, but considering they have been so helpful with the macOS builds, maybe it’s worth a try!
The only good news I have is that I implemented a working PostProcessBuild script, it adds the Accelerate framework correctly and also sets a signing option for old XCode versions (like the one that runs on my old Macbook :D, 12.4).

It is on this branch:

https://github.com/rorywalsh/CsoundUnity.git#fix/ios_builds

I had a few attempts at compiling on different versions of Unity,
2019.2 with CsoundUnity 3.2 - same crashing error
2022.2 (b13) with CsoundUnity 3.4 - same crashing error
I also tried with 2018 but had issues with the UPM / package setup, but while trying to copy in the files directly into the Assets folder I noticed you can add the Accelerate framework from within the plugin setup in Unity, you don’t need the build post process, I can’t alter that as its a package in my builds but I guess you can modify in your working copies before pushing to git.

1 Like

More findings:
I’m able to build Csound for iOS on my old Intel MacBook Pro with XCode 12.4, but the building doesn’t complete nicely on my M1 MacBook Pro with XCode 14.1 (it doesn’t create libcsound.a), with these logs:

Fatal Error Build iOS XCode 14.1.txt (1.8 MB)

An excerpt:

** BUILD SUCCEEDED **

+ lipo -create libcsound-device.a Release/libCsoundLib.a -output ../libcsound.a
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: can't open input file: libcsound-device.a (No such file or directory)

I tried removing i386 / arm7 archs from the devices, but the build still fails.
There are two xconfig files under the iOS folder:


This is now simulator.xconfig:

ONLY_ACTIVE_ARCH=YES
ARCHS = x86_64
VALID_ARCHS = x86_64
IPHONEOS_DEPLOYMENT_TARGET = 11.0
GCC_VERSION = com.apple.compilers.llvm.clang.1_0
//DEBUGGING_SYMBOLS=NO
STRIP_INSTALLED_PRODUCT=YES
//DEBUG_INFORMATION_FORMAT = dwarf-with-dsym
DEPLOYMENT_POSTPROCESSING=YES
USER_HEADER_SEARCH_PATHS=$(inherited) /usr/local/include
GCC_PREPROCESSOR_DEFINITIONS=$(inherited) IOS=1

And this is device.xconfig:

ONLY_ACTIVE_ARCH=YES
ARCHS =  arm64
VALID_ARCHS = arm64
IPHONEOS_DEPLOYMENT_TARGET = 11.0 
GCC_VERSION = com.apple.compilers.llvm.clang.1_0
STRIP_INSTALLED_PRODUCT=YES
//DEBUGGING_SYMBOLS=NO
//DEBUG_INFORMATION_FORMAT = dwarf-with-dsym
DEPLOYMENT_POSTPROCESSING=YES
GCC_PREPROCESSOR_DEFINITIONS=$(inherited) IOS=1

If I don’t modify the two files above I get those errors:

Errors
/Users/giovanni.bedetti/Downloads/csound-6.18.0/iOS/cs6iOS/Csound.xcodeproj: warning: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 6.1, but the range of supported deployment target versions is 11.0 to 16.1.99. (in target 'CsoundLib-static' from project 'Csound')

error: The i386 architecture is deprecated. You should update your ARCHS build setting to remove the i386 architecture. (in target 'CsoundLib-static' from project 'Csound')

warning: Run script build phase 'CMake Rules' will be run during every build because it does not specify any outputs. To address this warning, either add output dependencies to the script phase, or configure it to run in every build by unchecking "Based on dependency analysis" in the script phase. (in target 'CsoundLib-static' from project 'Csound')

/Users/giovanni.bedetti/Downloads/csound-6.18.0/iOS/cs6iOS/Csound.xcodeproj: warning: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 6.1, but the range of supported deployment target versions is 11.0 to 16.1.99. (in target 'ZERO_CHECK' from project 'Csound')

error: The i386 architecture is deprecated. You should update your ARCHS build setting to remove the i386 architecture. (in target 'ZERO_CHECK' from project 'Csound')

warning: Run script build phase 'CMake Rules' will be run during every build because it does not specify any outputs. To address this warning, either add output dependencies to the script phase, or configure it to run in every build by unchecking "Based on dependency analysis" in the script phase. (in target 'ZERO_CHECK' from project 'Csound')

**** BUILD FAILED ****

Not sure if I’m getting closer to discover something, but just wanted to flag this.
Will try again later this evening!

Great I wasn’t aware of that!
We can definitely get rid of the PostProcessBuild script then!
It is only useful for old XCodes atm :wink:

Incase my failures are useful - I also tried initialising Csound later, rather than in the Awake, I added a waiting co-routine for 5 secs and then started the initialisation, just incase there was some delay in getting the audio system setup in Unity, but I still got the same crash.

@giovannibedetti Using the supplied “build.sh” in the IOS folder it built without issue for me on an Intel OSX 12.5 / Xcode 13.4.1

1 Like

One thing I just noticed in the CMake output when building it here:

Building with 32-bit floats

I think it should be 64bit floats on iOS should it not? I think we should be enabling doubles. Modify the build.sh script so that -DUSE_DOUBLE=0 becomes -DUSE_DOUBLE=1 and see what happens. I would try myself but I need to run to class…

I was thinking about this too.
We have this also!

I will remove that too and see what happens