/* Copyright (c) 2017-2018 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 "freq.h" #include "pitch.h" #include "arch.h" #include "celt_lpc.h" #include #define PITCH_MIN_PERIOD 32 #define PITCH_MAX_PERIOD 256 #define PITCH_FRAME_SIZE 320 #define PITCH_BUF_SIZE (PITCH_MAX_PERIOD+PITCH_FRAME_SIZE) #define CEPS_MEM 8 #define NB_DELTA_CEPS 6 #define NB_FEATURES (2*NB_BANDS+3+LPC_ORDER) typedef struct { float analysis_mem[OVERLAP_SIZE]; float cepstral_mem[CEPS_MEM][NB_BANDS]; float pitch_buf[PITCH_BUF_SIZE]; float last_gain; int last_period; } DenoiseState; static int rnnoise_get_size() { return sizeof(DenoiseState); } static int rnnoise_init(DenoiseState *st) { memset(st, 0, sizeof(*st)); return 0; } static DenoiseState *rnnoise_create() { DenoiseState *st; st = malloc(rnnoise_get_size()); rnnoise_init(st); return st; } static void rnnoise_destroy(DenoiseState *st) { free(st); } static short float2short(float x) { int i; i = (int)floor(.5+x); return IMAX(-32767, IMIN(32767, i)); } int lowpass = FREQ_SIZE; int band_lp = NB_BANDS; 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, OVERLAP_SIZE); RNN_COPY(&x[OVERLAP_SIZE], in, FRAME_SIZE); RNN_COPY(st->analysis_mem, &in[FRAME_SIZE-OVERLAP_SIZE], OVERLAP_SIZE); apply_window(x); forward_transform(X, x); for (i=lowpass;ipitch_buf, &st->pitch_buf[FRAME_SIZE], PITCH_BUF_SIZE-FRAME_SIZE); RNN_COPY(&st->pitch_buf[PITCH_BUF_SIZE-FRAME_SIZE], in, FRAME_SIZE); RNN_COPY(pitch_buf, &st->pitch_buf[0], PITCH_BUF_SIZE); pitch_downsample(pitch_buf, PITCH_BUF_SIZE); pitch_search(pitch_buf+PITCH_MAX_PERIOD, pitch_buf, PITCH_FRAME_SIZE<<1, (PITCH_MAX_PERIOD-3*PITCH_MIN_PERIOD)<<1, &pitch_index); pitch_index = 2*PITCH_MAX_PERIOD-pitch_index; gain = remove_doubling(pitch_buf, 2*PITCH_MAX_PERIOD, 2*PITCH_MIN_PERIOD, 2*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/2+i]; apply_window(p); forward_transform(P, p); compute_band_energy(Ep, P); compute_band_corr(Exp, X, P); for (i=0;i \n", argv[0]); fprintf(stderr, " or %s -test \n", argv[0]); return 1; } f1 = fopen(argv[2], "r"); if (f1 == NULL) { fprintf(stderr,"Error opening input .s16 16kHz speech input file: %s\n", argv[2]); exit(1); } ffeat = fopen(argv[3], "w"); if (ffeat == NULL) { fprintf(stderr,"Error opening output feature file: %s\n", argv[3]); exit(1); } if (training) { fpcm = fopen(argv[4], "w"); if (fpcm == NULL) { fprintf(stderr,"Error opening output PCM file: %s\n", argv[4]); exit(1); } } while (1) { kiss_fft_cpx X[FREQ_SIZE], P[WINDOW_SIZE]; float Ex[NB_BANDS], Ep[NB_BANDS]; float Exp[NB_BANDS]; float features[NB_FEATURES]; float E=0; int silent; for (i=0;i=5000000 && one_pass_completed) break; if (training && ++gain_change_count > 2821) { speech_gain = pow(10., (-20+(rand()%40))/20.); if (rand()%20==0) speech_gain *= .01; if (rand()%100==0) speech_gain = 0; gain_change_count = 0; rand_resp(a_sig, b_sig); } biquad(x, mem_hp_x, x, b_hp, a_hp, FRAME_SIZE); biquad(x, mem_resp_x, x, b_sig, a_sig, FRAME_SIZE); preemphasis(x, &mem_preemph, x, PREEMPHASIS, FRAME_SIZE); for (i=0;i