I've just started programming in C.
I'm trying to write a code able to analyse the values from a wav audio file and write them into the array buf[num_samples]
; num_samples
is defined by reading the info contained in the header of the audio file (it is defined as a uint32_t
variable because the audio file can consist of a huge amount of samples). I found this piece of code:
uint16_t buf[num_samples];
// Open WAV file with FFmpeg and read raw samples via the pipe.
FILE *pipe = popen("ffmpeg -i 45s.wav -f s16le -ac 1 -", "r");
// check on the pipe work
if ((pipe = popen("ffmpeg -i 45s.wav -f s16le -ac 1 -", "r"))==NULL) {
perror("ERROR\n");
}
fread(buf, 2, num_samples, pipe);
pclose(pipe);
Where the second argument of fread()
is 2 (referring to bytes) because I'm using a recorder which works with the parameter Bit_per_sample = 16
.
However there are problems, in fact the array is filled with "illogical values": I expect values representing a waveform (more or less they should belong to the interval [-20,000; 32,767]
).
In order to verify the correct filling of the array I have added a printf with %d
of buf[i]
in a cicle from i=0
to i=num_samples-1
. Below some of the values I got from the output:
64947 65002 65046 65080 65110 65150 65188 65236 65264 65306 65338 65378 65434 65472 65517 8 81 128 168 206 241 268 306 322 424 458 497 520 543 566 586 622 676 699 728 745 765 796 825 840 917 950 970 996 1018 1040 1064 1072 1106 1128
Can anybody help me or give me some useful advises?
Thank you in advance, hoping I didn't waste your time.