void _flushbuf(int fno) { register int i; for(i=pointer[fno]*sfclass(&sfdesc[fno]); i<nbytes; i++) *(sndbuf[fno] + i) = 0; }
off_t outrepos(int samps, int fno) { int seeking,amt; if(!status[fno]) { fprintf(stderr,"file %d is write protected!\n",fno); perror("write"); closesf(); } amt = samps * sfchans(&sfdesc[fno]) + pointer[fno]; if ( (amt >= 0) && (amt < bufsize[fno]) ) { pointer[fno] = amt; /* inside current buffer */ return(pointer[fno]); } if(wipe_is_off[fno]) _backup(fno); if(!peakoff[fno]) _chkpeak(fno); _writeit(fno); /* write out current buffer */ seeking = (amt - bufsize[fno]) * sfclass(&sfdesc[fno]); if ( (filepointer[fno] = lseek(sfd[fno],seeking,1)) == -1 ) { fprintf(stderr,"CMIX: bad outrepos lseek, file: %d\n",fno); closesf(); } if(wipe_is_off[fno]) _readit(fno); pointer[fno] = 0; return(filepointer[fno]); }
double m_class(float *p, int n_args) { if(!isopen[(int)p[0]]) { fprintf(stderr, "You haven't opened file %d yet!\n", (int)p[0]); closesf(); } return(sfclass(&sfdesc[(int)p[0]])); }
int setnote(float start, float dur, int fno) { int nsamps,offset; int i; if(!isopen[fno]) { rtcmix_warn("setnote","You haven't opened file %d yet!\n",fno); closesf(); } if(start > 0.) /* if start < 0 it indicates number of samples to skip*/ offset = (int) (start * SR() + .5) * sfchans(&sfdesc[fno]) * sfclass(&sfdesc[fno]); else offset = -start * sfchans(&sfdesc[fno]) * sfclass(&sfdesc[fno]); /* make sure it falls on channel/block boundary */ offset -= offset % (sfchans(&sfdesc[fno]) * sfclass(&sfdesc[fno])); offset = (offset < 0) ? 0 : offset; nsamps = (dur > 0.) ? (int)((start+dur) * SR() - (offset/(sfchans(&sfdesc[fno])*sfclass(&sfdesc[fno])))+ .5) : (int)-dur; if(!istape[fno]) { if((filepointer[fno] = lseek(sfd[fno],offset+headersize[fno],0)) == -1) { rtcmix_warn("setnote", "CMIX: bad lseek in setnote\n"); closesf(); } } pointer[fno] = 0; _readit(fno); /* read in first buffer */ for(i=0; i<(sfchans(&sfdesc[fno]) * FLOAT); i++) *(peak[fno] + i) = 0; wipe_is_off[fno] = 1; /* for wipeout */ starttime[fno] = (start<0) ? -start/SR() : start; times(&clockin[fno]); /* read in starting time */ return(nsamps); }
double sfcopy(float p[], int n_args) { int maxread,n,input,output,bytes,jj; if(!n_args) fprintf(stderr,"(sfcopy(input_fno,output_fno,input_skip,output_skip,dur)\n"); input = (int)p[0]; output = (int)p[1]; if((sfclass(&sfdesc[input]) != sfclass(&sfdesc[output])) || (sfchans(&sfdesc[input]) != sfchans(&sfdesc[output]))) { fprintf(stderr, "Input and output specifications do not match. Canot copy.\n"); closesf(); } bytes = setnote(p[2],p[4],input) * sfchans(&sfdesc[input]) * sfclass(&sfdesc[input]); setnote(p[3],p[4],output); _backup(input); _backup(output); fprintf(stderr,"Copy %d bytes\n",bytes); while(bytes) { maxread = (bytes > nbytes) ? nbytes : bytes; if((n = read(sfd[input],sndbuf[input],maxread)) <= 0) { fprintf(stderr,"Apparent eof on input\n"); return -1.0; } if((jj = write(sfd[output],sndbuf[input],n)) <= 0) { fprintf(stderr,"Trouble writing output file\n"); closesf(); } bytes -= n; filepointer[input] += n; filepointer[output] += n; } if(fsync(sfd[output]) < 0 ) { fprintf(stderr,"trouble fsyncing file"); closesf(); } fprintf(stderr,"Copy completed\n"); return 0.0; }
int getsetnote(float start, float dur, int filenum) { int nsamples = setnote(start,dur,filenum); _backup(filenum); if(sfclass(&sfdesc[filenum]) == SF_FLOAT) getsample = getfsample; else getsample = getisample; return(nsamples); }
double m_dur(float *p, int n_args) { int i; float dur; i = (int) p[0]; if(!isopen[i]) { fprintf(stderr, "You haven't opened file %d yet!\n", i); closesf(); } dur = (float)(sfst[i].st_size - headersize[i]) /(float)sfclass(&sfdesc[i])/(float)sfchans(&sfdesc[i]) /sfsrate(&sfdesc[i]); return(dur); }
void _chkpeak(int fno) { register int i,incr; register short *ibuf,*bufend; register float *fbuf,*fbufend,*pk; short *ibufx; float *fbufx; long *pkloc,currentloc; pk = (float *)peak[fno]; incr = sfchans(&sfdesc[fno]); pkloc = (long *)peakloc[fno]; if(sfclass(&sfdesc[fno]) == SHORT) { ibufx = ibuf = (short *)sndbuf[fno]; bufend = ibuf + pointer[fno]; /* to allow for final check */ currentloc = (long) ((filepointer[fno]-headersize[fno])/(SHORT * incr)); while(ibuf<bufend) { for(i=0; i<incr; i++) { if(ABS(*(ibuf + i)) > (int)*(pk+i)) { *(pk+i) = ABS(*(ibuf + i)); *(pkloc+i) = currentloc + (long)((ibuf - ibufx)/incr); } } ibuf += incr; } } else { fbufx = fbuf = (float *)sndbuf[fno]; fbufend = fbuf + pointer[fno]; currentloc = (long) ((filepointer[fno]-headersize[fno])/(FLOAT * incr)); while(fbuf<fbufend) { for(i=0; i<incr; i++) { if(ABS(*(fbuf + i)) > *(pk+i)) { *(pk+i) = ABS(*(fbuf + i)); *(pkloc+i) = currentloc + (long)((fbuf - fbufx)/incr); } } fbuf += incr; } } }
void blayout(float *out, int *chlist, int fno, int size) { register int i,j; register short *ibuf; register float *fbuf; register int todo; register int nchans; int len = bufsize[fno]; nchans = sfchans(&sfdesc[fno]); refill: todo = ((pointer[fno] + size) > len) ? len - pointer[fno] : size; if(sfclass(&sfdesc[fno]) == SF_SHORT) { for(i=0,ibuf = (short *)sndbuf[fno] + pointer[fno];i<todo;i += nchans) { for(j=0; j<nchans; j++,ibuf++,out++) { if(chlist[j]) { *ibuf = (short) *out; } } } } else { for(i=0,fbuf = (float *)sndbuf[fno] + pointer[fno];i<todo;i += nchans) { for(j=0; j<nchans; j++,fbuf++,out++) { if(chlist[j]) { *fbuf = *out; } } } } pointer[fno] += todo; if(pointer[fno] == len) { _backup(fno); if(!peakoff[fno]) _chkpeak(fno); _writeit(fno); _readit(fno); pointer[fno] = 0; } if(size -= todo) goto refill; }
void bwipeout(float *out, int fno, int size) { register int i; register short *ibuf; register float *fbuf; register int todo; int len = bufsize[fno]; refill: todo = ((pointer[fno] + size) > len) ? len - pointer[fno] : size; if(sfclass(&sfdesc[fno]) == SHORT) { for(i=0,ibuf = (short *)sndbuf[fno] + pointer[fno];i<todo;i++) { *(ibuf++) = (short)*(out++); } } else { for(i=0,fbuf = (float *)sndbuf[fno] + pointer[fno];i<todo;i++) { *(fbuf++) = *(out++); } } pointer[fno] += todo; if(pointer[fno] == len) { if(wipe_is_off[fno]) { _backup(fno); wipe_is_off[fno] = 0; } if(!peakoff[fno]) _chkpeak(fno); _writeit(fno); pointer[fno] = 0; } if(size -= todo) goto refill; }
off_t inrepos(int samps, int fno) { int seeking,amt; amt = samps * sfchans(&sfdesc[fno]) + pointer[fno]; if ( (amt >= 0) && (amt < bufsize[fno]) ) { pointer[fno] = amt; /* reposition inside buffer */ return(pointer[fno]); } seeking = (amt - bufsize[fno]) * sfclass(&sfdesc[fno]); if ( (filepointer[fno] = lseek(sfd[fno],seeking,1)) == -1 ) { fprintf(stderr,"CMIX: Bad inrepos lseek, file: %d\n",fno); closesf(); } _readit(fno); pointer[fno] = 0; return(filepointer[fno]); }
int bgetin(float *input, int fno, int size) { register int i; register short *ibuf; register float *fbuf; register int todo; int n; int len = bufsize[fno]; refill: todo = ((pointer[fno] + size) > len) ? len - pointer[fno] : size; /* If it's a short */ if(sfclass(&sfdesc[fno]) == SHORT) { for(i=0,ibuf = (short *)sndbuf[fno] + pointer[fno];i<todo;i++) { *(input++) = (float) *(ibuf++); } } /* If it's a float */ else { for(i=0,fbuf = (float *)sndbuf[fno] + pointer[fno];i<todo;i++) { *(input++) = *(fbuf++); } } pointer[fno] += todo; if(pointer[fno] == len) { n = _readit(fno); pointer[fno] = 0; if(!n) return(n); } if(size -= todo) goto refill; return(i); }
void m_zapout(int fno, char *buffer, int nwrite, int *chlist) { float *fbuf; int i,j,nchunks,chans; short *ibuf; chans = sfchans(&sfdesc[fno]); if(sfclass(&sfdesc[fno]) == SF_SHORT) { ibuf = (short *) buffer; nchunks = nwrite/SF_SHORT; for(i=0; i<nchunks; i += chans) for(j=0; j<chans; j++) if(chlist[j]) *(ibuf+j+i) = 0; } else { fbuf = (float *) buffer; nchunks = nwrite/SF_FLOAT; for(i=0; i<nchunks; i += chans) for(j=0; j<chans; j++) if(chlist[j]) *(fbuf+j+i) = 0; } }
int main(int argc, char *argv[]) { int sf; int comment = 0; struct stat sfst; SFHEADER sfh; SFMAXAMP sfm; SFCOMMENT sfcm; SFCODE *sizer; FILE *fcom; char *sfname,*cp,*getsfcode(); int length,newchans,newclass,newpeak,result,i,n,tfd,tn,nchars=MINCOMM; float newsrate; int zap; zap = newchans = newclass = newsrate = newpeak = 0; length = 1; if(argc > 1) { if(*argv[1] != '-') { printf("usage: \"sfhedit -r [srate] -[i=int;f=float] -c [nchans] -p [peak] -w [comment] -z\"\n"); exit(1); } } else if(argc == 1) { printf("usage: \"sfhedit -r [srate] -[i=int;f=float] -c [nchans] -p [peak] -w [comment] -z\"\n"); exit(1); } while((*++argv)[0] == '-') { argc -= 2; /* Take away two args */ for(cp = argv[0]+1; *cp; cp++) { switch(*cp) { /* Grap options */ case 'r': newsrate = atof(*++argv); break; case 'i': newclass = SF_SHORT; break; case 'f': newclass = SF_FLOAT; break; case 'c': newchans = atoi(*++argv); if(newchans > 4) { printf(" Sorry, maximum is 4 channels\n"); exit(1); } break; case 'p': newpeak = 1; break; case 'w': comment = 1; break; case 'z': zap = 1; break; case 'l': length = 1; break; default: printf("don't know about option %c\n",*cp); } } } sfname = argv[0]; rwopensf(sfname,sf,sfh,sfst,"sfhedit",result,2); if(result < 0) { exit(1); } printsf(&sfh); if(newchans) { sfchans(&sfh) = newchans; printf("-->Channels reset to %d\n",newchans); } if(newsrate) { sfsrate(&sfh) = newsrate; printf("-->Sampling rate reset to %5.0f\n",newsrate); } if(newclass) { sfclass(&sfh) = newclass; printf("-->Class reset to %d\n",newclass); } if(newpeak) { for(i=0; i<sfchans(&sfh); i++) { printf("Enter peak and time for channel %d\t",i); scanf("%f %ld",&sfmaxamp(&sfm,i),&sfmaxamploc(&sfm,i)); } sfmaxamptime(&sfm) = time(NULL); putsfcode(&sfh,(char *)&sfm,&code); } if(zap) { if(ftruncate(sf,0) < 0) printf("Bad truncation\n"); for(i=0; i<sfchans(&sfh); i++) { sfmaxamp(&sfm,i) = sfmaxamploc(&sfm,i) = 0; putsfcode(&sfh,(char *)&sfm,&code); putlength(sfname,sf,&sfh); } printf("file truncated to 0, and header adjusted\n"); } if(comment) { cp = getsfcode(&sfh,SF_COMMENT); if(cp == NULL) { printf("No comment found. Adding a new one..\n"); system("vi /tmp/comment"); fcom = fopen("/tmp/comment","r"); i=0; while ( (sfcomm(&sfcm,i) = getc(fcom)) != EOF ) { if (++i > MAXCOMM) { printf("Gimme a break! I can only take %d characters\n",MAXCOMM); printf("comment truncated to %d characters\n",MAXCOMM); commentcode.bsize = MAXCOMM + sizeof(SFCODE); break; } } sfcomm(&sfcm,i) = '\0'; system("rm /tmp/comment"); if (nchars > MINCOMM) commentcode.bsize = nchars + sizeof(SFCODE); if (i > nchars) commentcode.bsize = i + sizeof(SFCODE); if (putsfcode(&sfh,(char *)&sfcm,&commentcode) < 0) { printf("comment didn't get written, sorry!\n"); exit(1); } goto skip; } strncpy((char *)&sfcm, sfcommentstr(&sfh), MAXCOMM - 1); sfcm.comment[MAXCOMM - 1] = '\0'; tfd = open("/tmp/tmpcom",O_CREAT|O_RDWR,0644); tn = write(tfd, &sfcomm(&sfcm,0), strlen(&sfcomm(&sfcm,0))); close(tfd); system("vi /tmp/tmpcom"); tfd = open("/tmp/tmpcom",0); n = read(tfd,&sfcomm(&sfcm,0),MAXCOMM); system("rm /tmp/tmpcom"); if (n < tn) { for (i = n; i <= tn; i++) { sfcomm(&sfcm,i) = '\0'; } } if (putsfcode(&sfh,(char *)&sfcm,&commentcode) < 0) { printf("comment didn't get written, sorry!\n"); exit(1); } } skip: lseek(sf,0,0); if(wheader(sf,(char *)&sfh)) { printf("Can't seem to write header on file %s\n",sfname); perror("main"); exit(1); } if (length) { /* do this last, after wheader */ putlength(sfname,sf,&sfh); } return 0; }
off_t _writeit(int fno) { int i; off_t n=0; short tisamp,*tibuf; float tfsamp,*tfbuf; float peakval; if(!status[fno]) { rtcmix_warn(NULL, "File %d is write-protected!\n",fno); closesf(); } #ifdef OLDRT /* to play before writing */ if((sfclass(&sfdesc[fno]) == SF_SHORT) && play_is_on) playbuf(sndbuf[fno],nbytes/SF_SHORT); /* swap and/or play floating point files */ if(play_is_on && (sfclass(&sfdesc[fno]) == SF_FLOAT)) { peakval = getpeakval(peakflag,fno); playfbuf(sndbuf[fno],peakval,swap_bytes[fno],nbytes/SF_FLOAT); } else { /* just swap if necessary */ if(swap_bytes[fno] && (sfclass(&sfde sc[fno]) == SF_FLOAT)) bytrev4(sndbuf[fno],nbytes); } #endif if(swap_bytes[fno]) { /* SHORT file */ if(sfclass(&sfdesc[fno]) == SF_SHORT) { tibuf = (short *)sndbuf[fno]; for (i=0;i<nbytes/SF_SHORT;i++) { tisamp = *(tibuf+i); *(tibuf+i) = (short) reverse_int2(&tisamp); } } /* FLOAT file */ if(sfclass(&sfdesc[fno]) == SF_FLOAT) { tfbuf = (float *)sndbuf[fno]; for (i=0;i<nbytes/SF_FLOAT;i++) { /* byte_reverse4(tfbuf+i); */ /* tfsamp = *(tfbuf+i); */ /* *(tfbuf+i) = (float) reverse_int4(&tfsamp); */ tfsamp = *(tfbuf+i); byte_reverse4(&tfsamp); *(tfbuf+i) = tfsamp; } } } if(play_is_on < 2) { if((n = write(sfd[fno],sndbuf[fno],nbytes)) != nbytes) { rtcmix_warn("CMIX", "Bad UNIX write, file %d, nbytes = %lld\n",fno,(long long)n); perror("write"); closesf(); } /* update output file size */ if((filepointer[fno] += nbytes) > originalsize[fno]) originalsize[fno] = filepointer[fno]; } if(!play_is_on) printf("."); return(n); }
off_t _readit(int fno) { int i; off_t n, maxread; short tisamp,*tibuf; float tfsamp,*tfbuf; /* check to see if we are attempting to read beyond current * EOF, and if so adjust amount to be read and flush rest of buffer */ /* From make warnings */ n=0; if(filepointer[fno] > originalsize[fno]) maxread = 0; else if((filepointer[fno]+nbytes) > originalsize[fno]) maxread = originalsize[fno]-filepointer[fno]; else maxread = nbytes; if((play_is_on < 3) || (status[fno] == 0)) { if((n = read(sfd[fno],sndbuf[fno],maxread)) != maxread) { if(!n) { /*if(istape[fno] && n) continue;*/ perror("read"); rtcmix_warn("CMIX", "Bad UNIX read, nbytes = %lld\n",(long long)n); rtcmix_warn(NULL, " sfd[fno]= %d\n",sfd[fno]); closesf(); } } } if(((play_is_on==2) && !maxread) || ((play_is_on==3) && (status[fno]))) memset(sndbuf[fno],'\0',nbytes); /* clean buffer out if not readin */ /* Swap input buffer */ if(maxread && swap_bytes[fno]) { /* SHORT file */ if(sfclass(&sfdesc[fno]) == SF_SHORT) { tibuf = (short *)sndbuf[fno]; for (i=0;i<nbytes/SF_SHORT;i++) { tisamp = *(tibuf+i); *(tibuf+i) = reverse_int2(&tisamp); } } /* FLOAT file */ if(sfclass(&sfdesc[fno]) == SF_FLOAT) { tfbuf = (float *)sndbuf[fno]; for (i=0;i<nbytes/SF_FLOAT;i++) { /* byte_reverse4(tfbuf+i); */ /* tfsamp = *(tfbuf+i); */ /* *(tfbuf+i) = (float)reverse_int4(&tfsamp); */ tfsamp = *(tfbuf+i); byte_reverse4(&tfsamp); *(tfbuf+i) = tfsamp; } } } /* if we haven't read in full buffer, zero out rest of buffer, * and adjust filepointer with lseek. Otherwise just update * filepointer. This will position pointer properly for any * situation. Only a write will change physical size of file. */ if(play_is_on < 2) { if(maxread < nbytes) { for(n=maxread; n<nbytes; n++) *(sndbuf[fno] + n) = 0; filepointer[fno] = lseek(sfd[fno],(nbytes-maxread),1); } else filepointer[fno] += nbytes; } else filepointer[fno] += nbytes; return(maxread ? n : maxread); }
int endnote(int xno) { struct timeval tp; struct timezone tzp; int i,j,final_bytes,fno; float notepeak,*pk; double total; long *pkloc; struct tms timbuf; float peakval; struct stat st; short tisamp,*tibuf; float tfsamp,*tfbuf; fno = ABS(xno); /* if fno is negative it means don't write final buffer,just pretend to */ if(wipe_is_off[fno]) _backup(fno); /* else _flushbuf(fno); */ if(!peakoff[fno]) _chkpeak(fno); final_bytes = pointer[fno] * sfclass(&sfdesc[fno]); /* This was DS's and PL's version of real time */ /* Not used in this version */ #ifdef OLDRT /* SHOULD NOT PLAY HERE -- LAST BUFFERS ALREADY PLAYED */ if ((sfclass(&sfdesc[fno]) == SF_SHORT) && play_is_on) playbuf(sndbuf[fno],final_bytes/SF_SHORT); else if ((sfclass(&sfdesc[fno]) == SF_FLOAT) && play_is_on) { peakval = getpeakval(peakflag,fno); playfbuf(sndbuf[fno],peakval,swap[fno],nbytes/SF_FLOAT); } #endif /* write out only fractional part of last record, god bless unix!*/ if(pointer[fno] && (play_is_on < 2)) { if(xno >= 0) { /* Swap bytes if necessary */ if(final_bytes && swap_bytes[fno]) { /* SHORT file */ if(sfclass(&sfdesc[fno]) == SF_SHORT) { tibuf = (short *)sndbuf[fno]; for (i=0;i<final_bytes/SF_SHORT;i++) { tisamp = *(tibuf+i); *(tibuf+i) = reverse_int2(&tisamp); } } /* FLOAT file */ if(sfclass(&sfdesc[fno]) == SF_FLOAT) { tfbuf = (float *)sndbuf[fno]; for (i=0;i<final_bytes/SF_FLOAT;i++) { /* byte_reverse4(tfbuf+i); */ /* tfsamp = *(tfbuf+i); */ /* *(tfbuf+i) = (float)reverse_int4(&tfsamp); */ tfsamp = *(tfbuf+i); byte_reverse4(&tfsamp); *(tfbuf+i) = tfsamp; } } } if((i = write(sfd[fno],sndbuf[fno],final_bytes)) != final_bytes) { rtcmix_warn("CMIX", "Bad UNIX write, file %d, nbytes = %d\n", fno,i); perror("write"); closesf(); } } if((filepointer[fno] += final_bytes) > originalsize[fno]) if(xno >0) originalsize[fno] = filepointer[fno]; } /* DT: if(play_is_on) flush_buffers(); */ pk = (float *)peak[fno]; pkloc = (long *)peakloc[fno]; total = ((double)filepointer[fno]-headersize[fno]) /((double)sfclass(&sfdesc[fno])) /(double)sfchans(&sfdesc[fno])/SR(); /* _writeit(fno); write out final record */ for(i = 0,notepeak=0; i<sfchans(&sfdesc[fno]); i++) { if(*(pk+i) > sfmaxamp(&sfm[fno],i)) { sfmaxamp(&sfm[fno],i) = *(pk+i); sfmaxamploc(&sfm[fno],i) = *(pkloc+i); } if(*(pk+i) > notepeak) notepeak = *(pk+i); } gettimeofday(&tp,&tzp); sfmaxamptime(&sfm[fno]) = tp.tv_sec; if((filepointer[fno] = lseek(sfd[fno],0L,0)) < 0) { rtcmix_warn("CMIX", "Bad lseek to beginning of file\n"); perror("lseek"); closesf(); } times(&timbuf); #ifndef MAXMSP // this really isn't used... printf("\n(%6.2f)",(float)( (timbuf.tms_stime-clockin[fno].tms_stime)+ (timbuf.tms_utime-clockin[fno].tms_utime))/60.); printf(" %9.4f .. %9.4f MM ",starttime[fno],total); if(!peakoff[fno]) { for(j=0;j<sfchans(&sfdesc[fno]);j++) printf(" c%d=%e",j,*(pk+j)); printf("\n"); if(punch[fno]) { printf("alter(%e,%e,%e/%e", (double)starttime[fno],(double)(total-starttime[fno]), punch[fno],notepeak); for(i=0; i<sfchans(&sfdesc[fno]); i++) printf(",1 "); printf(")\n"); printf("mix(%g,%g,%g,%g/%g", (double)starttime[fno],(double)starttime[fno],-(double)(total-starttime[fno]),punch[fno],notepeak); for(i=0; i<sfchans(&sfdesc[fno]); i++) printf(",%d ",i); printf(")\n"); } } #endif // MAXMSP /* Copy the updated peak stats into the SFHEADER struct for this output file. (No swapping necessary.) */ memcpy(&(sfmaxampstruct(&sfdesc[fno])), &sfm[fno], sizeof(SFMAXAMP)); /* Write header to file. */ if (wheader(sfd[fno], &sfdesc[fno])) { rtcmix_warn("endnote", "bad header write\n"); perror("write"); closesf(); } return 0; }
double m_open(float *p, short n_args, double *pp) { char *name,*cp,*getsfcode(); int fno,i,inew; float *opk; name = DOUBLE_TO_STRING(pp[0]); fno = p[1]; // JGG: will name ptr be valid for entire program run? Is its memory held by // parser? If not, we should malloc sfname[fno] below (with other mallocs) sfname[fno] = name; status[fno] = (n_args == 3) ? (int)p[2] : 2; if((fno >= NFILES) || (fno < 0)) { rtcmix_warn("m_open", "Only %d files allowed\n", NFILES); closesf(); } inew = 0; if(isopen[fno]) { close(sfd[fno]); } else inew = 1; istape[fno] = (n_args == 4) ? 1 : 0; /* in the case of a tape, there will be a 4th argument listing the file number */ rwopensf(name,sfd[fno],sfdesc[fno],sfst[fno],"CMIX",i,status[fno]); if (i < 0) closesf(); if (status[fno] == O_RDWR && !WRITEABLE_HEADER_TYPE(sfheadertype(&sfdesc[fno]))) { rtcmix_warn("m_open", "can't write this type of header.\n"); closesf(); } isopen[fno] = 1; swap_bytes[fno] = swap; /* swap and isNext set in rwopensf */ is_Next[fno] = isNext; headersize[fno] = getheadersize(&sfdesc[fno]); rtcmix_advise(NULL, "name: %s sr: %.3f nchans: %d class: %d\n",name, sfsrate(&sfdesc[fno]),sfchans(&sfdesc[fno]), sfclass(&sfdesc[fno])); rtcmix_advise(NULL, "Soundfile type: %s\n", mus_header_type_name(sfheadertype(&sfdesc[fno]))); rtcmix_advise(NULL, " data format: %s\n", mus_data_format_name(sfdataformat(&sfdesc[fno]))); rtcmix_advise(NULL, "Duration of file is %f seconds.\n", (float)(sfst[fno].st_size - headersize[fno])/(float)sfclass(&sfdesc[fno])/(float)sfchans(&sfdesc[fno])/sfsrate(&sfdesc[fno])); originalsize[fno] = istape[fno] ? 999999999 : sfst[fno].st_size; /* sfstats(sfd[fno]); */ if(inew) { if((sndbuf[fno] = (char *)malloc((unsigned)nbytes)) == NULL) { rtcmix_warn("CMIX", "malloc sound buffer error\n"); closesf(); } if((peakloc[fno] = (char *)malloc((unsigned)(sfchans(&sfdesc[fno]) * LONG))) == NULL) { rtcmix_warn("CMIX", "malloc ovpeak buffer error\n"); closesf(); } if((peak[fno] = (char *)malloc((unsigned)(sfchans(&sfdesc[fno])* FLOAT))) == NULL) { rtcmix_warn("CMIX", "malloc peak buffer error!\n"); closesf(); } peakoff[fno] = 0; /* default to peakcheckon when opening file*/ punch[fno] = 0; /* default to no punch when opening file*/ } if(sfclass(&sfdesc[fno]) == SHORT) { addoutpointer[fno] = _iaddout; layoutpointer[fno] = _ilayout; wipeoutpointer[fno] = _iwipeout; getinpointer[fno] = _igetin; } else { addoutpointer[fno] = _faddout; layoutpointer[fno] = _flayout; wipeoutpointer[fno] = _fwipeout; getinpointer[fno] = _fgetin; } if(!SR()) set_SR(sfsrate(&sfdesc[fno])); if(sfsrate(&sfdesc[fno])!= SR()) rtcmix_advise("CMIX", "Note--> SR reset to %f\n",SR()); /* read in former peak amplitudes, make sure zero'ed out to start.*/ /* In the sndlib version, we store peak stats differently. See comments in sndlibsupport.c for an explanation. The sndlib version of rwopensf reads peak stats, so here we just have to copy these into the sfm[fno] array. (No swapping necessary.) */ memcpy(&sfm[fno], &(sfmaxampstruct(&sfdesc[fno])), sizeof(SFMAXAMP)); for(opk = (float *)peak[fno], i = 0; i<sfchans(&sfdesc[fno]); i++) *(opk+i) = sfmaxamp(&sfm[fno],i); bufsize[fno] = nbytes / sfclass(&sfdesc[fno]);/* set size in words */ return 0.0; }
double m_clean(float p[], int n_args) /* a fast clean of file, after header */ { /* if p1-> = 0, clean whole file, else skip=p1, dur=p2, ch-on? p3--> */ int i; off_t n, nwrite, todo; char *point; int fno,segment,chlist[4]; int skipbytes; fno = (int) p[0]; skipbytes = 0; if(!status[fno]) { rtcmix_warn(NULL,"fno %d is write-protected!\n",fno); closesf(); } todo = originalsize[fno] - headersize[fno]; segment = (n_args > 1) ? 1 : 0; if(segment) { skipbytes = (p[1] > 0) ? p[1] * sfclass(&sfdesc[fno]) * SR() * sfchans(&sfdesc[fno]) : -p[1] * sfclass(&sfdesc[fno]) * sfchans(&sfdesc[fno]); todo = (p[2] > 0) ? p[2] * sfclass(&sfdesc[fno]) * SR() * sfchans(&sfdesc[fno]) : -p[2] * sfclass(&sfdesc[fno]) * sfchans(&sfdesc[fno]); for(i=0; i<sfchans(&sfdesc[fno]); i++) chlist[i] = p[i+3]; } point = (char *)sndbuf[fno]; if(!segment) for(i=0; i<nbytes; i++) *(point+i) = 0; if((filepointer[fno] = lseek(sfd[fno],skipbytes+headersize[fno],0)) == -1) { rtcmix_warn("CMIX", "bad sflseek in clean\n"); closesf(); } #ifdef MINGW // no %11 in Win libc, have to use %I64 instead: // http://stackoverflow.com/questions/13590735/printf-long-long-int-in-c-with-gcc? printf("Clean %I64d bytes\n",(long long)todo); #else printf("Clean %lld bytes\n",(long long)todo); #endif while(todo) { nwrite = (todo > nbytes) ? nbytes : todo; if(segment) { if((n = read(sfd[fno],sndbuf[fno],nwrite)) == 0) { /* allow for fractional reads*/ fprintf(stderr, "CMIX: Apparent eof in clean\n"); return -1.0; } if(lseek(sfd[fno],-n,1) < 0) { fprintf(stderr,"Bad UNIX lseek in clean\n"); closesf(); } m_zapout(fno,sndbuf[fno],n,chlist); nwrite = n; } if((n = write(sfd[fno],sndbuf[fno],nwrite)) == 0) { fprintf(stderr, "CMIX: Apparent eof in clean\n"); closesf(); } todo -= n; } if(!segment) { if((lseek(sfd[fno],0,0)) == -1) { fprintf(stderr,"CMIX: bad lseek in clean\n"); closesf(); } for(i = 0; i<sfchans(&sfdesc[fno]); i++) { sfmaxamp(&sfm[fno],i) = 0; sfmaxamploc(&sfm[fno],i) = 0; } putsfcode(&sfdesc[fno],(char *)&sfm[fno],&code); if(wheader(sfd[fno],(char *)&sfdesc[fno])) { fprintf(stderr,"Bad header write\n"); perror("write"); closesf(); } } else if((lseek(sfd[fno],headersize[fno],0)) == -1) { fprintf(stderr,"CMIX: bad lseek in clean\n"); closesf(); } filepointer[fno] = headersize[fno]; printf("Clean successfully finished.\n"); return 0.0; }