views:

39

answers:

1

Hi, I am trying to write a program to play a small .wav file in C++. I have programmed it following the DirectX SDK documents to write and play on a secondary static buffer. It runs correctly except that at the end of the playback for any .wav file, there is a very noticeable "clicking" noise. I am certain that it's not a defect on my audio hardware's part because any other game that I know uses DirectSound doesn't have it.

I've tried polling GetCurrentPosition for it every cycle and stopping it right before it ends but was unreliable. I can't play it on a primary buffer because .wav's played on primary buffers must be looped, which I don't want.

Does anybody know a fix to this problem? Thanks in advance.

+1  A: 

We'd need to see both the WAV file in question and the code to load the WAV file into the sound buffer. But's here's a few guesses.

My first guess is that if we were to load the WAV file you have into an a visual audio editor we'd see the sound coming to an abrupt end instead of tapering off into silence. This would result in an abrubt "popping" noise with almost any audio player.

My second guess is that you are copying garbage data into the sound buffer at the end. Some WAV files have extra metadata at the end of the file past the end of the DATA chunk. Not sure how you determined the positions in the sound file to copy samples from, but its easy to mess this up. Did you inspect the WAV file with a hex editor (like Visual Studio) to confirm the length of the DATA chunk is as big as it claims to be? When you debug the ReadFile call to copy data from file into the buffer, did you inspect the last several bytes of your buffer match what you saw in the hex editor?

selbie
Your first guess was correct, the wav had an abrupt end and after fading it out to silence the clicking noise has disappeared. There was also a clicking noise at the start as well which I noticed afterward, and the problem was I was loading in the fmt chunk into the buffer as well. After fixing that it all works exactly how I wanted it, with no clicks. Thank you very much for your support.
kaykun