Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/audiodocs/docs/effects/biquad-filter-node.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ It inherits all methods from [`AudioNode`](/docs/core/audio-node#methods).
| `frequencyArray` | `Float32Array` | Array of frequencies (in Hz), which you want to filter. |
| `magResponseOutput` | `Float32Array` | Output array to store the computed linear magnitude values for each frequency. For frequencies outside the range [0, $\frac{sampleRate}{2}$], the corresponding results are NaN. |
| `phaseResponseOutput` | `Float32Array` | Output array to store the computed phase response values (in radians) for each frequency. For frequencies outside the range [0, $\frac{sampleRate}{2}$], the corresponding results are NaN. |
| `length` | `number` | The number of frequency values to process. It should match the length of all input and output arrays. |

#### Returns `undefined`.

Expand All @@ -93,4 +92,4 @@ Numerically: Q ≈ 770.63678.
- Positive values correspond to amplification; negative to attenuation.

#### `type`
- [`BiquadFilterType`](#biquadfiltertype-enumeration-description). Default: `"lowpass"`.
- [`BiquadFilterType`](#biquadfiltertype-enumeration-description). Default: `"lowpass"`.
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
/*
* Copyright (C) 2010 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <audioapi/core/BaseAudioContext.h>
#include <audioapi/core/effects/BiquadFilterNode.h>
#include <audioapi/utils/AudioArray.h>
Expand All @@ -13,14 +41,14 @@ BiquadFilterNode::BiquadFilterNode(BaseAudioContext *context)
frequencyParam_ = std::make_shared<AudioParam>(
350.0, 0.0f, context->getNyquistFrequency(), context);
detuneParam_ = std::make_shared<AudioParam>(
0.0,
0.0f,
-1200 * LOG2_MOST_POSITIVE_SINGLE_FLOAT,
1200 * LOG2_MOST_POSITIVE_SINGLE_FLOAT,
context);
QParam_ = std::make_shared<AudioParam>(
1.0, MOST_NEGATIVE_SINGLE_FLOAT, MOST_POSITIVE_SINGLE_FLOAT, context);
1.0f, MOST_NEGATIVE_SINGLE_FLOAT, MOST_POSITIVE_SINGLE_FLOAT, context);
gainParam_ = std::make_shared<AudioParam>(
0.0,
0.0f,
MOST_NEGATIVE_SINGLE_FLOAT,
40 * LOG10_MOST_POSITIVE_SINGLE_FLOAT,
context);
Expand Down Expand Up @@ -77,8 +105,6 @@ void BiquadFilterNode::getFrequencyResponse(
float *magResponseOutput,
float *phaseResponseOutput,
const int length) {
applyFilter();

// Local copies for micro-optimization
float b0 = b0_;
float b1 = b1_;
Expand All @@ -87,14 +113,15 @@ void BiquadFilterNode::getFrequencyResponse(
float a2 = a2_;

for (size_t i = 0; i < length; i++) {
if (frequencyArray[i] < 0.0 || frequencyArray[i] > 1.0) {
if (frequencyArray[i] < 0.0f || frequencyArray[i] > 1.0f) {
// Out-of-bounds frequencies should return NaN.
magResponseOutput[i] = std::nanf("");
phaseResponseOutput[i] = std::nanf("");
continue;
}

auto omega = -PI * frequencyArray[i] / context_->getNyquistFrequency();
auto z = std::complex<float>(cos(omega), sin(omega));
auto omega = -PI * frequencyArray[i];
auto z = std::complex<float>(std::cos(omega), std::sin(omega));
auto response = (b0 + (b1 + b2 * z) * z) /
(std::complex<float>(1, 0) + (a1 + a2 * z) * z);
magResponseOutput[i] = static_cast<float>(std::abs(response));
Expand All @@ -120,12 +147,12 @@ void BiquadFilterNode::setNormalizedCoefficients(

void BiquadFilterNode::setLowpassCoefficients(float frequency, float Q) {
// Limit frequency to [0, 1] range
if (frequency >= 1.0) {
if (frequency >= 1.0f) {
setNormalizedCoefficients(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}

if (frequency <= 0.0) {
if (frequency <= 0.0f) {
setNormalizedCoefficients(0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}
Expand All @@ -143,11 +170,11 @@ void BiquadFilterNode::setLowpassCoefficients(float frequency, float Q) {
}

void BiquadFilterNode::setHighpassCoefficients(float frequency, float Q) {
if (frequency >= 1.0) {
if (frequency >= 1.0f) {
setNormalizedCoefficients(0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}
if (frequency <= 0.0) {
if (frequency <= 0.0f) {
setNormalizedCoefficients(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}
Expand All @@ -166,13 +193,13 @@ void BiquadFilterNode::setHighpassCoefficients(float frequency, float Q) {

void BiquadFilterNode::setBandpassCoefficients(float frequency, float Q) {
// Limit frequency to [0, 1] range
if (frequency <= 0.0 || frequency >= 1.0) {
if (frequency <= 0.0f || frequency >= 1.0f) {
setNormalizedCoefficients(0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}

// Limit Q to positive values
if (Q <= 0.0) {
if (Q <= 0.0f) {
setNormalizedCoefficients(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}
Expand All @@ -188,12 +215,12 @@ void BiquadFilterNode::setBandpassCoefficients(float frequency, float Q) {
void BiquadFilterNode::setLowshelfCoefficients(float frequency, float gain) {
float A = std::pow(10.0f, gain / 40.0f);

if (frequency >= 1.0) {
if (frequency >= 1.0f) {
setNormalizedCoefficients(A * A, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}

if (frequency <= 0.0) {
if (frequency <= 0.0f) {
setNormalizedCoefficients(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}
Expand All @@ -215,12 +242,12 @@ void BiquadFilterNode::setLowshelfCoefficients(float frequency, float gain) {
void BiquadFilterNode::setHighshelfCoefficients(float frequency, float gain) {
float A = std::pow(10.0f, gain / 40.0f);

if (frequency >= 1.0) {
if (frequency >= 1.0f) {
setNormalizedCoefficients(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}

if (frequency <= 0.0) {
if (frequency <= 0.0f) {
setNormalizedCoefficients(A * A, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}
Expand All @@ -247,12 +274,12 @@ void BiquadFilterNode::setPeakingCoefficients(
float gain) {
float A = std::pow(10.0f, gain / 40.0f);

if (frequency <= 0.0 || frequency >= 1.0) {
if (frequency <= 0.0f || frequency >= 1.0f) {
setNormalizedCoefficients(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}

if (Q <= 0.0) {
if (Q <= 0.0f) {
setNormalizedCoefficients(A * A, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}
Expand All @@ -271,12 +298,12 @@ void BiquadFilterNode::setPeakingCoefficients(
}

void BiquadFilterNode::setNotchCoefficients(float frequency, float Q) {
if (frequency <= 0.0 || frequency >= 1.0) {
if (frequency <= 0.0f || frequency >= 1.0f) {
setNormalizedCoefficients(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}

if (Q <= 0.0) {
if (Q <= 0.0f) {
setNormalizedCoefficients(0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}
Expand All @@ -290,12 +317,12 @@ void BiquadFilterNode::setNotchCoefficients(float frequency, float Q) {
}

void BiquadFilterNode::setAllpassCoefficients(float frequency, float Q) {
if (frequency <= 0.0 || frequency >= 1.0) {
if (frequency <= 0.0f || frequency >= 1.0f) {
setNormalizedCoefficients(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}

if (Q <= 0.0) {
if (Q <= 0.0f) {
setNormalizedCoefficients(-1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
return;
}
Expand All @@ -318,6 +345,9 @@ void BiquadFilterNode::applyFilter() {
auto Q = QParam_->processKRateParam(RENDER_QUANTUM_SIZE, currentTime);
auto gain = gainParam_->processKRateParam(RENDER_QUANTUM_SIZE, currentTime);

// NyquistFrequency is half of the sample rate.
// Normalized frequency is therefore:
// frequency / (sampleRate / 2) = (2 * frequency) / sampleRate
float normalizedFrequency = frequency / context_->getNyquistFrequency();
if (detune != 0.0f) {
normalizedFrequency *= std::pow(2.0f, detune / 1200.0f);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
/*
* Copyright (C) 2010 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include <audioapi/core/AudioNode.h>
#include <audioapi/core/AudioParam.h>
#include <audioapi/core/types/BiquadFilterType.h>
#ifdef AUDIO_API_TEST_SUITE
#include <gtest/gtest_prod.h>
#endif

#include <algorithm>
#include <cmath>
Expand All @@ -17,6 +48,11 @@ namespace audioapi {
class AudioBus;

class BiquadFilterNode : public AudioNode {
#ifdef AUDIO_API_TEST_SUITE
friend class BiquadFilterTest;
FRIEND_TEST(BiquadFilterTest, GetFrequencyResponse);
#endif

public:
explicit BiquadFilterNode(BaseAudioContext *context);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <numbers>
#include <cmath>
#include <limits>

Expand All @@ -19,7 +20,7 @@ static constexpr float MOST_POSITIVE_SINGLE_FLOAT = static_cast<float>(std::nume
static constexpr float MOST_NEGATIVE_SINGLE_FLOAT = static_cast<float>(std::numeric_limits<float>::lowest());
static float LOG2_MOST_POSITIVE_SINGLE_FLOAT = std::log2(MOST_POSITIVE_SINGLE_FLOAT);
static float LOG10_MOST_POSITIVE_SINGLE_FLOAT = std::log10(MOST_POSITIVE_SINGLE_FLOAT);
static constexpr float PI = static_cast<float>(M_PI);
static constexpr float PI = std::numbers::pi_v<float>;

// buffer sizes
static constexpr size_t PROMISE_VENDOR_THREAD_POOL_WORKER_COUNT = 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/3983f67e32fb3e9294487b9d4f9586efa6e5d088.zip
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
Expand Down Expand Up @@ -57,6 +58,8 @@ target_include_directories(rnaudioapi PUBLIC
${JSI_DIR}
"${REACT_NATIVE_DIR}/ReactCommon"
"${REACT_NATIVE_DIR}/ReactCommon/callinvoker"
${gtest_SOURCE_DIR}/include
${gmock_SOURCE_DIR}/include
)

target_include_directories(rnaudioapi_libs PUBLIC
Expand Down
Loading