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 th
To keep things simple, check all the specs of your audio file before compiling your program. A plain .wav
file has the following attributes:
So make sure the audio file you're parsing contains these attributes. Once you have verified that these attributes are common to your audio file, then you can begin testing. If your file does not contain these attributes, you may want to consider getting Audacity, or something similar, to make test .wav
files.
Your code is a little strange. First you cast the data as a char
, then to int
, and then into float
. That's going to give you some serious errors. All of these data types are different in size. Float
also has a completely different binary format. A int
of value 65
may be a float
of -34564.23
(or something like that). Just use
int16_t
.
I also see that you've opened two files for your code - don't bother, since it makes the code bigger. Keep your code as simple as you can until it does what you want - then add the auxiliary attributes.
Also, on your fwrites
you've written fwrite (&nuovoValArray[0], 1, 1, fp2)
but it should be fwrite (&nuovoValArray[0], 2, 1, fp2)
since the size of int16_t
is 2 bytes and not 1.
When it comes to reducing the volume of the file, here's a general approach that should work:
samp[i]
(16-bit or 2 bytes)samp[i] -= (int16_t) (samp[i] * percent);
i
Here's a snippet of code that might help:
// open file
// read into char * fileBuffer
int sampleCount = ((fileSize - dataOffset) / sizeof (int16_t));
int16_t * samp = (int16_t *) &fileBuffer[dataOffset];
float percent = 0.6f;
for (int i = 0; i < sampleCount; i++){
samp[i] -= (int16_t) (samp[i] * percent); // Should work +/- values
}
// save file
I previously had written an applications that graphs .wav
files for waveform analysis. All I had to read to learn the file format was this page - it should help you as well.