diff --git a/.claude/skills/utilities/SKILL.md b/.claude/skills/utilities/SKILL.md index 002193a060..57fbb27b0d 100644 --- a/.claude/skills/utilities/SKILL.md +++ b/.claude/skills/utilities/SKILL.md @@ -31,6 +31,8 @@ Constructors copy data — `AudioArray` always owns its buffer. Holds N channels of `AudioArrayBuffer`. Handles up/down-mixing automatically on `sum()` and `copy()`. +`sum()` and `copy()` between different channel counts apply the speaker mixing gains (4 → 2 halves, 4 → 1 quarters). When the extra channels are a node's own intermediate results and not a speaker layout (`ConvolverNode` true stereo), sum channel by channel with `getChannel(i)->sum(...)`. + Key operations: `getChannel(index)`, `getSharedChannel(index)`, `zero()`, `sum(source, interpretation)`, `copy(source)`, `deinterleaveFrom(float*, frames)`, `interleaveTo(float*, frames)`, `normalize()`, `scale(float)`. Channel layout constants: `ChannelType` (namespace scope, shared by every alignment): `MONO=0`, `LEFT=0`, `RIGHT=1`, `CENTER=2`, `LFE=3`, `SURROUND_LEFT=4`, `SURROUND_RIGHT=5`. diff --git a/packages/react-native-audio-api/common/cpp/audioapi/core/effects/ConvolverNode.cpp b/packages/react-native-audio-api/common/cpp/audioapi/core/effects/ConvolverNode.cpp index e76e4ea959..7f14b2b17e 100644 --- a/packages/react-native-audio-api/common/cpp/audioapi/core/effects/ConvolverNode.cpp +++ b/packages/react-native-audio-api/common/cpp/audioapi/core/effects/ConvolverNode.cpp @@ -91,6 +91,10 @@ float ConvolverNode::calculateNormalizationScale(const std::shared_ptrgetSampleRate(); + // True-stereo compensation, as in Chrome. + if (numberOfChannels == 4) { + power *= 0.5f; + } return power; } @@ -124,7 +128,18 @@ void ConvolverNode::processNode(int framesToProcess) { if (internalBufferIndex_ < framesToProcess) { performConvolution(audioBuffer_); // reads from audioBuffer_, result goes to intermediateBuffer_ audioBuffer_->zero(); - audioBuffer_->sum(*intermediateBuffer_); + if (intermediateBuffer_->getNumberOfChannels() == 4) { + // outputChannelMap_ puts the left terms in 0 and 2 and the right terms in 1 and 3, + // so channel % busChannels folds them at unity; a mono bus averages the pair. + const size_t busChannels = audioBuffer_->getNumberOfChannels(); + const float gain = busChannels == 1 ? 0.5f : 1.0f; + for (size_t channel = 0; channel < 4; ++channel) { + audioBuffer_->getChannel(channel % busChannels) + ->sum(*intermediateBuffer_->getChannel(channel), 0, 0, RENDER_QUANTUM_SIZE, gain); + } + } else { + audioBuffer_->sum(*intermediateBuffer_); + } internalBuffer_->copy(*audioBuffer_, 0, internalBufferIndex_, RENDER_QUANTUM_SIZE); internalBufferIndex_ += RENDER_QUANTUM_SIZE; diff --git a/packages/react-native-audio-api/common/cpp/test/src/core/effects/ConvolverTrueStereoTest.cpp b/packages/react-native-audio-api/common/cpp/test/src/core/effects/ConvolverTrueStereoTest.cpp new file mode 100644 index 0000000000..1bce2e52fd --- /dev/null +++ b/packages/react-native-audio-api/common/cpp/test/src/core/effects/ConvolverTrueStereoTest.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +using namespace audioapi; + +// NOLINTBEGIN + +namespace { + +constexpr int kQuantum = 128; +constexpr int kSampleRate = 44100; +constexpr float kInputLeft = 0.5f; +constexpr float kInputRight = 0.25f; +// One-sample impulse responses, so each convolver is a plain gain. +constexpr float kResponseGains[4] = {1.0f, 0.5f, 0.25f, 0.75f}; +// Chrome: L = IR0*L + IR2*R, R = IR1*L + IR3*R. +constexpr float kExpectedLeft = kResponseGains[0] * kInputLeft + kResponseGains[2] * kInputRight; +constexpr float kExpectedRight = kResponseGains[1] * kInputLeft + kResponseGains[3] * kInputRight; +// A mono bus feeds kInputLeft to all four responses and averages the stereo pair. +constexpr float kExpectedMono = 0.5f * kInputLeft * + (kResponseGains[0] + kResponseGains[1] + kResponseGains[2] + kResponseGains[3]); + +// Chrome's Reverb::CalculateNormalizationScale ends with a 0.5 true-stereo compensation. +float expectedNormalizationScale() { + float power = 0.0f; + for (float gain : kResponseGains) { + power += gain * gain; + } + power = std::sqrt(power / (4 * kQuantum)); + return 1.0f / power * std::pow(10.0f, GAIN_CALIBRATION * 0.05f) * 0.5f; +} + +class ConvolverHarness : public ConvolverNode { + public: + using ConvolverNode::ConvolverNode; + + void runQuantum(const std::vector &inputs) { + this->processInputs(inputs, kQuantum); + } +}; + +class ConvolverTrueStereoTest : public ::testing::Test { + protected: + std::shared_ptr eventRegistry; + std::shared_ptr context; + std::shared_ptr destination; + + void SetUp() override { + eventRegistry = std::make_shared(); + context = std::make_shared(2, 5 * kSampleRate, kSampleRate, eventRegistry); + destination = std::make_shared(context); + context->initialize(destination.get()); + } + + // Renders a constant input through a 4-channel response and returns {first, last} bus channel. + std::pair render(bool normalize, int busChannels = 2) { + auto response = std::make_shared(kQuantum, 4, kSampleRate); + response->zero(); + for (int channel = 0; channel < 4; ++channel) { + (*response->getChannel(channel))[0] = kResponseGains[channel]; + } + + std::vector> convolvers; + for (size_t channel = 0; channel < 4; ++channel) { + AudioArray channelData(*response->getChannel(channel)); + convolvers.push_back(std::make_shared()); + EXPECT_TRUE(convolvers.back()->init(kQuantum, channelData, response->getSize())); + } + + ConvolverHarness convolver(context, ConvolverOptions()); + const float scale = normalize ? convolver.calculateNormalizationScale(response) : 1.0f; + convolver.setOutputBuffer(std::make_shared(kQuantum, busChannels, kSampleRate)); + + convolver.setBuffer( + response, + std::move(convolvers), + /*threadPool=*/nullptr, + std::make_shared(kQuantum * 2, convolver.getChannelCount(), kSampleRate), + std::make_shared(kQuantum, 4, kSampleRate), + scale); + + auto input = std::make_shared(kQuantum, busChannels, kSampleRate); + for (int quantum = 0; quantum < 4; ++quantum) { + for (size_t i = 0; i < kQuantum; ++i) { + (*input->getChannel(0))[i] = kInputLeft; + (*input->getChannel(busChannels - 1))[i] = busChannels == 1 ? kInputLeft : kInputRight; + } + convolver.runQuantum({input.get()}); + } + + const auto *output = convolver.getOutput(); + EXPECT_EQ(output->getNumberOfChannels(), static_cast(busChannels)); + return { + (*output->getChannel(0))[kQuantum / 2], + (*output->getChannel(busChannels - 1))[kQuantum / 2]}; + } +}; + +} // namespace + +TEST_F(ConvolverTrueStereoTest, FourChannelResponseHasUnityGainWithoutNormalization) { + const auto [left, right] = render(/*normalize=*/false); + + EXPECT_NEAR(left, kExpectedLeft, 1e-4f); + EXPECT_NEAR(right, kExpectedRight, 1e-4f); +} + +TEST_F(ConvolverTrueStereoTest, FourChannelResponseIsHalvedByNormalization) { + const auto [left, right] = render(/*normalize=*/true); + + EXPECT_NEAR(left, kExpectedLeft * expectedNormalizationScale(), 1e-6f); + EXPECT_NEAR(right, kExpectedRight * expectedNormalizationScale(), 1e-6f); +} + +TEST_F(ConvolverTrueStereoTest, MonoBusAveragesTheStereoPair) { + const auto [mono, _] = render(/*normalize=*/false, /*busChannels=*/1); + + EXPECT_NEAR(mono, kExpectedMono, 1e-4f); +} + +TEST_F(ConvolverTrueStereoTest, MonoBusIsHalvedByNormalization) { + const auto [mono, _] = render(/*normalize=*/true, /*busChannels=*/1); + + EXPECT_NEAR(mono, kExpectedMono * expectedNormalizationScale(), 1e-6f); +} + +// NOLINTEND