Skip to content

Commit 5656a2d

Browse files
authored
test: gain node test (#545)
1 parent 9cec3a8 commit 5656a2d

File tree

4 files changed

+80
-19
lines changed

4 files changed

+80
-19
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
11
name: Tests
22

33
on:
4-
issue_comment:
5-
types: [created, edited]
4+
pull_request:
65
workflow_dispatch:
76

87
jobs:
98
tests:
109
runs-on: ubuntu-latest
11-
if: github.event.issue.pull_request != null && contains(github.event.comment.body, '/run-tests')
1210
steps:
13-
- name: check user
14-
uses: tspascoal/get-user-teams-membership@v3
15-
id: teamAffiliation
16-
with:
17-
GITHUB_TOKEN: ${{ secrets.TEAM_TOKEN }}
18-
username: ${{ github.event.comment.user.login }}
19-
organization: "software-mansion"
20-
team: "react-native-open-source"
2111

22-
- name: Stop workflow if user is no member
23-
if: ${{ steps.teamAffiliation.outputs.isTeamMember == 'false' }}
24-
run: |
25-
echo "You have no rights to trigger this job."
26-
exit 1
27-
2812
- name: Checkout
2913
uses: actions/checkout@v4
3014

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ class BaseAudioContext {
5757
int length);
5858
std::shared_ptr<AnalyserNode> createAnalyser();
5959

60-
#ifndef TESTING
6160
std::shared_ptr<AudioBuffer> decodeAudioDataSource(const std::string &path);
6261
std::shared_ptr<AudioBuffer> decodeAudioData(const void *data, size_t size);
6362
std::shared_ptr<AudioBuffer> decodeWithPCMInBase64(const std::string &data);
64-
#endif //TESTING
6563

6664
std::shared_ptr<PeriodicWave> getBasicWaveForm(OscillatorType type);
6765
[[nodiscard]] float getNyquistFrequency() const;

packages/react-native-audio-api/common/cpp/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ target_include_directories(rnaudioapi_libs PUBLIC
6666
add_executable(
6767
tests
6868
OscillatorTest.cpp
69+
GainTest.cpp
6970
)
7071

7172
target_link_libraries(tests
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)