# Urgent From C++ To C# plz help

This topic is 4361 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi guys i am making a software using DirectSound to detect if somebody is speaking in the microphone or not so i got this code to detect the amplitude of the wave but it is in C++ so can anyone help me to convert it to C# ??
	DWORD readpos, backpos;
LPBYTE p1, p2;
DWORD c1, c2;
if( backpos & 0x80000000 )
backpos += 44100*2*1;
CaptureBuffer->Lock( backpos, 2000, (LPVOID*)&p1, &c1, (LPVOID*)&p2, &c2, NULL );
DWORD a = 0;
for( char * p = (char*)p1+1; p < (char*)p1+c1; p += 4 )
a += *p>0?*p:-(*p);
for( p = (char*)p2+1; p < (char*)p2+c2; p += 4 )
a += *p>0?*p:-(*p);
a >>= 8;
CaptureBuffer->Unlock( p1, c1, p2, c2 );

bool istalking = (a >= 20);


thanks alot PS : there is no CaptureBuffer.lock in Direct Sound so how to lock the buffer ?? [Edited by - jad_salloum on March 14, 2006 8:27:15 AM]

##### Share on other sites

is there a way i could use this code in managed language without converting it ?? , in C++ we do this using #pragma but in c# how do we do this ????

##### Share on other sites
Quote:
 Original post by jad_salloumthere is no CaptureBuffer.lock in Direct Sound so how to lock the buffer ??
I'm not an MDX programmer, but a quick glance at the docs found CaptureBuffer.Read() - have you tried using that?

The rest of the code looks like some basic (if confusing) pointer arithmetic. I don't know how that'll translate over to C#, but it'll all depend on what (if anything) you get from CaptureBuffer.Read()...

hth
Jack

##### Share on other sites
Quote:
 Original post by jollyjeffers but a quick glance at the docs found CaptureBuffer.Read() - have you tried using that?

thanks for replying jollyjeffers and yes i tried to use the CaptureBuffer.Read() but i was stuck by the pointers and how to convert them :( , i didn't like C++ because of the pointers and LPVOID and Hersult which till now i don't now what they mean so sorry for repeating is there a way i could use this code in managed language without converting it ??

##### Share on other sites
jollyjeffers is right: The pointer arithmetic is confusing. Furthermore, it's actually neither good nor clean C++ language style. You shouldn't be using (or just do so if you can't find another way) pointer arithmetic with C++ at all, since it might easily result in alot of potential bugs and/or confusion.

However, what this code does is loop through the buffer in steps of 4 bytes, since p1 and p2 obviously point to some memory - that's what a pointer in C++ is for ;-)
It then adds the current value in the buffer (the highest byte only, actually) to some variable (a) or subtracts it, if this value is less then zero.
Same goes for the second for loop where it adds the values in p2.
This obviously creates an amplitude value by increasing/decreasing a, depending on what values the buffers hold. It then bitshifts the result by 8 bits.
See, this is confusing and I don't even know if this is mathematicaly/physicaly correct.

What I would do is: Check what CaptureBuffer.Read() returns and handle the result properly. Perhaps you've got this code from some kind of tutorial or a person you know and you can find out what this code should mathematicaly do and why it does so...

##### Share on other sites

thanks for replying ZMaster and thanks for make it alittle bit clearer , this code i brought from a friend and what it is used for is to read the values in a sound buffer that takes sound from microphone and check if it is greater than a certain value, so what i need to do actully is to make lipsing for my 3D Character so when i speak in the microphone the character starts to move his lips using Morphing asif it is speaking and the above code is for detecting if someone is speaking in the microphone or not.

hope i was clear.

##### Share on other sites
You could try using the DirectPlayVoice API. This enables you to set a threshold for automatically starting capture of an audio stream.

I used it when it was part of Direct 8. From the current DXSDK doco:

"DirectPlay is deprecated, and Microsoft strongly recommends against using it to develop new applications. Game developers should use Windows Sockets (see Windows Sockets) and the Windows Firewall APIs (see Games and Firewalls)."

DirectPlayVoice worked well and I dunno why it's not there anymore!