Skip to content

Commit 3e56266

Browse files
committed
[common] properly implement new streaming api
1 parent fcdc561 commit 3e56266

File tree

2 files changed

+31
-53
lines changed

2 files changed

+31
-53
lines changed

src/common/include/common/RnNoiseCommonPlugin.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ class RnNoiseCommonPlugin {
3232
std::shared_ptr<DenoiseState> m_denoiseState;
3333

3434
std::vector<float> m_inBuffer;
35-
size_t m_inBufferR;
36-
size_t m_inBufferW;
37-
size_t m_inBufferA;
38-
3935
std::vector<float> m_outBuffer;
4036
size_t m_outBufferR;
4137
size_t m_outBufferW;

src/common/src/RnNoiseCommonPlugin.cpp

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ void RnNoiseCommonPlugin::init() {
5555

5656
m_inBuffer .resize(k_denoiseFrameSize * 2);
5757
m_outBuffer.resize(k_denoiseFrameSize * 2);
58-
59-
m_inBufferR = 0;
60-
m_inBufferW = 0;
61-
m_inBufferA = 0;
6258
m_outBufferR = 0;
6359
m_outBufferW = 0;
6460
m_outBufferA = 0;
@@ -121,6 +117,8 @@ void RnNoiseCommonPlugin::process(const float *in, float *out, int32_t sampleFra
121117
sDn.input_frames = sampleFrames;
122118
sDn.end_of_input = 0;
123119
sDn.src_ratio = m_downRatio;
120+
sDn.data_out = &m_inBuffer[0];
121+
sDn.output_frames = m_inBuffer.size();
124122

125123
SRC_DATA sUp;
126124
sUp.data_out = out;
@@ -131,68 +129,52 @@ void RnNoiseCommonPlugin::process(const float *in, float *out, int32_t sampleFra
131129
long frames = 0;
132130
while(sDn.input_frames)
133131
{
134-
// resample the frames into a ringbuffer
135-
sDn.data_out = &m_inBuffer[m_inBufferW];
136-
sDn.output_frames = m_inBufferW < m_inBufferR ? m_inBufferR - m_inBufferW : m_inBuffer.size() - m_inBufferW;
137-
138132
if (m_resample)
133+
{
134+
// resample the samples and then scale them
139135
src_process(m_srcDown.get(), &sDn);
136+
for(long i = 0; i < sDn.output_frames_gen; ++i)
137+
m_inBuffer[i] *= std::numeric_limits<short>::max();
138+
}
140139
else
141140
{
142-
// simply copy the buffer if we are not resampling
141+
// just copy the data and scale it
143142
sDn.input_frames_used = sDn.input_frames;
144143
if (sDn.input_frames_used > sDn.output_frames)
145144
sDn.input_frames_used = sDn.output_frames;
146-
memcpy(sDn.data_out, sDn.data_in, sDn.input_frames_used * sizeof(float));
145+
sDn.output_frames_gen = sDn.input_frames_used;
146+
147+
for(long i = 0; i < sDn.output_frames_gen; ++i)
148+
m_inBuffer[i] = in[i] * std::numeric_limits<short>::max();
147149
}
148150

149151
sDn.data_in += sDn.input_frames_used;
150152
sDn.input_frames -= sDn.input_frames_used;
151153

152-
// scale the input for rnnoise
153-
for(long i = 0; i < sDn.output_frames_gen; ++i)
154-
m_inBuffer[m_inBufferW + i] *= std::numeric_limits<short>::max();
155-
156-
m_inBufferW += sDn.output_frames_gen;
157-
m_inBufferA += sDn.output_frames_gen;
158-
159-
// wrap the buffer around if needed
160-
if (m_inBufferW == m_inBuffer.size())
161-
m_inBufferW = 0;
162-
163-
// if we have enough data to denoise
164-
while (m_inBufferA >= k_denoiseFrameSize)
154+
float *denoise_in = &m_inBuffer[0];
155+
while(sDn.output_frames_gen)
165156
{
166-
int leftLen = m_inBufferW < m_inBufferR ? m_inBuffer.size() - m_inBufferR : m_inBufferW - m_inBufferR;
167-
if (leftLen > k_denoiseFrameSize)
168-
leftLen = k_denoiseFrameSize;
169-
const int rightLen = k_denoiseFrameSize - leftLen;
170-
171-
rnnoise_add_samples (m_denoiseState.get(), &m_inBuffer[m_inBufferR], leftLen );
172-
rnnoise_add_samples (m_denoiseState.get(), &m_inBuffer[0] , rightLen);
173-
rnnoise_process_frame(m_denoiseState.get(), &m_outBuffer[m_outBufferW]);
174-
175-
// scale the levels back to normal
176-
for(int32_t i = 0; i < k_denoiseFrameSize; ++i)
177-
m_outBuffer[m_outBufferW + i] *= mul;
178-
179-
m_inBufferR += k_denoiseFrameSize;
180-
m_inBufferA -= k_denoiseFrameSize;
181-
m_outBufferW += k_denoiseFrameSize;
182-
m_outBufferA += k_denoiseFrameSize;
183-
184-
if (m_inBufferR >= m_inBuffer.size())
185-
m_inBufferR -= k_denoiseFrameSize;
186-
187-
if (m_outBufferW == m_outBuffer.size())
188-
m_outBufferW = 0;
189-
157+
const int wrote = rnnoise_add_samples(m_denoiseState.get(), denoise_in, sDn.output_frames_gen);
158+
denoise_in += wrote;
159+
sDn.output_frames_gen -= wrote;
160+
161+
if (rnnoise_get_needed(m_denoiseState.get()) == 0)
162+
{
163+
rnnoise_process_frame(m_denoiseState.get(), &m_outBuffer[m_outBufferW]);
164+
165+
// scale the levels back to normal
166+
for(int32_t i = 0; i < k_denoiseFrameSize; ++i)
167+
m_outBuffer[m_outBufferW + i] *= mul;
168+
169+
m_outBufferW += k_denoiseFrameSize;
170+
m_outBufferA += k_denoiseFrameSize;
171+
if (m_outBufferW == m_outBuffer.size())
172+
m_outBufferW = 0;
173+
}
190174
resampleOut(sUp, frames);
191175
}
192176
}
193177

194-
resampleOut(sUp, frames);
195-
196178
// if we generated less frames then wanted, pad them across to the right
197179
if (frames && frames < sampleFrames)
198180
{

0 commit comments

Comments
 (0)