/* hotwired rescale program to do simple splicing */ /* could also make this into a neat general purpose alteration program ?*/ #include "../H/sfheader.h" #include "rescale.h" #include #include #include #include #include #include #define BUFSIZE 32768 static SFCODE ampcode = { SF_MAXAMP, sizeof(SFMAXAMP) + sizeof(SFCODE) }; static SFCODE commentcode = { SF_COMMENT, MINCOMM + sizeof(SFCODE) }; main(argc,argv) int argc; char *argv[]; { SFMAXAMP sfm,sfmnew; SFCOMMENT sfcm; SFHEADER sfh1,sfh2; SFCODE *sizer; struct stat sfst1,sfst2; char *cp,*sfin,*sfout,*getsfcode(); double atof(); short outbuffer[BUFSIZE]; float inbuffer[BUFSIZE]; int i,n,bytes,words,inbytes,outbytes,sf1,sf2,result,readbytes,durbytes; int empty,skipbytes,outskipbytes,newfile; float opeak,factor,skpin,dur,newpeak,outskip; char command[256]; char newname[128]; char *point,*point2,*strcat(); short *bufp; float Oresult = 32767; if(argc == 1) { usage: printf("usage: -s inskip -d dur -o outskip -e emptybuffers_at_end -z [create new file] inputfile [outputfile]\n"); printf("defaults: s=0, d=to_eof, o=0, e=1, write over old file, outputfile=inputfile.short\n"); exit(0); } newfile=skpin=dur=newpeak=outskip=factor=0; empty = 1; /* defaults to 1 empty buffers at end*/ while((*++argv)[0] == '-') { argc -= 2; /* Take away two args */ for(cp = argv[0]+1; *cp; cp++) { switch(*cp) { /* Grap options */ case 's': skpin = atof(*++argv); printf("input skip = %f\n",skpin); break; case 'd': dur = atof(*++argv); printf("rescale duration = %f\n",dur); empty = 0; /* no empty buffers for splice */ break; case 'o': outskip = atof(*++argv); printf("output skip = %f\n",outskip); break; case 'e': empty = atof(*++argv); printf("write %d empty buffers at end\n",empty); break; case 'z': newfile = 1; break; default: printf("uh-oh, unkown option\n"); goto usage; } } } sfin = argv[0]; sfout = argv[1]; readopensf(sfin,sf1,sfh1,sfst1,"rescale",result); if(result < 0) { close(sf1); exit(1); } /* do input skip on input file*/ if(skipbytes = skpin * sfclass(&sfh1) * sfsrate(&sfh1) * sfchans(&sfh1)) { skipbytes -= skipbytes % (sfclass(&sfh1) * sfchans(&sfh1)); /* make sure it lands on sample block */ if(sflseek(sf1,skipbytes,0) == -1) { printf("Bad skip on input file\n"); exit(1); } } printsf(&sfh1); if(sfclass(&sfh1) == SF_SHORT) { printf("Note: Input file has short integers.\n"); } if((cp=getsfcode(&sfh1,SF_COMMENT))) { sizer = (SFCODE *) cp; bcopy(cp + sizeof(SFCODE) , (char *) &sfcm, sizer->bsize); } newrwopensf(sfout,sf2,sfh2,sfst2,"rescale",result,2); if((result < 0) || (sfout == sfin)) { if(sfout == NULL) { point = sfin; if(sfclass(&sfh1) == SF_FLOAT) point = strcat(sfin,".short"); else point = strcat(sfin,".xshort"); } else point = sfout; sfmagic(&sfh2) = SF_MAGIC; sfclass(&sfh2) = SF_SHORT; sfchans(&sfh2) = sfchans(&sfh1); sfsrate(&sfh2) = sfsrate(&sfh1); if((sf2 = open(point,O_CREAT|O_RDWR,0644)) < 0 ) { printf("Can't open file %s\n",point); exit(-2); } if(newfile) ftruncate(sf2,SIZEOF_HEADER); for(i=0; i readbytes) ? inbytes : durbytes; durbytes -= inbytes; } if((bytes = read(sf1,(char *)inbuffer,inbytes)) <= 0) { printf("reached eof on input\n"); close(sf1); break; } words = bytes/sfclass(&sfh1); outbytes = words * SF_SHORT; if(sfclass(&sfh1) == SF_SHORT) for(i=0; i