Skip to main content
gamedev.net
🔒 Locked

C# Question.

Started by cilcoder 6 replies 500+ views
Original Post
cilcoder
cilcoder
Ok. I'm working on a wrapper for the PortAudio library. I have it working but I have 2 questions/problems. 1. One of the structs that I receive from the unmanaged library as the following fields: int numSampleRates; double * sampleRates; I am having trouble marshalling that because sampleRates is pointer to an array. Does anyone know how I can do it. Also would using an unsafe method with pointer work to get access to the data? I would think it would but is there a better way? 2. PortAudio works by using callbacks. I have a callback that works. Here is an example callback:
		// Our Callback
		unsafe static int patestCallback(IntPtr inputBuffer,IntPtr outputBuffer,
			UInt32 framesPerBuffer,double outTime,Object userData)
		{
			TestData data = userData as TestData;
			
			float * oBuf = (float*)outputBuffer.ToPointer();
			UInt32 i;
			int finished = 0;
			for( i=0; i= TABLE_SIZE) data.left_phase -= TABLE_SIZE;
				data.right_phase += 3; //higher pitch so we can distinguish left and right. 
				if(data.right_phase >= TABLE_SIZE ) data.right_phase -= TABLE_SIZE;
			}
			return finished;
		}
 
Is there anyway I could avoid using pointers for that? Thanks [edited by - cilcoder on November 25, 2003 12:18:58 AM]
Nypyren
Nypyren
look at the MarshalAs attribute... it''s got a particular type of array that you can probably use for the double*.

And for the bottom section of code - I think that if you have it working well, don''t mess with it. Pointers in C# aren''t bad, just don''t go crazy and use them everywhere
cilcoder
cilcoder
Ok. I looked at the MarshalAs stuff like I had been trying before and when I set it up to work with th LPArray type I got an exception saying that it can''t be used in a struct. All the examples show it being used in a function import. It looks like I''m going to have to use pointers again.
Nypyren
Nypyren
Oh... yeah... forgot about that... the deal is you can only do fixed-size arrays inside structs... :/

(My bad)
cilcoder
cilcoder
Ok, I added an array at the end of my class. It works but I''m wondering is it has some kind of bad affect or if adding a variable to a marshalled class is acceptable. I thought it would of crashed if it was too bad. Does anyone know?
cilcoder
cilcoder
quote:

(My bad)



No problem. I''ve gotten everything working how I want it to. I believe I have a fully working, possibly with a few bugs in it, wrapper for the PortAudio library.

BTW-This is just for the PortAudio part of the PortMusic project. In case anyone is interested in what PortAudio is go to here: http://www.portaudio.com/ and check it out.
Arild Fines
Arild Fines
quote:
Original post by cilcoder
Ok. I''m working on a wrapper for the PortAudio library. I have it working but I have 2 questions/problems.

1. One of the structs that I receive from the unmanaged library as the following fields:

int numSampleRates;
double * sampleRates;

I am having trouble marshalling that because sampleRates is pointer to an array. Does anyone know how I can do it. Also would using an unsafe method with pointer work to get access to the data? I would think it would but is there a better way?

Google around on the microsoft.public.dotnet.csharp and/or microsoft.public.dotnet.interop newsgroups. I know I have seen this question answered multiple times, but I don''t remember the answer.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
cilcoder
cilcoder
quote:

Google around on the microsoft.public.dotnet.csharp and/or microsoft.public.dotnet.interop newsgroups. I know I have seen this question answered multiple times, but I don't remember the answer.



I searched and I couldn't find what I was looking for. I didn't put too much effort into it though, anyways.. My problems are solved, and that seems liike the only way to do it.

[edited by - cilcoder on November 26, 2003 1:37:21 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.