libpd4unity
libpd4unity copied to clipboard
Error compiling LibPdFilterRead.cs
I'm getting a compilation error for this line:
public void OnAudioFilterRead (float[] data, int channels)
{ `
if(dataPtr == IntPtr.Zero)
{
dataHandle = GCHandle.Alloc(data,GCHandleType.Pinned);
dataPtr = dataHandle.AddrOfPinnedObject();
}
if (islibpdready) {
LibPD.Process(numberOfTicks, dataPtr, dataPtr);
}
}
The error is:
Assets/LibPdFilterRead.cs(52,31): error CS1502: The best overloaded method match for `LibPDBinding.LibPD.Process(int, double_, double_)' has some invalid arguments
Assets/LibPdFilterRead.cs(52,31): error CS1503: Argument
#2' cannot convert
System.IntPtr' expression to type `double*'
I'm not familiar with C#, but I'd try commenting out the if(dataPtr)
block and change the dataPtr's to data in the libPD.Process call.
Ping @residuum
Thanks dan!
Understood about commending out the if(dataPtr)
block.
Not sure what you mean by
change the dataPtr's to data in the libPD.Process call.
It looks like LibPD.Process is expecting arguments of type (int, short[], short[]) whereas the problem line in this code is passing it (int, IntPtr, IntPtr)
Are you suggesting that I try to cast thedataPtr
to short[]
?
There are multiple, overloaded Process functions, 1 of which takes float[] arrays, so I imagine you can just use the data variable directly.
Isn't this line try to use the dataPtr
variable "directly":
LibPD.Process(numberOfTicks, dataPtr, dataPtr);
?
Short answer:
Change it to LibPD.Process(numberOfTicks, data, data);
and see if it works.
Long answer:
I don't know the full context of the code, I'm only looking at your screenshot.
I see the Unity audio callback provides samples using a float[] data
variable. I see the LibPDBinding.Process function is being called with some sort of conversion variable called dataPtr
, for what reason I don't know. I see there is an error based on the type used by dataPtr
in the Process function.
My next thought is "well, maybe try using data
directly instead of converting via the dataPtr
variable" since I know libpd can process samples using different data types like float, short, double, etc. Maybe the unity wrapper has some reason for coverting to shorts? I dunno, you'll have to look around in the code.
That works!
Thank you dan.
Cool. That was a guess, really.
Great guess; I still have issues with the architecture of the libs:
Couldn't open /Users/ali/Google Drive/Development/Unity/libpd4unity-starter/Assets/Plugins/libpdcsharp.bundle/Contents/MacOS/libpdcsharp, error: dlopen(/Users/ali/Google Drive/Development/Unity/libpd4unity-starter/Assets/Plugins/libpdcsharp.bundle/Contents/MacOS/libpdcsharp, 2): no suitable image found. Did find: /Users/ali/Google Drive/Development/Unity/libpd4unity-starter/Assets/Plugins/libpdcsharp.bundle/Contents/MacOS/libpdcsharp: mach-o, but wrong architecture
It's probably built for 32bit and you may be running 64 bit. Try downloading libpd and building the csharp lib yourself or grabbing one of the pre built binaries on NuGet.
You can check the arches of a file using the file
command, like so:
file /Users/ali/Google Drive/Development/Unity/libpd4unity-starter/Assets/Plugins/libpdcsharp.bundle/Contents/MacOS/libpdcsharp
Some thoughts:
-
The parameters of(see below)LibPd.Process()
are: ticks, inBuffer and outBuffer, so setting inBuffer and outBuffer to the same variable is probably not what you want. -
LibPd.Process()
has several overloads, all include inBuffer and outBuffer to be of the same type. It can be arrays or pointer types, but notIntPtr
, because then the library cannot know what function in the native library to call (libpd_process_short
,libpd_process_float
orlibpd_process_double
). There isProcess(int, short[], short[])
,Process(int, short*, short*)
,Process(int, float[], float[])
,Process(int, float*, float*)
,Process(int, double[], double[])
andProcess(int, double*, double*)
. - If you use a 64 bit system and have downloaded the NuGet package, then you must include the correct dll, you must follow the advice from the NuGet readme: https://github.com/libpd/libpd/blob/master/csharp/readme.txt
The parameters of LibPd.Process() are: ticks, inBuffer and outBuffer, so setting inBuffer and outBuffer to the same variable is probably not what you want.
Considering the Unity audio callback only provides a float array, I assume it's intended for both aka read the input samples, process, then overwrite with the output samples. This is also how the AudioUnit callback in the Obj-C wrapper works as well.
I have just looked at the code: It is handled in #define PROCESS(_x, _y)
: Before calling the Pd functions, inBuffer
gets copied to a pointer, and after the Pd function, the result is copied over to outBuffer
, so that is no problem.
@batchku did you manage to get the architecture issue fixed?
Did anyone solve this problem of architecture? I am on Mac using Unity 2017.2 and getting the same errors @batchku. @danomatika how can I build it myself? I don't really understand this, is this the same for Mac, is there anything that can tell me what the"building C sharp lib in 32bit" process is? Thanks in advance
Depending on your IDE, you can specify whether to build a .NET or Mono project in 32 or 64 bit. This is what it looks like in Monodevelop.
@danomatika how can I build it myself?
I don't know, I have not worked with C#. I can only troubleshoot to a certain extent. The libpd and the C# wrapper can be built using the libpd makefile and I imagine you would need to add the 32 bit compiler flags to force a 32 bit build as clang builds 64 bit by default on macOS.
@residuum Do we need to add a macOS section to the readme? I see info about Windows and Linux only.
Well, I do not own a Mac, and I have no idea about the preferred way to compile C# projects on Mac OS. It is probably similar to Mac, but you need to compile your own .dylib
first via make csharplib
and then use the steps similar to the Linux version, but instead use the dylib instead.
Thanks so much for your responses!
I will look into this and feedback. I feel Libpd is such a good system still despite Heavy workflow becoming more popular and the uDP and OSC methods. If I can crack this then I can continue to work in this method in later version os Unity with the advantages of 2017 Unity.
Many thanks will report back on my MacOS, high Sierra, Unity 2017 64bit.dll compiling method to use LibPD4Unity!
Cheers
Hey guys, Ive spent the last two weeks trying to figure this out after you Kindly offering up extra info.
I'm Working on MacOS High Sierra, Unity 2017.2.0f3, & Visual Studio Pd Vanilla
The @batcku problem was solved with @danomatika answer earlier in the thread "Change it to LibPD.Process(numberOfTicks, data, data); and see if it works"
Then in comes the .dll error - wrong architecture
"/Users/ali/Google Drive/Development/Unity/libpd4unity-starter/Assets/Plugins/libpdcsharp.bundle/Contents/MacOS/libpdcsharp: mach-o, but wrong architecture"
or in unity window just .dll exception error in the debug log window.
After going through a lot of documentation and looking at various methods after your advice I first tried to use terminal and pull the correct full version of libpd then use - makefile to try and compile the csharplib myself, hoping somehow id get it into the right 64bit architecture.
in terminal I got this error:
Richards-MacBook-Pro:libpd richardhemming$ make csharplib
make: *** No rule to make target pure-data/src/d_arithmetic.o', needed by
libs/libpdcsharp.dylib'. Stop.
Richards-MacBook-Pro:libpd richardhemming$ cd csharp
Richards-MacBook-Pro:csharp richardhemming$ ls
Hooks.cs LibPDNativeMessaging.cs Properties
LibPDBinding.csproj LibPDNativeMethods.cs README.txt
LibPDBinding.nuspec LibPDNativeMidi.cs app.config
LibPDBinding.sln Managed
LibPDBinding.userprefs Native
Richards-MacBook-Pro:csharp richardhemming$ make csharplib
make: *** No rule to make target `csharplib'. Stop.
Richards-MacBook-Pro:csharp richardhemming$
As you can see if tried to change into the directory also (I am just guessing at this point and can't find much info on what i'm actually doing with this bit.
I then discovered NuGet. Already in the right architecture apparently at https://www.nuget.org/packages/LibPdBinding
I tried to install this through terminal but again I failed in what I was actually doing. I do not know the commands well enough and still unsure about how to replace the .dll in existing git download of libpdforunity with this weird file of libpdbinding.0.10.0.nupkg.
As @residuum Kindly pointed out you can use Mono to convert/work with this file. Cos I have MacOS High Sierra mono doesn't work so I have downloaded visual code. This doesn't seem to have the capability I need and I can't find method anywhere for this. Im getting really out of my depth here and correct me if I am wrong but I just need what seems to eb a 64 bit version of the:
/plugins folder. (of libpdforunity)
as this contains
/libpdcsharp.bundle libpdcsharp.dll pthreadGC2.dll
These seem to be the culprit files that im trying to change the architecture of.
I found a port of LibPD 64 on GitHub https://github.com/djkoloski/libpd64 from @djkoloski and got excited as I thought I could just take those particular files and drop them into the original libpdforunity hierarchy 'replacing' the problematic files. However, this seems to not be a port but identifies a method using MYSYS2 to convert the particular file in question, this is only available on windows.
All in all im at an utter loss as to what specific files I need to convert, on Mac , just the .dll, jut one, all of them? and how I actually go about that. I have tried all these different methods and as mentioned i'm not a programmer and would appreciate any further direction in solving this as I still really really believe in libpd and libpdforunity for procedural sound design work!
Many thanks in advance
Rich