1

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)

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
David
  • 287
  • 2
  • 3
  • 13
  • You realise that you're appending to that output file, not rewriting it? – Roger Rowland Mar 24 '13 at 10:02
  • Yes it's a new file anyway. I can change it to w+ but it gives the same result – David Mar 24 '13 at 10:11
  • So do you get the same effect if you just use Explorer to copy the file? – Roger Rowland Mar 24 '13 at 10:13
  • I get the same noise as the wav file – David Mar 24 '13 at 10:17
  • 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 Mar 24 '13 at 10:20
  • Tnx that fix the problem. I but now I have a ticking noise in the recorded file. It's sounds like an electronic current added to the audio – David Mar 24 '13 at 10:25
  • It's possible that somewhere where you write the data that your file is opened in text mode and is converting what it thinks are newline characters to crlf pairs - can you show any more code? – Roger Rowland Mar 24 '13 at 10:27
  • I get the data from a udp thread to this func: writeWav(char *filename, short *data) – David Mar 24 '13 at 10:29
  • Ok, can't do more than guess here but make sure there's nowhere you are confusing your binary data with null-terminated strings (e.g. by using strlen() on a char udp buffer. – Roger Rowland Mar 24 '13 at 10:31

1 Answers1

1

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.

Panos
  • 1,764
  • 21
  • 23