- Home /
Using a fixed size pointer in Unity C# with a C++ external function
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?
Your answer
Follow this Question
Related Questions
Shared memory between c# and c++ 1 Answer
Importing C++/CLI DLL causes errors 0 Answers
Multiple Cars not working 1 Answer
Native Plugin works in editor but not in build 0 Answers
How to assign a function to UnityEvent without the use of lambdas? 1 Answer