0
0

Hi,
I am trying to use readData from C#, but at the moment without success.
The problem is that the function expects an IntPtr, but I haven’t found a way to get an IntPtr from a short[] array.

I only found an example which did the oposite (getting values back from an array declared as an IntPtr):
short *stereo16bitbuffer = (short *)data.ToPointer();

When I try it this way:
[code:290no16t]unsafe {
short[] buffer = new short[2048];
IntPtr bufferPtr = new IntPtr(&buffer);
}[/code:290no16t]
I get a compiler error that it can’t get a pointer from a managed type, but I haven’t found a way yet to declare an unmanaged array yet.

  • You must to post comments
0
0

Can you do like this?

[code:2wb38p8d]
short[] buffer = new short[2048];
IntPtr[] bufferPtr = new IntPtr[2048];
[/code:2wb38p8d]

  • You must to post comments
0
0

[quote="Ken":192obvv4]Can you do like this?

[code:192obvv4]
short[] buffer = new short[2048];
IntPtr[] bufferPtr = new IntPtr[2048];
[/code:192obvv4][/quote:192obvv4]That uh.

That won’t work.

At all.

Adion: I think you have to ‘marshal’ the managed buffer into an unmanaged one. Not sure exactly how it works; haven’t ever needed to do it.

  • You must to post comments
0
0

In the meantime I found the following which I thought would work:

[code:30nk8a4o]short* buffer = stackalloc short[2048];
IntPtr bufferPtr = new IntPtr(&buffer);[/code:30nk8a4o]

Unfortunately, altough it doesn’t generate compiler errors, when I try to execute the code, it crashes at the readdata call.

I’ll see if I can find anything about this marshalling.

  • You must to post comments
0
0

I now found this:
[code:1wy07ol3]IntPtr bufferPtr = Marshal.AllocCoTaskMem(4096);[/code:1wy07ol3]
But unfortunately it still crashes, with following error code:
[code:1wy07ol3]The program ‘[3448] DDJFileChecker.vshost.exe: Managed’ has exited with code -1073741819 (0xc0000005).[/code:1wy07ol3]

Here is my call to readData that crashes:
[code:1wy07ol3]if (sound.readData(ref bufferPtr, 1024, ref read) == FMOD.RESULT.OK) { ... [/code:1wy07ol3]

Now I am starting to think that there’s a problem with the declaration of the readData function in C#.
The c/c++ declaration expects “void *buffer”, so basically a pointer to the data.
The c# function expects “ref IntPtr buffer”, which to my interpretation appears to be a pointer to an IntPtr, or in c terms “void **buffer”
Now I’m not that familiar with c# yet, so if anyone knows I’m wrong or right please tell me.

  • You must to post comments
0
0

I think I finally got it working. I’m not yet 100% sure that the values I receive are correct, but at least it seems to work and it doesn’t crash.

Here’s what I did:

First I initialize the memory by using:
[code:3p7z9xvo]IntPtr bufferPtr = Marshal.AllocCoTaskMem(4096);[/code:3p7z9xvo]

Then I read the data using:
[code:3p7z9xvo]sound.readData((int)bufferPtr.ToPointer(), 4096, ref read)[/code:3p7z9xvo]

And finally I can use the data by:
[code:3p7z9xvo]short* buffer = (short*)bufferPtr.ToPointer();
short l = buffer[j * sourceChannels];
short r = buffer[j * sourceChannels + 1];[/code:3p7z9xvo]

To be able to pass the pointer as an integer, I had to change the declarations in fmod.cs to

[code:3p7z9xvo]private static extern RESULT FMOD_Sound_ReadData (IntPtr sound, int buffer, uint lenbytes, ref uint read);[/code:3p7z9xvo]
and
[code:3p7z9xvo]public RESULT readData (int buffer, uint lenbytes, ref uint read)
{
return FMOD_Sound_ReadData(soundraw, buffer, lenbytes, ref read);
}[/code:3p7z9xvo]

Maybe just removing the ref keyword and requesting an IntPtr directly would have been correct as well?

  • You must to post comments
Showing 5 results
Your Answer

Please first to submit.