Forums

Extracting waveform from WAV files using libsndfile

Started by RaghuMenon October 28, 2006
Hello,
I have several traces of "flow noise" that I have recorded by locating
microphones in a flow field. These are stored as .WAV files on my PC. I
would like to process these traces (power spectrum, cross-correlation
etc.) and therefore need to get the amplitude Vs. time information out
of the WAV files. I just started using libsndfile and made some
progress. Not sure how to get at the wave form infomation. Also what is
the unit of the amplitude in WAV format?

This is the code I have come up with so far. Apprecate any help,
comments from the group.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sndfile.h>


int main(int argc, char **argv) {

	FILE *fp;
	char fin[32],fout[32];
	int i,num;
	SNDFILE *sfd;
	SF_INFO *sfinfo,sf;
	float *val;
	static char strbuffer [256] ;

	if(argc != 3) {
		printf ("Usage: audio.exe <wav file> <out file>\n");
		exit(1);
	}

	sf_command (NULL, SFC_GET_LIB_VERSION, strbuffer, sizeof (strbuffer))
;
	puts (strbuffer) ;

	sf.frames=0;
	sf.samplerate=0;
	sf.channels=0;
	sf.format=0;
	sf.sections=0;
	sf.seekable=0;
	sfinfo=&sf;

	strcpy(fin,argv[1]);
	strcpy(fout,argv[2]);

	if((fp=fopen(fout,"w")) == 0) {
		printf("Error opening %s\n",fout);
		exit(1);
	}

	if ((sfd=sf_open(fin,SFM_READ,sfinfo)) != 0) {
		printf("Frames=%d\n",sfinfo->frames);
		printf("Sample Rate=%d\n",sfinfo->samplerate);
		printf("Channels=%d\n",sfinfo->channels);
		printf("Format=%d\n",sfinfo->format);
		printf("Sections=%d\n",sfinfo->sections);
		printf("Seekable=%d\n",sfinfo->seekable);

/*		seekno=sf_seek(sfd,sfinfo->frames,SEEK_SET);
		printf("seekno=%d\n",seekno); */

	} else {
		printf("sfd=%d\n",sfd);
		printf("Error opening soundfile\n");
		exit(1);
	}

	sf_close(sfd);

	num=(int)sfinfo->frames;

	val=(float *)malloc((size_t)((num)*sizeof(float)));

	/* Read in the waveform */
	sf_read_float(sfd,val,num);

	for(i=0;i<num;i++){
		fprintf(fp,"%d\t%.4f\n",i,val[i]);
	}
	
	fclose(fp);

	return 0;
}


Thanks.
Raghu