Skip to content

Commit

Permalink
[common] properly implement new streaming api
Browse files Browse the repository at this point in the history
  • Loading branch information
gnif committed Mar 11, 2019
1 parent fcdc561 commit 3e56266
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 53 deletions.
4 changes: 0 additions & 4 deletions src/common/include/common/RnNoiseCommonPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class RnNoiseCommonPlugin {
std::shared_ptr<DenoiseState> m_denoiseState;

std::vector<float> m_inBuffer;
size_t m_inBufferR;
size_t m_inBufferW;
size_t m_inBufferA;

std::vector<float> m_outBuffer;
size_t m_outBufferR;
size_t m_outBufferW;
Expand Down
80 changes: 31 additions & 49 deletions src/common/src/RnNoiseCommonPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<short>::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<short>::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<short>::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)
{
Expand Down

0 comments on commit 3e56266

Please sign in to comment.