Playing wave files with 1 sec of buffer

Posted on 2006-04-28
Last Modified: 2010-04-01
Dear Experts,

We are trying to play a wave file by allocation 1 sec of buffer at a time.
For synchronising the whole, we are doing it as

UINT pfnThreadProc(LPVOID pParam)

      HANDLE handle = (HANDLE)pParam;
      return 0;




m_pHEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
DWORD no_chunk =  (data_chunk + 176400 -1)/ 176400;  //no. of 1 second chunks

for ( sec_chunk = 0; sec_chunk < no_chunk ; sec_chunk++) // loop for each 1 second chunk
     if(!obj_Mixer->GetMixedSample()) // this fn gives the 1 second samples
   if(sec_chunk !=0)
   objData.PlaySamples(obj_Mixer->mixedSample,obj_Mixer->maxSize);  // this funtion plays 1 sec buffer
   h_Thread = AfxBeginThread(pfnThreadProc, m_pHEvent, THREAD_PRIORITY_NORMAL, 0, 0, NULL);
   delete [] obj_Mixer->mixedSample;

The problem is that we are not able to syncronize it properly and there is lag or overlap between the samples.( we have tried to manipulated the milliseconds in Sleep as well.

Kindly let me know how can i remove lag or overlap between 1 second samples.

Question by:HanuSoftware
    LVL 8

    Expert Comment

    Is "PlaySamples()" asynchronous ? If yes wouldn't you need some signal to synchronize playback (PlaySamples() should for example set some synchronization object to signaled) ? I don't think that you thread will do any good here.
    LVL 8

    Expert Comment

    Sorry I missed something - you thread makes some sense but I'd prefer (if possible) better synchronization
    LVL 8

    Expert Comment

    But in fact wouldn't it be simpler to use Sleep(1000) just after PlaySamples() ?

    Author Comment

    I had used Sleep(1000) just after PlaySamples() in my very first approach.
    but there was a lag of some milliseconds between two consecutive 1 sec samples.

    So i thought it would be better not to call Sleep in the Main Thread so that the process of getting the next 1 second sample ready should continue in the mainThread.

    But the problem remained as it is.

    LVL 8

    Accepted Solution

    Sleep() is not very precise. It's occuracy is about 10 ms. But from the other hand waiting for some object to be signaled (your second approach) has the same problem. Maybe if you use some higher resolution method like:

    QueryPerformanceCounter() in loop after PlaySamples() but without additional thread ?

    It will however use processor time, besides it will block your main thread.

    Maybe you will try multimedia timer functions (occurary up to 1 ms when forced) like
    timeSetEvent() to call PlaySamples() ?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now