How do i replace fscanf with a function from the libsndfile library?
I opened wav file with libsndfile library and want to read through this file and get N number of points in FFT and get time-domain samples and calculate FFT.
any ideas how I could replace fscanf with function from libsndfile library? here 开发者_运维问答is line of code:
for(i=0; i < N; i++) fscanf(fs, "%lg%lg", &x[i][0], &x[i][1]);
So, problem is I don't know how to work with wav. without libsndfile and how to implement some things using libsndfile.
thanks, a.
You say you want to read N samples and then you do:
for(i=0; i < N; i++) fscanf(fs, "%lg%lg", &x[i][0], &x[i][1]);
so you're reading 2 * N samples and I'm not sure why.
If you grab the libsndfile source code tarball, there are example programs in the examples/ directory. Basically you do:
SNDFILE * sf ;
SF_INFO info ;
double array [N] ;
memset (&info, 0, sizeof (info));
if ((sf = sf_open (filename, SFM_READ, &info)) == NULL)
handle_error () ;
sf_read_double (sf, array, N) ;
sf_close (sf) ;
This code assumes you file is mono (only one channel).
You need to open the file with sf_open
, then read the file with sf_read_float
to get the samples back.
You can do this N times in your for loop and to write to an array and then send that off to your fft.
Source: libsndfile api http://www.mega-nerd.com/libsndfile/api.html
Also you may want to check Audacity's ImportPCM.cpp for a concrete example.
精彩评论