/* Copyright (c) 2018 Gregor Richards * 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 "kiss_fft.h" #include "common.h" #include #include "rnnoise.h" #include "pitch.h" #include "arch.h" #include "rnn.h" #include "rnn_data.h" #define FRAME_SIZE_SHIFT 2 #define FRAME_SIZE (120<rnn.model = model; else st->rnn.model = &rnnoise_model_orig; st->rnn.vad_gru_state = calloc(sizeof(float), st->rnn.model->vad_gru_size); st->rnn.noise_gru_state = calloc(sizeof(float), st->rnn.model->noise_gru_size); st->rnn.denoise_gru_state = calloc(sizeof(float), st->rnn.model->denoise_gru_size); return 0; } DenoiseState *rnnoise_create(RNNModel *model) { DenoiseState *st; st = malloc(rnnoise_get_size()); rnnoise_init(st, model); return st; } void rnnoise_destroy(DenoiseState *st) { free(st->rnn.vad_gru_state); free(st->rnn.noise_gru_state); free(st->rnn.denoise_gru_state); free(st); } #if TRAINING int lowpass = FREQ_SIZE; int band_lp = NB_BANDS; #endif static void frame_analysis(DenoiseState *st, kiss_fft_cpx *X, float *Ex, const float *in) { int i; float x[WINDOW_SIZE]; RNN_COPY(x, st->analysis_mem, FRAME_SIZE); for (i=0;ianalysis_mem, in, FRAME_SIZE); apply_window(x); forward_transform(X, x); #if TRAINING for (i=lowpass;i>1]; int pitch_index; float gain; float *(pre[1]); float tmp[NB_BANDS]; float follow, logMax; frame_analysis(st, X, Ex, in); RNN_MOVE(st->pitch_buf, &st->pitch_buf[FRAME_SIZE], PITCH_BUF_SIZE-FRAME_SIZE); RNN_COPY(&st->pitch_buf[PITCH_BUF_SIZE-FRAME_SIZE], in, FRAME_SIZE); pre[0] = &st->pitch_buf[0]; rnnoise_pitch_downsample(pre, pitch_buf, PITCH_BUF_SIZE, 1); rnnoise_pitch_search(pitch_buf+(PITCH_MAX_PERIOD>>1), pitch_buf, PITCH_FRAME_SIZE, PITCH_MAX_PERIOD-3*PITCH_MIN_PERIOD, &pitch_index); pitch_index = PITCH_MAX_PERIOD-pitch_index; gain = rnnoise_remove_doubling(pitch_buf, PITCH_MAX_PERIOD, PITCH_MIN_PERIOD, PITCH_FRAME_SIZE, &pitch_index, st->last_period, st->last_gain); st->last_period = pitch_index; st->last_gain = gain; for (i=0;ipitch_buf[PITCH_BUF_SIZE-WINDOW_SIZE-pitch_index+i]; apply_window(p); forward_transform(P, p); compute_band_energy(Ep, P); compute_band_corr(Exp, X, P); for (i=0;icepstral_mem[st->memid]; ceps_1 = (st->memid < 1) ? st->cepstral_mem[CEPS_MEM+st->memid-1] : st->cepstral_mem[st->memid-1]; ceps_2 = (st->memid < 2) ? st->cepstral_mem[CEPS_MEM+st->memid-2] : st->cepstral_mem[st->memid-2]; for (i=0;imemid++; for (i=0;imemid == CEPS_MEM) st->memid = 0; for (i=0;icepstral_mem[i][k] - st->cepstral_mem[j][k]; dist += tmp*tmp; } if (j!=i) mindist = MIN32(mindist, dist); } spec_variability += mindist; } features[NB_BANDS+3*NB_DELTA_CEPS+1] = spec_variability/CEPS_MEM-2.1; return TRAINING && E < 0.1; } static void frame_synthesis(DenoiseState *st, float *out, const kiss_fft_cpx *y) { float x[WINDOW_SIZE]; int i; inverse_transform(x, y); apply_window(x); for (i=0;isynthesis_mem[i]; RNN_COPY(st->synthesis_mem, &x[FRAME_SIZE], FRAME_SIZE); } static void biquad(float *y, float mem[2], const float *x, const float *b, const float *a, int N) { int i; for (i=0;ig[i]) r[i] = 1; else r[i] = Exp[i]*(1-g[i])/(.001 + g[i]*(1-Exp[i])); r[i] = MIN16(1, MAX16(0, r[i])); #else if (Exp[i]>g[i]) r[i] = 1; else r[i] = SQUARE(Exp[i])*(1-SQUARE(g[i]))/(.001 + SQUARE(g[i])*(1-SQUARE(Exp[i]))); r[i] = sqrt(MIN16(1, MAX16(0, r[i]))); #endif r[i] *= sqrt(Ex[i]/(1e-8+Ep[i])); } interp_band_gain(rf, r); for (i=0;imem_hp_x, in, b_hp, a_hp, FRAME_SIZE); silence = compute_frame_features(st, X, P, Ex, Ep, Exp, features, x); if (!silence) { compute_rnn(&st->rnn, g, &vad_prob, features); pitch_filter(X, P, Ex, Ep, Exp, g); for (i=0;ilastg[i]); st->lastg[i] = g[i]; } interp_band_gain(gf, g); #if 1 for (i=0;i \n", argv[0]); return 1; } f1 = fopen(argv[1], "r"); f2 = fopen(argv[2], "r"); maxCount = atoi(argv[3]); for(i=0;i<150;i++) { short tmp[FRAME_SIZE]; fread(tmp, sizeof(short), FRAME_SIZE, f2); } while (1) { kiss_fft_cpx X[FREQ_SIZE], Y[FREQ_SIZE], N[FREQ_SIZE], P[WINDOW_SIZE]; float Ex[NB_BANDS], Ey[NB_BANDS], En[NB_BANDS], Ep[NB_BANDS]; float Exp[NB_BANDS]; float Ln[NB_BANDS]; float features[NB_FEATURES]; float g[NB_BANDS]; short tmp[FRAME_SIZE]; float vad=0; float E=0; if (count==maxCount) break; if ((count%1000)==0) fprintf(stderr, "%d\r", count); if (++gain_change_count > 2821) { speech_gain = pow(10., (-40+(rand()%60))/20.); noise_gain = pow(10., (-30+(rand()%50))/20.); if (rand()%10==0) noise_gain = 0; noise_gain *= speech_gain; if (rand()%10==0) speech_gain = 0; gain_change_count = 0; rand_resp(a_noise, b_noise); 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; } } } if (speech_gain != 0) { fread(tmp, sizeof(short), FRAME_SIZE, f1); if (feof(f1)) { rewind(f1); fread(tmp, sizeof(short), FRAME_SIZE, f1); } for (i=0;i 1e9f) { vad_cnt=0; } else if (E > 1e8f) { vad_cnt -= 5; } else if (E > 1e7f) { vad_cnt++; } else { vad_cnt+=2; } if (vad_cnt < 0) vad_cnt = 0; if (vad_cnt > 15) vad_cnt = 15; if (vad_cnt >= 10) vad = 0; else if (vad_cnt > 0) vad = 0.5f; else vad = 1.f; frame_analysis(st, Y, Ey, x); frame_analysis(noise_state, N, En, n); for (i=0;ilast_gain, noisy->last_period); for (i=0;i 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==0 && noise_gain==0) g[i] = -1; } count++; #if 1 fwrite(features, sizeof(float), NB_FEATURES, stdout); fwrite(g, sizeof(float), NB_BANDS, stdout); fwrite(Ln, sizeof(float), NB_BANDS, stdout); fwrite(&vad, sizeof(float), 1, stdout); #endif } fprintf(stderr, "matrix size: %d x %d\n", count, NB_FEATURES + 2*NB_BANDS + 1); fclose(f1); fclose(f2); return 0; } #endif