I'm playing around with some realtime sound generation using mmsystem, and have come across a problem. I can't find a good way to swap and play my buffers (using doubble buffering). As a result, lots of nasty clicking sounds are generated once a buffer is finished playing.
I made it work properly by playing a buffer once WM_WOM_DONE was called (generated by mmsystem when a buffer is finished playing), but I want to avoid using callbacks if possible.
Anyway, at the moment, I'm using waveOutGetPosition to get the sample position, and if the sample position indicates that a buffer is done playing, it starts playback of the second buffer. The code goes like this:
get newSamplePosition from waveOutGetPosition
if newPosition - oldPosition > bufferLength or if newPosition = 0 then
begin
if buffer1 is active then
begin
playBuffer1
fillBuffer2
set activeBuffer to buffer2
end else
begin
playBuffer2
fillBuffer1
set activeBuffer to buffer1
end
oldPosition = newPosition
end
I'm sure there's a better (and working;) way of doing this, so if anyone has any tips, I would greatly appriciate it.. |