[Speex-dev] using speex in C#

SAITAMA Taro game72 at gmail.com
Thu Dec 28 00:09:08 PST 2006


Hi,

I have read the message below,
http://lists.xiph.org/pipermail/speex-dev/2006-October/004924.html
and try to use P/Invoke to use speex in C#.

This is a part of my code.

	[StructLayout(LayoutKind.Sequential)]
	public struct SpeexBits {
		IntPtr chars;			// "raw" data
		int nbBits;			// Total number of bits stored in the stream
		int charPtr;			// Position of the byte "cursor"
		int bitPtr;			// Position of the bit "cursor" within the current char
		int owner;			// Does the struct "own" the "raw" buffer (member "chars")
		int overflow;			// Set to one if we try to read past the valid data
		int buf_size;			// Allocated size for buffer
		int reserved1;			// Reserved for future use
		IntPtr reserved2;		// Reserved for future use
	}

	[DllImport("libspeex.dll")]
	public static extern IntPtr speex_lib_get_mode(int mode);	// Obtain one of the modes available

	[DllImport("libspeex.dll")]
	public static extern IntPtr speex_encoder_init(ref SpeexMode mode);

	[DllImport("libspeex.dll")]
	public static extern void speex_bits_init(ref SpeexBits bits);

	[DllImport("libspeex.dll")]
	public static extern int speex_encode_int(IntPtr state, Int16[] in_, ref SpeexBits bits);

	SpeexBits bits = new SpeexBits();
	SpeexMode mode = (SpeexMode)Marshal.PtrToStructure(Speex.speex_lib_get_mode(1), typeof(SpeexMode));
	Speex.speex_bits_init(ref bits);
	IntPtr enc_state = Speex.speex_encoder_init(ref mode);

	Int16[] CaptureData = null;
	(CaptureData receives data recorded from mic...)
	CaptureData = (Int16[])dsCapBuffer.Read(NextCaptureOffset, typeof(Int16), LockFlag.None, LockSize);

	Speex.speex_bits_reset(ref bits);
	Speex.speex_encode_int(enc_state, CaptureData, ref bits);	// at this point, System.AccessViolationException occurs

Whould anyone tell me what wrong about my code?
Please help me.

Regards

Kaihong


More information about the Speex-dev mailing list