|
| 1 | +#include <audioapi/core/OfflineAudioContext.h> |
| 2 | +#include <audioapi/core/effects/GainNode.h> |
| 3 | +#include <audioapi/utils/AudioArray.h> |
| 4 | +#include <audioapi/utils/AudioBus.h> |
| 5 | +#include <gtest/gtest.h> |
| 6 | +#include "MockAudioEventHandlerRegistry.h" |
| 7 | + |
| 8 | +class GainTest : public ::testing::Test { |
| 9 | + protected: |
| 10 | + std::shared_ptr<audioapi::IAudioEventHandlerRegistry> eventRegistry; |
| 11 | + std::unique_ptr<audioapi::OfflineAudioContext> context; |
| 12 | + static constexpr int sampleRate = 44100; |
| 13 | + |
| 14 | + void SetUp() override { |
| 15 | + eventRegistry = std::make_shared<MockAudioEventHandlerRegistry>(); |
| 16 | + context = std::make_unique<audioapi::OfflineAudioContext>( |
| 17 | + 2, 5 * sampleRate, sampleRate, eventRegistry); |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +class TestableGainNode : public audioapi::GainNode { |
| 22 | + public: |
| 23 | + explicit TestableGainNode(audioapi::BaseAudioContext *context) |
| 24 | + : audioapi::GainNode(context) {} |
| 25 | + |
| 26 | + void setGainParam(float value) { |
| 27 | + getGainParam()->setValue(value); |
| 28 | + } |
| 29 | + |
| 30 | + void processNode( |
| 31 | + const std::shared_ptr<audioapi::AudioBus> &processingBus, |
| 32 | + int framesToProcess) override { |
| 33 | + audioapi::GainNode::processNode(processingBus, framesToProcess); |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +TEST_F(GainTest, GainCanBeCreated) { |
| 38 | + auto gain = context->createGain(); |
| 39 | + ASSERT_NE(gain, nullptr); |
| 40 | +} |
| 41 | + |
| 42 | +TEST_F(GainTest, GainModulatesVolumeCorrectly) { |
| 43 | + static constexpr float GAIN_VALUE = 0.5f; |
| 44 | + static constexpr int FRAMES_TO_PROCESS = 4; |
| 45 | + auto gainNode = std::make_shared<TestableGainNode>(context.get()); |
| 46 | + gainNode->setGainParam(GAIN_VALUE); |
| 47 | + |
| 48 | + auto bus = |
| 49 | + std::make_shared<audioapi::AudioBus>(FRAMES_TO_PROCESS, 1, sampleRate); |
| 50 | + for (size_t i = 0; i < bus->getSize(); ++i) { |
| 51 | + bus->getChannel(0)->getData()[i] = i + 1; |
| 52 | + } |
| 53 | + |
| 54 | + gainNode->processNode(bus, FRAMES_TO_PROCESS); |
| 55 | + for (size_t i = 0; i < bus->getSize(); ++i) { |
| 56 | + EXPECT_FLOAT_EQ((*bus->getChannel(0))[i], (i + 1) * GAIN_VALUE); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +TEST_F(GainTest, GainModulatesVolumeCorrectlyMultiChannel) { |
| 61 | + static constexpr float GAIN_VALUE = 0.5f; |
| 62 | + static constexpr int FRAMES_TO_PROCESS = 4; |
| 63 | + auto gainNode = std::make_shared<TestableGainNode>(context.get()); |
| 64 | + gainNode->setGainParam(GAIN_VALUE); |
| 65 | + |
| 66 | + auto bus = |
| 67 | + std::make_shared<audioapi::AudioBus>(FRAMES_TO_PROCESS, 2, sampleRate); |
| 68 | + for (size_t i = 0; i < bus->getSize(); ++i) { |
| 69 | + bus->getChannel(0)->getData()[i] = i + 1; |
| 70 | + bus->getChannel(1)->getData()[i] = -i - 1; |
| 71 | + } |
| 72 | + |
| 73 | + gainNode->processNode(bus, FRAMES_TO_PROCESS); |
| 74 | + for (size_t i = 0; i < bus->getSize(); ++i) { |
| 75 | + EXPECT_FLOAT_EQ((*bus->getChannel(0))[i], (i + 1) * GAIN_VALUE); |
| 76 | + EXPECT_FLOAT_EQ((*bus->getChannel(1))[i], (-i - 1) * GAIN_VALUE); |
| 77 | + } |
| 78 | +} |
0 commit comments