5
Hi, I’m trying to read a file. 5s wav filled with "silence" to test the code in C. I was able to read the header but while trying to extract the date field where it contains the content itself ( which I believe should be zero) generates some numbers. as shown below.
Code:
#include <stdio.h>
#include <stdlib.h>
char buff[10000];
int main(){
int i;
FILE * fp;
FILE *flog;
if ((fp = fopen("Silencio2.wav", "rb")) == NULL){
printf("Fail!");
exit(1);
}
fseek(fp, 44, 0); // Lê apartir do campo DATA como segue na documentação.
fread(buff,sizeof(int),1024,fp);
flog = fopen("Silencio2.txt","w"); // Cria um log para jogar no matlab e gerar o gráfico
// Imprime em hexadecimal para visualizar melhor e salva em decimal no log.
for(i = 0; i < 5000 ; i++){
printf("%i == %hhx \n",i,buff[i]);
fprintf(flog,"%d\n",buff[i]);
buff[i] = 0;
}
fclose(flog);
return 0;
}
Header Reading Image . Wav:
As we can notice the program correctly reads the header bringing the coherent information, RIFF, WAVE, FMT, 1 channel, 16bits, 16khz and the beginning of the date (40~44).
Font I am using to read the file . wav: http://soundfile.sapp.org/doc/WaveFormat/
Features of file . wav:
Bitrate: 16 bits PCM;
Canais: 1;
SampleRate: 16khz.
Tempo: 5s;
Gerado pelo Audacity com a função de gerar silencio.
After saving this log in txt I am pasting the same in Matlab and reading the original file in it, in order to print the comparative graph between the 2 to validate my data, however I am getting the following output:
1st Graphic Processing of Matlab with the readaudio function(); 2º Graphic out of my code log;
Script Matlab:
[Sile2,Fs] = audioread('Silencio2.wav');
title('Gerado pelo MATLAB');
subplot(2, 1, 2);
plot(CodeSile2);
title('Meu código');
subplot(2, 1, 1);
plot(Sile2);
The question that remains is: Are my results consistent? Because beyond the divergence of magnitude of the signal there is a noise, that I can not explain why. The signal was generated through a silent generator.
PS: Yes I know libsnd but how I want to embed this code in stm32/esp32, the less libraries use better.
Thank you very much, I hadn’t really realized this detail by making the adjustment in Tlab and picking the right sample size worked super well! Hug. PS: Exactly I will read by parts as indicated, I was trying to understand first if the code was functional in this way.
– Gabriel Lavoura
what good @Gabriellavoura is always good to help :-)
– ederwander