Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by baba · Apr 09, 2014 at 11:00 PM · pluginpluginspointer

how to properly pass a float pointer from a C library to its C# wrapper

I have a function in my custom C library which returns a pointer to a float. (This is actually an array of floats which contains audio values).

I want to use this C library (and returned float array) in Unity3d so I am writing a C# wrapper for it. How do I properly import/declare my C function in my C# script ?

my C declarations looks like this:

 float*  getAudioBuffer(mySynth_t synth);

In C# my guess is that I should declared my imported function as returning float[] like this:

 [DllImport (dll)]
 private static extern float[]  aae_getAudioBuffer(  IntPtr synth)

Is that correct? Shall I use ref keyword? Shall I use out keyword somewhere? I familiar with C++ but bot at all with C# so I do not really understand mashalling etc. I read somewhere that there is a pinning process involved

Also the data in the float array are audio data that I want to copy to Unity3d's audio buffer in OnFilterRead. So this will be called many times. I read somewhere that there is a pinning process involved and I am not sure what to do of it.

Any hint, sample code, or pointer on best practices to get a float pointer (or float array) from C and pass it (or copy it if I have to) would be appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by baba · Apr 16, 2014 at 11:44 PM

here is how I did it:

 [DllImport (dll)]
 private static extern IntPtr  getAudioBuffer(  IntPtr synth);

then to use it:

 private float[] buffer = new float[1024];
 
 void OnAudioFilterRead (float[] data, int channels)  
     {
          IntPtr buf = getAudioBuffer();
 
          Marshal.Copy(buf, buffer, 0, buffer.Length);
           int i, j;
            for ( i = 0, j=0; j < buffer.Length; i = i + channels, j++)
            { 
               data[i] = buffer[j];
               if (channels ==2)
                 { data [i + 1] = data [i]; }
            }        
     }

Any hint on how to do it more efficiently would be welcome.

Some good reads: here, here and also here.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iamau5 · Jun 28, 2017 at 11:28 AM 0
Share

O$$anonymous$$G This is exactly what Im looking for, Thanksss!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detecting a word clicked on in 3D text? 1 Answer

Too many method references; max is 65536. 4 Answers

Anyway to workaround .NET 4.0 framework outside/inside Unity? 1 Answer

Sharing screenshot on Facebook 2 Answers

I am using these Plugins --> Unity Admob Mediation (Applovin, Unity Ads, Facebook Ads, Inmobi) + Unity IAP + Facebook Analytics + GPGS 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges