Click here to Skip to main content
15,891,856 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am a newbie in directshow. I have written a program to take sample audio from microphone into a buffer using ISampleGrabber.

C#
void MICDetect::Run_Graph(IMediaControl* m_pControl)
{
    m_hr = m_pControl->Run();// Now run the graph, i.e. start listening!
    if(SUCCEEDED(m_hr))
        {
            cout<<"Graph is Running"<<endl;
        }
    else HR_Failed(m_hr);
    cout<<"Waiting for buffer";
    for(int i=5;i>=1;--i)
    {
        Sleep(500);
        cout<<".";
    }
    cout<<endl;
    m_hr = m_pGrabber->GetCurrentBuffer(&m_Size, NULL);
    if(FAILED(m_hr))
    {
        HR_Failed(m_hr);
    }
     pBuffer = (BYTE*)CoTaskMemAlloc(m_Size);
    if (!pBuffer)
    {
        m_hr = E_OUTOFMEMORY;
        HR_Failed(m_hr);
    }
    for(int i=1000000;i>1;i--)
    {
        Sleep(1);
    m_hr = m_pGrabber->GetCurrentBuffer(&m_Size, (long*)pBuffer);
    if (FAILED(m_hr))
    {
        HR_Failed(m_hr);
    }
    
    }
system("pause");
}

Since I need a sample of 1000 samples per sec for start I pause getcurrentbuffer for 1ms and loop it. I'm not sure of this method. The only way for me to be sure is to stream the grabbed sample to an audio render.

How am i supposed to do that?
Posted
Updated 28-Feb-11 5:49am
v4

1 solution

You should find another way since Sleep(1); is completely unreliable (Windows is not a real-time OS). See also the remarks at Sleep documentation page[^].
:)
 
Share this answer
 
Comments
cougar10 1-Mar-11 0:40am    
thanks for the response..i was thinking the same...but used sleep since it is the easiest option available..
but my main problem is..is this the correct way of using GetCurrentBuffer to grab sample continuously..?

and how to stream these grabbed samples to output filter ( speaker)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900