#include #include #include #include #include int fd; FILE *fp; int steps; #define MAXLENGHT 1*1024*1024 /* 1 meg samples enough ? */ short dva[MAXLENGHT]; 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),4); /* Sample rate */ iwrite(fd,(2*44100),4); iwrite(fd,((short) 2),2); iwrite(fd,((short) 16),2); write(fd,"data",4); iwrite(fd,(2*steps),4); return(0); } outputbin(i) int i; { char *cp; int t; if (fd > 0) { /* t = e[i]/PREMUL; */ cp = (char *) &t; write(fd,cp,1); write(fd,cp+1,1); } } read_convert(fp) FILE *fp; { float t, v, vmin, vmax; short dv, vdmin, vdmax; vmin=+1e6; vmax=-1e6; steps=0; while (fscanf(fp,"%f %f\n",&t,&v) == 2) { dv = (short) (10000.0 * v); /* puts(fd, &dv , 2); */ steps++; if (v > vmax) { vmax=v; vdmax = dv;} ; if (v < vmin) { vmin=v; vdmin = dv;} ; if (v >= 3.2766) dv=32766; if (v <=-3.2766) dv=-32766; dva[steps] = dv; } fclose(fp); printf(" converted %d samples, max/min: %f(%d) %f(%d)\n",steps,vmax,vdmax,vmin,vdmin); } writewav(fd) { int i; for (i=0; i \n",argv[0]); printf("(infile contains lines with (time sample) float tuplets,\n"); printf("(outfile is a .wav soundfile).\n"); exit(-1); } if ((fp = fopen(argv[1],"r")) == NULL) { printf("Cannot open input file %s.\n",argv[1]); exit(-2); } read_convert(fp); if (initwav(argv[2]) != 0) { printf("Cannot open output file %s.\n", argv[2]); exit(-3); } writewav(fd); }