1

I'm recording audio into a wav file and also stream the same audio to the speakers (c++, vs2010, win7). when i hear it in the speakers i can hear the audio clear, but when i write it to a wav file i get a weird ticking/electricity noise added to the audio, i try to open the file using audacity and i can clearly see that there is a peak every 0.05 sec. i don't know from where the noise is been added can you help me?

i added the writing into a file from my code:

writeWav(char *filename,short *data) 
{ 
     FILE *wav; 
     wav = fopen(filename,"ab+"); 
     for(int i=0;i<1024;i++) 
     { 
        writeLE(data[i],2,wav); 
     } 
     fclose(wav); 
} 

void writeLE(short data,int nBytes,FILE *wav) //write in little-endian
{ 
     unsigned buf; 
     while(nBytes > 0) 
     { 
         buf = word & 0xff; 
         fwrite(&buf,1,1,wav); 
         nBytes--; 
         word >>= 8; 
     } 
}

the function writeWavis been called every time a packet is received (size = 1024).

David
  • 287
  • 2
  • 3
  • 13
  • I get it from a receiver in a Udp thread the receive packets of audio. I send the same data to the speakers and to the file. I'm sure is something in that I wrote, the data is good. – David Mar 24 '13 at 14:39

1 Answers1

1

It looks like your for loop is wrong.

To process 1024 items it should be:

for (int i = 0; i < 1024; i++)
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Simon Jenkins
  • 529
  • 5
  • 9
  • Sorry I mistyped it here. In my code it's written as 1024. I edited the code now – David Mar 24 '13 at 14:24
  • Is that also why the writeLE parameter is called "data" but referred to as "word" in the body? – Simon Jenkins Mar 24 '13 at 14:28
  • It's just the var name. What else could it mean? – David Mar 24 '13 at 14:32
  • There is no var named "word". – Simon Jenkins Mar 24 '13 at 14:34
  • In the function writeLE the first line is the declaration of the var word – David Mar 24 '13 at 14:36
  • Also, how about: word >>= 8; – Simon Jenkins Mar 24 '13 at 14:36
  • This is a part of my code. I mistyped it sorry . I edited the post – David Mar 24 '13 at 14:42
  • what is your sampling frequency? – fatihk Mar 24 '13 at 14:45
  • The sampling frequency is 44100 – David Mar 24 '13 at 14:46
  • it seems that a "foreign" data is added for every 2 blocks, are you sure, you are not doing any file operations other than stated here? – fatihk Mar 24 '13 at 15:00
  • I only write at the end the file header. But even if I only take the raw data there is still that ticking noise. What foreign data is added? Where it is been written? – David Mar 24 '13 at 15:04
  • does the tickling stems from a single sample peak? if that is so try to write 1023 samples into file by changing to i < 1023 – fatihk Mar 24 '13 at 15:07
  • Is this with a USB audio interface? Are you sure all received packets are 1024 words? If every second packet was 1022 for example you'd be writing an extra word to your file from off the end of the buffer. – Simon Jenkins Mar 24 '13 at 15:10
  • It appears about 20 samples that create the peak. But I'm not sure how to calculate how many samples create the peak – David Mar 24 '13 at 15:14
  • Yes it's a usb interface. The data is good I can hear it clearly in the speakers when I stream it – David Mar 24 '13 at 15:21
  • Some of the buffer data may be empty, then you may just write about 1000 samples and observe the results. – fatihk Mar 24 '13 at 15:24
  • With each call to writeWav, are you sure there are exactly 1024 samples in the data buffer? You don't pass a size argument but later assume there always are 1024 samples in writeLE. – sizzzzlerz Mar 24 '13 at 15:27
  • I checked that before I called the write function – David Mar 24 '13 at 15:28
  • if you can add the full versions of your methods, you could gete more constructive answers – fatihk Mar 24 '13 at 15:31
  • Can you write the samples out to a raw binary file, not to a wavfile. Don't convert to LE. Don't massage the samples at all. Once in the file, import it into Audacity as a raw file. You'll need to specify the sample rate, size, and endiness. Does the glitch still appear? – sizzzzlerz Mar 24 '13 at 15:35
  • I tried to write the data to an excel file and noticed that every 200 samples there is a peak – David Mar 24 '13 at 15:54