1
votes

I'm writing a program (c++, vs2010,win7) that records audio into a wav file. When I try to hear the audio it has a lot of white noise. I tried to reopen the file with another program that I wrote. The only thing this program does is :

Char buffer[8000*60*2] = {} 
File *wav, *out
Wav = fopen ("raw", "r+") 
Out = fopen("out", "a+") 
fread(buffer, sizeof (char) *8000*60*2,1,wav)
fwrite(buffer, sizeof (char) *8000*60*2,1,out) 
fclose (wav) 
fclose (out) 

After I pass the raw data through this program I can hear a small part of the original wav file without any noise(i open this in audacity as raw data). My problem is that I'm not changing anything in the data just writing it again and like a magic I can hear clearly. What am I missing? I don't make any change of the data. When I write the data I write it as a short var. data is short fwrite(data, 1024,1,wav)

1
You realise that you're appending to that output file, not rewriting it? - Roger Rowland
Yes it's a new file anyway. I can change it to w+ but it gives the same result - David
So do you get the same effect if you just use Explorer to copy the file? - Roger Rowland
I get the same noise as the wav file - David
Ok, so you have some difference created by your program. If you didn't change the default, it should be reading/writing as binary but you might try "rb" and "wb" as file modes instead. - Roger Rowland

1 Answers

1
votes

When writing a WAVE file a lot of things may cause it to have noise. Here are a couple of things you should look for:

Capping the values

A vanilla WAVE file has a 16-bit data value ( or two values if it is stereo ) and so you have to make sure that ALL of your values are in the range of (32768, -32768) if you scale up the values and they exceed that limit then you will get a random* value instead and thus when writing the file will get noise.

Writing in binary

As mentioned by Roger on the comments of the original question it is important that you write in binary since if you try to write to a file with the standard "r" you will just get a lot of white noise.

Missing headers

Sometimes if you forget a header you may also get noise at the output WAVE file.

Hopefully that helps out.