Skip to content

Commit 2787143

Browse files
fix: fixed typos and oscillator perf fix (#990)
* fix: fixed typos * refactor: refined processing loop in OscillatorNode for better performance and readability * fix: constants refinement * ci: formatting * refactor: static constexpr to inline constexpr fo global constants --------- Co-authored-by: maciejmakowski2003 <maciejmakowski2003@users.noreply.github.com>
1 parent efba6a3 commit 2787143

6 files changed

Lines changed: 41 additions & 43 deletions

File tree

packages/custom-node-generator/templates/basic/shared/MyProcessorNode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace audioapi {
44
MyProcessorNode::MyProcessorNode(
5-
const std::shared_ptr<BaseAudioContext> &context, )
5+
const std::shared_ptr<BaseAudioContext> &context)
66
: AudioNode(context) {
77
isInitialized_.store(true, std::memory_order_release);
88
}

packages/custom-node-generator/templates/basic/shared/MyProcessorNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace audioapi {
77

88
class MyProcessorNode : public AudioNode {
99
public:
10-
explicit MyProcessorNode(const std::shared_ptr<BaseAudioContext> &context, );
10+
explicit MyProcessorNode(const std::shared_ptr<BaseAudioContext> &context);
1111

1212
protected:
1313
std::shared_ptr<DSPAudioBuffer>

packages/react-native-audio-api/common/cpp/audioapi/core/sources/OscillatorNode.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <audioapi/core/BaseAudioContext.h>
22
#include <audioapi/core/sources/OscillatorNode.h>
3+
#include <audioapi/core/utils/Constants.h>
34
#include <audioapi/dsp/AudioUtils.hpp>
45
#include <audioapi/types/NodeOptions.h>
56
#include <audioapi/utils/AudioArray.hpp>
@@ -75,42 +76,38 @@ std::shared_ptr<DSPAudioBuffer> OscillatorNode::processNode(
7576
}
7677

7778
auto time =
78-
context->getCurrentTime() + static_cast<double>(startOffset) * 1.0 / context->getSampleRate();
79+
context->getCurrentTime() + static_cast<double>(startOffset) / context->getSampleRate();
7980
auto detuneSpan = detuneParam_->processARateParam(framesToProcess, time)->getChannel(0)->span();
8081
auto freqSpan = frequencyParam_->processARateParam(framesToProcess, time)->getChannel(0)->span();
8182

8283
const auto tableSize = static_cast<float>(periodicWave_->getPeriodicWaveSize());
8384
const auto tableScale = periodicWave_->getScale();
8485
const auto numChannels = processingBuffer->getNumberOfChannels();
8586

86-
auto finalPhase = phase_;
87+
auto channelSpan = processingBuffer->getChannel(0)->span();
88+
float currentPhase = phase_;
8789

88-
for (size_t ch = 0; ch < numChannels; ch += 1) {
89-
auto channelSpan = processingBuffer->getChannel(ch)->span();
90-
float currentPhase = phase_;
90+
for (size_t i = startOffset; i < offsetLength; i += 1) {
91+
auto detuneRatio = detuneSpan[i] == 0 ? 1.0f : exp2f(detuneSpan[i] * CENTS_TO_RATIO);
92+
auto detunedFrequency = freqSpan[i] * detuneRatio;
93+
auto phaseIncrement = detunedFrequency * tableScale;
9194

92-
for (size_t i = startOffset; i < offsetLength; i += 1) {
93-
auto detuneRatio = detuneSpan[i] == 0 ? 1.0f : std::pow(2.0f, detuneSpan[i] / 1200.0f);
94-
auto detunedFrequency = freqSpan[i] * detuneRatio;
95-
auto phaseIncrement = detunedFrequency * tableScale;
95+
channelSpan[i] = periodicWave_->getSample(detunedFrequency, currentPhase, phaseIncrement);
9696

97-
channelSpan[i] = periodicWave_->getSample(detunedFrequency, currentPhase, phaseIncrement);
97+
currentPhase += phaseIncrement;
9898

99-
currentPhase += phaseIncrement;
100-
101-
if (currentPhase >= tableSize) {
102-
currentPhase -= tableSize;
103-
} else if (currentPhase < 0.0f) {
104-
currentPhase += tableSize;
105-
}
106-
}
107-
108-
if (ch == 0) {
109-
finalPhase = currentPhase;
99+
if (currentPhase >= tableSize) {
100+
currentPhase -= tableSize;
101+
} else if (currentPhase < 0.0f) {
102+
currentPhase += tableSize;
110103
}
111104
}
112105

113-
phase_ = finalPhase;
106+
phase_ = currentPhase;
107+
108+
for (size_t ch = 1; ch < numChannels; ch += 1) {
109+
processingBuffer->getChannel(ch)->copy(*processingBuffer->getChannel(0));
110+
}
114111
handleStopScheduled();
115112

116113
return processingBuffer;

packages/react-native-audio-api/common/cpp/audioapi/core/utils/Constants.h

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,31 @@
99

1010
namespace audioapi {
1111
// audio
12-
static constexpr int RENDER_QUANTUM_SIZE = 128;
13-
static constexpr size_t MAX_FFT_SIZE = 32768;
14-
static constexpr int MAX_CHANNEL_COUNT = 32;
15-
static constexpr float DEFAULT_SAMPLE_RATE = 44100.0f;
16-
static constexpr int OCTAVE_RANGE = 1200;
17-
static constexpr int BIQUAD_GAIN_DB_FACTOR = 40;
12+
inline constexpr int RENDER_QUANTUM_SIZE = 128;
13+
inline constexpr size_t MAX_FFT_SIZE = 32768;
14+
inline constexpr int MAX_CHANNEL_COUNT = 32;
15+
inline constexpr float DEFAULT_SAMPLE_RATE = 44100.0f;
16+
inline constexpr int OCTAVE_RANGE = 1200;
17+
inline constexpr int BIQUAD_GAIN_DB_FACTOR = 40;
18+
inline constexpr float CENTS_TO_RATIO = 1.0f / 1200.0f;
1819

1920
// stretcher
20-
static constexpr float UPPER_FREQUENCY_LIMIT_DETECTION = 333.0f;
21-
static constexpr float LOWER_FREQUENCY_LIMIT_DETECTION = 55.0f;
21+
inline constexpr float UPPER_FREQUENCY_LIMIT_DETECTION = 333.0f;
22+
inline constexpr float LOWER_FREQUENCY_LIMIT_DETECTION = 55.0f;
2223

2324
// general
24-
static constexpr float MOST_POSITIVE_SINGLE_FLOAT =
25+
inline constexpr float MOST_POSITIVE_SINGLE_FLOAT =
2526
static_cast<float>(std::numeric_limits<float>::max());
26-
static constexpr float MOST_NEGATIVE_SINGLE_FLOAT =
27+
inline constexpr float MOST_NEGATIVE_SINGLE_FLOAT =
2728
static_cast<float>(std::numeric_limits<float>::lowest());
28-
static float LOG2_MOST_POSITIVE_SINGLE_FLOAT = std::log2(MOST_POSITIVE_SINGLE_FLOAT);
29-
static float LOG10_MOST_POSITIVE_SINGLE_FLOAT = std::log10(MOST_POSITIVE_SINGLE_FLOAT);
30-
static constexpr float PI = std::numbers::pi_v<float>;
29+
inline float LOG2_MOST_POSITIVE_SINGLE_FLOAT = std::log2(MOST_POSITIVE_SINGLE_FLOAT);
30+
inline float LOG10_MOST_POSITIVE_SINGLE_FLOAT = std::log10(MOST_POSITIVE_SINGLE_FLOAT);
31+
inline constexpr float PI = std::numbers::pi_v<float>;
3132

3233
// buffer sizes
33-
static constexpr size_t PROMISE_VENDOR_THREAD_POOL_WORKER_COUNT = 4;
34-
static constexpr size_t PROMISE_VENDOR_THREAD_POOL_LOAD_BALANCER_QUEUE_SIZE = 32;
35-
static constexpr size_t PROMISE_VENDOR_THREAD_POOL_WORKER_QUEUE_SIZE = 32;
34+
inline constexpr size_t PROMISE_VENDOR_THREAD_POOL_WORKER_COUNT = 4;
35+
inline constexpr size_t PROMISE_VENDOR_THREAD_POOL_LOAD_BALANCER_QUEUE_SIZE = 32;
36+
inline constexpr size_t PROMISE_VENDOR_THREAD_POOL_WORKER_QUEUE_SIZE = 32;
3637

3738
// Cache line size
3839
#ifdef __cpp_lib_hardware_interference_size

packages/react-native-audio-api/common/cpp/audioapi/utils/AudioArray.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ class AlignedAudioArray {
271271
size_t size_ = 0;
272272
};
273273

274-
static constexpr size_t kDSPAlignment = 64;
274+
inline constexpr size_t DSP_ALIGNMENT = 64;
275275
using AudioArray = AlignedAudioArray<alignof(std::max_align_t)>;
276-
using DSPAudioArray = AlignedAudioArray<kDSPAlignment>;
276+
using DSPAudioArray = AlignedAudioArray<DSP_ALIGNMENT>;
277277

278278
} // namespace audioapi

packages/react-native-audio-api/common/cpp/audioapi/utils/AudioBuffer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,6 @@ class AlignedAudioBuffer {
453453
};
454454

455455
using AudioBuffer = AlignedAudioBuffer<alignof(std::max_align_t)>;
456-
using DSPAudioBuffer = AlignedAudioBuffer<kDSPAlignment>;
456+
using DSPAudioBuffer = AlignedAudioBuffer<DSP_ALIGNMENT>;
457457

458458
} // namespace audioapi

0 commit comments

Comments
 (0)