From 7f5736063d900c3962d62f08f42a81a351ec89d3 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Tue, 12 Mar 2019 16:05:23 +1100 Subject: [PATCH] [rnnoise] alter library to accept streamed samples --- src/rnnoise/include/rnnoise/rnnoise.h | 7 ++++++- src/rnnoise/src/denoise.c | 30 +++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/rnnoise/include/rnnoise/rnnoise.h b/src/rnnoise/include/rnnoise/rnnoise.h index 8e41cc51..002c87c2 100644 --- a/src/rnnoise/include/rnnoise/rnnoise.h +++ b/src/rnnoise/include/rnnoise/rnnoise.h @@ -52,7 +52,12 @@ RNNOISE_EXPORT DenoiseState *rnnoise_create(); RNNOISE_EXPORT void rnnoise_destroy(DenoiseState *st); -RNNOISE_EXPORT float rnnoise_process_frame(DenoiseState *st, float *out, const float *in); +RNNOISE_EXPORT int rnnoise_get_needed(DenoiseState *st); + +RNNOISE_EXPORT int rnnoise_add_samples(DenoiseState *st, const float *in, int in_len); + +RNNOISE_EXPORT float rnnoise_process_frame(DenoiseState *st, float *out); + #ifdef __cplusplus } diff --git a/src/rnnoise/src/denoise.c b/src/rnnoise/src/denoise.c index caf9a899..57e3e1d0 100644 --- a/src/rnnoise/src/denoise.c +++ b/src/rnnoise/src/denoise.c @@ -85,6 +85,9 @@ typedef struct { } CommonState; struct DenoiseState { + float input[FRAME_SIZE]; + int input_pos; + float analysis_mem[FRAME_SIZE]; float cepstral_mem[CEPS_MEM][NB_BANDS]; int memid; @@ -469,11 +472,27 @@ void pitch_filter(kiss_fft_cpx *X, const kiss_fft_cpx *P, const float *Ex, const } } -float rnnoise_process_frame(DenoiseState *st, float *out, const float *in) { +int rnnoise_get_needed(DenoiseState *st) { + return FRAME_SIZE - st->input_pos; +} + +int rnnoise_add_samples(DenoiseState *st, const float *in, int in_len) { + static const float a_hp[2] = {-1.99599, 0.99600}; + static const float b_hp[2] = {-2, 1}; + + const int needed = FRAME_SIZE - st->input_pos; + const int take = needed > in_len ? in_len : needed; + + biquad(st->input + st->input_pos, st->mem_hp_x, in, b_hp, a_hp, take); + st->input_pos += take; + + return take; +} + +float rnnoise_process_frame(DenoiseState *st, float *out) { int i; kiss_fft_cpx X[FREQ_SIZE]; kiss_fft_cpx P[WINDOW_SIZE]; - float x[FRAME_SIZE]; float Ex[NB_BANDS], Ep[NB_BANDS]; float Exp[NB_BANDS]; float features[NB_FEATURES]; @@ -481,10 +500,9 @@ float rnnoise_process_frame(DenoiseState *st, float *out, const float *in) { float gf[FREQ_SIZE]={1}; float vad_prob = 0; int silence; - static const float a_hp[2] = {-1.99599, 0.99600}; - static const float b_hp[2] = {-2, 1}; - biquad(x, st->mem_hp_x, in, b_hp, a_hp, FRAME_SIZE); - silence = compute_frame_features(st, X, P, Ex, Ep, Exp, features, x); + + silence = compute_frame_features(st, X, P, Ex, Ep, Exp, features, st->input); + st->input_pos = 0; if (!silence) { compute_rnn(&st->rnn, g, &vad_prob, features);