Skip to content

Commit 0991e05

Browse files
committed
fix: performance
1 parent 3f12af9 commit 0991e05

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

packages/react-native-audio-api/common/cpp/audioapi/core/effects/IIRFilterNode.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ IIRFilterNode::IIRFilterNode(
3838
isInitialized_ = true;
3939
channelCountMode_ = ChannelCountMode::MAX;
4040

41-
x_.resize(IIRFilterNode::bufferLength, 0.0f);
42-
y_.resize(IIRFilterNode::bufferLength, 0.0f);
41+
int maxChannels = 32; // TODO: magic number, jaki jest faktyczy Channels?
42+
xBuffers_.resize(maxChannels);
43+
yBuffers_.resize(maxChannels);
44+
45+
for (int c = 0; c < maxChannels; ++c) {
46+
xBuffers_[c].resize(bufferLength, 0.0f);
47+
yBuffers_[c].resize(bufferLength, 0.0f);
48+
}
4349

4450
size_t feedforwardLength = feedforward_.size();
4551
size_t feedbackLength = feedback_.size();
@@ -108,6 +114,8 @@ void IIRFilterNode::getFrequencyResponse(
108114
// y[n] = sum(b[k] * x[n - k], k = 0, M) - sum(a[k] * y[n - k], k = 1, N)
109115
// where b[k] are the feedforward coefficients and a[k] are the feedback coefficients of the filter
110116

117+
// TODO: tail
118+
111119
std::shared_ptr<AudioBus> IIRFilterNode::processNode(
112120
const std::shared_ptr<AudioBus> &processingBus,
113121
int framesToProcess) {
@@ -117,34 +125,36 @@ std::shared_ptr<AudioBus> IIRFilterNode::processNode(
117125
size_t feedbackLength = feedback_.size();
118126
int minLength = std::min(feedbackLength, feedforwardLength);
119127

128+
int mask = bufferLength - 1;
129+
120130
for (int c = 0; c < numChannels; ++c) {
121131
// for (int c = 0; c < 1; ++c) {
122132
auto channelData = processingBus->getChannel(c)->getData();
123133
// auto channelData2 = processingBus->getChannel(1)->getData();
134+
auto &x = xBuffers_[c];
135+
auto &y = yBuffers_[c];
124136

125137
for (int n = 0; n < framesToProcess; ++n) {
126138
float yn = feedforward_[0] * channelData[n];
127139

128140
for (int k = 1; k < minLength; ++k) {
129-
int m = (m_bufferIndex - k) & (bufferLength - 1);
130-
yn += feedforward_[k] * x_[m];
131-
yn -= feedback_[k] * y_[m];
141+
int m = (n - k) & mask;
142+
yn += feedforward_[k] * x[m];
143+
yn -= feedback_[k] * y[m];
132144
}
133145

134146
for (int k = minLength; k < feedforwardLength; ++k) {
135-
yn += feedforward_[k] * x_[(m_bufferIndex - k) & (bufferLength - 1)];
147+
yn += feedforward_[k] * x[(n - k) & mask];
136148
}
137149
for (int k = minLength; k < feedbackLength; ++k) {
138-
yn -= feedback_[k] * y_[(m_bufferIndex - k) & (bufferLength - 1)];
150+
yn -= feedback_[k] * y[(n - k) & mask];
139151
}
140152

141153
channelData[n] = yn;
142154
// channelData2[n] = yn;
143155

144-
x_[m_bufferIndex] = channelData[n];
145-
y_[m_bufferIndex] = yn;
146-
147-
m_bufferIndex = (m_bufferIndex + 1) & (bufferLength - 1);
156+
x[n & mask] = channelData[n];
157+
y[n & mask] = yn;
148158
}
149159
}
150160
return processingBus;

packages/react-native-audio-api/common/cpp/audioapi/core/effects/IIRFilterNode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class IIRFilterNode : public AudioNode {
5959
std::vector<float> feedforward_;
6060
std::vector<float> feedback_;
6161

62-
std::vector<float> x_;
63-
std::vector<float> y_;
62+
std::vector<std::vector<float>> xBuffers_; // xBuffers_[channel][index]
63+
std::vector<std::vector<float>> yBuffers_;
6464

6565
static std::complex<float> evaluatePolynomial(
6666
const std::vector<float> coefficients,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ class IIRFilterTest : public ::testing::Test {
9191
}
9292
};
9393

94+
TEST_F(IIRFilterTest, IIRFilterCanBeCreated) {
95+
const std::vector<float> feedforward = {1.0};
96+
const std::vector<float> feedback = {1.0};
97+
auto node = context->createIIRFilter(feedforward, feedback);
98+
ASSERT_NE(node, nullptr);
99+
}
100+
94101
TEST_F(IIRFilterTest, GetFrequencyResponse) {
95102
const std::vector<float> feedforward = {
96103
0.0050662636, 0.0101325272, 0.0050662636};

0 commit comments

Comments
 (0)