static void tr_parseoptions(int argc, char** argv) { int option_index = 1; int opt; while((opt = getopt_long(argc, argv, "vhso:", tr_long_options, &option_index)) != -1) { switch(opt) { case 'h': tr_help(); exit(0); break; case 'v': tr_version(); exit(0); break; case 's': quiet = 1; break; case 'o': outfilename = strdup(optarg); break; default: fprintf(stderr, "ERROR: Invalid argument. Use -h for help \n"); exit(1); /* We probably could survive, better to just bail for now; at least that way we can guarentee nothing bad will happen */ break; } } }
static int tr_options(t_env *env, int argc, char **argv) { int opt; int value; value = 0; while ((opt = getopt(argc, argv, "hnm:q:f:w:")) != -1) { if (opt == 'h') tr_help(); else if (opt == 'n') env->flags |= FLAGS_N; else if (opt == 'm') { value = ft_atoi(optarg); (value > 0 && value <= 255) ? env->hops = value : tr_e_i(); } else if (opt == 'q') { value = ft_atoi(optarg); (value > 0 && value <= 10) ? env->nqueries = value : tr_e_i(); } else if (opt == 'f') { value = ft_atoi(optarg); (value > 0 && value <= (int)env->hops) ? env->ttl_count = value : tr_e_i(); } else if (opt == 'w') { value = ft_atoi(optarg); (value >= 0) ? env->timeout = value : tr_e_i(); } else tr_error_usage(); } return (optind); }
int main(int argc, char** argv) { if(argc == 1) { tr_help(); return 1; } tr_parseoptions(argc, argv); if(!quiet) { tr_version(); } if(argc - optind < 2) { fprintf(stderr, "ERROR: Requires input and response file to be specified. Use -h for help \n"); return 1; } const char* infilename = argv[optind]; const char* responsefilename = argv[optind+1]; /* Make a filename from the input and response names */ if(!outfilename) { char* innameend = strrchr(infilename, '.'); int innamelen = innameend ? innameend-infilename : strlen(infilename); char* responsenameend = strrchr(responsefilename, '.'); int responsenamelen = responsenameend ? responsenameend-responsefilename : strlen(responsefilename); outfilename = malloc(innamelen+responsenamelen + 5); /* + '.wav '*/ strncpy(outfilename, infilename, innamelen); strncat(outfilename, responsefilename, responsenamelen); strcat(outfilename, ".wav"); } InitEndian(); /* Setup the input file */ FILE* infile = fopen(infilename, "rb"); if(infile == 0) { fprintf(stderr, "ERROR: Failed opening input file %s\n", infilename); return 1; } tr_wavfile inputwav; if( !tr_wavopen(infile, &inputwav, 'r') ) { fprintf(stderr, "ERROR: Invalid file type, %s is not a wav file\n", infilename); return 1; } if(!quiet) { fprintf(stdout, "\n"); fprintf(stdout, "Input file : %s\n", infilename); fprintf(stdout, " Samples : %u\n", inputwav.totalsamples); fprintf(stdout, " Channels : %i\n", inputwav.channels); fprintf(stdout, " Sample rate : %u\n", inputwav.samplerate); fprintf(stdout, " Bytes per sample : %u\n", inputwav.bytespersample); fprintf(stdout, " Duration seconds : %.2f\n", ((float)inputwav.totalsamples / (float)inputwav.samplerate / (float)inputwav.channels)); } /* Now setup the response file */ FILE* responsefile = fopen(responsefilename, "rb"); if(responsefile == 0) { fprintf(stderr, "ERROR: Failed opening response file %s\n", responsefilename); return 1; } tr_wavfile responsewav; if(!tr_wavopen(responsefile, &responsewav, 'r')) { fprintf(stderr, "ERROR: Invalid file type, %s is not a wav file\n", responsefilename); return 1; } if(!quiet) { fprintf(stdout, "\n"); fprintf(stdout, "Response file : %s\n", responsefilename); fprintf(stdout, " Samples : %u\n", responsewav.totalsamples); fprintf(stdout, " Channels : %i\n", responsewav.channels); fprintf(stdout, " Sample rate : %u\n", responsewav.samplerate); fprintf(stdout, " Bytes per sample : %u\n", responsewav.bytespersample); fprintf(stdout, " Duration seconds : %.2f\n", ((float)responsewav.totalsamples / (float)responsewav.samplerate / (float)responsewav.channels)); fprintf(stdout, "\n"); } /* Make sure we can handle processing the two files */ if(inputwav.channels != responsewav.channels) { fprintf(stderr, "ERROR: Channels do not match. %s has %i channels, %s has %i\n", infilename, inputwav.channels, responsefilename, responsewav.channels); return 1; } else if(inputwav.samplerate != responsewav.samplerate) { fprintf(stderr, "ERROR: Sample rates do not match. %s is at %uHz, %s is at %uHz\n", infilename, inputwav.samplerate, responsefilename, responsewav.samplerate); return 1; } else if(!quiet) { fprintf(stdout, "Reading %s and %s into memory\n", infilename, responsefilename); } /* Both are open, now read them into memory */ float* inputbuffer = malloc(inputwav.totalsamples * sizeof(float)); if( !tr_wavread(&inputwav, inputbuffer, inputwav.totalsamples) ) { fprintf(stderr, "ERROR: Failed reading input file\n"); return 1; } float* responsebuffer = malloc(responsewav.totalsamples * sizeof(float)); if(!tr_wavread(&responsewav, responsebuffer, responsewav.totalsamples)) { fprintf(stderr, "ERROR: Failed reading input file\n"); return 1; } /* Prepare a buffer to accept the data from our processing */ unsigned int samplesinput = inputwav.totalsamples; unsigned int samplesresponse = responsewav.totalsamples; unsigned int samplestotal = samplesinput + samplesresponse + 1; float* outputbuffer = malloc(samplestotal*sizeof(float)); float* conv = outputbuffer; unsigned int inoffset; unsigned int reoffset; unsigned int n_hi; unsigned int n_lo; if(!quiet) { fprintf(stdout, "Processing audio, please be patient\n"); } /* Do the processing (time domain convolution) */ unsigned int i; for(i = 0; i < samplestotal; i++) { n_lo = i < samplesresponse ? 0 : i - samplesresponse; n_hi = samplesinput < i ? samplesinput : i; inoffset = n_lo; reoffset = i - n_lo; *conv = 0.0f; unsigned int n; for(n = n_lo; n < n_hi; n++) { *conv += *(inputbuffer+inoffset) * *(responsebuffer+reoffset); ++inoffset; --reoffset; } conv++; } if(!quiet) { fprintf(stdout, "Normalising audio\n"); } /* Normalise the data */ conv = outputbuffer; float maxsample = 0.0f; for(i = 0; i < samplestotal; i++) { if(*conv > maxsample) { maxsample = *conv; } ++conv; } conv = outputbuffer; float normalise = 1.0f / maxsample; for(i = 0; i < samplestotal; i++) { *conv++ *= normalise; } /* Setup the output file */ FILE* outfile = fopen(outfilename, "wb"); if(outfile == 0) { fprintf(stderr, "ERROR: Failed opening output file %s\n", outfilename); return 1; } tr_wavfile outwav; tr_wavopen(outfile, &outwav, 'w'); outwav.channels = inputwav.channels; outwav.samplerate = inputwav.samplerate; outwav.bytespersample = inputwav.bytespersample; if(!quiet) { fprintf(stdout, "\n"); fprintf(stdout, "Output file : %s\n", outfilename); fprintf(stdout, " Samples : %u\n", samplestotal); fprintf(stdout, " Channels : %i\n", outwav.channels); fprintf(stdout, " Sample rate : %u\n", outwav.samplerate); fprintf(stdout, " Bytes per sample : %u\n", outwav.bytespersample); fprintf(stdout, " Duration seconds : %.2f\n", ((float)samplestotal / (float)outwav.samplerate / (float)outwav.channels)); fprintf(stdout, "\n"); fprintf(stdout, "Writing data out to %s\n", outfilename); } /* Write out data from the processing to file */ if(!tr_wavwrite(&outwav, outputbuffer, samplestotal)) { fprintf(stderr, "ERROR: Failed during write of %s. File may be malformed\n", outfilename); } /* Clean up */ free(inputbuffer); free(responsebuffer); free(outputbuffer); tr_wavclose(&inputwav); tr_wavclose(&responsewav); tr_wavclose(&outwav); fclose(infile); fclose(responsefile); fclose(outfile); return 0; }