/* Copyright (c) 2024 Jean-Marc Valin * Copyright (c) 2017 Mozilla */ /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include #include #include #include #include "rnnoise.h" #include "common.h" #include "denoise.h" #include "arch.h" #include "kiss_fft.h" #include "src/_kiss_fft_guts.h" int lowpass = FREQ_SIZE; int band_lp = NB_BANDS; #define SEQUENCE_LENGTH 2000 #define SEQUENCE_SAMPLES (SEQUENCE_LENGTH*FRAME_SIZE) #define RIR_FFT_SIZE 65536 #define RIR_MAX_DURATION (RIR_FFT_SIZE/2) #define FILENAME_MAX_SIZE 1000 struct rir_list { int nb_rirs; int block_size; kiss_fft_state *fft; kiss_fft_cpx **rir; kiss_fft_cpx **early; }; kiss_fft_cpx *load_rir(const char *rir_file, kiss_fft_state *fft, int early) { kiss_fft_cpx *x, *X; float rir[RIR_MAX_DURATION]; int len; int i; FILE *f; f = fopen(rir_file, "rb"); if (f==NULL) { fprintf(stderr, "cannot open %s: %s\n", rir_file, strerror(errno)); exit(1); } x = (kiss_fft_cpx*)calloc(fft->nfft, sizeof(*x)); X = (kiss_fft_cpx*)calloc(fft->nfft, sizeof(*X)); len = fread(rir, sizeof(*rir), RIR_MAX_DURATION, f); if (early) { for (i=0;i<240;i++) { rir[480+i] *= (1 - i/240.f); } RNN_CLEAR(&rir[240+480], RIR_MAX_DURATION-240-480); } for (i=0;inb_rirs = 0; allocated = 2; rirs->fft = rnn_fft_alloc_twiddles(RIR_FFT_SIZE, NULL, NULL, NULL, 0); rirs->rir = malloc(allocated*sizeof(rirs->rir[0])); rirs->early = malloc(allocated*sizeof(rirs->early[0])); while (fgets(rir_filename, FILENAME_MAX_SIZE, f) != NULL) { /* Chop trailing newline. */ rir_filename[strcspn(rir_filename, "\n")] = 0; if (rirs->nb_rirs+1 > allocated) { allocated *= 2; rirs->rir = realloc(rirs->rir, allocated*sizeof(rirs->rir[0])); rirs->early = realloc(rirs->early, allocated*sizeof(rirs->early[0])); } rirs->rir[rirs->nb_rirs] = load_rir(rir_filename, rirs->fft, 0); rirs->early[rirs->nb_rirs] = load_rir(rir_filename, rirs->fft, 1); rirs->nb_rirs++; } fclose(f); } void rir_filter_sequence(const struct rir_list *rirs, float *audio, int rir_id, int early) { int i; kiss_fft_cpx x[RIR_FFT_SIZE] = {{0,0}}; kiss_fft_cpx y[RIR_FFT_SIZE] = {{0,0}}; kiss_fft_cpx X[RIR_FFT_SIZE] = {{0,0}}; const kiss_fft_cpx *Y; if (early) Y = rirs->early[rir_id]; else Y = rirs->rir[rir_id]; i=0; while (ifft, x, X); for (j=0;jfft, X, y); for (j=0;j0) { float r, theta; r = rand()/(double)RAND_MAX; r = .7*r*r; theta = rand()/(double)RAND_MAX; theta = M_PI*theta*theta; a[0] = -2*r*cos(theta); a[1] = r*r; } else { float r0,r1; r0 = 1.4*uni_rand(); r1 = 1.4*uni_rand(); a[0] = -r0-r1; a[1] = r0*r1; } } static void rand_resp(float *a, float *b) { rand_filt(a); rand_filt(b); } short speech16[SEQUENCE_LENGTH*FRAME_SIZE]; short noise16[SEQUENCE_LENGTH*FRAME_SIZE]; short fgnoise16[SEQUENCE_LENGTH*FRAME_SIZE]; float x[SEQUENCE_LENGTH*FRAME_SIZE]; float n[SEQUENCE_LENGTH*FRAME_SIZE]; float fn[SEQUENCE_LENGTH*FRAME_SIZE]; float xn[SEQUENCE_LENGTH*FRAME_SIZE]; #define P00 0.99f #define P01 0.01f #define P10 0.01f #define P11 0.99f #define LOGIT_SCALE 0.5f static void viterbi_vad(const float *E, int *vad) { int i; float Enoise, Esig; int back[SEQUENCE_LENGTH][2]; float curr; Enoise = Esig = 1e-30; for (i=0;i (1-curr)*P01) { back[i][1] = 1; prior = curr*P11; } else { back[i][1] = 0; prior = (1-curr)*P01; } pspeech = prior*p0; if ((1-curr)*P00 > curr*P10) { back[i][0] = 0; prior = (1-curr)*P00; } else { back[i][0] = 1; prior = curr*P10; } pnoise = prior*(1-p0); curr = pspeech / (pspeech + pnoise); /*printf("%f ", curr);*/ } vad[SEQUENCE_LENGTH-1] = curr > .5; for (i=SEQUENCE_LENGTH-2;i>=0;i--) { if (vad[i+1]) { vad[i] = back[i+1][1]; } else { vad[i] = back[i+1][0]; } } for (i=0;i=1;i--) { if (vad[i-1]) vad[i] = 1; } } static void clear_vad(float *x, int *vad) { int i; int active = vad[0]; for (i=0;i=1 && vad[i]==0 && vad[i-1]==0) { int j; for (j=0;j6) { if (strcmp(argv[1], "-rir_list")==0) { rir_filename = argv[2]; argv+=2; argc-=2; } } if (argc!=6) { fprintf(stderr, "usage: %s [-rir_list list] \n", argv0); return 1; } f1 = fopen(argv[1], "rb"); f2 = fopen(argv[2], "rb"); f3 = fopen(argv[3], "rb"); fout = fopen(argv[4], "wb"); fseek(f1, 0, SEEK_END); speech_length = ftell(f1); fseek(f1, 0, SEEK_SET); fseek(f2, 0, SEEK_END); noise_length = ftell(f2); fseek(f2, 0, SEEK_SET); fseek(f3, 0, SEEK_END); fgnoise_length = ftell(f3); fseek(f3, 0, SEEK_SET); maxCount = atoi(argv[5]); if (rir_filename) load_rir_list(rir_filename, &rirs); for (count=0;count speech_length-(long)sizeof(speech16)) speech_pos = speech_length-sizeof(speech16); if (noise_pos > noise_length-(long)sizeof(noise16)) noise_pos = noise_length-sizeof(noise16); if (fgnoise_pos > fgnoise_length-(long)sizeof(fgnoise16)) fgnoise_pos = fgnoise_length-sizeof(fgnoise16); speech_pos -= speech_pos&1; noise_pos -= noise_pos&1; fgnoise_pos -= fgnoise_pos&1; fseek(f1, speech_pos, SEEK_SET); fseek(f2, noise_pos, SEEK_SET); fseek(f3, fgnoise_pos, SEEK_SET); fread(speech16, sizeof(speech16), 1, f1); fread(noise16, sizeof(noise16), 1, f2); fread(fgnoise16, sizeof(fgnoise16), 1, f3); if (rand()%4) start_pos = 0; else start_pos = -(int)(1000*log(rand()/(float)RAND_MAX)); start_pos = IMIN(start_pos, SEQUENCE_LENGTH*FRAME_SIZE); speech_gain = pow(10., (-45+randf(45.f)+randf(10.f))/20.); noise_gain = pow(10., (-30+randf(40.f)+randf(15.f))/20.); fgnoise_gain = pow(10., (-30+randf(40.f)+randf(15.f))/20.); if (rand()%8==0) noise_gain = 0; if (rand()%8!=0) fgnoise_gain = 0; if (rand()%12==0) { noise_gain *= 0.03; fgnoise_gain *= 0.03; } noise_gain *= speech_gain; fgnoise_gain *= speech_gain; rand_resp(a_noise, b_noise); rand_resp(a_fgnoise, b_fgnoise); rand_resp(a_sig, b_sig); lowpass = FREQ_SIZE * 3000./24000. * pow(50., rand()/(double)RAND_MAX); for (i=0;i lowpass) { band_lp = i; break; } } for (frame=0;frame 1) g[i] = 1; if (silence || i > band_lp) g[i] = -1; if (Ey[i] < 5e-2 && Ex[i] < 5e-2) g[i] = -1; if (vad_target==0 && noise_gain==0 && fgnoise_gain==0) g[i] = -1; } #if 0 { short tmp[FRAME_SIZE]; for (j=0;j