diff --git a/src/common/include/common/RnNoiseCommonPlugin.h b/src/common/include/common/RnNoiseCommonPlugin.h index 12b23a08..51456f4e 100644 --- a/src/common/include/common/RnNoiseCommonPlugin.h +++ b/src/common/include/common/RnNoiseCommonPlugin.h @@ -32,10 +32,6 @@ class RnNoiseCommonPlugin { std::shared_ptr m_denoiseState; std::vector m_inBuffer; - size_t m_inBufferR; - size_t m_inBufferW; - size_t m_inBufferA; - std::vector m_outBuffer; size_t m_outBufferR; size_t m_outBufferW; diff --git a/src/common/src/RnNoiseCommonPlugin.cpp b/src/common/src/RnNoiseCommonPlugin.cpp index 20d0690b..8bc38281 100644 --- a/src/common/src/RnNoiseCommonPlugin.cpp +++ b/src/common/src/RnNoiseCommonPlugin.cpp @@ -55,10 +55,6 @@ void RnNoiseCommonPlugin::init() { m_inBuffer .resize(k_denoiseFrameSize * 2); m_outBuffer.resize(k_denoiseFrameSize * 2); - - m_inBufferR = 0; - m_inBufferW = 0; - m_inBufferA = 0; m_outBufferR = 0; m_outBufferW = 0; m_outBufferA = 0; @@ -121,6 +117,8 @@ void RnNoiseCommonPlugin::process(const float *in, float *out, int32_t sampleFra sDn.input_frames = sampleFrames; sDn.end_of_input = 0; sDn.src_ratio = m_downRatio; + sDn.data_out = &m_inBuffer[0]; + sDn.output_frames = m_inBuffer.size(); SRC_DATA sUp; sUp.data_out = out; @@ -131,68 +129,52 @@ void RnNoiseCommonPlugin::process(const float *in, float *out, int32_t sampleFra long frames = 0; while(sDn.input_frames) { - // resample the frames into a ringbuffer - sDn.data_out = &m_inBuffer[m_inBufferW]; - sDn.output_frames = m_inBufferW < m_inBufferR ? m_inBufferR - m_inBufferW : m_inBuffer.size() - m_inBufferW; - if (m_resample) + { + // resample the samples and then scale them src_process(m_srcDown.get(), &sDn); + for(long i = 0; i < sDn.output_frames_gen; ++i) + m_inBuffer[i] *= std::numeric_limits::max(); + } else { - // simply copy the buffer if we are not resampling + // just copy the data and scale it sDn.input_frames_used = sDn.input_frames; if (sDn.input_frames_used > sDn.output_frames) sDn.input_frames_used = sDn.output_frames; - memcpy(sDn.data_out, sDn.data_in, sDn.input_frames_used * sizeof(float)); + sDn.output_frames_gen = sDn.input_frames_used; + + for(long i = 0; i < sDn.output_frames_gen; ++i) + m_inBuffer[i] = in[i] * std::numeric_limits::max(); } sDn.data_in += sDn.input_frames_used; sDn.input_frames -= sDn.input_frames_used; - // scale the input for rnnoise - for(long i = 0; i < sDn.output_frames_gen; ++i) - m_inBuffer[m_inBufferW + i] *= std::numeric_limits::max(); - - m_inBufferW += sDn.output_frames_gen; - m_inBufferA += sDn.output_frames_gen; - - // wrap the buffer around if needed - if (m_inBufferW == m_inBuffer.size()) - m_inBufferW = 0; - - // if we have enough data to denoise - while (m_inBufferA >= k_denoiseFrameSize) + float *denoise_in = &m_inBuffer[0]; + while(sDn.output_frames_gen) { - int leftLen = m_inBufferW < m_inBufferR ? m_inBuffer.size() - m_inBufferR : m_inBufferW - m_inBufferR; - if (leftLen > k_denoiseFrameSize) - leftLen = k_denoiseFrameSize; - const int rightLen = k_denoiseFrameSize - leftLen; - - rnnoise_add_samples (m_denoiseState.get(), &m_inBuffer[m_inBufferR], leftLen ); - rnnoise_add_samples (m_denoiseState.get(), &m_inBuffer[0] , rightLen); - rnnoise_process_frame(m_denoiseState.get(), &m_outBuffer[m_outBufferW]); - - // scale the levels back to normal - for(int32_t i = 0; i < k_denoiseFrameSize; ++i) - m_outBuffer[m_outBufferW + i] *= mul; - - m_inBufferR += k_denoiseFrameSize; - m_inBufferA -= k_denoiseFrameSize; - m_outBufferW += k_denoiseFrameSize; - m_outBufferA += k_denoiseFrameSize; - - if (m_inBufferR >= m_inBuffer.size()) - m_inBufferR -= k_denoiseFrameSize; - - if (m_outBufferW == m_outBuffer.size()) - m_outBufferW = 0; - + const int wrote = rnnoise_add_samples(m_denoiseState.get(), denoise_in, sDn.output_frames_gen); + denoise_in += wrote; + sDn.output_frames_gen -= wrote; + + if (rnnoise_get_needed(m_denoiseState.get()) == 0) + { + rnnoise_process_frame(m_denoiseState.get(), &m_outBuffer[m_outBufferW]); + + // scale the levels back to normal + for(int32_t i = 0; i < k_denoiseFrameSize; ++i) + m_outBuffer[m_outBufferW + i] *= mul; + + m_outBufferW += k_denoiseFrameSize; + m_outBufferA += k_denoiseFrameSize; + if (m_outBufferW == m_outBuffer.size()) + m_outBufferW = 0; + } resampleOut(sUp, frames); } } - resampleOut(sUp, frames); - // if we generated less frames then wanted, pad them across to the right if (frames && frames < sampleFrames) {