I am writing a C program for editing a Wav audio file. I have loaded all file datas in an array of unsigned integer values (UINT16_T).
Now, i would like to reduce the volume of the file. I thought it was enough to decrease the value (of a certain percentage) of the single values. But if i do that, i obtain an audio file with noise (I think I understand is called "static" or "click noise")
Why? Which is the right procedure?
Thank You!
This is the piece of code affected:
FILE* fp;
FILE* fp2;
/*Size of my file*/
#define BUFFER_SIZE 28242852
/*Array with file data*/
unsigned char *buffer;
/*Array used for converting two bytes in an unsigned int*/
unsigned char uintBytes[2];
/*The unsigned int obtained*/
uint16_t * conv;
/*The new value calculated*/
uint16_t nuovoValore;
/*Array used for the reverse conversion, form UINT to bytes*/
unsigned char* nuovoValArray;
for(i=44; i<BUFFER_SIZE;i++){
if(i%2==0){
/*I read 2 bytes form the array and "convert" it in an unsigned int*/
uintBytes[0]=buffer[i];
uintBytes[1]=buffer[i+1];
conv=(uint16_t *) &uintBytes[0];
/*Calculate the new value (-30%) to write in the new file*/
nuovoValore= *conv - ((float)*conv*30/100);
if(nuovoValore<0) nuovoValore=0;
nuovoValArray=malloc(2);
memset(nuovoValArray,'\0',2);
nuovoValArray=(unsigned char*)&nuovoValore;
/*Write the two bytes of the new file*/
fwrite(&nuovoValArray[0], 1, 1, fp2);
fwrite(&nuovoValArray[1], 1, 1, fp2);
}
}