Reputation: 301
I'm generating a wav file from an array of samples. I noticed that if I start and then stop copying a waveform, it produces a "click" sound. Here's a simple example, where I periodically copy a sine wave followed by no sound (16 bit signed stereo pcm at 44100 hz):
int c, counter = 0;
short *wavdat = malloc(numberOfSamples * 2);
for(c = 0; c < numberOfSamples * 2; c += 2){
counter++;
if(counter % 10000 < 5000){
wavdat[c] = sinf(counter * .1f) * 16000;
wavdat[c+1] = wavdat[c];
}else{
wavdat[c] = wavdat[c+1] = 0;
}
}
Here's what the wav looks like in audacity, zoomed in at a point where the sine wave is cut off:
The sharp spike at the end seems to be the cause of the click sound I hear. Why does this cause a click sound instead of just stopping the sound instantaneously? How can I stop a sound without the audible click? I need to stop copying samples at an exact time, so I'm not sure I can fade them out.
This problem happens to me even with a much more complex waveform (such as stopping a voice clip).
Here's the recorded sound file if anyone's interested: http://clyp.it/yc2mpqni
Upvotes: 2
Views: 843
Reputation: 19037
Any abrupt spike in a PCM waveform is in fact a click.
If you know in advance how many samples you're going to copy out, you can start fading them out early -- a linear cutoff slope over 0.05 seconds of time is a good starting point.
Upvotes: 3
Reputation: 180808
Stop the sample at the zero-crossing point. That will eliminate the spike.
Upvotes: 0