Quantcast
Channel: Questions in topic: "dll"
Viewing all articles
Browse latest Browse all 706

Using a fixed size pointer in Unity C# with a C++ external function

$
0
0
I am porting a synthesis language written in C++ into Unity as a dylib. The function that writes in audio takes in a pointer and then writes the samples onto the buffer. [DllImport ("librtcmix_embedded")] unsafe private static extern int RTcmix_runAudio(void* k, void *outAudioBuffer, int nframes); The outAudioBuffer is the pointer to where the samples should be written and nframes is the buffersize. Ideally I'd like to give this function a pointer and take all the resulting samples and write them onto the data[] of OnAudioFilterRead. OnAudioFilterRead has a buffer of 2048, but I'm really not sure of how to either feed the externed function a properly sized pointer or access the new information in order to write to data[]. Attempts to do this with fixed and various structs have just crashed unity. Here's my current crashing code. unsafe struct FixedBufferExample { public fixed int _buffer[2048]; // This is a fixed buffer. } void OnAudioFilterRead(float[] data, int channels) { unsafe{ fixed (int* buffer = _fixedBufferExample._buffer) { RTcmix_runAudio (null, buffer, 2048); for(int i = 0; i<2048; i++){ data[i] = (float) buffer[i]; } } } } What I'd really like is something along the lines of: void OnAudioFilterRead(float[] data, int channels) { int *buffer = malloc(2048); RTcmix_runAudio (null, buffer, 2048); for(int i = 0; i<2048; i++){ data[i] = (float) buffer[i]; } } Any suggestions for how to deal with these pointers?

Viewing all articles
Browse latest Browse all 706

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>