/********************************************************** File: distort.c Author: Scot Cunningham Usage: distort infile outfile begin end algorithm This program distorts the input file (argv[1]) and puts the results in the output file (argv[2]). It distorts from the begin point in seconds (argv[3]) to the end point (argv[4]) using one of the following algorithms: suppress distort invert permute The "suppress" algorithm simply replaces the audio with silence. The "distort" is a custom algorithm that preserves prosidic features. The "invert" algorithm inverts the sign of every other sample. The "permute" algoritym performs a uniform block permutation. Compile with -lm option to get math libraries **********************************************************/ #define DLEN 32 /* The number of samples to read at one time */ #define SHORT 2 /* The size of each sample */ #define SRATE 8000 /* Our assumed sampling rate. */ /* A couple of useful macros */ #define mmin(A,B) ((A)<(B)?(A):(B)) #define mmax(A,B) ((A)>(B)?(A):(B)) #include #include #include #include /********************************************************** Supress the contents of read_file beginning at "begin" and ending and "end". Output results to write_file *********************************************************/ int suppress(begin, end, read_file, write_file) int begin; int end; int read_file; int write_file; { int ns; /* Keeps track of the number of suppressed samples */ int n_read, n_write; /* Number of bytes read/written */ int i; /* Loop incrementer */ short ibuff[DLEN]; /* Input buffer */ /* Keep reading into the buffer and suppressing when in range */ /* Output suppressed results to write_file */ n_read = read(read_file, ibuff, DLEN*SHORT); ns=0; while (n_read != 0) { if (ns > begin && ns < end) { for (i=0; i begin && ns < end) { /* Invert the sign of every other sample */ for (i=0; i begin && ns < end && n_read==(pDLEN*SHORT)) { /* Permute ibuff to obuff */ int newBlockNum; int j; printf("************\n"); for (i=0; i1024) { for (i=0; i maxsamp) maxsamp=abs(ibuff[i]); } } if ((ns+DLEN) > begin && ns < end) { for (i=0; i0) { ibuff[i] = ibuff[i] - distlevel; }else{ ibuff[i] = ibuff[i] + distlevel; } // Completely suppress lower level audio if (abs(ibuff[i]) < lowbar) ibuff[i] = 0; // Don't exceed max levels if (abs(ibuff[i]) > maxsamp) ibuff[i] = rand() % mmax(maxsamp,1); } } n_write = write(write_file, ibuff, n_read); if(n_write < 0) { perror("ERROR writing the output file"); exit(0); } ns = ns + DLEN; n_read = read(read_file, ibuff, DLEN*SHORT); if(n_read < 0) { perror("ERROR reading the input file"); exit(0); } } return ns; } main(argc, argv) int argc; char *argv[]; { int i; // Loop incrementer int ns; // Number of samples processed double sec_begin, sec_end; // These mark the range to distort in seconds int samp_begin, samp_end; // That same range in samples, not seconds int read_file, write_file; // The input and output files char input_file[100], output_file[100]; // Input and output file names char algorithm[100]; // The name of the algorithm to run int lowbar; // Suppression range for distort algorithm // Get the input arguments if (argc < 6 ) { printf("Usage error: distort infile outfile begin end algorithm\n"); exit(0); } strcpy(input_file, argv[1]); strcpy(output_file, argv[2]); sec_begin = atof(argv[3]); sec_end = atof(argv[4]); samp_begin = sec_begin * SRATE; samp_end = sec_end * SRATE; strcpy(algorithm, argv[5]); printf("Distorting from %d to %d \n", samp_begin, samp_end); read_file = open(input_file, O_RDONLY, 0); if (read_file < 0) { perror("ERROR opening the input file for reading"); exit(0); } write_file = open(output_file, O_WRONLY|O_CREAT, 0644); if (write_file < 0) { perror("ERROR opening the output file for writing"); exit(0); } // Invoke the requested algorithm if (strcmp(algorithm, "distort") == 0) { lowbar = atoi(argv[6]); ns = distort(samp_begin, samp_end, read_file, write_file, lowbar); } else if (strcmp(algorithm, "suppress") == 0) { ns = suppress(samp_begin, samp_end, read_file, write_file); } else if (strcmp(algorithm, "invert") == 0) { ns = invert(samp_begin, samp_end, read_file, write_file); } else if (strcmp(algorithm, "permute") == 0) { int numblocks = atoi(argv[6]); ns = permute(samp_begin, samp_end, read_file, write_file, numblocks, 0); } else if (strcmp(algorithm, "permuteinvert") == 0) { int numblocks = atoi(argv[6]); ns = permute(samp_begin, samp_end, read_file, write_file, numblocks, 1); } else { perror("Unknown algorithm\n"); exit(0); } // Output number of samples processed to stdout if (ns < 0) { printf("No Samples\n"); exit(0); }else{ printf("%d Samples processed\n",ns); } }