Skip to content

Commit 3966d60

Browse files
maciejmakowski2003Maciej Makowski
andauthored
Fix/unpushed changes (#279)
* refactor: refactored types and constants in AudioArray * refactor: refactored types and constants in AnalyserNode * refactor: refactored types and constants in AudioBus * fix: added necessary include * refactor: refactored types and constants in AudioBuffer * refactor: refactored types and constants in AudioBufferSourceNode * refactor: refactored types and constants in BaseAudioContext * refactor: refactored types and constants in AudioContext * refactor: refactored types and constants in AudioDecoder and AudioDestinationNode * refactor: refactored types and constants in AudioNode * refactor: refactored types and constants in AudioNodeManager * refactor: refactored types and constants in AudioParam * refactor: refactored types and constants in AudioScheduledSourceNode * refactor: refactored types and constants in BiquadFilterNode * refactor: refactored types and constants in GainNode * refactor: refactored types and constants in OscillatorNode * refactor: refactored types and constants in StereoPannerNode * refactor: refactored types and constants in PeriodicWave * refactor: refactored types in utils * ci: yarn format * fix: fixed analyser types * refactor: host objects * fix: fixed analyser node * refactor: refactored ios audio player * ci: yarn format * refactor: replaced M_PI with our PI constant --------- Co-authored-by: Maciej Makowski <maciej.makowski2608@gmail.com>
1 parent 147e724 commit 3966d60

File tree

7 files changed

+27
-29
lines changed

7 files changed

+27
-29
lines changed

packages/react-native-audio-api/common/cpp/HostObjects/AnalyserNodeHostObject.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,25 +126,25 @@ class AnalyserNodeHostObject : public AudioNodeHostObject {
126126

127127
JSI_PROPERTY_SETTER(fftSize) {
128128
auto analyserNode = std::static_pointer_cast<AnalyserNode>(node_);
129-
auto fftSize = static_cast<size_t>(value.getNumber());
129+
auto fftSize = static_cast<int>(value.getNumber());
130130
analyserNode->setFftSize(fftSize);
131131
}
132132

133133
JSI_PROPERTY_SETTER(minDecibels) {
134134
auto analyserNode = std::static_pointer_cast<AnalyserNode>(node_);
135-
auto minDecibels = static_cast<int>(value.getNumber());
135+
auto minDecibels = static_cast<float>(value.getNumber());
136136
analyserNode->setMinDecibels(minDecibels);
137137
}
138138

139139
JSI_PROPERTY_SETTER(maxDecibels) {
140140
auto analyserNode = std::static_pointer_cast<AnalyserNode>(node_);
141-
auto maxDecibels = static_cast<int>(value.getNumber());
141+
auto maxDecibels = static_cast<float>(value.getNumber());
142142
analyserNode->setMaxDecibels(maxDecibels);
143143
}
144144

145145
JSI_PROPERTY_SETTER(smoothingTimeConstant) {
146146
auto analyserNode = std::static_pointer_cast<AnalyserNode>(node_);
147-
auto smoothingTimeConstant = static_cast<int>(value.getNumber());
147+
auto smoothingTimeConstant = static_cast<float>(value.getNumber());
148148
analyserNode->setSmoothingTimeConstant(smoothingTimeConstant);
149149
}
150150
};

packages/react-native-audio-api/common/cpp/core/AnalyserNode.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void AnalyserNode::getByteFrequencyData(uint8_t *data, int length) {
8080
doFFTAnalysis();
8181

8282
auto magnitudeBufferData = magnitudeBuffer_->getData();
83-
length = std::min(static_cast<int>(magnitudeBuffer_->getSize()), length);
83+
length = std::min(static_cast<int>(magnitudeBuffer_->getSize()), length);
8484

8585
const auto rangeScaleFactor =
8686
maxDecibels_ == minDecibels_ ? 1 : 1 / (maxDecibels_ - minDecibels_);
@@ -103,7 +103,7 @@ void AnalyserNode::getByteFrequencyData(uint8_t *data, int length) {
103103
}
104104

105105
void AnalyserNode::getFloatTimeDomainData(float *data, int length) {
106-
auto size = fftSize_ ? fftSize_ < length : length;
106+
auto size = std::min(fftSize_, length);
107107

108108
for (int i = 0; i < size; i++) {
109109
data[i] = inputBuffer_->getData()
@@ -113,7 +113,7 @@ void AnalyserNode::getFloatTimeDomainData(float *data, int length) {
113113
}
114114

115115
void AnalyserNode::getByteTimeDomainData(uint8_t *data, int length) {
116-
auto size = fftSize_ ? fftSize_ < length : length;
116+
auto size = std::min(fftSize_, length);
117117

118118
for (int i = 0; i < size; i++) {
119119
auto value = inputBuffer_->getData()
@@ -148,7 +148,7 @@ void AnalyserNode::processNode(
148148

149149
if (vWriteIndex_ + framesToProcess > inputBuffer_->getSize()) {
150150
auto framesToCopy =
151-
static_cast<int>(inputBuffer_->getSize() - vWriteIndex_);
151+
static_cast<int>(inputBuffer_->getSize()) - vWriteIndex_;
152152
memcpy(
153153
inputBuffer_->getData() + vWriteIndex_,
154154
downMixBus_->getChannel(0)->getData(),
@@ -229,8 +229,7 @@ void AnalyserNode::applyWindow(float *data, int length) {
229229

230230
for (int i = 0; i < length; ++i) {
231231
auto x = static_cast<float>(i) / static_cast<float>(length);
232-
auto window = a0 - a1 * cos(2 * static_cast<float>(M_PI) * x) +
233-
a2 * cos(4 * static_cast<float>(M_PI) * x);
232+
auto window = a0 - a1 * cos(2 * PI * x) + a2 * cos(4 * PI * x);
234233
data[i] *= window;
235234
}
236235
}

packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ void BiquadFilterNode::getFrequencyResponse(
6767
float a2 = a2_;
6868

6969
for (size_t i = 0; i < frequencyArraySize; i++) {
70-
auto omega = static_cast<float>(M_PI) * frequencyArray[i] /
71-
context_->getNyquistFrequency();
70+
auto omega = PI * frequencyArray[i] / context_->getNyquistFrequency();
7271
auto z = std::complex<float>(cos(omega), sin(omega));
7372
auto response = ((b0 * z + b1) * z + b2) / ((z + a1) * z + a2);
7473
magResponseOutput[i] = static_cast<float>(abs(response));
@@ -113,7 +112,7 @@ void BiquadFilterNode::setLowpassCoefficients(float frequency, float Q) {
113112
Q = std::max(0.0f, Q);
114113
float g = std::pow(10.0f, 0.05f * Q);
115114

116-
float theta = M_PI * frequency;
115+
float theta = PI * frequency;
117116
float alpha = std::sin(theta) / (2 * g);
118117
float cosW = std::cos(theta);
119118
float beta = (1 - cosW) / 2;
@@ -136,7 +135,7 @@ void BiquadFilterNode::setHighpassCoefficients(float frequency, float Q) {
136135
Q = std::max(0.0f, Q);
137136
float g = std::pow(10.0f, 0.05f * Q);
138137

139-
float theta = M_PI * frequency;
138+
float theta = PI * frequency;
140139
float alpha = std::sin(theta) / (2 * g);
141140
float cosW = std::cos(theta);
142141
float beta = (1 - cosW) / 2;
@@ -159,7 +158,7 @@ void BiquadFilterNode::setBandpassCoefficients(float frequency, float Q) {
159158
return;
160159
}
161160

162-
float w0 = M_PI * frequency;
161+
float w0 = PI * frequency;
163162
float alpha = std::sin(w0) / (2 * Q);
164163
float k = std::cos(w0);
165164

@@ -181,7 +180,7 @@ void BiquadFilterNode::setLowshelfCoefficients(float frequency, float gain) {
181180
return;
182181
}
183182

184-
float w0 = M_PI * frequency;
183+
float w0 = PI * frequency;
185184
float alpha =
186185
0.5f * std::sin(w0) * std::sqrt((A + 1 / A) * (1 / 1.0f - 1) + 2);
187186
float k = std::cos(w0);
@@ -210,7 +209,7 @@ void BiquadFilterNode::setHighshelfCoefficients(float frequency, float gain) {
210209
return;
211210
}
212211

213-
float w0 = M_PI * frequency;
212+
float w0 = PI * frequency;
214213
float alpha =
215214
0.5f * std::sin(w0) * std::sqrt((A + 1 / A) * (1 / 1.0f - 1) + 2);
216215
float k = std::cos(w0);
@@ -243,7 +242,7 @@ void BiquadFilterNode::setPeakingCoefficients(
243242
return;
244243
}
245244

246-
float w0 = M_PI * frequency;
245+
float w0 = PI * frequency;
247246
float alpha = std::sin(w0) / (2 * Q);
248247
float k = std::cos(w0);
249248

@@ -270,7 +269,7 @@ void BiquadFilterNode::setNotchCoefficients(float frequency, float Q) {
270269
return;
271270
}
272271

273-
float w0 = M_PI * frequency;
272+
float w0 = PI * frequency;
274273
float alpha = std::sin(w0) / (2 * Q);
275274
float k = std::cos(w0);
276275

@@ -291,7 +290,7 @@ void BiquadFilterNode::setAllpassCoefficients(float frequency, float Q) {
291290
return;
292291
}
293292

294-
float w0 = M_PI * frequency;
293+
float w0 = PI * frequency;
295294
float alpha = std::sin(w0) / (2 * Q);
296295
float k = std::cos(w0);
297296

packages/react-native-audio-api/ios/core/AudioPlayer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ typedef void (^RenderAudioBlock)(AudioBufferList *outputBuffer, int numFrames);
1212
@property (nonatomic, strong) AVAudioFormat *format;
1313
@property (nonatomic, strong) AVAudioSourceNode *sourceNode;
1414
@property (nonatomic, copy) RenderAudioBlock renderAudio;
15-
@property (nonatomic, assign) int sampleRate;
15+
@property (nonatomic, assign) float sampleRate;
1616

1717
- (instancetype)initWithRenderAudioBlock:(RenderAudioBlock)renderAudio;
1818

19-
- (instancetype)initWithRenderAudioBlock:(RenderAudioBlock)renderAudio sampleRate:(int)sampleRate;
19+
- (instancetype)initWithRenderAudioBlock:(RenderAudioBlock)renderAudio sampleRate:(float)sampleRate;
2020

21-
- (int)getSampleRate;
21+
- (float)getSampleRate;
2222

2323
- (void)start;
2424

packages/react-native-audio-api/ios/core/AudioPlayer.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ - (instancetype)initWithRenderAudioBlock:(RenderAudioBlock)renderAudio
4949
return self;
5050
}
5151

52-
- (instancetype)initWithRenderAudioBlock:(RenderAudioBlock)renderAudio sampleRate:(int)sampleRate
52+
- (instancetype)initWithRenderAudioBlock:(RenderAudioBlock)renderAudio sampleRate:(float)sampleRate
5353
{
5454
if (self = [super init]) {
5555
self.renderAudio = [renderAudio copy];
@@ -96,7 +96,7 @@ - (instancetype)initWithRenderAudioBlock:(RenderAudioBlock)renderAudio sampleRat
9696
return self;
9797
}
9898

99-
- (int)getSampleRate
99+
- (float)getSampleRate
100100
{
101101
return self.sampleRate;
102102
}

packages/react-native-audio-api/ios/core/IOSAudioPlayer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class IOSAudioPlayer {
2424
const std::function<void(AudioBus *, int)> &renderAudio);
2525
IOSAudioPlayer(
2626
const std::function<void(AudioBus *, int)> &renderAudio,
27-
int sampleRate);
27+
float sampleRate);
2828
~IOSAudioPlayer();
2929

30-
int getSampleRate() const;
30+
float getSampleRate() const;
3131

3232
void start();
3333
void stop();

packages/react-native-audio-api/ios/core/IOSAudioPlayer.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
audioBus_ = new AudioBus([audioPlayer_ getSampleRate], RENDER_QUANTUM_SIZE, CHANNEL_COUNT);
3838
}
3939

40-
IOSAudioPlayer::IOSAudioPlayer(const std::function<void(AudioBus *, int)> &renderAudio, int sampleRate)
40+
IOSAudioPlayer::IOSAudioPlayer(const std::function<void(AudioBus *, int)> &renderAudio, float sampleRate)
4141
: renderAudio_(renderAudio), audioBus_(0)
4242
{
4343
RenderAudioBlock renderAudioBlock = ^(AudioBufferList *outputData, int numFrames) {
@@ -86,7 +86,7 @@
8686
return [audioPlayer_ stop];
8787
}
8888

89-
int IOSAudioPlayer::getSampleRate() const
89+
float IOSAudioPlayer::getSampleRate() const
9090
{
9191
return [audioPlayer_ getSampleRate];
9292
}

0 commit comments

Comments
 (0)