0

First post. Let me know if I break any rules!

I would like to experiment with Pure Data and Unity 5. It seems uPD would be the best fit. I have followed the instructions on the github page and loaded the first sample scene. The unity console reports errors with the dll:

Failed to load 'Assets/Magicolo/AudioTools/PureData/Plugins/libpdcsharp.dll', expected 64 bit architecture (IMAGE_FILE_MACHINE_AMD64), but was IMAGE_FILE_MACHINE_I386. You must recompile your plugin for 64 bit architecture.

So I cloned the main libpd repo and used the included batch file to build an x64 dll. I replaced the libpdcsharp.dll in the unity project and the console now reports:

DllNotFoundException: Assets/Magicolo/AudioTools/PureData/Plugins/libpdcsharp.dll
LibPDBinding.LibPD.ProcessArgs[Int32] (System.Int32[] args, System.String& debug) (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMessaging.cs:402)
LibPDBinding.LibPD.SendMessage[Int32] (System.String receiver, System.String message, System.Int32[] args) (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMessaging.cs:354)
(wrapper synchronized) LibPDBinding.LibPD:SendMessage (string,string,int[])
LibPDBinding.LibPD.ComputeAudio (Boolean state) (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:238)
(wrapper synchronized) LibPDBinding.LibPD:ComputeAudio (bool)
LibPDBinding.LibPD.Release () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:207)
(wrapper synchronized) LibPDBinding.LibPD:Release ()
LibPDBinding.LibPD.ReInit () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:79)
(wrapper synchronized) LibPDBinding.LibPD:ReInit ()
LibPDBinding.LibPD..cctor () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:65)
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for LibPDBinding.LibPD
Magicolo.AudioTools.PureDataBridge.SetAudioSettings () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:46)
Magicolo.AudioTools.PureDataBridge.StartLibPD () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:25)
Magicolo.AudioTools.PureDataBridge.Start () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:50)
PureData.StartAll () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:141)
PureData.Initialize () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:58)
PureData.Awake () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:173)

I have also tried copying the libPDBinding.dll file from the main libpd repo to the unity project no avail. I lack the understanding to know where to go next so any help at all would be appreciated!

Further Progress

It was suggested to try NuGet to obtain the binding dll. On this - I get an error about a framework mismatch between my unity project/visual studio solution and the nuget package libPDBinding version 0.10.0. This is to do with unitys profiles as described in an article titled: "Using NuGet in Visual Studio Tools for Unity". I cant post the link here because I am too new.

However, I think I am sucessful in compiling my own libPDBinding.dll. Apologies for dragging this out, just making sure I am doing this right: After using libPD\mingw64_build_csharp.bat to compile a x64 version of libpdcsharp.dll, I reference this and libpd\libs\mingw64\libwinthread-1.dll in the VS solution and get the resulting LibPDBinding.dll which was built with .NET 3.5. As mentioned libpdcsharp.dll was compiled using the batch/make file, so am I correct to assume this file has been built with the correct settings?

I proceed to copy these 3 dll's the unity assets folder For reference it's a blank unity project with uPD as the only package, loaded to a uPD example scene.

I still get some errors at run time and some visual studio warnings as follows.

Unity run time error:

EntryPointNotFoundException: libpd_safe_init
LibPDBinding.LibPD.ReInit () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:81)
(wrapper synchronized) LibPDBinding.LibPD:ReInit ()
LibPDBinding.LibPD..cctor () (at Assets/Magicolo/AudioTools/PureData/LibPD/LibPDNativeMethods.cs:65)
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for LibPDBinding.LibPD
Magicolo.AudioTools.PureDataBridge.SetAudioSettings () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:46)
Magicolo.AudioTools.PureDataBridge.StartLibPD () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:25)
Magicolo.AudioTools.PureDataBridge.Start () (at Assets/Magicolo/AudioTools/PureData/PureDataBridge.cs:50)
PureData.StartAll () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:141)
PureData.Initialize () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:58)
PureData.Awake () (at Assets/Magicolo/AudioTools/PureData/PureData.cs:173)

Visual studio warnings x 50 or so:

Warning CS0436  The type 'LibPD' in 'D:\unity projects\libpdinunity\Assets\Magicolo\AudioTools\PureData\LibPD\LibPdNativeUnity.cs' conflicts with the imported type 'LibPD' in 'LibPDBinding, Version=0.3.0.177, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'D:\unity projects\libpdinunity\Assets\Magicolo\AudioTools\PureData\LibPD\LibPdNativeUnity.cs'.   libpdinunity.CSharp D:\unity projects\libpdinunity\Assets\Magicolo\AudioTools\PureData\PureDataCommunicator.cs  87  Active

Thanks again for any help. Much appreciated.

Rick
  • 1
  • 3
  • Just a note @Rick, if you're an experienced developer new to Unity. For something like uPD you have to use it in a "manager" class (by manager class, I simply mean something like - imagine your sound effects manager or scores manager - which is available for everything in all scenes of the Unity project). To do that in Unity, you do this ... http://stackoverflow.com/a/35891919/294884 – Fattie Oct 25 '16 at 20:53
  • Thanks for the tip @JoeBlow. This is good to know. So, for now, I bypassed LibPD and any derivatives and have started to play with [Heavy](https://enzienaudio.com/) which interprets PD patches and provides source to compile your own DLL's. I think I need more experience before I try to tackle and debug LibPD packages. – Rick Oct 30 '16 at 04:42

3 Answers3

0

Looking at your error, libpdcsharp is currently placed at <ProjectDirectory>/Assets/Magicolo/AudioTools/PureData/Plugins/

This DLL file should be placed at <ProjectDirectory>/Asset.

Also, make sure that libpdcsharp is compiled with .NET 2.0 or 3.5. This won't work if you compile it with .NET 4.0 and above.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • This helped. I no longer have the DLL not found exception an the LibPDBinding.dll was being built with .Net 4.x. I am still having issues getting it to run. I have added more detail about the remaining error. above. Thanks. – Rick Oct 25 '16 at 15:45
0

I am the maintainer of C# binding for LibPD.

You also need to change the link to the correct libwinpthread-1.dll version (64 bit instead of 32). In your final project you need all 3 dlls:

  • LibPDBinding.dll (the managed wrapper)
  • libpdcsharp.dll (the native dll)
  • libwinpthread-1.dll (for enabling POSIX threads on Windows)

Also: Have you tried the version from NuGet? No need to compile your own version of the binding.

EDIT: Your warning are from a different source: You have two classes named LibPD, and the C# compiler does not know, which one to use.

I am not sure, if these types are in the same namespace.

If they are in the same namespace, then change your namespace.

If they are in different namespaces, add a line using LibPd = LibPDBinding.LibPD; if you want to use the version from NuGet, or using LibPd = <yournamespace>.LibPD; if you want to use your version.

Residuum
  • 11,878
  • 7
  • 40
  • 70
  • I have added some more detail to the end of my last post. Including all the DLL's did reduce the errors but It is still failing at runtime. Let me know if you can help further. Thanks! – Rick Oct 25 '16 at 15:43
  • You are right. This led me to find the answer. I should have realized the uPD creator essentially created their own binding code for libpd, thus there were conflicting namespaces. After I deleted the LibPDBinding.dll from the project, I of course no longer had the conflicting type error. the only error left was "entry point not found" so that led me to check the specified entry point which turned out to be incorrect. Or at least it was not letting the library function. – Rick Oct 25 '16 at 16:48
0

It's working. The uPD github code has an error as far as I can tell. So hopefully it gets fixed. In the meantime I just started the process from scratch and this is what I did to get the uPD test scene working in unity 5:

  1. Get uPD from github. Theres a unity package in there
  2. Get libPD from github.
  3. Follow the instructions on the libPD github page to compile a x64 vresion of libpdcsharp.dll
  4. replace the existing libpdcsharp.dll inside your unity project with the x64 version you compiled
  5. locate libpd\libs\mingw64\libwinthread-1.dll in the libpd files and place this in the your unity project - the same place as the other dll is fine. I don't think it matters where you put the Dll's (at least it made no difference in my tests)
  6. Run the project and notice you get an error in unity EntryPointNotFoundException: libpd_safe_init
  7. double click the error to open up the problem script.
  8. Find the following line of code: [DllImport("libpdcsharp", EntryPoint="libpd_safe_init")]
  9. Change it to [DllImport("libpdcsharp", EntryPoint="libpd_init")] That's the real entry point for the init function in libpdcsharp. I don't know what libpd_safe_init is.

Thanks to the contributors who helped me to troubleshoot and find the issue!

Rick
  • 1
  • 3
  • [`libpd_safe_init()` has been removed in 2014](https://github.com/libpd/libpd/pull/54/files). It seems that uPD is not up to date with the libPd API. As a side note: I am currently proposing an update to the C# API, and will start in the near future with the work, so that the binding will be more idiomatic C#. Feel free to give feedback: https://github.com/libpd/libpd/issues/153 – Residuum Oct 25 '16 at 16:55