📄 oper.c
字号:
display_usage(); else { while (argc > 1 && argv[1][0] == '-') if (strcmp(argv[1],"-delay")==0) { /* Get skip length */ delay = atol(argv[2]); /* Move arg{c,v} over the option to the next argument */ argc -= 2; argv += 2; } else if (strcmp(argv[1], "-random") == 0) { /* Define a random delay between the given boundary (seconds) */ double rnd_max = atof(argv[2]); /* Wait a second */ if (better_seed) sleep((unsigned int)1); /* Convert seconds to samples: 8000 samples/s for 8kHz */ rnd_max *= 8000; srand((unsigned int)time(NULL)); delay = 2*(((double)rand()/(double)RAND_MAX)-0.5)*rnd_max; fprintf(stderr, "Delay used is %ld samples\n", delay); /* Move arg{c,v} over the option to the next argument */ argc -= 2; argv += 2; } else if (strcmp(argv[1], "-gain") == 0) { /* Set gains in dB power, or as linear gains */ if (strcmp(argv[2], "dB") == 0 || strcmp(argv[2], "DB") == 0 || strcmp(argv[2], "db") == 0) { gain_in_dB = 1; } else if (strcmp(argv[2], "linear") == 0) gain_in_dB = 0; else HARAKIRI("Invalid gain type (use dB or linear)\n",5); /* Move arg{c,v} over the option to the next argument */ argc-=2; argv+=2; } else if (strcmp(argv[1], "-round") == 0) { /* Round samples after integer operations */ round = 0.5; /* Update argc/argv to next valid option/argument */ argv++; argc--; } else if (strcmp(argv[1], "-trunc") == 0) { /* Truncate samples after integer operations */ round = 0; /* Update argc/argv to next valid option/argument */ argv++; argc--; } else if (strcmp(argv[1], "-size") == 0) { /* Order to trim output file size by the size of file 1, 2, or a compromise of both [default] */ trim_by = atoi(argv[2]); /* Move arg{c,v} over the option to the next argument */ argc -= 2; argv += 2; } else if (strcmp(argv[1], "-float") == 0) { /* Set data type as real */ TypeOfData = 'R'; /* Move arg{c,v} over the option to the next argument */ argc--; argv++; } else if (strcmp(argv[1], "-double") == 0) { /* Set data type as double real */ TypeOfData = 'D'; /* Move arg{c,v} over the option to the next argument */ argc--; argv++; } else if (strcmp(argv[1], "-short") == 0) { /* Set data type as real */ TypeOfData = 'I'; /* Move arg{c,v} over the option to the next argument */ argc--; argv++; } else if (strcmp(argv[1], "-long") == 0) { /* Set data type as real */ TypeOfData = 'L'; /* Move arg{c,v} over the option to the next argument */ argc--; argv++; } else if (strcmp(argv[1], "-") == 0) { /* No more options; Move arg{c,v} to the next argument and break */ argc--; argv++; break; } else if (strcmp(argv[1], "-q") == 0) { /* Set quiet compare - only log the total differences */ quiet = 1; /* Move arg{c,v} over the option to the next argument */ argc--; argv++; } else { fprintf(stderr, "ERROR! Invalid option \"%s\" in command line\n\n", argv[1]); display_usage(); } } /* Read parameters for processing */ GET_PAR_D(1, "A? .................................... ", A); GET_PAR_S(2, "First file? ........................... ", File1); GET_PAR_S(3, "Operation (+,-,*,/)? .................. ", Oper); GET_PAR_D(4, "B? .................................... ", B); GET_PAR_S(5, "Second file? .......................... ", File2); GET_PAR_D(6, "C? .................................... ", C); GET_PAR_S(7, "Result file? .......................... ", RFile); FIND_PAR_L(8, "Record Length? ........................ ", N, 256); FIND_PAR_L(9, "Starting Record? ...................... ", N1, 1); FIND_PAR_L(10, "Number of Records? .................... ", N2, 0); FIND_PAR_C(11, "Short(I), long(L), float (R) or double (D) data? .. ", c[0], TypeOfData); TypeOfData = c[0]; /* ......... SOME INITIALIZATIONS ......... */ --N1; fprintf(stderr, "Trimming by the size of the %s file %s\n", trim_str[(int)trim_by], "(if different sizes)"); /* Classify operation */ switch(Oper[0]) { case '+': oper_f = add; break; case '-': oper_f = sub; break; case '*': case 'x': case 'X': oper_f = mult; break; case '/': oper_f = divide; break; default: HARAKIRI("Oper: Undefined operation - aborted\n",5); } /* Convert gain if necessary */ if(gain_in_dB) { A = dB2linear(A); B = dB2linear(B); } /* Define sample size */ switch(TypeOfData) { case 'R': samplesize = sizeof(float); break; case 'D': samplesize = sizeof(double); break; case 'I': samplesize = sizeof(short); break; case 'L': samplesize = sizeof(long); break; default: HARAKIRI("++==++==++ UNSUPPORTED DATA TYPE ++==++==++\007\n",7); } /* Define 1st sample to compare */ start_byte1 = start_byte2 = samplesize; if (delay>=0) { start_byte1 *= (N1*N + delay); start_byte2 *= (N1*N); } else { start_byte1 *= (N1*N); start_byte2 *= (N1*N - delay); } /* Check if is to process the whole file */ if (N2 == 0) { struct stat st; long k, l; /* ... find the size of the 2 files and the number of blks from it */ /* ... hey, need to skip the delayed samples! ... */ stat(File1, &st); k = (st.st_size - start_byte1) / (N * samplesize); stat(File2, &st); l = (st.st_size - start_byte2) / (N * samplesize); if (trim_by==1) N2 = k; else if (trim_by==2) N2 = l; else if (trim_by==3) { /* Trim by the size of the longest file */ N2 = k > l ? k : l; /* Convert the trim flag to the equivalent of trimming by the size * of file 1 or file 2, the longest of them; this eases implementation */ trim_by = (k>l)? 1 : 2; } else { /* Trim by the size of the shortest file */ N2 = k < l ? k : l; } if (k != l) fprintf(stderr, "WARNING: Files have different sizes!\n"); } /* Opening test file; abort if there's any problem */#ifdef VMS sprintf(mrs, "mrs=%d", sizeof(short) * N);#endif /* Open input files */ if ((f1=fopen(File1,RB))==NULL) KILL(File1,3); if ((f2=fopen(File2,RB))==NULL) KILL(File2,4); if ((fr=fopen(RFile,WB))==NULL) KILL(RFile,5); fh1= fileno(f1); fh2= fileno(f2); fhr= fileno(fr); /* If samples of the primary files are to be skipped, dump them into the output file */ if (delay>0) { long i, dump = start_byte1/samplesize-delay; short *a=(short *)calloc(sizeof(short),delay); double register tmp; if (lseek(fh1, dump * samplesize, 0l) < 0l) KILL(File1, 3); if (read(fh1, a, delay * samplesize)!= samplesize*delay) KILL(File1,6); for (i=0;i<delay;i++) { tmp = (A * (double)a[i] + C + round); a[i] = (short)(tmp > 32767 ? 32767 : (tmp < -32768 ? -32768 : tmp)); } write(fhr, a, delay * samplesize); free(a); } /* Move pointer to 1st block of interest */ if (lseek(fh1, start_byte1, 0l) < 0l) KILL(File1, 3); if (lseek(fh2, start_byte2, 0l) < 0l) KILL(File2, 4); /* Some preliminaries */ N1++; /* Operates on the file to the screen */ switch (TypeOfData) { case 'I': /* short data */ Prcd = operate_shorts(File1,File2,RFile,fh1,fh2,fhr, N,N1,N2,A,B,C,oper_f,trim_by,round); break;#ifdef DATA_TYPE_DEFINED case 'L': /* long data */ NrDiffs = operate_longs(); break; case 'R': /* float data */ NrDiffs = operate_floats(); break; case 'D': /* double data */ NrDiffs = operate_doubles(); break;#endif } /* Finalizations */ close(fh1); close(fh2);#ifndef VMS return(0);#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -