I am working on an application that continuously plays audio using the waveOut... API waveOut... from winmm.dll . The application uses βleapfrogβ buffers, which are basically a collection of arrays of samples that you upload to the audio queue. Windows plays them sequentially sequentially, and as each buffer exits, Windows calls the callback function. Inside this function, I load the next set of samples into a buffer, process them, and then delete the buffer back into the audio queue. Thus, the sound is played endlessly.
For the purpose of animation, I am trying to include waveOutGetPosition in the application (since buffer callbacks are irregular enough to cause jerky animation). waveOutGetPosition returns the current playback position, so it is hyper-accurate.
The problem is that in my application, the waveOutGetPosition calls ultimately cause the application to block - the sound stops and the call never returns. I swallowed everything to a simple application that demonstrates the problem. You can run the application here:
http://www.musigenesis.com/SO/waveOut%20demo.exe
If you just hear the tiny piano over and over, it works. It just demonstrated the problem. The source code for this project is here (all meat is in LeapFrogPlayer.cs):
http://www.musigenesis.com/SO/WaveOutDemo.zip
The first button launches the application in jump mode without making calls to waveOutGetPosition . If you click on this, the application will play forever without hacking (the X button will close it and turn it off). The second button launches scapperger and also starts a form timer that calls waveOutGetPosition and displays the current position. Press this button, and the application will start for a short time, and then block. On my laptop, it usually blocks after 15-30 seconds; at best it took a minute.
I have no idea how to fix this, so any help or suggestions would be most welcome. I have found very few posts on this issue, but it seems that there is a potential dead end, whether from multiple calls to waveOutGetPosition or from calls to this and waveOutWrite that occur simultaneously. Perhaps I too often call this to process the system.
Edit : forgot to mention, I am running this on Windows Vista. This may not happen at all on other OSs.
Change 2 . I have little knowledge of this problem on the Internet, except for these (unanswered) messages:
http://social.msdn.microsoft.com/Forums/en-US/windowsgeneraldevelopmentissues/thread/c6a1e80e-4a18-47e7-af11-56a89f638ad7
Change 3 . Well, now I can reproduce this problem on my own. If I call waveOutGetPosition immediately after waveOutWrite (in the next line of code), the application freezes every time. It also hangs especially poorly - it seems that it blocks my entire OS for a while, and not just on the application itself. Thus, it turns out that waveOutGetPosition deadlocked, if it happens almost at the same time as waveOutWrite , and not just literally at the same time, which may explain why the locks do not work for me. Ish.