int main(int argc, char **argv) { SNDFILE *fin, *fin2, *fout; SF_INFO input_info, input_info2; float *win, *in, *in2, *out, *spec, *spec2; int outsize, specsize, fftsize=1024, hopsize=256; int dataratio = fftsize/hopsize; int frames, extraframes; int read=0, written = 0, i, j, dur; float *buff; if(argc < 3) { printf("%s: unsufficient number of arguments(got %d, needed 2)", argv[0], argc-1); usage(); exit(-1); } if(!(fin = sf_open(argv[1], SFM_READ, &input_info))) { printf("could not open %s", argv[1]); exit(-1); } if(!(fin2 = sf_open(argv[2], SFM_READ, &input_info2))) { printf("could not open %s", argv[2]); exit(-1); } if(!formats_equal(input_info, input_info2)) { sf_close(fin2); sf_close(fin); printf("%s and %s formats are not equal or files not mono\n", argv[1], argv[2]); exit(-1); } dur = input_info.frames > input_info2.frames ? input_info2.frames : input_info.frames; buff = (float*)malloc(sizeof(float)*100); win = (float*)malloc(sizeof(float)*fftsize); in = (float*)malloc(sizeof(float)*dur); in2 = (float*)malloc(sizeof(float)*dur); // number of full dataframes frames = ((dur*dataratio)/fftsize); // extra frames [not completely filled] extraframes = 1 + (frames*fftsize - dur*dataratio)/fftsize; // size of spectral data specsize = (frames + extraframes)*fftsize; spec = (float*)malloc(sizeof(float)*specsize); spec2 = (float*)malloc(sizeof(float)*specsize); outsize = specsize/dataratio; out = (float*)malloc(sizeof(float)*outsize); if(!(fout = sf_open(argv[3], SFM_WRITE, &input_info))) { printf("could not open %s \n", argv[3]); exit(-1); } // generera fönster for(i=0; i < fftsize; i++) win[i] = 0.5f - (float)(0.5*cos(i*twopi/fftsize)); // läs in filer for(j=0; j < dur; j+=read) { read = sf_read_float(fin, buff, 100); for(i=0;i<read;i++) in[i+j] = buff[i]; } for(j=0; j < dur; j+=read) { read = sf_read_float(fin2, buff, 100); for(i=0;i<read;i++) in2[i+j] = buff[i]; } outsize = stft(in, win, spec, dur, fftsize, hopsize); stft(in2, win, spec2, dur, fftsize, hopsize); for(i=0; i < outsize; i+= fftsize) crosspec(&spec[i], &spec2[i], &spec[i], fftsize); dur = istft(spec, win, out, outsize, fftsize, hopsize); for(j=0; j < dur; j+= written) { for(i=0; i < 100 && j < dur; i++) { if(i+j < dur) buff[i] = out[i+j]; else buff[i] = 0.f; } written = sf_write_float(fout, buff, i); } free(out); free(in); free(in2); free(spec); free(spec2); free(win); free(buff); sf_close(fout); sf_close(fin); sf_close(fin2); return 0; }
int main(int argc, char** argv) { int showhelp = 0; unsigned int channels = 1; int window_size = 1024; int shift = 256; snd_pcm_format_t format = SND_PCM_FORMAT_UNKNOWN; int opt; while ((opt = getopt(argc, argv, "hw:s:f:")) != -1) { switch (opt) { case 'h': showhelp = 1; break; case 'w': window_size = atoi(optarg); break; case 's': shift = atoi(optarg); break; case 'f': format = snd_pcm_format_value(optarg); break; default: /* '?' */ showhelp = 1; break; } } if (showhelp || argc - optind < 2) { fprintf(stderr, "Usage: %s [-w window_size] [-s shift] " "[-f format] <inputFile> <outputFile>\n" , argv[0]); exit(EXIT_SUCCESS); } if (format == SND_PCM_FORMAT_UNKNOWN) { fprintf(stderr, "Unknown format\n"); exit(EXIT_FAILURE); } // Load Audio File FILE* input = fopen(argv[optind], "r"); if (input == NULL) { fprintf(stderr, "Cannot Open File %s : %s\n", argv[optind], strerror(errno)); exit(EXIT_FAILURE); } double** data; unsigned long int count = pcm_size(input, channels, format); count = read_file(input, count, channels, format, &data); fprintf(stderr, "%lu samples read\n", count); fclose(input); // Transform int nos = number_of_spectrum(count, window_size, shift); TimeFreq* tf = alloc_tf(window_size, nos); stft(data[0], count, window_size, shift, tf); free(data[0]); // Spectral subtraction TimeFreq* ntf = alloc_tf(window_size, nos); do_filter(zerophase, NULL, tf, ntf); // Inverse transform count = istft_size(tf); data[0] = malloc(count * sizeof(double)); istft(ntf, data[0]); FILE* output = fopen(argv[optind + 1], "w"); if (output == NULL) { fprintf(stderr, "Cannot Open File %s : %s\n", argv[optind + 1], strerror(errno)); exit(EXIT_FAILURE); } count = write_file(output, count, channels, format, data); fclose(output); free_data(data, channels); free_tf(tf); free_tf(ntf); exit(EXIT_SUCCESS); }