[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Playing wave files with 1 sec of buffer

Posted on 2006-04-28
5
Medium Priority
?
221 Views
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;
      Sleep(1000);
      SetEvent(handle);
      return 0;
}

main()

{

  .
  .
  .

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
    {
         return;
    }
   if(sec_chunk !=0)
   {
          ::WaitForSingleObject(m_pHEvent,INFINITE);
          TerminateThread(h_Thread,NULL);
          ResetEvent(m_pHEvent);                  
   }
   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.

Regards,
Mayank
0
Comment
Question by:HanuSoftware
  • 4
5 Comments
 
LVL 8

Expert Comment

by:mrblue
ID: 16562699
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.
0
 
LVL 8

Expert Comment

by:mrblue
ID: 16562796
Sorry I missed something - you thread makes some sense but I'd prefer (if possible) better synchronization
0
 
LVL 8

Expert Comment

by:mrblue
ID: 16562837
But in fact wouldn't it be simpler to use Sleep(1000) just after PlaySamples() ?
0
 

Author Comment

by:HanuSoftware
ID: 16567393
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.




 
0
 
LVL 8

Accepted Solution

by:
mrblue earned 750 total points
ID: 16567859
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() ?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

872 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