#include #include #include #include int fd; int steps; #define MAXLENGTH 10*44100 /* 3 sec. samples enough ? */ #define MSEC(t) (int)(t*44.1) short os[5][MAXLENGTH]; int consume_until(fd,str,len) int fd, len; char *str; { int i; char b[4]; read(fd,b,len); /* printf("[c,%s,%d]:%c%c%c%c,%d\n",str,len,b[0],b[1],b[2],b[3], strncmp(b,str,len)); */ while (strncmp(b,str,len) != 0) { for (i=3; i>0; i--) b[i-1]=b[i]; /* printf("[c ]:%d %d %d %d\n",(int) b[0],(int) b[1], (int) b[2], (int) b[3] ); */ if (read(fd,b+3,1) != 1) { printf("Error: file format error\n"); return(-1); } } return(0); /* match */ } int readwav(s) /* Proto wav header, n=nnodes, filename */ char *s; { char bb[4]; short is, channels; int ii, length, samplerate; struct sample *p; fd = open(s,O_RDONLY|O_BINARY); if (fd < 0) return(-1); consume_until(fd,"RIFF",4); read(fd,&ii,4); consume_until(fd,"WAVE",4); consume_until(fd,"fmt ",4); read(fd,bb,4); /* 0x10 */ read(fd,bb,2); /* 0x01 */ read(fd,&channels,2); /* (short) 1*/ /* Mono */ if (channels != 1) { printf("Error: number of channels must be one.\n"); return(-1); } read(fd,&samplerate,4); if (samplerate != 44100) { printf("Error: Sample rate should be 44100 Hz\n"); } read(fd,&ii,4); read(fd,&is,2); read(fd,&is,2); consume_until(fd,"data",4); read(fd,&length,4); /* 2* steps (length of data) */ if (length/2 >= MAXLENGTH) { printf("Error: Memory limit %d samples exceeded\n",MAXLENGTH); exit(-1); } steps = length/2; if (read(fd,&(os[0][0]),length) != length) { printf("Error: not all data could be read of %s\n",s); } close(fd); return(0); } iwrite(fd,n,l) int fd,l; unsigned int n; { write(fd,&n,l); } int initwav(s) /* Proto wav header, n=nnodes, filename */ char *s; { fd = open(s,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY); if (fd == NULL) return(-1); write(fd,"RIFF",4); iwrite(fd,(2*steps+36),4); write(fd,"WAVE",4); write(fd,"fmt ",4); iwrite(fd,(0x10),4); iwrite(fd,((short) 0x01),2); iwrite(fd,((short) 1),2); /* Mono */ iwrite(fd,(44100/1),4); /* Sample rate */ iwrite(fd,(2*44100/1),4); iwrite(fd,((short) 2),2); iwrite(fd,((short) 16),2); write(fd,"data",4); iwrite(fd,(2*steps),4); return(0); } writewav(fd) { int i; write(fd,&os[4][i],2*steps); close(fd); } clear_os() { int i; for (i=0; i \n",argv[0]); exit(-1); } clear_os(); readwav(argv[1]); sscanf(argv[3],"%f",&co1); sscanf(argv[4],"%f",&co2); sscanf(argv[5],"%f",&re1); sscanf(argv[6],"%f",&re2); filtern(co1,co2,re1,re2,4); if (initwav(argv[2]) != 0) { printf("Cannot open output file %s.\n", argv[2]); exit(-3); } writewav(fd); return(0); }