Skip to content

Commit 1b1cddc

Browse files
author
Maciej Makowski
committed
feat: added errror handling to BaseAudioContext
1 parent 73d0410 commit 1b1cddc

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ void BiquadFilterNode::getFrequencyResponse(
8383
}
8484
}
8585

86-
float BiquadFilterNode::clamp(float value, float min, float max) {
87-
return std::min(std::max(value, min), max);
88-
}
89-
9086
void BiquadFilterNode::resetCoefficients() {
9187
x1_ = 0.0;
9288
x2_ = 0.0;
@@ -110,7 +106,7 @@ void BiquadFilterNode::setNormalizedCoefficients(
110106
}
111107

112108
void BiquadFilterNode::setLowpassCoefficients(float frequency, float Q) {
113-
frequency = clamp(frequency, 0.0, 1.0);
109+
frequency = std::clamp(frequency, 0.0f, 1.0f);
114110
if (frequency == 1.0) {
115111
setNormalizedCoefficients(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
116112
return;
@@ -133,7 +129,7 @@ void BiquadFilterNode::setLowpassCoefficients(float frequency, float Q) {
133129
}
134130

135131
void BiquadFilterNode::setHighpassCoefficients(float frequency, float Q) {
136-
frequency = clamp(frequency, 0.0, 1.0);
132+
frequency = std::clamp(frequency, 0.0f, 1.0f);
137133
if (frequency == 1.0) {
138134
setNormalizedCoefficients(0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
139135
return;
@@ -156,7 +152,7 @@ void BiquadFilterNode::setHighpassCoefficients(float frequency, float Q) {
156152
}
157153

158154
void BiquadFilterNode::setBandpassCoefficients(float frequency, float Q) {
159-
frequency = clamp(frequency, 0.0, 1.0);
155+
frequency = std::clamp(frequency, 0.0f, 1.0f);
160156
Q = std::max(0.0f, Q);
161157

162158
if (frequency <= 0.0 || frequency >= 1.0) {
@@ -178,7 +174,7 @@ void BiquadFilterNode::setBandpassCoefficients(float frequency, float Q) {
178174
}
179175

180176
void BiquadFilterNode::setLowshelfCoefficients(float frequency, float gain) {
181-
frequency = clamp(frequency, 0.0, 1.0);
177+
frequency = std::clamp(frequency, 0.0f, 1.0f);
182178
float A = std::pow(10.0f, gain / 40.0f);
183179

184180
if (frequency == 1.0) {
@@ -207,7 +203,7 @@ void BiquadFilterNode::setLowshelfCoefficients(float frequency, float gain) {
207203
}
208204

209205
void BiquadFilterNode::setHighshelfCoefficients(float frequency, float gain) {
210-
frequency = clamp(frequency, 0.0, 1.0);
206+
frequency = std::clamp(frequency, 0.0f, 1.0f);
211207
float A = std::pow(10.0f, gain / 40.0f);
212208

213209
if (frequency == 1.0) {
@@ -239,7 +235,7 @@ void BiquadFilterNode::setPeakingCoefficients(
239235
float frequency,
240236
float Q,
241237
float gain) {
242-
frequency = clamp(frequency, 0.0, 1.0);
238+
frequency = std::clamp(frequency, 0.0f, 1.0f);
243239
Q = std::max(0.0f, Q);
244240
float A = std::pow(10.0f, gain / 40.0f);
245241

@@ -267,7 +263,7 @@ void BiquadFilterNode::setPeakingCoefficients(
267263
}
268264

269265
void BiquadFilterNode::setNotchCoefficients(float frequency, float Q) {
270-
frequency = clamp(frequency, 0.0, 1.0);
266+
frequency = std::clamp(frequency, 0.0f, 1.0f);
271267
Q = std::max(0.0f, Q);
272268

273269
if (frequency <= 0.0 || frequency >= 1.0) {
@@ -288,7 +284,7 @@ void BiquadFilterNode::setNotchCoefficients(float frequency, float Q) {
288284
}
289285

290286
void BiquadFilterNode::setAllpassCoefficients(float frequency, float Q) {
291-
frequency = clamp(frequency, 0.0, 1.0);
287+
frequency = std::clamp(frequency, 0.0f, 1.0f);
292288
Q = std::max(0.0f, Q);
293289

294290
if (frequency <= 0.0 || frequency >= 1.0) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ class BiquadFilterNode : public AudioNode {
111111
float a1_ = 1.0;
112112
float a2_ = 0;
113113

114-
static float clamp(float value, float min, float max);
115114
void resetCoefficients();
116115
void setNormalizedCoefficients(
117116
float b0,

packages/react-native-audio-api/src/core/BaseAudioContext.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import StereoPannerNode from './StereoPannerNode';
77
import BiquadFilterNode from './BiquadFilterNode';
88
import AudioBufferSourceNode from './AudioBufferSourceNode';
99
import AudioBuffer from './AudioBuffer';
10+
import { RangeError } from '../errors';
1011

1112
export default class BaseAudioContext {
1213
readonly destination: AudioDestinationNode;
@@ -52,11 +53,21 @@ export default class BaseAudioContext {
5253
length: number,
5354
sampleRate: number
5455
): AudioBuffer {
55-
if (numOfChannels !== 1 && numOfChannels !== 2) {
56-
throw new Error(
57-
'The number of channels provided (' +
58-
numOfChannels +
59-
') is outside the range [1, 32]'
56+
if (numOfChannels < 1 || numOfChannels > 3) {
57+
throw new RangeError(
58+
`The number of channels provided (${numOfChannels}) is outside the range [1, 2]`
59+
);
60+
}
61+
62+
if (length <= 0) {
63+
throw new RangeError(
64+
`The number of frames provided (${length}) is less than or equal to the minimum bound (0)`
65+
);
66+
}
67+
68+
if (sampleRate <= 0) {
69+
throw new RangeError(
70+
`The sample rate provided (${sampleRate}) is outside the range [3000, 768000]`
6071
);
6172
}
6273

0 commit comments

Comments
 (0)